LeetCode 75 - 70. Climbing Stairs

LeetCode 75 - 70. Climbing Stairs

1. Description of the topic:

Suppose you are climbing stairs. It takes n steps before you can reach the top of the building.

You can climb 1 or 2 steps at a time. How many different ways can you get to the top of the building?

Example 1:

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top of the building.

  1. Tier 1 + Tier 1
  2. 2nd stage

Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top of the building.

  1. Tier 1 + Tier 1 + Tier 1
  2. Tier 1 + Tier 2
  3. 2nd stage + 1st stage

hint:

1 <= n <= 45

Source: LeetCode

Link: https://leetcode.cn/problems/climbing-stairs

The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, for non-commercial reprints, please indicate the source.

2. Thinking analysis:

  1. What thought does this question examine? What is your train of thought?

    My first thought for this question is naturally recursion. Use dfs to complete the question, and return 1 when n is 0 and 1 as the exit of recursive exit. As for the recursive method, the sum of the returned values ​​​​of n-1 and n-2 is used as the value of the current climbStairs(n).

    func climbStairs(n int) int {
          
          
        if n == 0 || n == 1{
          
          
            return 1
        }
        return climbStairs(n-1) + climbStairs(n-2)
    }
    

    However, I never expected that this actually timed out. When n is equal to 44, it timed out!

    image-20221019211355689

    So, I have an idea, which is to use slices to save the results of each calculation, and each time I only need to find the two values ​​​​in front of the slice element and add them together. We only need to put the initial value of 2 1s into the slice, and then if n is less than 2, we directly return 1. Then starting from i equal to 2, until n, we assign the slice element to the sum of the first two elements and then put it into the slice. Finally, return the element with index n of the slice arr.

    func climbStairs(n int) int {
          
          
       var arr []int = make([]int,0,5)
       if n<2{
          
          
           return 1
       }
       arr = append(arr,1,1)
       for i:=2; i<=n; i++{
          
          
           arr = append(arr,arr[i-1]+arr[i-2])
       }
       return arr[n]
    }
    
  2. When doing the questions, did you pass it once, what problems did you encounter, and what details did you need to pay attention to?

    It is not a one-time pass, and the recursive method will time out, so I use an array to exchange space for time, and successfully solve this problem, but the memory consumption is large!

    image-20221019212702336

  3. There are several solutions, which solution has the lowest time complexity, which solution has the lowest space complexity, and what is the optimal solution? What are other people's solutions, and who is more efficient? If implemented in different languages, which language is the fastest?

    The following method uses less memory than my method. It only needs 3 variables and does not need to save all paths. If n is equal to a large value, my method consumes more memory, so this is an optimization solution!

    class Solution {
          
          
    public:
        int climbStairs(int n) {
          
          
            if(n == 1){
          
          return 1;}
            if(n == 2){
          
          return 2;}
            int a = 1, b = 2, temp;
            for(int i = 3; i <= n; i++){
          
          
                temp = a;
                a = b;
                b = temp + b;
            }
            return b;   
        }
    };
    

    image-20221019213220246

    type matrix [2][2]int
    
    func mul(a, b matrix) (c matrix) {
          
          
        for i := 0; i < 2; i++ {
          
          
            for j := 0; j < 2; j++ {
          
          
                c[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j]
            }
        }
        return c
    }
    
    func pow(a matrix, n int) matrix {
          
          
        res := matrix{
          
          {
          
          1, 0}, {
          
          0, 1}}
        for ; n > 0; n >>= 1 {
          
          
            if n&1 == 1 {
          
          
                res = mul(res, a)
            }
            a = mul(a, a)
        }
        return res
    }
    
    func climbStairs(n int) int {
          
          
        res := pow(matrix{
          
          {
          
          1, 1}, {
          
          1, 0}}, n)
        return res[0][0]
    }
    
    作者:LeetCode-Solution
    链接:https://leetcode.cn/problems/climbing-stairs/solution/pa-lou-ti-by-leetcode-solution/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    

3. AC code:

func climbStairs(n int) int {
     
     
   var arr []int = make([]int,0,5)
   if n<2{
     
     
       return 1
   }
   arr = append(arr,1,1)
   for i:=2; i<=n; i++{
     
     
       arr = append(arr,arr[i-1]+arr[i-2])
   }
   return arr[n]
}

4. Summary:

Among these three solutions, the time complexity and space complexity of my method are both O(n). Among the other solutions, the time complexity of the first solution is also O(n), but its space complexity is O(1). The time complexity of matrix fast power solution is O(log n), and the space complexity is O(1).

Guess you like

Origin blog.csdn.net/qq_36045898/article/details/131497968