Given a nested list of integers, return the sum of all integers in the list weighted by their depth.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.
**Example 1:**Given the list [[1,1],2,[1,1]]
, return **8**. (four 1's at depth 1, one 2 at depth 2)
**Example 2:**Given the list [1,[4,[6]]]
, return **17**. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)
思路大致和 339题 Nested List Weight Sum http://www.jianshu.com/p/49eb0423df15相同,用累积和的形式代替level计算。
Solution1:BFS
Time Complexity: O(N) Space Complexity: O(N)
Solution2:DFS(recursive) two pass
Time Complexity: O(N) Space Complexity: O(N)
Solution3:DFS(stack) two pass
Time Complexity: O(N) Space Complexity: O(N)
Solution1 Code:
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if (nestedList == null) return 0;
/*
Queue<NestedInteger> queue = new LinkedList<NestedInteger>();
for (NestedInteger next: nestedList) {
queue.offer(next);
}
*/
Queue<NestedInteger> queue = new LinkedList<NestedInteger>(nestedList);
int prev = 0;
int total = 0;
while (!queue.isEmpty()) {
int size = queue.size();
int levelSum = 0;
for (int i = 0; i < size; i++) {
NestedInteger current = queue.poll();
if (current.isInteger()) levelSum += current.getInteger();
else {
queue.addAll(current.getList());
}
/*
List<NestedInteger> nextList = current.getList();
if (nextList != null) {
for (NestedInteger next: nextList) {
queue.offer(next);
}
}
*/
}
prev += levelSum;
total += prev;
}
return total;
}
}
Solution2 Code:
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if(nestedList == null || nestedList.size() == 0) return 0;
int h = helper(nestedList);
int res = getSum(nestedList, h);
return res;
}
private int getSum(List<NestedInteger> l, int layer) {
int sum = 0;
if(l == null || l.size() == 0) return sum;
for(NestedInteger n : l) {
if(n.isInteger()) sum += n.getInteger() * layer;
else sum += getSum(n.getList(), layer - 1);
}
return sum;
}
private int helper(List<NestedInteger> l) {
if(l == null || l.size() == 0) return 0;
int max = 0;
for(NestedInteger n : l) {
if(n.isInteger()) max = Math.max(max, 1);
else max = Math.max(max, helper(n.getList()) + 1);
}
return max;
}
}
Solution3 Code:
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if(nestedList == null || nestedList.size() == 0) return 0;
int h = helper(nestedList);
int res = getSum(nestedList, h);
return res;
}
private int getSum(List<NestedInteger> nestedList, int layer) {
int res = 0;
Stack<Iterator<NestedInteger>> stk = new Stack<>();
stk.push(nestedList.iterator());
while (!stk.isEmpty()) {
Iterator<NestedInteger> itr = stk.peek();
while (itr.hasNext()) {
NestedInteger n = itr.next();
if (n.isInteger()) res += n.getInteger() * (layer + 1 - stk.size());
else {
stk.push(n.getList().iterator());
break;
}
}
if (!stk.peek().hasNext()) stk.pop();
}
return res;
}
private int helper(List<NestedInteger> l) {
if(l == null || l.size() == 0) return 0;
int max = 0;
for(NestedInteger n : l) {
if(n.isInteger()) max = Math.max(max, 1);
else max = Math.max(max, helper(n.getList()) + 1);
}
return max;
}
}