70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to 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: 2
Output: 2
Explanation: There are two ways to climb to the top.
- 1 step + 1 step
- 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
4. 1 step + 1 step + 1 step
5. 1 step + 2 steps
6. 2 steps + 1 step
Constraints:
1 <= n <= 45
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/climbing-stairs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Think and method:
And made it a bias towards math skills topics, from the way of thinking to understand not complex topic and asked us to give n order staircase to plan, each time can only go 1 or 2 terrace stairs, from the simple algorithm idea, T (n) = T (n - 1) + T (n - 2) can realize the problem solving, of course that for n greater than or equal to 2, direct return 1 when n = 1, otherwise there is mistake.
1. Violent law
The first method is obvious. It is enough to directly convert the above mathematical formula into code, which is of course inefficient. Faced with such a relatively simple problem, we should try some efficient solutions, and the violent method will not be considered.
2. Dynamic programming
In the face of this problem, which requires the information of previous steps to be considered, dynamic programming is obviously very easy to think of. We consider a and B to represent T(n-1) and T(n-2), and then consider boundary conditions. Therefore, result is calculated from 1 instead of 0, and result=a+b.
A and B can update the value dynamically.
The code is very clean and much more efficient than the brute force method. Loops only need to be executed n times and use very little space.
Time complexity : O(n)
Space complexity : O(1)
3. Matrix fast power
I’m going to look at somebody else’s line of thinking, the math is so beautiful, and I’m also going to think about optimizing the violence method, but in general we just need to do an optimization of the recursive process, not necessarily limited to the idea of dynamic programming, but instead of using loops, just convert to powers.
So let’s do the math
Let’s call it a for T(n-1) and b for T(n-2).
So, instead of going through a loop to get the result, using a fast power algorithm, we can get a faster algorithm than dynamic programming
Time complexity : O(logn)
Space complexity : O(1)
Code:
2、
func climbStairs(n int) int {
a, b, result := 0, 0, 1
if n == 1{
return 1
}
for i := 0; i < n; i++ {
a = b
b = result
result = a + b
}
return result
}
3、
func climbStairs(n int) int {
if n == 1{
return 1
}
a1 := [][]int{
[]int{1, 1},
[]int{1, 2},
}
a2 := [][]int{
[]int{0, 1},
[]int{1, 1},
}
res := matriceMultiple(a1, matircePow(a2, n-2))
//resuly[1][1] is the Tn
return res[1][1]
}
// Matrix fast power
func matircePow(a [][]int, n int)[][]int{
tag := a
res := [][]int{
[]int{1, 0},
[]int{0, 1},
}
for n!=0{
if n&1 != 0{
res = matriceMultiple(res, tag)
}
tag = matriceMultiple(tag, tag)
n >>= 1
}
return res
}
// Matrix Multiple
func matriceMultiple(a [][]int, b [][]int)[][]int{
return [][]int{
[]int{ a[0][0] * b[0][0] + a[0][1]*b[1][0], a[0][0] * b[0][1] + a[0][1]*b[1][1]},
[]int{ a[1][0] * b[0][0] + a[1][1]*b[1][0], a[1][0] * b[0][1] + a[1][1]*b[1][1]},
}
}
Test:
Example 1:
Input: 2
output:
Example 2:
Input: 3