diff --git a/C++/climbing-stairs.cpp b/C++/climbing-stairs.cpp index aeb1393..ab0eead 100644 --- a/C++/climbing-stairs.cpp +++ b/C++/climbing-stairs.cpp @@ -1,12 +1,50 @@ -// Time: O(n) +// Time: O(logn) // Space: O(1) class Solution { public: - /** - * @param n: An integer - * @return: An integer - */ + int climbStairs(int n) { + vector> T = {{1, 1}, + {1, 0}}; + return matrixExpo(T, n)[0][0]; + } + +private: + vector> matrixExpo(const vector>& A, int pow) { + vector> result(A.size(), vector(A.size())); + vector> A_exp(A); + for (int i = 0; i < A.size(); ++i) { + result[i][i] = 1; + } + while (pow) { + if (pow % 2 == 1) { + result = matrixMult(result, A_exp); + } + A_exp = matrixMult(A_exp, A_exp); + pow /= 2; + } + return result; + } + + vector> matrixMult(const vector>& A, const vector>& B) { + vector> result(A.size(), vector(A.size())); + for (int i = 0; i < A.size(); ++i) { + for (int j = 0; j < B[0].size(); ++j) { + int64_t entry = 0; + for (int k = 0; k < B.size(); ++k) { + entry = (static_cast(A[i][k]) * B[k][j] + entry); + } + result[i][j] = static_cast(entry); + } + } + return result; + } +}; + +// Time: O(n) +// Space: O(1) +class Solution2 { +public: int climbStairs(int n) { vector steps(3, 0); steps[0] = 1; @@ -17,4 +55,3 @@ class Solution { return steps[n % 3]; } }; - diff --git a/C++/find-peak-element.cpp b/C++/find-peak-element.cpp index 8b829b6..3e2f561 100644 --- a/C++/find-peak-element.cpp +++ b/C++/find-peak-element.cpp @@ -12,10 +12,7 @@ class Solution { while (left < right) { const auto mid = left + (right - left) / 2; - if ((mid == 0 || A[mid - 1] < A[mid]) && - (mid + 1 == A.size() || A[mid] > A[mid + 1])) { - return mid; - } else if (!(mid == 0 || A[mid - 1] < A[mid])) { + if (A[mid] > A[mid + 1]) { right = mid; } else { left = mid + 1; diff --git a/README.md b/README.md index 2ef95fb..0641d31 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# [LintCode](http://www.lintcode.com/en/problem/) ![Language](https://img.shields.io/badge/language-C++%2011-orange.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.md) ![Progress](https://img.shields.io/badge/progress-289%20%2F%20289-ff69b4.svg) +# [LintCode](http://www.lintcode.com/en/problem/) ![Language](https://img.shields.io/badge/language-C++11-orange.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.md) ![Progress](https://img.shields.io/badge/progress-289%20%2F%20289-ff69b4.svg) Up to date (2016-08-22), there are `289` problems on [LintCode Online Judge](http://lintcode.com/). The number of problems is increasing recently. Here is the classification of all `289` problems. -For more problems and solutions, you can see my [LeetCode](https://github.com/kamyu104/LeetCode) repository. +For more problems and solutions, you can see my [LeetCode-Solutions](https://github.com/kamyu104/LeetCode-Solutions) repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. ## Algorithms @@ -366,7 +366,7 @@ I'll keep updating for full summary and better solutions. Stay tuned for updates |108|[Palindrome Partitioning II](http://lintcode.com/en/problem/palindrome-partitioning-ii/)| [C++](./C++/palindrome-partitioning-ii.cpp)| _O(n^2)_ | _O(n)_ | Medium | LeetCode, EPI | | |109|[Triangle](http://lintcode.com/en/problem/triangle/)| [C++](./C++/triangle.cpp)| _O(n)_ | _O(n)_ | Easy | LeetCode, EPI | | |110|[Minimum Path Sum](http://lintcode.com/en/problem/minimum-path-sum/)| [C++](./C++/minimum-path-sum.cpp)| _O(m * n)_ | _O(min(m, n))_ | Easy | LeetCode, EPI | | -|111|[Climbing Stairs](http://lintcode.com/en/problem/climbing-stairs/)| [C++](./C++/climbing-stairs.cpp)| _O(n)_ | _O(1)_ | Easy | LeetCode | | +|111|[Climbing Stairs](http://lintcode.com/en/problem/climbing-stairs/)| [C++](./C++/climbing-stairs.cpp)| _O(logn)_ | _O(1)_ | Easy | LeetCode | | |115|[Unique Paths II](http://lintcode.com/en/problem/unique-paths-ii/)| [C++](./C++/unique-paths-ii.cpp)| _O(m * n)_ | _O(min(m, n))_ | Easy | LeetCode, CTCI | DP, Math | |118|[Distinct Subsequences](http://lintcode.com/en/problem/distinct-subsequences/)| [C++](./C++/distinct-subsequences.cpp)| _O(m * n)_ | _O(m)_ | Medium | LeetCode | DP | |119|[Edit Distance](http://lintcode.com/en/problem/edit-distance/)| [C++](./C++/edit-distance.cpp)| _O(m * n)_ | _O(min(m, n))_ | Medium | LeetCode, CTCI | DP |