Skip to content

Commit 2dd8ca2

Browse files
authored
feat: add swift implementation to lcof2 problem: No.088 (doocs#3473)
1 parent c0815c2 commit 2dd8ca2

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lcof2/剑指 Offer II 088. 爬楼梯的最少成本/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ function minCostClimbingStairs(cost: number[]): number {
141141
}
142142
```
143143

144+
#### Swift
145+
146+
```swift
147+
class Solution {
148+
func minCostClimbingStairs(_ cost: [Int]) -> Int {
149+
let n = cost.count
150+
var dp = Array(repeating: 0, count: n + 1)
151+
for i in 2...n {
152+
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
153+
}
154+
return dp[n]
155+
}
156+
}
157+
```
158+
144159
<!-- tabs:end -->
145160

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

0 commit comments

Comments
 (0)