Min Cost Climbing Stairs
Min Cost Climbing Stairs is LeetCode 746 (Easy). You get an array cost, where cost[i] is the price of stepping off step i.
- After paying
cost[i]you climb either one or two steps. - You may start on step
0or step1for free. - The top is just past the last step, at position
n = len(cost).
Return the minimum total cost to reach the top.
- 2 <= cost.length <= 1000
- 0 <= cost[i] <= 999
Intuition
The only ways to arrive at a position are a one-step climb from the position just below it or a two-step climb from two below. So the cheapest cost to reach position i is the cheaper of those two arrivals:
best(i) = min(best(i - 1) + cost[i - 1], best(i - 2) + cost[i - 2])
with best(0) = best(1) = 0, because you may start on either for free. Each value depends only on the two before it, so you fill positions 2, 3, … up to n in order, keeping just the last two numbers. The answer is best(n), the top.
Moves of fixed sizes along a line, a price per move, and a question about the cheapest or best total. Each position's best answer is built from a few earlier positions, which is one-dimensional dynamic programming.
Approach
Before reading on, write down the two positions you could have come from to land on step i, and what each arrival costs. Then decide what the first two values are. Aim for O(n) time and O(1) extra space.
Set the two free starting points
Set two_back = 0 and one_back = 0: the cheapest cost to reach steps 0 and 1. Starting on either is free, and a step's cost is paid only when you leave it.
Compute each position from the two below
For i from 2 to n, the cost to reach i is min(one_back + cost[i - 1], two_back + cost[i - 2]). The first term arrives by one step from i - 1, the second by two steps from i - 2.
Slide the window forward
Set two_back, one_back = one_back, here. Position i - 2 is never read again, so dropping it keeps the extra space constant.
Return the value for the top
After the loop, one_back holds the cost to reach position n, one past the last step. That is the answer, because the top is beyond the array, not on its last element.
Code in Python, C++ and Java
Common pitfalls
Treating the last step as the top
for i in range(2, len(cost)):
...
return one_backfor i in range(2, len(cost) + 1):
...
return one_backOn [10, 15, 20] the shorter loop returns 10, the cost to reach step 2. You still have to pay to leave a step, so the real answer is 15: start on step 1, pay 15, climb two.
Always taking the cheaper next step
start on the cheaper of steps 0 and 1, then always climb to the cheaper next step
here = min(one_back + cost[i - 1],
two_back + cost[i - 2])On [10, 20, 30] the greedy route starts on 10, moves to 20 and pays 30. Starting on the dearer step 1 and jumping straight to the top pays only 20. A cheap step can lead only to expensive ones.
Charging the cost on arrival
here = cost[i] + min(one_back, two_back)
here = min(one_back + cost[i - 1], two_back + cost[i - 2])
This charges the step you land on, so it reads cost[n] past the end of the array when computing the top, and the free start no longer means free.
Edge cases
The loop runs once, for the top, and returns min(cost[0], cost[1]): start on the cheaper step and climb past the end.
Nothing special: a free step simply makes one arrival cost no more than the value below it.
Complexity
Min Cost Climbing Stairs FAQ
How does this differ from Climbing Stairs?
Climbing Stairs counts routes, so it adds the two values below. Here each move has a price, so you take the minimum of two priced arrivals. The shape of the recurrence is the same.
Can I solve it with recursion and memoization?
Yes. Let f(i) be the cheapest cost from step i to the top: cost[i] + min(f(i + 1), f(i + 2)), with f(i) = 0 once i >= n. The answer is min(f(0), f(1)). Caching each f(i) makes it O(n), but it uses O(n) memory and up to 1000 levels of recursion.