1
+ package com .leetcode .arrays ;
2
+
3
+ import java .util .*;
4
+
5
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6
+
7
+ /**
8
+ * Level: Medium
9
+ * Problem Link: https://leetcode.com/problems/nested-list-weight-sum-ii/ (premium)
10
+ * Problem Description:
11
+ * Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element
12
+ * is either an integer, or a list – whose elements may also be integers or other lists.
13
+ * <p>
14
+ * Different from {@link NestedListWeightSum} where weight is increasing from root to leaf, now the weight is defined
15
+ * from bottom up, i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.
16
+ * <p>
17
+ * Example 1:
18
+ * Given the list [[1,1],2,[1,1]], return 8. (four 1’s at depth 1, one 2 at depth 2)
19
+ * <p>
20
+ * Example 2:
21
+ * Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 13 + 42 + 6*1 = 17)
22
+ * <p>
23
+ * Note: For a simpler variant, see {@link NestedListWeightSum}.
24
+ *
25
+ * @author rampatra
26
+ * @since 2019-07-27
27
+ */
28
+ public class NestedListWeightSumII {
29
+
30
+ /**
31
+ * @param nestedList
32
+ * @return
33
+ */
34
+ public static long nestedSum (List <NestedInteger > nestedList ) {
35
+ long weightedSum = 0 ;
36
+ long unweightedSum = 0 ;
37
+ Queue <NestedInteger > queue = new LinkedList <>();
38
+
39
+ for (NestedInteger nestedInteger : nestedList ) {
40
+ if (nestedInteger .isInteger ()) {
41
+ unweightedSum += nestedInteger .getInteger ();
42
+ } else {
43
+ queue .addAll (nestedInteger .getList ());
44
+ while (!queue .isEmpty ()) {
45
+ NestedInteger ni = queue .poll ();
46
+ if (ni .isInteger ()) {
47
+ unweightedSum += ni .getInteger ();
48
+ } else {
49
+ nestedList .addAll (ni .getList ());
50
+ }
51
+ }
52
+ unweightedSum += unweightedSum ;
53
+ weightedSum = unweightedSum ;
54
+ }
55
+ }
56
+
57
+ return weightedSum ;
58
+ }
59
+
60
+ public static void main (String [] args ) {
61
+ assertEquals (0 , nestedSum (Collections .singletonList (new NestedInteger ())));
62
+
63
+ assertEquals (0 , nestedSum (Collections .singletonList (new NestedInteger ().add (new NestedInteger ()))));
64
+
65
+ NestedInteger ni = new NestedInteger (2 );
66
+ ni .add (new NestedInteger ().add (new NestedInteger (1 )).add (new NestedInteger (1 )));
67
+ ni .add (new NestedInteger ().add (new NestedInteger (1 )).add (new NestedInteger (1 )));
68
+
69
+ assertEquals (10 , nestedSum (Collections .singletonList (ni )));
70
+
71
+ ni = new NestedInteger (1 );
72
+ ni .add (new NestedInteger (4 ).add (new NestedInteger (6 )));
73
+
74
+ assertEquals (27 , nestedSum (Collections .singletonList (ni )));
75
+
76
+ /*assertEquals(10, nestedSum(new Object[]{new Object[]{1, 1}, 2, new Object[]{1, 1}}));
77
+ assertEquals(27, nestedSum(new Object[]{1, new Object[]{4, new Object[]{6}}}));*/
78
+ }
79
+ }
80
+
81
+ class NestedInteger {
82
+
83
+ private Integer integer ;
84
+ private List <NestedInteger > list ;
85
+
86
+ public NestedInteger () {
87
+ this .list = new ArrayList <>();
88
+ }
89
+
90
+ public NestedInteger (int integer ) {
91
+ this .integer = integer ;
92
+ this .list = new ArrayList <>();
93
+ }
94
+
95
+ public boolean isInteger () {
96
+ return this .integer != null ;
97
+ }
98
+
99
+ public Integer getInteger () {
100
+ return integer ;
101
+ }
102
+
103
+ public void setInteger (Integer integer ) {
104
+ this .integer = integer ;
105
+ }
106
+
107
+ public List <NestedInteger > getList () {
108
+ return list ;
109
+ }
110
+
111
+ public NestedInteger add (NestedInteger nestedInteger ) {
112
+ this .list .add (nestedInteger );
113
+ return this ;
114
+ }
115
+ }
0 commit comments