LeetCode #746 Easy

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 0 or step 1 for free.
  • The top is just past the last step, at position n = len(cost).

Return the minimum total cost to reach the top.

Constraints
  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999
dynamic-programmingarray
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def minCostClimbingStairs(self, cost):
▶3 two_back, one_back = 0, 0
▶4 for i in range(2, len(cost) + 1):
▶5 here = min(one_back + cost[i - 1], two_back + cost[i - 2])
▶6 two_back, one_back = one_back, here
▶7 return one_back
10015020topcost0step 00step 1steps 0 and 1 cost nothing to reach
two back0step 0
one back0step 1
The number above a step is the cheapest total paid to stand on it. You may start on step 0 or step 1, so both are 0. A step's own cost is paid only when you leave it, and the top, past the last step, is the goal.
1001502010topcost0at 1+15leave=15via 10at 0+10leave=10via 0step 2: via 1 costs 15, via 0 costs 10 → 10
two back0step 1
one back10step 2
You arrive at step 2 by a one-step climb from step 1 or a two-step climb from step 0, paying the cost of the step you leave. Coming from step 0 is cheaper, so step 2 costs 10.
1001502010top15cost10at 2+20leave=30via 20at 1+15leave=15via 1the top: via 2 costs 30, via 1 costs 15 → 15
two back10step 2
one back15the top
You arrive at the top by a one-step climb from step 2 or a two-step climb from step 1, paying the cost of the step you leave. Coming from step 1 is cheaper, so the top costs 15. Step 0's number fades: nothing later reads it, so the code keeps only two values.
1001502010top15cost15step 1=15totalanswer: 15, start on step 1
answer15cheapest way to the top
The top's number is the answer: 15. Following each choice back from the top gives the route: start on step 1 and pay for step 1, then climb past the end.
05

Common pitfalls

Treating the last step as the top

✗ Wrong
for i in range(2, len(cost)):
    ...
return one_back
✓ Right
for i in range(2, len(cost) + 1):
    ...
return one_back

On [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

✗ Wrong
start on the cheaper of steps 0 and 1,
then always climb to the cheaper next step
✓ Right
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

✗ Wrong
here = cost[i] + min(one_back, two_back)
✓ Right
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.

06

Edge cases

Only two steps

The loop runs once, for the top, and returns min(cost[0], cost[1]): start on the cheaper step and climb past the end.

Steps that cost 0

Nothing special: a free step simply makes one arrival cost no more than the value below it.

07

Complexity

Time
O(n)
Space
O(1)
One pass over positions 2 to n with constant work each. Only the last two values are kept instead of a full table.
08

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.