Redian新闻
>
问一道L烂大街的题 题意还是有点不懂 顺便报FG面经
avatar
问一道L烂大街的题 题意还是有点不懂 顺便报FG面经# JobHunting - 待字闺中
c*h
1
问一道L家烂大街的题 nestedint reversed weighted sum
Compute the reverse depth sum of a nested list meaning the reverse depth of
each node (ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
of leafs, etc.) times the value of that node.
这个例子应该怎么算?
{{1,2}, 1, {2, {2,1}}} = ?
{1,2}的weight应该是1 还是2?按照定义它应该是leaf吧 所以应该是
(1+2)*1 + 1 * 3 + (2*2 + (2+1) * 1) = 13?
对说好的FG面经
F考了个BST里面找successor 就一题 各种变 有parent 没parent 用stack不用stack
G家考了俩 第一个是给了两个有重复元素的list 求差集 第二题是LC min stack变种
维护最小次小元素
avatar
y*e
2
关注!
avatar
r*j
3
{1,2}的weight应该是2

of
parents

【在 c******h 的大作中提到】
: 问一道L家烂大街的题 nestedint reversed weighted sum
: Compute the reverse depth sum of a nested list meaning the reverse depth of
: each node (ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
: of leafs, etc.) times the value of that node.
: 这个例子应该怎么算?
: {{1,2}, 1, {2, {2,1}}} = ?
: {1,2}的weight应该是1 还是2?按照定义它应该是leaf吧 所以应该是
: (1+2)*1 + 1 * 3 + (2*2 + (2+1) * 1) = 13?
: 对说好的FG面经
: F考了个BST里面找successor 就一题 各种变 有parent 没parent 用stack不用stack

avatar
c*h
4
那请问“(ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
of leafs, etc.) ”应该怎么理解呢 {1,2}不是leaf么?

【在 r******j 的大作中提到】
: {1,2}的weight应该是2
:
: of
: parents

avatar
z*b
5
这题首先需要过一遍list找出最大的深度先?谁能给个简洁的代码?

of
parents

【在 c******h 的大作中提到】
: 问一道L家烂大街的题 nestedint reversed weighted sum
: Compute the reverse depth sum of a nested list meaning the reverse depth of
: each node (ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
: of leafs, etc.) times the value of that node.
: 这个例子应该怎么算?
: {{1,2}, 1, {2, {2,1}}} = ?
: {1,2}的weight应该是1 还是2?按照定义它应该是leaf吧 所以应该是
: (1+2)*1 + 1 * 3 + (2*2 + (2+1) * 1) = 13?
: 对说好的FG面经
: F考了个BST里面找successor 就一题 各种变 有parent 没parent 用stack不用stack

