Problem overview
ClimbStairs receives the number of steps n and returns how many distinct ways there are to climb them when each move is 1 or 2 steps. Order matters: 1 then 2 and 2 then 1 are different ways.
For n = 4 there are five ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. The method returns 5. For n = 3 it returns 3, and for n = 2 it returns 2.
Approach: build up from the ground
Think 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. Those two groups of ways are different, and together they cover everything, so:
ways(i) = ways(i - 1) + ways(i - 2)
This is the Fibonacci recurrence. Computing it recursively from the top repeats the same subproblems many times and takes exponential time. Computing it from the bottom up solves each step once. And because step i only needs the two steps below it, there is no need for a whole table: two variables are enough.
The starting values are ways(0) = 1 (one way to stand on the ground: don't move) and ways(1) = 1 (a single 1-step move). Counting the ground as 1 makes ways(2) = 1 + 1 = 2 come out right.
Step by step
- Create
prev= 1. It holds the ways to reach step 0. - Create
curr= 1. It holds the ways to reach step 1. Whennis 1 the loop never runs andcurris already the answer. - Loop
ifrom 2 whilei <= n. Use<=: stepnis the top and needs its own count. - Compute
ways = prev + curr. At the start of each roundcurris the count for stepi - 1andprevthe count for stepi - 2. - Slide the window up one step:
prev = currfirst, thencurr = ways. Swapping the order would overwrite the valueprevneeds. - Return
curr. After the last round it holds the count for stepn.
For n = 5 the loop produces ways = 2, 3, 5, 8 for i = 2, 3, 4, 5, and the method returns 8.
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;
}
}
Details worth knowing:
intis enough for this lesson. The answer forn = 45is 1836311903, which fits in a 32-bitint, and the constraints stop at 45.- Overflow wraps silently. Beyond the constraints, C# does not warn you. The same method returns -1323752223 for
n = 46and 512559680 forn = 47. For largern, uselong(good up ton = 91) orSystem.Numerics.BigInteger. wayslives inside the loop. A variable declared in the loop body is created fresh each round, which is what you want for a temporary value.
Complexity
Time: O(n). The loop runs n - 1 times with constant work in each round.
Space: O(1). Only prev, curr, ways and i exist, however large n is. A table dp[0..n] would work too but needs O(n) memory for no benefit.
Edge cases and common mistakes
n = 1. The loop is skipped and the method returns 1.- Starting
prevat 0. It seems natural, because there are no steps on the ground. But thenways(2)comes out as 1 instead of 2. - Using
i < n. The loop stops one round early, and the method returns the count for stepn - 1. - Updating
currbeforeprev. Both variables end up equal and the counts stop growing correctly.
Testing
n = 1gives1.n = 2gives2.n = 3gives3.n = 4gives5.n = 10gives89.n = 45gives1836311903.
