339. Nested List Weight Sum I & II
Nested List Weight Sum I
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.
Example 1:
Given the list [[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1)
From:
1 public int depthSum(List<NestedInteger> nestedList) { 2 return helper(nestedList, 1); 3 } 4 5 public int helper(List<NestedInteger> nestedList, int depth) { 6 if (nestedList == null || nestedList.size() == 0) 7 return 0; 8 9 int sum = 0; 10 for (NestedInteger ni : nestedList) { 11 if (ni.isInteger()) { 12 sum += ni.getInteger() * depth; 13 } else { 14 sum += helper(ni.getList(), depth + 1); 15 } 16 } 17 18 return sum; 19 }
1 public int depthSum(List<NestedInteger> nestedList) { 2 int sum = 0; 3 4 Queue<NestedInteger> queue = new LinkedList<>(); 5 Queue<Integer> depth = new LinkedList<>(); 6 7 for (NestedInteger ni : nestedList) { 8 queue.offer(ni); 9 depth.offer(1); 10 } 11 12 while (!queue.isEmpty()) { 13 NestedInteger top = queue.poll(); 14 int dep = depth.poll(); 15 16 if (top.isInteger()) { 17 sum += dep * top.getInteger(); 18 } else { 19 for (NestedInteger ni : top.getList()) { 20 queue.offer(ni); 21 depth.offer(dep + 1); 22 } 23 } 24 } 25 26 return sum; 27 }
Nested List Weight Sum II
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)
From: https://cyqz.wordpress.com/2016/06/23/leetcode-364-nested-list-weight-sum-ii/
1 /** 2 * // This is the interface that allows for creating nested lists. 3 * // You should not implement it, or speculate about its implementation 4 * public interface NestedInteger { 5 * // Constructor initializes an empty nested list. 6 * public NestedInteger(); 7 * 8 * // Constructor initializes a single integer. 9 * public NestedInteger(int value); 10 * 11 * // @return true if this NestedInteger holds a single integer, rather than a nested list. 12 * public boolean isInteger(); 13 * 14 * // @return the single integer that this NestedInteger holds, if it holds a single integer 15 * // Return null if this NestedInteger holds a nested list 16 * public Integer getInteger(); 17 * 18 * // Set this NestedInteger to hold a single integer. 19 * public void setInteger(int value); 20 * 21 * // Set this NestedInteger to hold a nested list and adds a nested integer to it. 22 * public void add(NestedInteger ni); 23 * 24 * // @return the nested list that this NestedInteger holds, if it holds a nested list 25 * // Return null if this NestedInteger holds a single integer 26 * public List<NestedInteger> getList(); 27 * } 28 */ 29 public class Solution { 30 public int depthSumInverse(List<NestedInteger> nestedList) { 31 if (nestedList == null || nestedList.size() == 0) return 0; 32 int h = helper(nestedList); 33 return getSum(nestedList, h); 34 } 35 36 private int getSum(List<NestedInteger> list, int layer) { 37 int sum = 0; 38 if (list == null || list.size() == 0) return sum; 39 for (NestedInteger n : list) { 40 if (n.isInteger()) 41 sum += n.getInteger() * layer; 42 else 43 sum += getSum(n.getList(), layer - 1); 44 } 45 return sum; 46 } 47 48 private int helper(List<NestedInteger> list) { 49 if (list == null || list.size() == 0) return 0; 50 int max = 0; 51 for (NestedInteger n : list) { 52 if (n.isInteger()) 53 max = Math.max(max, 1); 54 else 55 max = Math.max(max, helper(n.getList()) + 1); 56 } 57 return max; 58 } 59 }
1 public int depthSumInverse(List<NestedInteger> nestedList) { 2 if (nestedList == null || nestedList.size() == 0) 3 return 0; 4 5 HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); 6 7 // two stacks: one is for processing nested integer, the other is for tracking 8 // layers. 9 Stack<NestedInteger> stack = new Stack<>(); 10 Stack<Integer> layers = new Stack<>(); 11 12 // put all NestedIntegers to Stack and record its layer to be 1 13 for (NestedInteger ni : nestedList) { 14 stack.push(ni); 15 layers.push(1); 16 } 17 18 int maxLayer = Integer.MIN_VALUE; 19 20 while (!stack.isEmpty()) { 21 NestedInteger top = stack.pop(); 22 int topLayer = layers.pop(); 23 24 maxLayer = Math.max(maxLayer, topLayer); 25 26 if (top.isInteger()) { 27 if (map.containsKey(topLayer)) { 28 map.get(topLayer).add(top.getInteger()); 29 } else { 30 ArrayList<Integer> list = new ArrayList<Integer>(); 31 list.add(top.getInteger()); 32 map.put(topLayer, list); 33 } 34 } else { 35 for (NestedInteger ni : top.getList()) { 36 stack.push(ni); 37 layers.push(topLayer + 1); 38 } 39 } 40 } 41 42 // calcualte sum 43 int result = 0; 44 for (int i = maxLayer; i >= 1; i--) { 45 if (map.get(i) != null) { 46 for (int v : map.get(i)) { 47 result += v * (maxLayer - i + 1); 48 } 49 } 50 } 51 return result; 52 }