[LeetCode] Climbing Stairs

简介: Note: If you feel unwilling to read the long codes, just take the idea with you. The codes are unnecessarily long due to the inconvenient handle of matrices.

Note: If you feel unwilling to read the long codes, just take the idea with you. The codes are unnecessarily long due to the inconvenient handle of matrices.

Well, a classic and interesting problem. The recursion is simply f(n) = f(n - 1) + f(n - 2), which means that we can either climb to n - 1 and then climb 1 step or climb to n - 2 and then climb 2 steps. So this problem is actually asking for the n-th Fibonacci number. However, if you code it in such a recursive way, it will meet TLE due to the large number of overlapping sub-problems.

There are mainly two ways to solve this problem. The first one uses the above formula in a bottom-up manner and takes O(n) time. This link shares the O(n) solution in all the supported languages of the LeetCode OJ. You may take a look at it and appreciate the appetite of each language :-)

Now I will focus on another solution, which takes O(logn) time. The idea is to use the matrix power. In fact, [f(n), f(n - 1); f(n - 1), f(n - 2)] = [1, 1; 1, 0] ^ n for n >= 2. And similar to the problem Pow(x, n), the power of a matrix can be computed in O(logn) time.

The C++ and Python codes are shown below. Note that the computation of the power of the matrix[1, 1; 1, 0] is hard-coded. Since it is a bit trickier to handle matrix multiplications, the codes become much longer.


C++

 1 class Solution {  
 2 public:
 3     int climbStairs(int n) {
 4         if (n < 2) return n;
 5         vector<int> fibs = {1, 1, 1, 0};
 6         vector<int> ans = fibPower(fibs, n);
 7         return ans[0];
 8     }
 9 private:
10     vector<int> matrixProd(vector<int>& l, vector<int>& r) {
11         vector<int> ans(4, 0);
12         ans[0] = l[0] * r[0] + l[1] * r[2];
13         ans[1] = l[0] * r[1] + l[1] * r[3];
14         ans[2] = l[2] * r[0] + l[3] * r[2];
15         ans[3] = l[2] * r[1] + l[3] * r[3]; 
16         return ans;
17     }
18     vector<int> fibPower(vector<int>& fibs, int n){
19         if (n == 1) return fibs;
20         vector<int> half1 = fibPower(fibs, n / 2);
21         vector<int> half2 = fibPower(fibs, n / 2);
22         vector<int> ans = matrixProd(half1, half2);
23         if (n % 2 == 0) return ans;
24         ans[1] = (ans[0] += ans[1]) - ans[1];
25         ans[3] = (ans[2] += ans[3]) - ans[3];
26         return ans;
27     }
28 };

 


Python

class Solution:
    # @param {integer} n
    # @return {integer}
    def climbStairs(self, n):
        if n  < 2:
            return n
        fibs = [1, 1, 1, 0]
        ans = self.fibsPower(fibs, n)
        return ans[0] 

    def matrixProd(self, l, r):
        ans = [0] * 4
        ans[0] = l[0] * r[0] + l[1] * r[2]
        ans[1] = l[0] * r[1] + l[1] * r[3]
        ans[2] = l[2] * r[0] + l[3] * r[2]
        ans[3] = l[2] * r[1] + l[3] * r[3]
        return ans

    def fibsPower(self, fibs, n):
        if n == 1:
            return fibs
        half1 = self.fibsPower(fibs, n / 2)
        half2 = self.fibsPower(fibs, n / 2)
        ans = self.matrixProd(half1, half2) 
        if n % 2 == 0:
            return ans
        ans[0], ans[1], ans[2], ans[3] = ans[0] + ans[1], ans[0], ans[2] + ans[3], ans[2]
        return ans

 

目录
相关文章
LeetCode 70. Climbing Stairs
你正在爬楼梯。 它需要n步才能达到顶峰。 每次你可以爬1或2步。 您可以通过多少不同的方式登顶? 注意:给定n将是一个正整数。
60 0
LeetCode 70. Climbing Stairs
LeetCode 70. 爬楼梯 Climbing Stairs
LeetCode 70. 爬楼梯 Climbing Stairs
Leetcode-Easy 70. Climbing Stairs
Leetcode-Easy 70. Climbing Stairs
101 0
Leetcode-Easy 70. Climbing Stairs
|
算法 移动开发
[LeetCode]--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? 这是一个很经典的爬楼梯问题,面试也会经常遇
1161 0
LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50514606 翻译 你正在爬一个楼梯。
932 0
|
算法 Python
leetcode 70 Climbing Stairs
 Climbing Stairs                       You are climbing a stair case. It takes n steps to reach to the top.
1125 0
|
机器学习/深度学习
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
3月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
114 2