forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinCostClimbingStairs.swift
More file actions
25 lines (22 loc) · 842 Bytes
/
Copy pathMinCostClimbingStairs.swift
File metadata and controls
25 lines (22 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Question Link: https://leetcode.com/problems/min-cost-climbing-stairs/description/
* Primary idea: Dynamic Programming, dp[i] represents the current element will be
* added to previous smallest sum, so the Bellman equation is
* dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class MinCostClimbingStairs {
func minCostClimbingStairs(_ cost: [Int]) -> Int {
var dp = [Int](repeating: Int.max, count: cost.count + 1)
(dp[0], dp[1]) = (cost[0], cost[1])
for i in 2...cost.count {
if i == cost.count {
dp[i] = min(dp[i - 1], dp[i - 2])
} else {
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
}
}
return dp[cost.count]
}
}