LeetCode 0070. Climbing Stairs Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0070. Climbing Stairs

Description

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

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).

PythonJavaC++GoTypeScriptRustJavaScriptPHP
class Solution: def climbStairs(self, n: int) -> int: a, b = 0, 1 for _ in range(n): a, b = b, a + b return b(code-box)

Solution 2: Matrix Quick Power to Accelerate Recursion

We set Fib(n) to represent a 1 × 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) × base = Fib(n), that is:

$$ \begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix} \times base = \begin{bmatrix} F_n & F_{n - 1} \end{bmatrix} $$

Since F_n = F_{n - 1} + F_{n - 2}, the first column of the matrix base is:

$$ \begin{bmatrix} 1 \ 1 \end{bmatrix} $$

The second column is:

$$ \begin{bmatrix} 1 \ 0 \end{bmatrix} $$

Therefore:

$$ \begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix} \times \begin{bmatrix}1 & 1 \ 1 & 0\end{bmatrix} = \begin{bmatrix} F_n & F_{n - 1} \end{bmatrix} $$

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).

PythonJavaC++GoTypeScriptJavaScript
import numpy as np class Solution: def climbStairs(self, n: int) -> int: res = np.asmatrix([(1, 1)], np.dtype("O")) factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O")) n -= 1 while n: if n & 1: res *= factor factor *= factor n >>= 1 return res[0, 0](code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !