Problem overview
The method climbStairs receives the number of steps n and returns how many distinct ways there are to climb them, taking 1 or 2 steps at a time. Order matters: 1 + 2 and 2 + 1 count as two different ways.
For n = 4 the answer is 5: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. The constraints are 1 <= n <= 45, and the answer fits in an int.
Approach: build up from the ground
Listing every sequence of moves works for small n but the number of sequences grows quickly, so it is better to count instead of list. Look at the last move before standing on step i. It was either a 1-step move from step i - 1 or a 2-step move from step i - 2. Those two groups never overlap, so
ways(i) = ways(i - 1) + ways(i - 2)
This is dynamic programming: each answer reuses answers for smaller steps. The pattern is the Fibonacci sequence. Running it for n = 1, 2, 3, 4, 5, 10 gives 1, 2, 3, 5, 8, 89.
A table with one entry per step would work, but ways(i) only needs the two entries just below it. Two int variables are enough, which keeps the extra space constant.
Step by step
- Create
prev= 1, the ways to stand on step 0. There is exactly one: 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. Whennis 1, the loop never runs andcurris already the answer. - Loop
ifrom 2 toninclusive. Stepnis the top, and it needs its count too, so the condition isi <= n. At the start of each round,currholds the count for stepi - 1andprevthe count for stepi - 2. - Add the two counts into
ways, the count for stepi. - Slide the window up. Set
prev = currfirst, thencurr = ways. Reversing the order overwrites the valueprevneeds. - Return
curr. After the last round it holds the count for stepn.
The solution in Java
class Solution {
public int climbStairs(int n) {
int prev = 1;
int curr = 1;
for (int i = 2; i <= n; i++) {
int ways = prev + curr;
prev = curr;
curr = ways;
}
return curr;
}
}
Java details worth noticing:
- Local variables only.
prev,currandwaysare plainintlocals.waysis declared inside the loop, so a fresh one exists in every round. intoverflow is silent. Java does not throw when anintgoes past 2,147,483,647: it wraps around to a negative number. The constraintn <= 45is what keeps this safe. Forn = 45the method returns 1836311903, but forn = 46it returns -1323752223, and forn = 50it returns -1109825406. Nothing warns you, so alongwould be needed for bigger inputs, and eventually even that overflows.- Assignment order. The two assignments inside the loop run top to bottom. The sequence
prev = curr; curr = ways;is correct becausewayswas computed before either variable changed.
Complexity
Time: O(n). The loop runs n - 1 times and each round does a constant amount of work.
Space: O(1). Only three integers are used, whatever the value of n. A full table of n + 1 entries would cost O(n), and a plain recursive version with no memory of earlier answers would take exponential time, since it recomputes the same steps many times.
Edge cases and common mistakes
n = 1. The loop starts at 2 and never runs, and the method returns the initialcurr, 1.n = 2. One round runs:ways = 1 + 1 = 2, and the method returns 2.- Writing
i < n. The loop then stops one round early and returns the count for stepn - 1. Forn = 4that gives 3 instead of 5. - Starting
prevat 0. The count for the ground is 1, not 0. With 0, step 2 would count 0 + 1 = 1 way instead of 2. - Overwriting in the wrong order.
curr = ways; prev = curr;leaves both variables equal, so the sum doubles one count instead of adding two.
Testing
n = 1gives1.n = 2gives2.n = 3gives3.n = 4gives5.n = 5gives8.n = 45gives1836311903.
