Go•min-cost-climbing-stairs/solution.go
package main
func minCostClimbingStairs(cost []int) int {
oneAhead, twoAhead := 0, 0
for index := len(cost) - 1; index >= 0; index-- {
current := cost[index] + minCost(oneAhead, twoAhead)
twoAhead, oneAhead = oneAhead, current
}
return minCost(oneAhead, twoAhead)
}
func minCost(a int, b int) int {
if a < b {
return a
}
return b
}
Python•min-cost-climbing-stairs/solution.py
class Solution:
def minCostClimbingStairs(self, cost: list[int]) -> int:
one_ahead = two_ahead = class="syntax-number">0
for value in reversed(cost):
current = value + min(one_ahead, two_ahead)
two_ahead, one_ahead = one_ahead, current
return min(one_ahead, two_ahead)
TypeScript•min-cost-climbing-stairs/solution.ts
function minCostClimbingStairs(cost: number[]): number {
let oneAhead = class="syntax-number">0;
let twoAhead = class="syntax-number">0;
for (let index = cost.length - class="syntax-number">1; index >= class="syntax-number">0; index -= class="syntax-number">1) {
const current = cost[index] + Math.min(oneAhead, twoAhead);
twoAhead = oneAhead; oneAhead = current;
}
return Math.min(oneAhead, twoAhead);
}