Climbing Stairs

A staircase has n steps. You start at the bottom (step 0) and want to stand exactly on step n. Each move takes you up either 1 step or 2 steps.

Return the number of different move sequences that reach the top. Two sequences are different if they differ in at least one move or in the order of moves, so 1, 2 and 2, 1 count separately.

Example 1

Input: n = 4 Output: 5 Why: The five sequences are 1+1+1+1, 2+1+1, 1+2+1, 1+1+2 and 2+2.

Example 2

Input: n = 1 Output: 1 Why: The only option is a single 1-step move.

Example 3

Input: n = 6 Output: 13 Why: Counting gives 1, 2, 3, 5, 8, 13 ways for 1 to 6 steps; each value is the sum of the two before it.

Constraints

  • 1 <= n <= 45

Pattern Path · Dynamic Programming · problem 146 of 169

Think about the very last move of any sequence that reaches step n. Where could you have been just before it?
You were either on step n - 1 (then took 1 step) or on step n - 2 (then took 2 steps), and these two groups never overlap.
So ways(n) = ways(n - 1) + ways(n - 2), with ways(0) = ways(1) = 1. Build it up from the bottom, keeping only the last two values.
Target: O(n) time, O(1) extra space.

Start simple

Recursively try both moves from every step without remembering results: roughly O(1.6^n) time, which is far too slow for n = 45.

The key idea

Classify every valid sequence by its last move. If the last move was a single step, the moves before it form some sequence that reaches step n - 1; if it was a double step, the earlier moves reach step n - 2. Every sequence falls into exactly one of these groups, so

ways(n) = ways(n - 1) + ways(n - 2)

with ways(0) = 1 (the empty sequence: you are already there) and ways(1) = 1. These are the Fibonacci numbers shifted by one.

Evaluating the recurrence by plain recursion recomputes the same subproblems an exponential number of times. Computing it bottom-up instead visits each step once. And since each value needs only the previous two, two variables are enough: slide them forward n - 1 times. For n = 45 the answer is about 1.8 billion, which still fits in a signed 32-bit integer.

Complexity

  • Time: O(n)
  • Space: O(1)

Python

class Solution:
    def climbStairs(self, n):
        # ways(i) = ways(i - 1) + ways(i - 2); only the last two values are needed.
        prev, curr = 1, 1  # ways to reach step 0 and step 1
        for _ in range(n - 1):
            prev, curr = curr, prev + curr
        return curr

JavaScript

var climbStairs = function(n) {
  // ways(i) = ways(i - 1) + ways(i - 2); only the last two values are needed.
  let prev = 1; // ways to reach step 0
  let curr = 1; // ways to reach step 1
  for (let i = 2; i <= n; i++) {
    [prev, curr] = [curr, prev + curr];
  }
  return curr;
};

What the interviewer is checking

This is the classic first dynamic programming question: the interviewer wants you to derive the recurrence from the last decision, spot the overlapping subproblems, and reduce memory to O(1).

Mistakes to avoid

  • Plain recursion without memoisation, which times out around n = 40.
  • Getting the base cases wrong (for example ways(2) = 1), which shifts every answer.
  • Treating 1+2 and 2+1 as the same sequence; order matters here.

Follow-ups to expect

  • What if you can climb 1, 2 or 3 steps? (Sum the previous three values.)
  • What if some steps are broken and cannot be used? (Set their way count to zero.)
  • Can you compute it in O(log n)? (Fast matrix exponentiation of Fibonacci.)

In real software

Counting the ways to compose a total from fixed-size pieces, such as tiling a strip or splitting a payment into fixed denominations in order, uses this same last-step recurrence.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url