Problem overview
The function climb_stairs receives n, the number of steps in a staircase, and returns in how many distinct ways you can climb to the top when each move is 1 or 2 steps. Order matters: 1 step then 2 steps is a different way from 2 steps then 1.
For n = 3 the answer is 3: 1+1+1, 1+2 and 2+1. For n = 4 it is 5. The constraints keep n between 1 and 45, and the answer always fits in an i32.
Approach: build the counts from the ground up
Think about the very last move. To stand on step k, you either came from step k - 1 with a 1-step move, or from step k - 2 with a 2-step move. These two groups of ways never overlap, because the last move differs. So:
ways(k) = ways(k - 1) + ways(k - 2)
This is dynamic programming: each answer is built from smaller answers that are already known. Computing ways(k) by plain recursion would repeat the same sub-problems many times and take exponential time. Going from the bottom up computes each step once.
The recurrence only looks two steps back, so there is no need for a whole table. Two variables are enough: the count for the step just below the current one, and the count for the step below that.
Step by step
- Create
prevwithlet mut, set to 1. It stands for step 0, the ground. There is exactly one way to be there: don't move. Counting it is what makes step 2 come out as 1 + 1 = 2. - Create
curr, also 1. Step 1 can only be reached with a single 1-step move. Whennis 1, this is already the answer. - Loop over the steps 2 to
nwithfor _ in 2..=n. The range..=includes its end, so stepnis counted. The loop doesn't need the step number, so the variable is named_. Forn = 1the range2..=1is empty and the body never runs. - Add the two counts into a new variable
ways. That is the number of ways to reach the step this round is counting. - Shift the window up one step:
prevtakes the value ofcurr, thencurrtakesways. The order matters. Ifcurrwere overwritten first,prevwould receive the new value and the old one would be lost. - Return
curr, with no semicolon, as the last expression. After the last round it holds the count for stepn.
The solution in Rust
impl Solution {
fn climb_stairs(n: i32) -> i32 {
let mut prev = 1;
let mut curr = 1;
for _ in 2..=n {
let ways = prev + curr;
prev = curr;
curr = ways;
}
curr
}
}
For n = 5, the values after each round are:
step 2: prev=1 curr=2
step 3: prev=2 curr=3
step 4: prev=3 curr=5
step 5: prev=5 curr=8
so climb_stairs(5) returns 8. The counts 1, 1, 2, 3, 5, 8 are the Fibonacci numbers.
The Rust details worth a closer look:
- Inferred types.
prevandcurrare never annotated. The finalcurris returned as ani32, so the compiler gives both variables that type. mutis required. Variables are immutable by default.prev,currand nothing else change, which is whywaysis a plainlet...=against...2..nstops beforenand would leave out the last step.- The
_loop variable. It tells Rust, and the reader, that the counter is deliberately unused. A named but unused variable would trigger a warning. - The final expression. A line without a semicolon is the value of the block, so
curron its own is the return value.
Complexity
Time: O(n). The loop runs n - 1 times and each round does a constant amount of work.
Space: O(1). Only prev, curr and ways are stored, however large n is. A table with one entry per step would need O(n).
Edge cases and common mistakes
- Starting
prevat 0. The ground then has no ways to stand on it, andclimb_stairs(2)would return 1 instead of 2. - Using
2..n. The loop would run one round too few and return the answer for stepn - 1. - Updating in the wrong order. Writing
curr = waysbeforeprev = currsets both variables to the same number. - Overflow beyond the constraints. The lesson builds with optimizations, where integer overflow wraps silently.
climb_stairs(45)returns 1836311903, butclimb_stairs(46)returns -1323752223 andclimb_stairs(47)returns 512559680. In a debug build the same overflow would panic. That is why the constraints stop at 45; for largern, usei64oru64. - Counting the same way twice. The two groups, last move of 1 and last move of 2, are disjoint, so adding them is right; no subtraction is needed.
Testing
n = 1gives1.n = 2gives2.n = 3gives3.n = 4gives5.n = 10gives89.n = 45gives1836311903.
