Problem overview
The method climbStairs receives the number of steps n and returns in how many distinct ways you can climb them, taking 1 or 2 steps at a time. Order counts: 1 step then 2 is a different way from 2 steps then 1.
For n = 3 the answer is 3 (1+1+1, 1+2, 2+1), and for n = 4 it is 5. The constraints say 1 <= n <= 45 and that the answer fits in a 32-bit integer, so a plain int is enough.
Approach: build up from the ground
Listing every sequence of moves works for tiny n, but the number of sequences grows quickly. A better question is about the last move. To stand on step i, you came either from step i - 1 with a 1-step move, or from step i - 2 with a 2-step move. These two groups of ways don't overlap, so
ways(i) = ways(i - 1) + ways(i - 2)
That is dynamic programming: each answer is built from answers to smaller versions of the same problem. Since ways(i) only needs the two previous values, there is no need for a table. Two variables, moved forward one step at a time, are enough.
The base cases are what make the formula start. There is one way to be on step 0, the ground: don't move. There is one way to reach step 1: a single 1-step move. Counting the ground as 1 makes step 2 come out right, since 1 + 1 = 2.
A direct recursive version, climbStairs(n - 1) + climbStairs(n - 2), follows the same formula but solves the same smaller problems over and over: climbStairs(2) alone is recomputed many times when n is 45. Going from the bottom up computes each value exactly once, which is the whole improvement.
Step by step
- Create
prevas 1. It holds the ways to stand on step 0. - Create
curras 1. It holds the ways to reach step 1. Whennis 1 the loop below never runs, andcurris already the answer. - Loop from step 2 up to
ninclusive. The condition isi <= n: stepnis the top and needs its count too. At the start of each round,curris the count for stepi - 1andprevfor stepi - 2. - Add the two counts into
ways. This is the formula above, for stepi. - Slide the window up one step.
prevtakes the value ofcurr, thencurrtakesways. The order matters: ifcurrwere changed first,prevwould copy the new value and the old one would be lost. - Return
curr. After the last round it holds the count for stepn.
The solution in C++
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;
}
};
Here is what the variables hold for n = 4, after each round of the loop:
i | ways | prev | curr |
|---|---|---|---|
| 2 | 2 | 1 | 2 |
| 3 | 3 | 2 | 3 |
| 4 | 5 | 3 | 5 |
The function returns 5, matching the five ways listed in the statement.
Some C++ details:
wayslives inside the loop. It is declared in the loop body, so each round gets a fresh variable. Nothing outside the loop needs it.- The sequence is Fibonacci. The counts for
n = 1, 2, 3, 4, 5, 6, 7are 1, 2, 3, 5, 8, 13, 21. The code is the usual two-variable Fibonacci loop, shifted by one position. - Signed overflow is undefined behaviour. For
n = 45the function returns 1836311903, which fits in anint. The next value, forn = 46, is 2971215073, which doesn't. Signed overflow in C++ isn't guaranteed to wrap: the behaviour is undefined, so the boundn <= 45in the statement matters.
Complexity
Time: O(n). The loop runs n - 1 times with a constant amount of work in each round.
Space: O(1). Three integers, whatever the value of n. Keeping a whole vector of counts would also work, but costs O(n) memory for no benefit, since only the last two entries are ever read.
Edge cases and common mistakes
n = 1. The loop condition2 <= 1is false, so the function returnscurr, which is 1. That is whycurrstarts at 1 and not 0.n = 2. One round runs:ways = 1 + 1 = 2. This is where counting the ground as 1 pays off.- Using
i < n. The loop stops one step early and returns the count for stepn - 1. Forn = 4it would give 3 instead of 5. - Assigning in the wrong order. With
curr = ways;beforeprev = curr;, both variables end up equal towaysand the sequence doubles instead of following the formula. Forn = 4that version returns 8 instead of 5. - Plain recursion.
climbStairs(n - 1) + climbStairs(n - 2)is correct but recomputes the same values again and again, so it takes exponential time and is too slow forn = 45.
Testing
n = 1gives1.n = 2gives2.n = 3gives3.n = 4gives5.n = 10gives89.n = 45gives1836311903.
