Problem overview
The function climbStairs receives the number of steps n and returns how many distinct ways there are to reach the top, climbing 1 or 2 steps at a time. Order matters: 1 step then 2 steps is a different way from 2 steps then 1. The constraints are 1 <= n <= 45, and the answer always fits in a 32-bit integer.
For n = 4 there are five ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. For n = 3 there are three, and for n = 2 there are two.
Approach: build the answer from the two steps below
Think about the last move onto 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 of ways never overlap, because their last moves differ, so the counts add up:
ways(i) = ways(i - 1) + ways(i - 2)
This is dynamic programming: each answer reuses answers for smaller inputs. Writing it as a plain recursive function would recompute the same counts again and again, and the number of calls grows exponentially. Counting from the bottom up computes each step once.
Since ways(i) needs only the two previous counts, there is no need to store a whole table. Two variables are enough, and they move up the staircase one step per round.
The base cases are what start the chain. Standing on the ground, step 0, counts as one way: don't move. Step 1 has one way, a single 1-step move. Counting the ground as 1 is what makes step 2 come out right, because 1 + 1 = 2.
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. Ifnis 1, the loop never runs andcurris already the answer. - Loop from step 2 up to and including
n. At the start of each round,curris the count for stepi - 1andprevthe count for stepi - 2. The condition isi <= n, because stepnneeds its own count. - Add the two counts into a new variable
ways: the number of ways to reach stepi. - Shift both counts up one step. Set
prevtocurrfirst, thencurrtoways. The order matters, because changingcurrfirst would overwrite the valueprevneeds. - Return
curr. After the last round it holds the count for stepn.
Here is the loop for n = 5:
i 2 prev 1 curr 1 ways 2
i 3 prev 1 curr 2 ways 3
i 4 prev 2 curr 3 ways 5
i 5 prev 3 curr 5 ways 8
Each ways becomes the next curr, and each curr becomes the next prev. The sequence 1, 1, 2, 3, 5, 8 is the Fibonacci sequence, so the answer for n steps is the Fibonacci number that follows n.
The solution in Go
func climbStairs(n int) int {
prev := 1
curr := 1
for i := 2; i <= n; i++ {
ways := prev + curr
prev = curr
curr = ways
}
return curr
}
The Go details worth a closer look:
:=declares,=assigns.prev,currandwaysare created with:=. Inside the loop,prev = currandcurr = waysonly change existing variables. Writing:=there would be a compile error, because there would be no new variable on the left.wayslives inside the loop body. It is created fresh in every round and disappears at the closing brace, which is whyreturn curris the only value that survives.intis 64 bits on a 64-bit machine. The largest answer here, forn = 45, is 1836311903, which also fits in 32 bits, as the constraints promise.- Go has a single loop keyword. The three-part
forcovers what other languages write asforandwhile.
Complexity
Time: O(n). The loop runs n - 1 times and each round does a fixed amount of work.
Space: O(1). Three integer variables, whatever the value of n.
Edge cases and common mistakes
- Using
i < n. The loop stops one round early and returns the count for stepn - 1. Forn = 4that gives 3 instead of 5. - Updating in the wrong order. If
curr = wayscomes beforeprev = curr, both variables end up equal toways, and the counts double each round. Forn = 4this gives 8 instead of 5. - Starting
prevat 0. It is tempting to say the ground has no ways to reach. Then step 2 would be 0 + 1 = 1, but there are two ways to climb two steps. - The smallest input. For
n = 1the loop body never runs and the function returns 1, which is correct.
Testing
n = 1gives1.n = 2gives2.n = 3gives3.n = 4gives5.n = 6gives13.n = 10gives89.n = 45gives1836311903.
