cost[i] is the price of stepping on stair i. After paying you may climb one or two stairs, and you may start on stair 0 or 1. Return the minimum cost to reach the top (just past the last stair).
dp[i] = cheapest way to arrive at step i = min(dp[i−1] + cost[i−1], dp[i−2] + cost[i−2]). Only the last two values are needed.
1function minCostClimbingStairs(cost: number[]): number {2let a = 0, b = 0; // cost to reach step i-2 and i-13for (let i = 2; i <= cost.length; i++) {4const c = Math.min(b + cost[i - 1], a + cost[i - 2]);5a = b; b = c;6}7return b;8}
Reach step 2 from 1 (pay 100) or 0 (pay 1).
Space: play/pause · ←/→: step