Skip to content

Commit 163f372

Browse files
authored
feat: add swift implementation to lcof2 problem: No.100 (doocs#3506)
1 parent 399f853 commit 163f372

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lcof2/剑指 Offer II 100. 三角形中最小路径之和/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ class Solution:
155155
return dp[0]
156156
```
157157

158+
#### Swift
159+
160+
```swift
161+
class Solution {
162+
func minimumTotal(_ triangle: [[Int]]) -> Int {
163+
let n = triangle.count
164+
var dp = Array(repeating: 0, count: n + 1)
165+
166+
for i in (0..<n).reversed() {
167+
for j in 0...i {
168+
dp[j] = min(dp[j], dp[j + 1]) + triangle[i][j]
169+
}
170+
}
171+
172+
return dp[0]
173+
}
174+
}
175+
```
176+
158177
<!-- tabs:end -->
159178

160179
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
func minimumTotal(_ triangle: [[Int]]) -> Int {
3+
let n = triangle.count
4+
var dp = Array(repeating: 0, count: n + 1)
5+
6+
for i in (0..<n).reversed() {
7+
for j in 0...i {
8+
dp[j] = min(dp[j], dp[j + 1]) + triangle[i][j]
9+
}
10+
}
11+
12+
return dp[0]
13+
}
14+
}

0 commit comments

Comments
 (0)