Skip to content

Commit 0ff1c87

Browse files
nested list weight sum II
1 parent d4233aa commit 0ff1c87

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package medium;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Queue;
6+
7+
import classes.NestedInteger;
8+
9+
public class NestedListWeightSumII {
10+
11+
public int depthSumInverse(List<NestedInteger> nestedList) {
12+
Queue<NestedInteger> q = new LinkedList<NestedInteger>();
13+
for(NestedInteger next : nestedList){
14+
q.offer(next);
15+
}
16+
int prev = 0, total = 0;
17+
18+
while(!q.isEmpty()){
19+
int size = q.size();
20+
int levelSum = 0;
21+
for(int i = 0; i < size; i++){
22+
NestedInteger next = q.poll();
23+
if(next.isInteger()) levelSum += next.getInteger();
24+
else{
25+
List<NestedInteger> list = next.getList();
26+
for(NestedInteger n : list){
27+
q.offer(n);
28+
}
29+
}
30+
}
31+
prev += levelSum;
32+
total += prev;
33+
}
34+
return total;
35+
}
36+
37+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../../blob/master/EASY/src/easy/GuessNumberHigherorLower.java)| O(logn)|O(1) | Easy| Binary Search
3434
|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../../blob/master/MEDIUM/src/medium/RangeAddition.java)| O(n+k)|O(1) | Medium|
3535
|366|[Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/)|[Solution](../../blob/master/MEDIUM/src/medium/FindLeavesofBinaryTree.java)| O(n)|O(h) | Medium| DFS
36+
|364|[Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/)|[Solution](../../blob/master/MEDIUM/src/medium/NestedListWeightSumII.java)| O(n)|O(h) | Medium| DFS
3637
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../../blob/master/EASY/src/easy/LoggerRateLimiter.java)| amortized O(1)|O(k) | Easy| HashMap
3738
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
3839
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy