From 747dfa44ed598082b154e6fdd0781123984391f5 Mon Sep 17 00:00:00 2001 From: kamyu Date: Fri, 25 Aug 2017 00:09:51 +0800 Subject: [PATCH 1/7] Update find-peak-element.cpp --- C++/find-peak-element.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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; From 9208c5b2aa651536b09537f7926999a4a9ccafce Mon Sep 17 00:00:00 2001 From: kamyu Date: Fri, 12 Oct 2018 01:55:56 +0800 Subject: [PATCH 2/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ef95fb..63a94eb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ 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-Solution](https://github.com/kamyu104/LeetCode-Solution) repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. ## Algorithms From 6350a9658d4da21a6dcbed3f1b909fcfd819d808 Mon Sep 17 00:00:00 2001 From: kamyu Date: Sat, 13 Oct 2018 03:46:12 +0800 Subject: [PATCH 3/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 63a94eb..08199e7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ 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-Solution](https://github.com/kamyu104/LeetCode-Solution) 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 From 7053608ce6d01c6c4568400667381f76d3e8ab83 Mon Sep 17 00:00:00 2001 From: kamyu Date: Tue, 1 Jan 2019 16:57:04 +0800 Subject: [PATCH 4/7] Update climbing-stairs.cpp --- C++/climbing-stairs.cpp | 50 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/C++/climbing-stairs.cpp b/C++/climbing-stairs.cpp index aeb1393..67225e7 100644 --- a/C++/climbing-stairs.cpp +++ b/C++/climbing-stairs.cpp @@ -1,12 +1,51 @@ -// 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 +56,3 @@ class Solution { return steps[n % 3]; } }; - From 4cc0cf1b2203002e8e2951e5f4a4ebb11d17580c Mon Sep 17 00:00:00 2001 From: kamyu Date: Tue, 1 Jan 2019 16:58:33 +0800 Subject: [PATCH 5/7] Update climbing-stairs.cpp --- C++/climbing-stairs.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/C++/climbing-stairs.cpp b/C++/climbing-stairs.cpp index 67225e7..ab0eead 100644 --- a/C++/climbing-stairs.cpp +++ b/C++/climbing-stairs.cpp @@ -4,9 +4,8 @@ class Solution { public: int climbStairs(int n) { - vector> T = {{1, 1}, - {1, 0}}; - + vector> T = {{1, 1}, + {1, 0}}; return matrixExpo(T, n)[0][0]; } From 8f6ee0ff38cb7cf8bf9fca800243823931604155 Mon Sep 17 00:00:00 2001 From: kamyu Date: Tue, 1 Jan 2019 16:59:50 +0800 Subject: [PATCH 6/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08199e7..f2e38fa 100644 --- a/README.md +++ b/README.md @@ -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 | From 78f9523bf7cdb077d1023a2b2077e7c9cf411fca Mon Sep 17 00:00:00 2001 From: kamyu Date: Mon, 23 Sep 2019 11:20:29 +0800 Subject: [PATCH 7/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f2e38fa..0641d31 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# [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.