Skip to content

Commit d94e6bb

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18 MB (8.12%)
1 parent 3b5aa34 commit d94e6bb

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

0339-nested-list-weight-sum/solution.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Approach 1: Depth-first Search
1+
# Approach 1: Depth First Search
2+
3+
# n = total no. of nested elements
4+
# Time: O(n)
5+
# Space: O(n)
26

37
# """
48
# This is the interface that allows for creating nested lists.
@@ -45,15 +49,14 @@
4549

4650
class Solution:
4751
def depthSum(self, nestedList: List[NestedInteger]) -> int:
48-
49-
def dfs(nested_list, depth):
52+
def dfs(nestedList, depth):
5053
total = 0
51-
for nested in nested_list:
52-
if nested.isInteger():
53-
total += nested.getInteger() * depth
54+
for element in nestedList:
55+
if element.isInteger():
56+
total += element.getInteger() * depth
5457
else:
55-
total += dfs(nested.getList(), depth + 1)
58+
total += dfs(element.getList(), depth + 1)
5659
return total
57-
60+
5861
return dfs(nestedList, 1)
5962

0 commit comments

Comments
 (0)