Welcome to Subscribe On Youtube

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

Solution 2: Matrix Quick Power to Accelerate Recursion

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:

\[\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)$.

  • class Solution {
        public int climbStairs(int n) {
            int a = 0, b = 1;
            for (int i = 0; i < n; ++i) {
                int c = a + b;
                a = b;
                b = c;
            }
            return b;
        }
    }
    
  • class Solution {
    public:
        int climbStairs(int n) {
            int a = 0, b = 1;
            for (int i = 0; i < n; ++i) {
                int c = a + b;
                a = b;
                b = c;
            }
            return b;
        }
    };
    
  • class Solution:
        def climbStairs(self, n: int) -> int:
            a, b = 0, 1
            for _ in range(n):
                a, b = b, a + b
            return b
    
    
  • func climbStairs(n int) int {
    	a, b := 0, 1
    	for i := 0; i < n; i++ {
    		a, b = b, a+b
    	}
    	return b
    }
    
  • function climbStairs(n: number): number {
        let p = 1;
        let q = 1;
        for (let i = 1; i < n; i++) {
            [p, q] = [q, p + q];
        }
        return q;
    }
    
    
  • /**
     * @param {number} n
     * @return {number}
     */
    var climbStairs = function (n) {
        let a = 0,
            b = 1;
        for (let i = 0; i < n; ++i) {
            const c = a + b;
            a = b;
            b = c;
        }
        return b;
    };
    
    
  • class Solution {
        /**
         * @param Integer $n
         * @return Integer
         */
        function climbStairs($n) {
            if ($n <= 2) {
                return $n;
            }
            $dp = [0, 1, 2];
            for ($i = 3; $i <= $n; $i++) {
                $dp[$i] = $dp[$i - 2] + $dp[$i - 1];
            }
            return $dp[$n];
        }
    }
    
  • impl Solution {
        pub fn climb_stairs(n: i32) -> i32 {
            let (mut p, mut q) = (1, 1);
            for i in 1..n {
                let t = p + q;
                p = q;
                q = t;
            }
            q
        }
    }
    
    

All Problems

All Solutions