Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created June 8, 2021 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/b12f0007b701e285a77f58382077048b to your computer and use it in GitHub Desktop.
Save vrat28/b12f0007b701e285a77f58382077048b to your computer and use it in GitHub Desktop.
Min cost climbing stairs (Swift| LC 746)
class Solution {
func minCostClimbingStairs(_ cost: [Int]) -> Int {
var dp = [Int](repeating:0, count: cost.count+1)
for i in 2..<dp.count{
let oneStepCost = dp[i - 1] + cost[i - 1]
let twoStepCost = dp[i - 2] + cost[i - 2]
dp[i] = min(oneStepCost, twoStepCost)
}
return dp[cost.count]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment