Problem overview
The method climbStairs receives n, the number of steps in a staircase, and returns how many distinct ways there are to reach the top when each move climbs 1 or 2 steps. Order matters: 1 + 2 and 2 + 1 are different ways. The constraints are 1 <= n <= 45, and the answer fits in 32 bits.
For n = 4 the answer is 5: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2.
Approach: build the answer from the smaller staircases
Listing every sequence of moves works for tiny staircases but the number of sequences grows quickly. The useful observation is about the last move. To stand on step i, you arrived either from step i - 1 with a 1-step move or from step i - 2 with a 2-step move. Those two groups of ways never overlap, so:
ways(i) = ways(i - 1) + ways(i - 2)
That is dynamic programming: each answer reuses two smaller answers, so each is computed once. The recurrence only looks two steps back, so there is no need for a whole table. Two variables are enough.
Step by step
- Create
prev= 1, the ways to be on step 0, the ground. There is exactly one way to be there: don't move. Counting it makes step 2 come out as 1 + 1 = 2. - Create
curr= 1, the ways to reach step 1: a single 1-step move. Forn = 1this is already the answer. - Loop
ioverrange(2, n + 1), the steps from 2 ton. At the start of each roundcurrcounts stepi - 1andprevcounts stepi - 2. - Store
prev + currinways, the count for stepi. - Slide the window up one step:
prevtakes the value ofcurr, and thencurrtakesways. The order matters, because assigningcurrfirst would destroy the valueprevneeds. - Return
curr, which after the last round counts stepn.
The solution in Python
class Solution:
def climbStairs(self, n: int) -> int:
prev = 1
curr = 1
for i in range(2, n + 1):
ways = prev + curr
prev = curr
curr = ways
return curr
Tracing n = 5 shows the pair sliding along. These are the values of prev and curr at the end of each round:
i | prev | curr |
|---|---|---|
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | 3 | 5 |
| 5 | 5 | 8 |
Python details worth knowing:
range(2, n + 1)stops before its end, son + 1is what makes stepncount. Forn = 1the range is empty and the loop body never runs.- Python integers have no fixed size, so there is no overflow. The constraints keep the result inside a 32-bit signed integer anyway:
climbStairs(45)returns 1836311903. - Python can swap and slide in one line:
prev, curr = curr, prev + currevaluates the right side first, so it is equivalent. The lesson's three lines make each move visible.
Complexity
Time: O(n), one loop round per step from 2 to n, with constant work in each. Space: O(1), as only prev, curr, ways and i are stored. A recursive version that calls ways(i - 1) + ways(i - 2) without remembering results recomputes the same values again and again and takes exponential time.
Edge cases and common mistakes
- Starting
prevat 0. Then step 2 would give 0 + 1 = 1 instead of 2. The ground counts as one way. - Using
range(2, n). The loop stops one step early and returns the answer forn - 1. - Updating in the wrong order. Setting
curr = waysbeforeprev = currmakes both variables equal. - Returning
ways. Forn = 1the loop never runs andwaysdoesn't exist.curris always defined.
Testing
n = 1gives1.n = 2gives2.n = 3gives3.n = 4gives5.n = 10gives89.n = 45gives1836311903.