avatar
C*t
6
def revSum(a):
d = height(a)
level, sum = 1, [0]
helper(a, d, level, sum)
return sum[0]
def height(a):
d = 1
for ele in a:
if type(ele) == list:
d = max(d, 1+height(ele))
return d
def helper(a, d, level, sum):
for i in range(len(a)):
if type(a[i]) != list:
sum[0] += a[i]*(d-level+1)
else:
helper(a[i], d, level+1, sum)
avatar
x*9
7
nested_list = [[1,2], 1, [2, [2,1]]]
def revSum(nlist):
(depth, res) = dfs(nlist)
return res
def dfs(nlist):
nlist_sum = 0
s = 0
max_lv = 1
for item in nlist:
if isinstance(item, list):
(depth, res) = dfs(item)
nlist_sum += res
max_lv = max(max_lv, depth + 1)
else:
s += item
nlist_sum += s * max_lv
return (max_lv, nlist_sum)
print revSum(nested_list)
(已加入肯德基豪华午餐
avatar
c*h
8
问一道L家烂大街的题 nestedint reversed weighted sum
Compute the reverse depth sum of a nested list meaning the reverse depth of
each node (ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
of leafs, etc.) times the value of that node.
这个例子应该怎么算?
{{1,2}, 1, {2, {2,1}}} = ?
{1,2}的weight应该是1 还是2?按照定义它应该是leaf吧 所以应该是
(1+2)*1 + 1 * 3 + (2*2 + (2+1) * 1) = 13?
对说好的FG面经
F考了个BST里面找successor 就一题 各种变 有parent 没parent 用stack不用stack
G家考了俩 第一个是给了两个有重复元素的list 求差集 第二题是LC min stack变种
维护最小次小元素
avatar
y*e
9
关注!
avatar
r*j
10
{1,2}的weight应该是2

of
parents

【在 c******h 的大作中提到】
: 问一道L家烂大街的题 nestedint reversed weighted sum
: Compute the reverse depth sum of a nested list meaning the reverse depth of
: each node (ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
: of leafs, etc.) times the value of that node.
: 这个例子应该怎么算?
: {{1,2}, 1, {2, {2,1}}} = ?
: {1,2}的weight应该是1 还是2?按照定义它应该是leaf吧 所以应该是
: (1+2)*1 + 1 * 3 + (2*2 + (2+1) * 1) = 13?
: 对说好的FG面经
: F考了个BST里面找successor 就一题 各种变 有parent 没parent 用stack不用stack

avatar
c*h
11
那请问“(ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
of leafs, etc.) ”应该怎么理解呢 {1,2}不是leaf么?

【在 r******j 的大作中提到】
: {1,2}的weight应该是2
:
: of
: parents

avatar
z*b
12
这题首先需要过一遍list找出最大的深度先?谁能给个简洁的代码?

of
parents

【在 c******h 的大作中提到】
: 问一道L家烂大街的题 nestedint reversed weighted sum
: Compute the reverse depth sum of a nested list meaning the reverse depth of
: each node (ie, 1 for leafs, 2 for parents of leafs, 3 for parents of parents
: of leafs, etc.) times the value of that node.
: 这个例子应该怎么算?
: {{1,2}, 1, {2, {2,1}}} = ?
: {1,2}的weight应该是1 还是2?按照定义它应该是leaf吧 所以应该是
: (1+2)*1 + 1 * 3 + (2*2 + (2+1) * 1) = 13?
: 对说好的FG面经
: F考了个BST里面找successor 就一题 各种变 有parent 没parent 用stack不用stack

avatar
C*t
13
def revSum(a):
d = height(a)
level, sum = 1, [0]
helper(a, d, level, sum)
return sum[0]
def height(a):
d = 1
for ele in a:
if type(ele) == list:
d = max(d, 1+height(ele))
return d
def helper(a, d, level, sum):
for i in range(len(a)):
if type(a[i]) != list:
sum[0] += a[i]*(d-level+1)
else:
helper(a[i], d, level+1, sum)
avatar
x*9
14
nested_list = [[1,2], 1, [2, [2,1]]]
def revSum(nlist):
(depth, res) = dfs(nlist)
return res
def dfs(nlist):
nlist_sum = 0
s = 0
max_lv = 1
for item in nlist:
if isinstance(item, list):
(depth, res) = dfs(item)
nlist_sum += res
max_lv = max(max_lv, depth + 1)
else:
s += item
nlist_sum += s * max_lv
return (max_lv, nlist_sum)
print revSum(nested_list)
(已加入肯德基豪华午餐
avatar
y*e
15
我看似乎得这么做,下面那个python的代码也是这么写的,如果有更简单的One pass的
办法,还请大神们指教

【在 z***b 的大作中提到】
: 这题首先需要过一遍list找出最大的深度先?谁能给个简洁的代码?
:
: of
: parents

avatar
x*9
16

nested list 就是一个递归的结构
很难one pass
当然,从理论上说,DFS也是一个one pass,每一个元素都被且只被遍历了一次。。。
如果不用递归的话,可以用stack模拟

【在 y*****e 的大作中提到】
: 我看似乎得这么做,下面那个python的代码也是这么写的,如果有更简单的One pass的
: 办法,还请大神们指教

avatar
y*e
17
不好意思我当时没看到你这个solution,我说的是你前面那个python,他是先遍历了一
遍求了一个depth, 然后又遍历的list求sum。你的答案的确是one pass,谢谢分享!

【在 x*******9 的大作中提到】
:
: nested list 就是一个递归的结构
: 很难one pass
: 当然,从理论上说,DFS也是一个one pass,每一个元素都被且只被遍历了一次。。。
: 如果不用递归的话,可以用stack模拟

avatar
x*9
18
:)
Good luck for job hunting

【在 y*****e 的大作中提到】
: 不好意思我当时没看到你这个solution,我说的是你前面那个python,他是先遍历了一
: 遍求了一个depth, 然后又遍历的list求sum。你的答案的确是one pass,谢谢分享!

avatar
c*h
19
就是说 {1,2} 的weight 是1了对吧 因为nlist_sum没有乘max_lv
我觉得是对的
2L说{1,2} weight是2不知道为啥

nested_list = [[1,2], 1, [2, [2,1]]]
def revSum(nlist):
(depth, res) = dfs(nlist)
return res
def dfs(nlist):
nlist_sum = 0
s = 0
max_lv = 1
for item in nlist:
if isinstance(item, list):
(depth, res) = dfs(item)
nlist_sum += res
max_lv = max(max_lv, depth + 1)
else:
s += item
nlist_sum += s * max_lv
return (max_lv, nlist_sum)
print revSum(nested_list)

【在 x*******9 的大作中提到】
: nested_list = [[1,2], 1, [2, [2,1]]]
: def revSum(nlist):
: (depth, res) = dfs(nlist)
: return res
: def dfs(nlist):
: nlist_sum = 0
: s = 0
: max_lv = 1
: for item in nlist:
: if isinstance(item, list):

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。