Min Cost Climbing Stairs
Reach the top of a staircase for the least total price when every step has a price and you climb 1 or 2.
The problem
cost[i] is the price of step i: once you pay it, you may climb 1 or 2 steps from there. You may begin on step 0 or on step 1, and the top is the position just past the last step. Return the smallest total price to reach the top.
Input cost = [5, 2, 8, 1, 6]
Output 3
Start on step 1 and pay 2, climb 2 to step 3 and pay 1, then climb 2 to the top.
Input cost = [1, 9, 2, 9, 3, 9]
Output 6
Start on step 0 and pay only for steps 0, 2 and 4: 1 + 2 + 3 = 6.
Key constraint: The top is position n, one past the last index, and standing there costs nothing.
How to spot the pattern
- You want the cheapest way to reach a goal through 1-step or 2-step moves.
- The best price for a step depends only on the best prices of the two steps below it.
The idea
State: reach(i) is the cheapest total price to arrive at step i, where step n is the top. You arrive at i either from step i - 1, paying cost[i - 1], or from step i - 2, paying cost[i - 2], so reach(i) = min(reach(i - 1) + cost[i - 1], reach(i - 2) + cost[i - 2]). Starting on step 0 or 1 is free, so reach(0) = reach(1) = 0, and the answer is reach(n).
Brute force: Try every sequence of 1-step and 2-step moves from both starting steps, which takes O(2^n) time.
Step by step
- 1Start with
twoBack = reach(0) = 0andoneBack = reach(1) = 0. - 2For each
ifrom 2 ton, computehere = min(oneBack + cost[i - 1], twoBack + cost[i - 2]). - 3Shift the pair:
twoBackbecomesoneBack, andoneBackbecomeshere. - 4Return
oneBack, which isreach(n), the cheapest way to the top.
See the pattern move
The 1d dynamic programming pattern on a small example. Step through it, or press play.
Code
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
# reach(i) = cheapest way to arrive at step i; the top is step len(cost)
# reach(i) = min(reach(i - 1) + cost[i - 1], reach(i - 2) + cost[i - 2])
two_back, one_back = 0, 0 # reach(0) and reach(1): you may start on either for free
for i in range(2, len(cost) + 1):
here = min(one_back + cost[i - 1], two_back + cost[i - 2])
two_back, one_back = one_back, here
return one_backComplexity
Time
O(n)
Space
O(1)
One pass over the steps that keeps only the last two reach values.
Watch out for
- Returning the best price for step
n - 1: the top is one past the last index. - Charging for the first step: you may start on step 0 or step 1 without paying to get there, so both reach values start at 0.
Follow-up questions
What if you could climb up to k steps at a time?
Take the minimum over the last k values of reach(j) + cost[j]. A sliding-window minimum with a monotonic deque keeps that O(n) instead of O(n * k).
How would you list the steps you paid for?
Remember for each step whether its best value came from 1 or 2 steps below, then walk back from the top.
In Hinglish
Har step par socho: ek step neeche se aaye ya do step neeche se, jo sasta pade. Top `n` par hai, `n - 1` par nahi, yeh mat bhoolna.