Problem overview
The function climbStairs receives n, the number of steps in a staircase, and returns the number of distinct ways to climb to the top. Each move is 1 step or 2 steps, and the order of the moves matters: 1 then 2 is a different way from 2 then 1. The constraints say 1 <= n <= 45 and that the answer fits in an Int.
For n = 3 the three ways are 1+1+1, 1+2 and 2+1, so the answer is 3. For n = 4 there are five ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2.
Approach: build the answer from the ground up
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 are different last moves, so no way is counted twice, and the number of ways to reach step i is the sum of the two counts below it.
That is a recurrence, ways(i) = ways(i - 1) + ways(i - 2), and it is the Fibonacci rule. A plain recursive function would recompute the same values again and again, and its running time grows exponentially. Counting from the bottom up computes each value once. Because a value only depends on the previous two, there is no need for an array: two variables are enough.
The base cases are step 0, the ground, with 1 way (do nothing), and step 1 with 1 way (one 1-step move). Counting the ground as 1 is what makes step 2 come out right: 1 + 1 = 2, the ways 1+1 and 2.
Step by step
- Create
prevas avarequal to 1. It holds the ways to be on step 0. - Create
curras avarequal to 1. It holds the ways to reach step 1. - Loop
iover2..n. At the start of each round,curris the count for stepi - 1andprevis the count for stepi - 2. - Add the two counts into a
valnamedways. This is the count for stepi. - Slide the window up one step. Set
prevtocurrfirst, thencurrtoways. The reverse order would overwrite the valueprevneeds. - Return
curr. After the last round it holds the count for stepn.
Here is the loop for n = 4, printing the two variables at the start of each round:
i=2 prev=1 curr=1
i=3 prev=1 curr=2
i=4 prev=2 curr=3
end prev=3 curr=5
The last curr, 5, matches the five ways listed above.
The solution in Kotlin
class Solution {
fun climbStairs(n: Int): Int {
var prev = 1
var curr = 1
for (i in 2..n) {
val ways = prev + curr
prev = curr
curr = ways
}
return curr
}
}
The Kotlin details worth a look:
varandval.prevandcurrare reassigned in every round, so they must bevar.waysis created fresh in each round and never changes, sovalis right.2..nis inclusive on both ends. Stepnis therefore processed. Whennis 1, the range2..1is empty, the loop body never runs, and the function returns the initialcurr, which is 1. That is the correct answer for one step.- The loop variable is a
val.ican't be assigned inside the loop. Intis 32 bits. Overflow wraps silently instead of raising an error. Forn = 45the result is 1836311903, which fits. The constraint stops at 45 for this reason: carrying the same loop on ton = 50wraps around to the negative number -1109825406.- The function has a block body with an explicit
return. The last expression of a block is not returned automatically in Kotlin, as it would be in some other languages.
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, ways and the loop counter are stored, however large n is. A table with one entry per step would use O(n).
Edge cases and common mistakes
- The smallest input. For
n = 1the answer is 1, because the loop is skipped. Writing the loop as1..nwould add an extra round and return 2. - Starting
prevat 0. It is tempting to say there are no ways to stand on the ground. Then step 2 gives 0 + 1 = 1 instead of 2, and every later value is wrong. - Updating in the wrong order. If
curris changed beforeprevis set,prevreceives the new value and the two counts become equal, so the sequence stops growing as it should. - Looping to
n - 1or usinguntil n. The range2 until nstops one step short, and the function returns the count for stepn - 1. - Ordered or unordered. The problem counts 1+2 and 2+1 separately. A solution that counts only the number of 2-steps would give a smaller answer for
n = 3.
Testing
These inputs give the following results from the reference solution:
n = 1gives1.n = 2gives2.n = 3gives3.n = 4gives5.n = 5gives8.n = 10gives89.n = 45gives1836311903.
