File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed
lcof2/剑指 Offer II 088. 爬楼梯的最少成本 Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -141,6 +141,21 @@ function minCostClimbingStairs(cost: number[]): number {
141
141
}
142
142
```
143
143
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
+
144
159
<!-- tabs: end -->
145
160
146
161
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments