You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:
1 <= n <= 45
Solutions
Solution 1: Recursion
Thinking
The first idea is recursion: the last step is \(1\) or \(2\), so \(f(n) = f(n-1) + f(n-2)\). \(n \le 45\), and a naive recursion re-expands the same subproblems, which explodes.
The bottleneck is overlap. This is the Fibonacci sequence, so we can compute it bottom-up.
\(f[i]\) depends only on the previous two terms, so two rolling variables suffice: \(O(n)\) time and \(O(1)\) extra space.
We define \(f[i]\) to represent the number of ways to climb to the \(i\)-th step, then \(f[i]\) can be transferred from \(f[i - 1]\) and \(f[i - 2]\), that is:
\[ f[i] = f[i - 1] + f[i - 2] \]
The initial conditions are \(f[0] = 1\) and \(f[1] = 1\), that is, the number of ways to climb to the 0th step is 1, and the number of ways to climb to the 1st step is also 1.
The answer is \(f[n]\).
Since \(f[i]\) is only related to \(f[i - 1]\) and \(f[i - 2]\), we can use two variables \(a\) and \(b\) to maintain the current number of ways, reducing the space complexity to \(O(1)\).
The time complexity is \(O(n)\), and the space complexity is \(O(1)\).
Solution 2: Matrix Quick Power to Accelerate Recursion
Thinking
Solution 1 is already \(O(n)\), which is enough for \(n \le 45\), but a linear recurrence is still slow when \(n\) grows.
A linear homogeneous recurrence is matrix multiplication: \(F_n\) comes from \(F_{n-1}\) times a fixed \(2 \times 2\) transition. Fast matrix exponentiation compresses \(n\) steps into \(O(\log n)\). Constants are larger; use it when the same recurrence needs to be sped up.
We set \(Fib(n)\) to represent a \(1 \times 2\) matrix \(\begin{bmatrix} F_n & F_{n - 1} \end{bmatrix}\), where \(F_n\) and \(F_{n - 1}\) are the \(n\)-th and \((n - 1)\)-th Fibonacci numbers respectively.
We hope to derive \(Fib(n)\) based on \(Fib(n-1) = \begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix}\). That is to say, we need a matrix \(base\), so that \(Fib(n - 1) \times base = Fib(n)\), that is:
We define the initial matrix \(res = \begin{bmatrix} 1 & 1 \end{bmatrix}\), then \(F_n\) is equal to the first element of the first row of the result matrix of \(res\) multiplied by \(base^{n - 1}\). We can solve it using matrix quick power.
The time complexity is \(O(\log n)\), and the space complexity is \(O(1)\).