Skip to content

Commit 2c73601

Browse files
committed
no message
1 parent da998c1 commit 2c73601

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift/339_Nested_List_Weight_Sum.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import Foundation
2222
* public func getList() -> [NestedInteger]
2323
* }
2424
*/
25+
26+
// Old solution
2527
class Solution {
2628
func depthSum(_ nestedList: [NestedInteger]) -> Int {
2729
var wrightSum = 0
@@ -41,3 +43,24 @@ class Solution {
4143
}
4244
}
4345
}
46+
47+
48+
49+
// New solution
50+
class Solution {
51+
func depthSum(_ nestedList: [NestedInteger]) -> Int {
52+
return depthSumDFSHelper(nestedList, 1)
53+
}
54+
55+
func depthSumDFSHelper(_ nestedList: [NestedInteger], _ currentDepth: Int) -> Int {
56+
var currentSum = 0
57+
for nInt in nestedList {
58+
if nInt.isInteger() {
59+
currentSum += (nInt.getInteger() * currentDepth)
60+
} else {
61+
currentSum += (depthSumDFSHelper(nInt.getList(), currentDepth + 1))
62+
}
63+
}
64+
return currentSum
65+
}
66+
}

0 commit comments

Comments
 (0)