File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
0339-nested-list-weight-sum Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ # time complexity: O(n)
2+ # space complexity: O(n)
3+
4+ # class NestedInteger:
5+ # def __init__(self, value=None):
6+ # """
7+ # If value is not specified, initializes an empty list.
8+ # Otherwise initializes a single integer equal to value.
9+ # """
10+ #
11+ # def isInteger(self):
12+ # """
13+ # @return True if this NestedInteger holds a single integer, rather than a nested list.
14+ # :rtype bool
15+ # """
16+ #
17+ # def add(self, elem):
18+ # """
19+ # Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
20+ # :rtype void
21+ # """
22+ #
23+ # def setInteger(self, value):
24+ # """
25+ # Set this NestedInteger to hold a single integer equal to value.
26+ # :rtype void
27+ # """
28+ #
29+ # def getInteger(self):
30+ # """
31+ # @return the single integer that this NestedInteger holds, if it holds a single integer
32+ # The result is undefined if this NestedInteger holds a nested list
33+ # :rtype int
34+ # """
35+ #
36+ # def getList(self):
37+ # """
38+ # @return the nested list that this NestedInteger holds, if it holds a nested list
39+ # The result is undefined if this NestedInteger holds a single integer
40+ # :rtype List[NestedInteger]
41+ # """
42+
43+ from typing import List
44+
45+
46+ class Solution :
47+ def depthSum (self , nestedList : List [NestedInteger ]) -> int :
48+ def dfs (currList , depth ):
49+ result = 0
50+ for element in currList :
51+ if element .isInteger ():
52+ result += (element .getInteger () * depth )
53+ else :
54+ result += dfs (element .getList (), depth + 1 )
55+ return result
56+ return dfs (nestedList , 1 )
57+
58+
59+
You can’t perform that action at this time.
0 commit comments