min-cost-climbing-stairs

This commit is contained in:
Gleb Koval 2022-07-10 03:15:01 +01:00
parent c9bc975b1c
commit 297be9db08
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package main
// Basically a slightly altered,
// more hard-coded version of jump-game-vi
func minCostClimbingStairs(cost []int) int {
last := [2]int{0, 0}
for _, c := range cost {
if last[0] < last[1] {
last[0], last[1] = last[1], last[0]+c
} else {
last[0], last[1] = last[1], last[1]+c
}
}
if last[0] < last[1] {
return last[0]
}
return last[1]
}