Problem overview
The method climbStairs receives n, the number of steps in a staircase. Each move climbs either 1 or 2 steps, and the method returns how many different sequences of moves reach step n exactly. Order matters: 1 + 2 and 2 + 1 are 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 keep n between 1 and 45, and the answer for 45 is 1836311903, which is still an ordinary JavaScript number with no precision problems.
Approach: count each step from the two below it
Listing every sequence works for n = 4 but grows quickly. Instead, ask how you can arrive at step i. The last move was either a 1-step from step i - 1 or a 2-step from step i - 2. Those two groups of ways never overlap, because they end with different moves, so:
ways(i) = ways(i - 1) + ways(i - 2)
This is dynamic programming: each answer is built from answers to smaller versions of the same problem. The base cases are ways(0) = 1 (standing on the ground is one way: don't move) and ways(1) = 1 (a single 1-step). Counting the ground as 1 is what makes ways(2) = 1 + 1 = 2 come out right.
A naive recursive function would recompute the same values again and again. Going from the bottom up avoids that, and since step i only needs the two steps below it, two variables are enough instead of a whole array.
Step by step
- Create
prevwithlet, set to 1. It holds the ways to reach step 0. - Create
currthe same way, also 1. It holds the ways to reach step 1. Whennis 1 the loop never runs and this is already the answer. - Loop
ifrom 2 up to and includingn. The condition isi <= n, because stepnneeds its count too. At the start of each round,curris the count for stepi - 1andprevfor stepi - 2. - Add the two into a
constnamedways. That is the count for stepi. - Shift the window:
prevtakes the oldcurr, thencurrtakesways. The order matters, because overwritingcurrfirst would lose the valueprevneeds. - Return
curr. After the last round it is the count for stepn.
The solution in JavaScript
class Solution {
climbStairs(n) {
let prev = 1;
let curr = 1;
for (let i = 2; i <= n; i++) {
const ways = prev + curr;
prev = curr;
curr = ways;
}
return curr;
}
}
Two JavaScript details are worth noting. The counters use let because they are reassigned in the loop, while ways is a const: a new one is created in every round and never changes after that. And because const is scoped to the loop body, ways doesn't exist after the loop, which is why the method returns curr.
Tracing n = 5
Starting from prev = 1 and curr = 1, the loop produces these values at the end of each round:
i=2 prev=1 curr=2
i=3 prev=2 curr=3
i=4 prev=3 curr=5
i=5 prev=5 curr=8
The answer for 5 steps is 8. The sequence 1, 2, 3, 5, 8, 13 is the Fibonacci sequence, shifted by one position.
Complexity
The loop runs n - 1 times with a constant amount of work per round, so the time is O(n). Only prev, curr and ways are stored, so the space is O(1). A table dp[0..n] would give the same time with O(n) space.
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. - Looping with
i < n. The loop stops one step early and returns the count for stepn - 1. Forn = 4it returns 3 instead of 5. - Updating in the wrong order. If
curr = wayscomes beforeprev = curr, both variables end up equal toways, and every later round just doubles it: 2, 4, 8 instead of 2, 3, 5. n = 1. The loop body never runs, and the method returns 1.
Testing
Results from the reference solution:
n = 1 -> 1
n = 2 -> 2
n = 3 -> 3
n = 4 -> 5
n = 6 -> 13
n = 10 -> 89
n = 45 -> 1836311903
