Problem: Given the nested list of integer, calculate the depth sum.
Sample Input: [[1,1], 2, [1,1]]
Output: 10
Explanation: 1* 2 + 1 * 2 + 2 * 1+ 1 * 2+ 1 * 2 = 10. ( Number in the red color are the level in the nested list. )
Sample Input: [1 [4 [6]]]
Output: 27
Explanation: 1 * 1 + 4 * 2 + 6 * 3 = 27( Number in the red color are the level in the nested list. )
Solution:
There are two ways to solve this problem. Either iterative or recursive.
- Iterative: You need a data structure to keep track of nested list. This can be Queue. At each level, if you see a list, add it in a queue and keep on traversing till queue is empty.
- Recursive: Easy way to solve it compared to iterative. At each level, you need to check if it is a list then increment the current level and make a recursive call. Return the sum once you hit the number.
Simplification of the depth tracking in Recursive approach:
So, in above approach, we are going to each list value and also keep track of depth and do a sum. Another way to approach is to, capture sums at each depth by maintaining an array. This array index represents the depth of nested list.
For example:
Input data : [ [ 1,5 ], 3, [ 8, [ 9 ] ] ]
Resulting array will be : [3, 14, 9] . At level one we have only one element, which is 3. At level 2 we have 1, 5, 8 = 14 and at level 3 we have 9.
Now, it easy to calculate the sum : 3 * 1 + 14 * 2 + 9 * 3 = 58
One tricky variation is to consider depth in decreasing order.
If you take above approach then its easy to solve. Once you have a result array you can calculate the sum in reverse order. i.e. 3 * 3 + 14 * 2 + 9 * 1 = 46
Refer to method caclulateDepthSumRecursiveV1 for updated version.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
package interviewque; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class DepthSum { public static int caclulateDepthSumIterative(NestedList nestedList) { Queue<NestedList> queue = new LinkedList<NestedList>(); queue.add(nestedList); Queue<Integer> depthQueue = new LinkedList<Integer>(); int depth = 0; int sum = 0; depthQueue.add(depth); while (!queue.isEmpty()) { nestedList = queue.poll(); depth = depthQueue.poll(); if (nestedList.isList) { ++depth; for (NestedList nL : nestedList.getNestedList()) { queue.add(nL); depthQueue.add(depth); } } else { sum += nestedList.getNumber() * depth; } } return sum; } public static int caclulateDepthSumRecursive(NestedList nestedList, int level) { int sum = 0; if (nestedList.isList) { for (NestedList nestedList2 : nestedList.getNestedList()) { sum += caclulateDepthSumRecursive(nestedList2, level + 1); } } else { sum = nestedList.number * level; } return sum; } public static void caclulateDepthSumRecursiveV1(NestedList nestedList, int level, List<Integer> results) { if (results.size() < level) { results.add(0); } if (nestedList.isList) { for (NestedList nestedList2 : nestedList.getNestedList()) { caclulateDepthSumRecursiveV1(nestedList2, level + 1, results); } } else { results.set(level - 1, results.get(level - 1) + nestedList.number); } } public static void main(String[] args) { NestedList n1Val = new NestedList(1); NestedList n2Val = new NestedList(1); NestedList n3Val = new NestedList(2); NestedList n4Val = new NestedList(1); NestedList n5Val = new NestedList(1); List<NestedList> nestedList1 = new ArrayList<NestedList>(); nestedList1.add(n1Val); nestedList1.add(n2Val); List<NestedList> nestedList2 = new ArrayList<NestedList>(); nestedList2.add(n4Val); nestedList2.add(n5Val); NestedList n1 = new NestedList(nestedList1); NestedList n2 = new NestedList(nestedList2); List<NestedList> nestedList3 = new ArrayList<NestedList>(); nestedList3.add(n1); nestedList3.add(n3Val); nestedList3.add(n2); NestedList n3 = new NestedList(nestedList3); System.out.println("[[1,1], 2, [1,1]] Iterative depth sum: " + caclulateDepthSumIterative(n3)); System.out.println("[[1,1], 2, [1,1]] Recursive depth sum: " + caclulateDepthSumRecursive(n3, 0)); List<Integer> results = new ArrayList<Integer>(); caclulateDepthSumRecursiveV1(n3, 0, results); System.out.println("\nVersion 1 : Depth is in increasing order"); int depth = 1; int sum = 0; for (Integer i : results) { sum += i * depth; depth++; } System.out.println("[[1,1], 2, [1,1]] Recursive depth sum: " + sum); System.out.println("\nVersion 1 : Depth is in decresing order"); depth = results.size(); sum = 0; for (Integer i : results) { sum += i * depth; depth--; } System.out.println("[[1,1], 2, [1,1]] Recursive depth sum: " + sum); NestedList n6Val = new NestedList(6); NestedList n7Val = new NestedList(4); NestedList n8Val = new NestedList(1); List<NestedList> nestedList6 = new ArrayList<NestedList>(); nestedList6.add(n6Val); NestedList n6 = new NestedList(nestedList6); List<NestedList> nestedList7 = new ArrayList<NestedList>(); nestedList7.add(n7Val); nestedList7.add(n6); NestedList n7 = new NestedList(nestedList7); List<NestedList> nestedList8 = new ArrayList<NestedList>(); nestedList8.add(n8Val); nestedList8.add(n7); NestedList n8 = new NestedList(nestedList8); System.out.println("\n[ 1 [ 4 [ 6 ] ] ] Iterative depth sum " + caclulateDepthSumIterative(n8)); System.out.println("[ 1 [ 4 [ 6 ] ] ] Recursive depth sum " + caclulateDepthSumRecursive(n8, 0)); results = new ArrayList<Integer>(); caclulateDepthSumRecursiveV1(n8, 0, results); System.out.println("\nVersion 1 : Depth is in increasing order"); depth = 1; sum = 0; for (Integer i : results) { sum += i * depth; depth++; } System.out.println("[ 1 [ 4 [ 6 ] ] ] Recursive depth sum: " + sum); System.out.println("\nVersion 1 : Depth is in decresing order"); depth = results.size(); sum = 0; for (Integer i : results) { sum += i * depth; depth--; } System.out.println("[ 1 [ 4 [ 6 ] ] ] Recursive depth sum: " + sum); } } class NestedList { int number; List<NestedList> nestedList; boolean isList = false; public NestedList(int number) { this.number = number; isList = false; } public NestedList(List<NestedList> nestedLists) { this.nestedList = nestedLists; isList = true; } public int getNumber() { return number; } public List<NestedList> getNestedList() { return nestedList; } } |
Complexity: O(n)
Github url: DepthSum.java