From 6ab180770d66a7d71eceeb729be809196925f0d7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 6 Apr 2019 23:17:38 -0700 Subject: [PATCH] 0077 new algo added. --- 0077-Combinations/cpp-0077/CMakeLists.txt | 7 +- 0077-Combinations/cpp-0077/main.cpp | 9 +- 0077-Combinations/cpp-0077/main2.cpp | 9 +- 0077-Combinations/cpp-0077/main3.cpp | 57 ++++++++++ .../cpp-1012/CMakeLists.txt | 6 - .../cpp-1012/main.cpp | 34 ------ .../cpp-1013/CMakeLists.txt | 6 - .../cpp-1013/main.cpp | 34 ------ .../cpp-1014/CMakeLists.txt | 6 - .../cpp-1014/main.cpp | 56 --------- .../cpp-1015/CMakeLists.txt | 6 - .../cpp-1015/main.cpp | 106 ------------------ .../1120A/CMakeLists.txt | 6 - .../1120A/main.cpp | 44 -------- .../cpp-1021/CMakeLists.txt | 6 - 1021-Best-Sightseeing-Pair/cpp-1021/main.cpp | 48 -------- 1021-Best-Sightseeing-Pair/cpp-1021/main2.cpp | 45 -------- .../cpp-1022/CMakeLists.txt | 6 - .../cpp-1022/main.cpp | 49 -------- .../cpp-1022/main2.cpp | 36 ------ .../cpp-1023/CMakeLists.txt | 6 - .../cpp-1023/main.cpp | 44 -------- .../cpp-1028/CMakeLists.txt | 6 - 1028-Convert-to-Base--2/cpp-1028/main.cpp | 35 ------ .../cpp-1029/CMakeLists.txt | 6 - .../cpp-1029/main.cpp | 35 ------ .../cpp-1030/CMakeLists.txt | 6 - .../cpp-1030/main.cpp | 78 ------------- .../cpp-1031/CMakeLists.txt | 6 - 1031-Number-of-Enclaves/cpp-1031/main.cpp | 74 ------------ 30 files changed, 72 insertions(+), 800 deletions(-) create mode 100644 0077-Combinations/cpp-0077/main3.cpp delete mode 100644 1012-Complement-of-Base-10-Integer/cpp-1012/CMakeLists.txt delete mode 100644 1012-Complement-of-Base-10-Integer/cpp-1012/main.cpp delete mode 100644 1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/CMakeLists.txt delete mode 100644 1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/main.cpp delete mode 100644 1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/CMakeLists.txt delete mode 100644 1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/main.cpp delete mode 100644 1015-Numbers-With-1-Repeated-Digit/cpp-1015/CMakeLists.txt delete mode 100644 1015-Numbers-With-1-Repeated-Digit/cpp-1015/main.cpp delete mode 100644 1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/CMakeLists.txt delete mode 100644 1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/main.cpp delete mode 100644 1021-Best-Sightseeing-Pair/cpp-1021/CMakeLists.txt delete mode 100644 1021-Best-Sightseeing-Pair/cpp-1021/main.cpp delete mode 100644 1021-Best-Sightseeing-Pair/cpp-1021/main2.cpp delete mode 100644 1022-Smallest-Integer-Divisible-by-K/cpp-1022/CMakeLists.txt delete mode 100644 1022-Smallest-Integer-Divisible-by-K/cpp-1022/main.cpp delete mode 100644 1022-Smallest-Integer-Divisible-by-K/cpp-1022/main2.cpp delete mode 100644 1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/CMakeLists.txt delete mode 100644 1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/main.cpp delete mode 100644 1028-Convert-to-Base--2/cpp-1028/CMakeLists.txt delete mode 100644 1028-Convert-to-Base--2/cpp-1028/main.cpp delete mode 100644 1029-Binary-Prefix-Divisible-By-5/cpp-1029/CMakeLists.txt delete mode 100644 1029-Binary-Prefix-Divisible-By-5/cpp-1029/main.cpp delete mode 100644 1030-Next-Greater-Node-In-Linked-List/cpp-1030/CMakeLists.txt delete mode 100644 1030-Next-Greater-Node-In-Linked-List/cpp-1030/main.cpp delete mode 100644 1031-Number-of-Enclaves/cpp-1031/CMakeLists.txt delete mode 100644 1031-Number-of-Enclaves/cpp-1031/main.cpp diff --git a/0077-Combinations/cpp-0077/CMakeLists.txt b/0077-Combinations/cpp-0077/CMakeLists.txt index ef1b1abe..e079e426 100644 --- a/0077-Combinations/cpp-0077/CMakeLists.txt +++ b/0077-Combinations/cpp-0077/CMakeLists.txt @@ -1,7 +1,6 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.13) project(cpp_0077) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +set(CMAKE_CXX_STANDARD 14) -set(SOURCE_FILES main.cpp) -add_executable(cpp_0077 ${SOURCE_FILES}) \ No newline at end of file +add_executable(cpp_0077 main3.cpp) \ No newline at end of file diff --git a/0077-Combinations/cpp-0077/main.cpp b/0077-Combinations/cpp-0077/main.cpp index 576e629e..92c8c11c 100644 --- a/0077-Combinations/cpp-0077/main.cpp +++ b/0077-Combinations/cpp-0077/main.cpp @@ -7,10 +7,12 @@ using namespace std; + /// Naive Recursive -/// Time Complexity: O(n^k) +/// Time Complexity: O(k * C(n, k)) /// Space Complexity: O(k) class Solution { + private: vector> res; @@ -29,6 +31,7 @@ class Solution { return; } + public: vector> combine(int n, int k) { @@ -44,7 +47,7 @@ class Solution { }; -void printVec(const vector& vec){ +void print_vec(const vector& vec){ for(int e: vec) cout << e << " "; @@ -55,6 +58,6 @@ int main() { vector> res = Solution().combine(4,2); for( int i = 0 ; i < res.size() ; i ++ ) - printVec(res[i]); + print_vec(res[i]); return 0; } \ No newline at end of file diff --git a/0077-Combinations/cpp-0077/main2.cpp b/0077-Combinations/cpp-0077/main2.cpp index dbae914b..d24f087d 100644 --- a/0077-Combinations/cpp-0077/main2.cpp +++ b/0077-Combinations/cpp-0077/main2.cpp @@ -7,10 +7,12 @@ using namespace std; + /// Naive Recursive Optimized -/// Time Complexity: O(n^k) +/// Time Complexity: O(k * C(n, k)) /// Space Complexity: O(k) class Solution { + private: vector> res; @@ -30,6 +32,7 @@ class Solution { return; } + public: vector> combine(int n, int k) { @@ -45,7 +48,7 @@ class Solution { }; -void printVec(const vector& vec){ +void print_vec(const vector& vec){ for(int e: vec) cout << e << " "; @@ -56,6 +59,6 @@ int main() { vector> res = Solution().combine(4,2); for( int i = 0 ; i < res.size() ; i ++ ) - printVec(res[i]); + print_vec(res[i]); return 0; } \ No newline at end of file diff --git a/0077-Combinations/cpp-0077/main3.cpp b/0077-Combinations/cpp-0077/main3.cpp new file mode 100644 index 00000000..c2e7fc62 --- /dev/null +++ b/0077-Combinations/cpp-0077/main3.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/combinations/description/ +/// Author : liuyubobobo +/// Time : 2019-03-29 + +#include +#include + +using namespace std; + + +/// Using bit mask +/// Time Complexity: O(2^n * n) +/// Space Complexity: O(1) +class Solution { + +public: + vector> combine(int n, int k) { + + vector> res; + + int LIMIT = (1 << n); + for(int i = 0; i < LIMIT; i ++){ + + vector tres = get_vector(i); + if(tres.size() == k) res.push_back(tres); + } + return res; + } + +private: + vector get_vector(int num){ + + vector res; + int i = 1; + while(num){ + if(num % 2) res.push_back(i); + i ++, num /= 2; + } + return res; + } +}; + + +void print_vec(const vector& vec){ + + for(int e: vec) + cout << e << " "; + cout << endl; +} + +int main() { + + vector> res = Solution().combine(4,2); + for( int i = 0 ; i < res.size() ; i ++ ) + print_vec(res[i]); + return 0; +} \ No newline at end of file diff --git a/1012-Complement-of-Base-10-Integer/cpp-1012/CMakeLists.txt b/1012-Complement-of-Base-10-Integer/cpp-1012/CMakeLists.txt deleted file mode 100644 index 2d73f980..00000000 --- a/1012-Complement-of-Base-10-Integer/cpp-1012/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(A) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(A main.cpp) \ No newline at end of file diff --git a/1012-Complement-of-Base-10-Integer/cpp-1012/main.cpp b/1012-Complement-of-Base-10-Integer/cpp-1012/main.cpp deleted file mode 100644 index 77056385..00000000 --- a/1012-Complement-of-Base-10-Integer/cpp-1012/main.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/// Source : https://leetcode.com/problems/complement-of-base-10-integer/ -/// Author : liuyubobobo -/// Time : 2019-03-16 - -#include -#include - -using namespace std; - - -/// Simulation -/// Time Complexity: O(logN) -/// Space Complexity: O(logN) -class Solution { -public: - int bitwiseComplement(int N) { - - if(!N) return 1; - - vector binary; - while(N) binary.push_back(1 - N % 2), N /= 2; - reverse(binary.begin(), binary.end()); - - int res = 0; - for(int b: binary) res = res * 2 + b; - return res; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/CMakeLists.txt b/1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/CMakeLists.txt deleted file mode 100644 index b4b16ecd..00000000 --- a/1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(B) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(B main.cpp) \ No newline at end of file diff --git a/1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/main.cpp b/1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/main.cpp deleted file mode 100644 index 64ada6ea..00000000 --- a/1013-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/cpp-1013/main.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/// Source : https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/ -/// Author : liuyubobobo -/// Time : 2019-03-16 - -#include -#include - -using namespace std; - - -/// Using HashMap -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - int numPairsDivisibleBy60(vector& time) { - - vector rem(60, 0); - for(int t: time) rem[t % 60] ++; - - long long res = 0; - if(rem[0]) res += (long long)rem[0] * (rem[0] - 1) / 2; - if(rem[30]) res += (long long)rem[30] * (rem[30] - 1) / 2; - for(int i = 1; i < 30; i ++) - res += (long long)rem[i] * rem[60 - i]; - return res; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/CMakeLists.txt b/1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/CMakeLists.txt deleted file mode 100644 index 2235a661..00000000 --- a/1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(C) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(C main.cpp) \ No newline at end of file diff --git a/1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/main.cpp b/1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/main.cpp deleted file mode 100644 index af45bf5c..00000000 --- a/1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/main.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/// Source : https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ -/// Author : liuyubobobo -/// Time : 2019-03-16 - -#include -#include -#include - -using namespace std; - - -/// Binary Search -/// Time Complexity: O(nlog(sum(weights))) -/// Space Complexity: O(1) -class Solution { -public: - int shipWithinDays(vector& weights, int D) { - - int l = *max_element(weights.begin(), weights.end()), - r = accumulate(weights.begin(), weights.end(), 0); - while(l < r){ -// cout << "check " << l << " " << r << endl; - int mid = (l + r) / 2; - if(ok(weights, mid, D)) - r = mid; - else - l = mid + 1; - } - return l; - } - -private: - bool ok(const vector& weights, int C, int D){ - - int d = 0, cur = 0; - for(int w: weights) - if(cur + w <= C) cur += w; - else d ++, cur = w; - if(cur) d ++; - return d <= D; - } -}; - - -int main() { - - vector weight1 = {1,2,3,4,5,6,7,8,9,10}; - cout << Solution().shipWithinDays(weight1, 5) << endl; - // 15 - - vector weight2 = {1,2,3,1,1}; - cout << Solution().shipWithinDays(weight2, 4) << endl; - // 3 - - return 0; -} \ No newline at end of file diff --git a/1015-Numbers-With-1-Repeated-Digit/cpp-1015/CMakeLists.txt b/1015-Numbers-With-1-Repeated-Digit/cpp-1015/CMakeLists.txt deleted file mode 100644 index d20b982d..00000000 --- a/1015-Numbers-With-1-Repeated-Digit/cpp-1015/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(D) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(D main.cpp) \ No newline at end of file diff --git a/1015-Numbers-With-1-Repeated-Digit/cpp-1015/main.cpp b/1015-Numbers-With-1-Repeated-Digit/cpp-1015/main.cpp deleted file mode 100644 index 15c8d7a2..00000000 --- a/1015-Numbers-With-1-Repeated-Digit/cpp-1015/main.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/// Source : https://leetcode.com/problems/numbers-with-1-repeated-digit/ -/// Author : liuyubobobo -/// Time : 2019-03-16 - -#include -#include -#include - -using namespace std; - - -/// Combination Mathematics -/// Time Complexity: O(log(N)^2) -/// Space Complexity: O(logN) -class Solution { -public: - int numDupDigitsAtMostN(int N) { - - vector diff(10, 0); - for(int i = 1; i <= 9; i ++) - diff[i] = get_diff(i); - for(int i = 1; i < 10; i ++) diff[i] += diff[i - 1]; -// for(int e: diff) cout << e << " "; cout << endl; - - vector power10(10, 1); - for(int i = 1; i < 10; i ++) power10[i] = power10[i - 1] * 10; -// for(int e: power10) cout << e << " "; cout << endl; - - vector num; - while(N) num.push_back(N % 10), N /= 10; - reverse(num.begin(), num.end()); -// for(int e: num) cout << e << " "; cout << endl; - - int res = power10[num.size() - 1] - 1 - diff[num.size() - 1]; -// cout << res << endl; - - unordered_set digits; - for(int i = 0; i < num.size(); i ++){ - - if(i == num.size() - 1){ - for(int d = 0; d <= num[i]; d ++) - if(digits.count(d)) res ++; - break; - } - else if(num[i]){ - int tres = (num[i] - (i == 0)) * power10[num.size() - 1 - i]; - if(tres){ - tres -= howmanydiff(num.size() - i, digits, num[i], i != 0); - res += tres; - } - } - - if(!digits.count(num[i])) - digits.insert(num[i]); - else{ - res += 1 + get_num(num, i + 1); - break; - } - } - return res; - } - -private: - int howmanydiff(int n, const unordered_set& digits, int first, bool canZero){ - - int res = 0; - for(int i = canZero ? 0 : 1; i < first; i ++) - if(!digits.count(i)) res ++; - n --; - - int cur = 10 - (digits.size() + 1); - while(n --) - res *= cur--; - return res; - } - - int get_num(const vector& num, int s){ - int res = 0; - for(int i = s; i < num.size(); i ++) - res = res * 10 + num[i]; - return res; - } - - int get_diff(int n){ - - int res = 9; - n --; - - int cur = 9; - while(n --) res *= cur --; - return res; - } -}; - - -int main() { - - cout << Solution().numDupDigitsAtMostN(20) << endl; // 1 - cout << Solution().numDupDigitsAtMostN(100) << endl; // 10 - cout << Solution().numDupDigitsAtMostN(1000) << endl; // 262 - cout << Solution().numDupDigitsAtMostN(11) << endl; // 1 - cout << Solution().numDupDigitsAtMostN(101) << endl; // 11 - cout << Solution().numDupDigitsAtMostN(110) << endl; // 12 - - return 0; -} \ No newline at end of file diff --git a/1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/CMakeLists.txt b/1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/CMakeLists.txt deleted file mode 100644 index 2d73f980..00000000 --- a/1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(A) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(A main.cpp) \ No newline at end of file diff --git a/1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/main.cpp b/1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/main.cpp deleted file mode 100644 index 84592903..00000000 --- a/1020-Partition-Array-Into-Three-Parts-With-Equal-Sum/1120A/main.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/// Source : https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/ -/// Author : liuyubobobo -/// Time : 2019-03-23 - -#include -#include -#include -#include - -using namespace std; - - -/// Linear Scan -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - bool canThreePartsEqualSum(vector& A) { - - int sum = accumulate(A.begin(), A.end(), 0); - if(sum % 3) return false; - - int cur = 0, i = 0, j = A.size() - 1; - do{ - cur += A[i ++]; - }while(i < A.size() && cur != sum / 3); - - if(i == A.size()) return false; - - cur = 0; - do{ - cur += A[j --]; - }while(j >= 0 && cur != sum / 3); - if(j < 0) return false; - - return i <= j; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/1021-Best-Sightseeing-Pair/cpp-1021/CMakeLists.txt b/1021-Best-Sightseeing-Pair/cpp-1021/CMakeLists.txt deleted file mode 100644 index e47439c9..00000000 --- a/1021-Best-Sightseeing-Pair/cpp-1021/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(C) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(C main2.cpp) \ No newline at end of file diff --git a/1021-Best-Sightseeing-Pair/cpp-1021/main.cpp b/1021-Best-Sightseeing-Pair/cpp-1021/main.cpp deleted file mode 100644 index 28448dc9..00000000 --- a/1021-Best-Sightseeing-Pair/cpp-1021/main.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/// Source : https://leetcode.com/problems/best-sightseeing-pair/ -/// Author : liuyubobobo -/// Time : 2019-03-23 - -#include -#include - -using namespace std; - - -/// Dynamic Programming -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { -public: - int maxScoreSightseeingPair(vector& A) { - - int n = A.size(); - - vector B = A; - for(int i = 0; i < n; i ++) B[i] -= i; - for(int i = n - 2; i >= 0; i --) - B[i] = max(B[i], B[i + 1]); - - int res = A[0] + B[1]; - for(int i = 1; i + 1 < n; i ++) - res = max(res, A[i] + i + B[i + 1]); - return res; - } -}; - - -int main() { - - vector A1 = {8,1,5,2,6}; - cout << Solution().maxScoreSightseeingPair(A1) << endl; - // 11 - - vector A2 = {3,7,2,3}; - cout << Solution().maxScoreSightseeingPair(A2) << endl; - // 9 - - vector A3 = {1, 3, 5}; - cout << Solution().maxScoreSightseeingPair(A3) << endl; - // 7 - - return 0; -} \ No newline at end of file diff --git a/1021-Best-Sightseeing-Pair/cpp-1021/main2.cpp b/1021-Best-Sightseeing-Pair/cpp-1021/main2.cpp deleted file mode 100644 index 7686fddc..00000000 --- a/1021-Best-Sightseeing-Pair/cpp-1021/main2.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/best-sightseeing-pair/ -/// Author : liuyubobobo -/// Time : 2019-03-23 - -#include -#include - -using namespace std; - - -/// Dynamic Programming -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - int maxScoreSightseeingPair(vector& A) { - - int n = A.size(); - - int res = A[0] + A[n - 1] - (n - 1), maxv = A[n - 1] - (n - 1); - for(int i = n - 2; i >= 0; i --){ - res = max(res, A[i] + i + maxv); - maxv = max(maxv, A[i] - i); - } - return res; - } -}; - - -int main() { - - vector A1 = {8,1,5,2,6}; - cout << Solution().maxScoreSightseeingPair(A1) << endl; - // 11 - - vector A2 = {3,7,2,3}; - cout << Solution().maxScoreSightseeingPair(A2) << endl; - // 9 - - vector A3 = {1, 3, 5}; - cout << Solution().maxScoreSightseeingPair(A3) << endl; - // 7 - - return 0; -} \ No newline at end of file diff --git a/1022-Smallest-Integer-Divisible-by-K/cpp-1022/CMakeLists.txt b/1022-Smallest-Integer-Divisible-by-K/cpp-1022/CMakeLists.txt deleted file mode 100644 index 5a882133..00000000 --- a/1022-Smallest-Integer-Divisible-by-K/cpp-1022/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(B) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(B main2.cpp) \ No newline at end of file diff --git a/1022-Smallest-Integer-Divisible-by-K/cpp-1022/main.cpp b/1022-Smallest-Integer-Divisible-by-K/cpp-1022/main.cpp deleted file mode 100644 index 490003f0..00000000 --- a/1022-Smallest-Integer-Divisible-by-K/cpp-1022/main.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2019-03-23 - -#include -#include -#include - -using namespace std; - - -/// Multiplication Simulation -/// Time Complexity: O(K) -/// Space Complexity: O(K) -class Solution { -public: - int smallestRepunitDivByK(int K) { - - if(K % 2 == 0 || K % 5 == 0) return -1; - - unordered_set visited; - - int left = 0, len = 0; - while(true){ - - int i = 0; - for(i = 0; i <= 9; i ++) - if((K * i + left) % 10 == 1){ - left = (K * i + left) / 10; - len ++; - break; - } - - if(i == 10) return -1; - if(left == 0) return len; - - if(visited.count(left)) break; - visited.insert(left); - } - return -1; - } -}; - -int main() { - - cout << Solution().smallestRepunitDivByK(3) << endl; - - return 0; -} \ No newline at end of file diff --git a/1022-Smallest-Integer-Divisible-by-K/cpp-1022/main2.cpp b/1022-Smallest-Integer-Divisible-by-K/cpp-1022/main2.cpp deleted file mode 100644 index bf59e397..00000000 --- a/1022-Smallest-Integer-Divisible-by-K/cpp-1022/main2.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2019-03-23 - -#include -#include -#include - -using namespace std; - - -/// Multiplication Simulation -/// Time Complexity: O(K) -/// Space Complexity: O(1) -class Solution { -public: - int smallestRepunitDivByK(int K) { - - if(K % 2 == 0 || K % 5 == 0) return -1; - - int r = 0; - for(int len = 1; len <= K; len ++){ - r = (r * 10 + 1) % K; - if(r == 0) return len; - } - return -1; - } -}; - - -int main() { - - cout << Solution().smallestRepunitDivByK(3) << endl; - - return 0; -} \ No newline at end of file diff --git a/1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/CMakeLists.txt b/1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/CMakeLists.txt deleted file mode 100644 index d20b982d..00000000 --- a/1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(D) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(D main.cpp) \ No newline at end of file diff --git a/1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/main.cpp b/1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/main.cpp deleted file mode 100644 index a24acf50..00000000 --- a/1023-Binary-String-With-Substrings-Representing-1-To-N/cpp-1023/main.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/ -/// Author : liuyubobobo -/// Time : 2019-03-23 - -#include - -using namespace std; - - -/// Brute Force -/// Time Complexity: O(|S|^2) -/// Space Complexity: O(logN) -class Solution { -public: - bool queryString(string S, int N) { - - int n = S.size(); - for(int i = N; i >= 1; i --){ - - string s = get_binary_string(i); - if(S.find(s) == string::npos) - return false; - } - return true; - } - -private: - string get_binary_string(int x){ - - string ret = ""; - while(x){ - ret += ('0' + x % 2); - x /= 2; - } - reverse(ret.begin(), ret.end()); - return ret; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/1028-Convert-to-Base--2/cpp-1028/CMakeLists.txt b/1028-Convert-to-Base--2/cpp-1028/CMakeLists.txt deleted file mode 100644 index b4b16ecd..00000000 --- a/1028-Convert-to-Base--2/cpp-1028/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(B) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(B main.cpp) \ No newline at end of file diff --git a/1028-Convert-to-Base--2/cpp-1028/main.cpp b/1028-Convert-to-Base--2/cpp-1028/main.cpp deleted file mode 100644 index 7144fe80..00000000 --- a/1028-Convert-to-Base--2/cpp-1028/main.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/// Source : https://leetcode.com/problems/convert-to-base-2/ -/// Author : liuyubobobo -/// Time : 2019-03-30 - -#include - -using namespace std; - - -/// Mathematics -/// Time Complexity: O(logn) -/// Space Complexity: O(1) -class Solution { -public: - string baseNeg2(int N) { - - string res = ""; - while(N){ - if(abs(N) % 2) res += "1"; - else res += "0"; - - if(abs(N) % 2) N --; - N /= -2; - } - - reverse(res.begin(), res.end()); - return res.size() ? res : "0"; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/1029-Binary-Prefix-Divisible-By-5/cpp-1029/CMakeLists.txt b/1029-Binary-Prefix-Divisible-By-5/cpp-1029/CMakeLists.txt deleted file mode 100644 index 2d73f980..00000000 --- a/1029-Binary-Prefix-Divisible-By-5/cpp-1029/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(A) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(A main.cpp) \ No newline at end of file diff --git a/1029-Binary-Prefix-Divisible-By-5/cpp-1029/main.cpp b/1029-Binary-Prefix-Divisible-By-5/cpp-1029/main.cpp deleted file mode 100644 index 0a9a9313..00000000 --- a/1029-Binary-Prefix-Divisible-By-5/cpp-1029/main.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-prefix-divisible-by-5/ -/// Author : liuyubobobo -/// Time : 2019-03-30 - -#include -#include - -using namespace std; - - -/// Mathematics -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - vector prefixesDivBy5(vector& A) { - - int n = A.size(); - vector res; - - int a = 0; - for(int e: A){ - a = a * 2 + e; - res.push_back(a % 5 == 0); - a %= 10; - } - return res; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/1030-Next-Greater-Node-In-Linked-List/cpp-1030/CMakeLists.txt b/1030-Next-Greater-Node-In-Linked-List/cpp-1030/CMakeLists.txt deleted file mode 100644 index 2235a661..00000000 --- a/1030-Next-Greater-Node-In-Linked-List/cpp-1030/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(C) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(C main.cpp) \ No newline at end of file diff --git a/1030-Next-Greater-Node-In-Linked-List/cpp-1030/main.cpp b/1030-Next-Greater-Node-In-Linked-List/cpp-1030/main.cpp deleted file mode 100644 index 56a80c7b..00000000 --- a/1030-Next-Greater-Node-In-Linked-List/cpp-1030/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/// Source : https://leetcode.com/problems/next-greater-node-in-linked-list/ -/// Author : liuyubobobo -/// Time : 2019-03-30 - -#include -#include -#include - -using namespace std; - - -/// Using Stack -/// Time Complexity: O(n) -/// Space Complexity: O(n) - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -class Solution { -public: - vector nextLargerNodes(ListNode* head) { - - vector nums = get_nums(head); - return nextLargerNodes(nums); - } - - vector nextLargerNodes(const vector& nums){ - - stack stack; - vector res(nums.size(), 0); - for(int i = 0; i < nums.size(); i ++){ - - while(!stack.empty() && nums[stack.top()] < nums[i]) - res[stack.top()] = nums[i], stack.pop(); - - stack.push(i); - } - return res; - } - -private: - vector get_nums(ListNode* head){ - - vector res; - - ListNode* cur = head; - while(cur){ - res.push_back(cur->val); - cur = cur->next; - } - return res; - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) cout << e << " "; - cout << endl; -} - -int main() { - - vector nums1 = {2, 1, 5}; - print_vec(Solution().nextLargerNodes(nums1)); - - vector nums2 = {2, 7, 4, 3, 5}; - print_vec(Solution().nextLargerNodes(nums2)); - - vector nums3 = {1,7,5,1,9,2,5,1}; - print_vec(Solution().nextLargerNodes(nums3)); - - - return 0; -} \ No newline at end of file diff --git a/1031-Number-of-Enclaves/cpp-1031/CMakeLists.txt b/1031-Number-of-Enclaves/cpp-1031/CMakeLists.txt deleted file mode 100644 index d20b982d..00000000 --- a/1031-Number-of-Enclaves/cpp-1031/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(D) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(D main.cpp) \ No newline at end of file diff --git a/1031-Number-of-Enclaves/cpp-1031/main.cpp b/1031-Number-of-Enclaves/cpp-1031/main.cpp deleted file mode 100644 index 0a6774fa..00000000 --- a/1031-Number-of-Enclaves/cpp-1031/main.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/// Source : https://leetcode.com/problems/number-of-enclaves/ -/// Author : liuyubobobo -/// Time : 2019-03-30 - -#include -#include - -using namespace std; - - -/// Floodfill -/// Time Complexity: O(n * m) -/// Space Complexity: O(n * m) -class Solution { - -private: - int n, m; - const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; - -public: - int numEnclaves(vector>& A) { - - if(A.size() == 0 || A[0].size() == 0) return 0; - - n = A.size(), m = A[0].size(); - for(int i = 0; i < n; i ++){ - if(A[i][0]) dfs(A, i, 0); - if(A[i][m - 1]) dfs(A, i, m - 1); - } - - for(int j = 0; j < m; j ++){ - if(A[0][j]) dfs(A, 0, j); - if(A[n - 1][j]) dfs(A, n - 1, j); - } - -// for(int i = 0; i < n; i ++) { -// for (int j = 0; j < m; j++) cout << A[i][j] << " "; -// cout << endl; -// } - - int res = 0; - for(int i = 0; i < n; i ++) - for(int j = 0; j < m; j ++) - if(A[i][j]) res += dfs(A, i, j); - return res; - } - -private: - int dfs(vector>& A, int x, int y){ - - A[x][y] = 0; - int res = 1; - - for(int i = 0; i < 4; i ++){ - int nextx = x + d[i][0], nexty = y + d[i][1]; - if(inArea(nextx, nexty) && A[nextx][nexty]) - res += dfs(A, nextx, nexty); - } - return res; - } - - bool inArea(int x, int y){ - return x >= 0 && x < n && y >= 0 && y < m; - } -}; - - -int main() { - - vector> A2 = {{0,1,1,0},{0,0,1,0},{0,0,1,0},{0,0,0,0}}; - cout << Solution().numEnclaves(A2) << endl; - - return 0; -} \ No newline at end of file