From 9ccc013e3414e9be64f57b16c8614266ffd43db5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 11 Apr 2022 19:56:13 -0700 Subject: [PATCH 001/390] 0289 codes updated. --- 0001-0500/0289-Game-of-Life/cpp-0289/main.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/0001-0500/0289-Game-of-Life/cpp-0289/main.cpp b/0001-0500/0289-Game-of-Life/cpp-0289/main.cpp index 2e7ac5aa..43f87bd4 100644 --- a/0001-0500/0289-Game-of-Life/cpp-0289/main.cpp +++ b/0001-0500/0289-Game-of-Life/cpp-0289/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/game-of-life/ /// Author : liuyubobobo /// Time : 2020-12-30 +/// Updated: 2022-04-11 #include #include @@ -13,21 +14,18 @@ using namespace std; /// Space Complexity: O(m * n) class Solution { -private: - int R, C; - public: void gameOfLife(vector>& board) { - R = board.size(), C = board[0].size(); + int R = board.size(), C = board[0].size(); vector> res(R, vector(C, 0)); for(int i = 0; i < R; i ++) for(int j = 0; j < C; j ++){ int num = 0; - for(int ii = max(0, i - 1); ii <= min(i + 1, R + 1); ii ++) - for(int jj = max(0, j - 1); jj <= min(j + 1, C + 1); jj ++){ + for(int ii = max(0, i - 1); ii <= min(i + 1, R - 1); ii ++) + for(int jj = max(0, j - 1); jj <= min(j + 1, C - 1); jj ++){ if(ii == i && jj == j) continue; num += board[ii][jj]; } From b1fe2360d293efd26a1b91a67788996eee48855f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 12 Apr 2022 23:03:11 -0700 Subject: [PATCH 002/390] 0210 codes updated. --- .../cpp-0210/CMakeLists.txt | 2 +- .../0210-Course-Schedule-II/cpp-0210/main.cpp | 44 ++++---- .../cpp-0210/main2.cpp | 73 ------------ .../cpp-0210/main3.cpp | 104 ------------------ 4 files changed, 21 insertions(+), 202 deletions(-) delete mode 100644 0001-0500/0210-Course-Schedule-II/cpp-0210/main2.cpp delete mode 100644 0001-0500/0210-Course-Schedule-II/cpp-0210/main3.cpp diff --git a/0001-0500/0210-Course-Schedule-II/cpp-0210/CMakeLists.txt b/0001-0500/0210-Course-Schedule-II/cpp-0210/CMakeLists.txt index c53c8482..3ccd37f7 100644 --- a/0001-0500/0210-Course-Schedule-II/cpp-0210/CMakeLists.txt +++ b/0001-0500/0210-Course-Schedule-II/cpp-0210/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0210) set(CMAKE_CXX_STANDARD 11) -add_executable(cpp_0210 main3.cpp) \ No newline at end of file +add_executable(cpp_0210 main.cpp) \ No newline at end of file diff --git a/0001-0500/0210-Course-Schedule-II/cpp-0210/main.cpp b/0001-0500/0210-Course-Schedule-II/cpp-0210/main.cpp index 37eb088a..fa88156f 100644 --- a/0001-0500/0210-Course-Schedule-II/cpp-0210/main.cpp +++ b/0001-0500/0210-Course-Schedule-II/cpp-0210/main.cpp @@ -1,7 +1,7 @@ /// Source : https://leetcode.com/problems/course-schedule-ii/ /// Author : liuyubobobo /// Time : 2018-12-16 -/// Updated: 2021-05-03 +/// Updated: 2022-04-12 #include #include @@ -10,14 +10,13 @@ using namespace std; -/// Using Priority Queue -/// Time Complexity: O(ElogE) +/// Topological Order +/// Time Complexity: O(V + E) /// Space Complexity: O(V + E) class Solution { public: vector findOrder(int numCourses, vector>& prerequisites) { - priority_queue, vector>, greater>> pq; vector pre(numCourses, 0); vector> g(numCourses); for(const vector& p: prerequisites){ @@ -27,30 +26,27 @@ class Solution { pre[to] ++; } + queue q; for(int i = 0; i < numCourses; i ++) - pq.push(make_pair(pre[i], i)); + if(pre[i] == 0) + q.push(i); - vector learnt(numCourses, false); vector res; - while(!pq.empty()){ - int x = pq.top().first; - int id = pq.top().second; - pq.pop(); - - if(!learnt[id]){ - if(x) return {}; - - res.push_back(id); - learnt[id] = true; - - for(int next: g[id]){ - pre[next] --; - pq.push(make_pair(pre[next], next)); - } + while(!q.empty()){ + int id = q.front(); + q.pop(); + + res.push_back(id); + for(int next: g[id]){ + pre[next] --; + if(pre[next] == 0) + q.push(next); } } - return res; + if(res.size() == numCourses) + return res; + return {}; } }; @@ -63,11 +59,11 @@ void print_vec(const vector& vec){ int main() { - vector> pre1 = {{1,0}}; + vector> pre1 = {{1,0}}; print_vec(Solution().findOrder(2, pre1)); // 0 1 - vector> pre2 = {{1,0},{2,0},{3,1},{3,2}}; + vector> pre2 = {{1,0},{2,0},{3,1},{3,2}}; print_vec(Solution().findOrder(4, pre2)); // 0 1 2 3 diff --git a/0001-0500/0210-Course-Schedule-II/cpp-0210/main2.cpp b/0001-0500/0210-Course-Schedule-II/cpp-0210/main2.cpp deleted file mode 100644 index 044bd852..00000000 --- a/0001-0500/0210-Course-Schedule-II/cpp-0210/main2.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/// Source : https://leetcode.com/problems/course-schedule-ii/ -/// Author : liuyubobobo -/// Time : 2018-12-16 -/// Updated: 2021-05-03 - -#include -#include -#include - -using namespace std; - - -/// Using Queue is enough -/// Since we are only interested in 0-indegree vertex :-) -/// -/// Time Complexity: O(E) -/// Space Complexity: O(V + E) -class Solution { -public: - vector findOrder(int numCourses, vector>& prerequisites) { - - vector pre(numCourses, 0); - vector> g(numCourses); - for(const vector& p: prerequisites){ - int from = p[1]; - int to = p[0]; - g[from].push_back(to); - pre[to] ++; - } - - queue q; - for(int i = 0; i < numCourses; i ++) - if(pre[i] == 0) - q.push(i); - - vector res; - while(!q.empty()){ - int id = q.front(); - q.pop(); - - res.push_back(id); - for(int next: g[id]){ - pre[next] --; - if(pre[next] == 0) - q.push(next); - } - } - - if(res.size() == numCourses) - return res; - return {}; - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - vector> pre1 = {{1,0}}; - print_vec(Solution().findOrder(2, pre1)); - // 0 1 - - vector> pre2 = {{1,0},{2,0},{3,1},{3,2}}; - print_vec(Solution().findOrder(4, pre2)); - // 0 1 2 3 - - return 0; -} \ No newline at end of file diff --git a/0001-0500/0210-Course-Schedule-II/cpp-0210/main3.cpp b/0001-0500/0210-Course-Schedule-II/cpp-0210/main3.cpp deleted file mode 100644 index 3f64f44f..00000000 --- a/0001-0500/0210-Course-Schedule-II/cpp-0210/main3.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/// Source : https://leetcode.com/problems/course-schedule-ii/ -/// Author : liuyubobobo -/// Time : 2018-12-16 -/// Updated: 2021-05-03 - -#include -#include -#include - -using namespace std; - - -/// Topological Order based on DFS -/// Time Complexity: O(V + E) -/// Space Complexity: O(V + E) -class Solution { -public: - vector findOrder(int numCourses, vector>& prerequisites) { - - vector> g(numCourses); - for(const vector& p: prerequisites){ - int from = p[1]; - int to = p[0]; - g[from].push_back(to); - } - - if(hasCircle(g)) return {}; - return topoOrder(g); - } - -private: - bool hasCircle(const vector>& g){ - - vector visited(g.size(), false); - vector onPath(g.size(), false); - for(int i = 0; i < g.size(); i ++) - if(!visited[i]) - if(circleDFS(g, i, visited, onPath)) - return true; - return false; - } - - bool circleDFS(const vector>& g, int v, - vector& visited, vector& onPath){ - - visited[v] = true; - onPath[v] = true; - for(int next: g[v]) - if(!visited[next]){ - if(circleDFS(g, next, visited, onPath)) - return true; - } - else if(onPath[next]) - return true; - - onPath[v] = false; - return false; - } - - vector topoOrder(const vector>& g){ - - vector visited(g.size(), false); - stack st; - for(int i = 0; i < g.size(); i ++) - if(!visited[i]) - topoDFS(g, i, visited, st); - - vector res; - while(!st.empty()){ - res.push_back(st.top()); - st.pop(); - } - return res; - } - - void topoDFS(const vector>& g, int v, vector& visited, stack& st){ - - visited[v] = true; - for(int next: g[v]) - if(!visited[next]) - topoDFS(g, next, visited, st); - st.push(v); - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - vector> pre1 = {{1,0}}; - print_vec(Solution().findOrder(2, pre1)); - // 0 1 - - vector> pre2 = {{1,0},{2,0},{3,1},{3,2}}; - print_vec(Solution().findOrder(4, pre2)); - // 0 1 2 3 - - return 0; -} \ No newline at end of file From 30ff7e4b59bc3b0b7bcf936cdb7bf6ee844afb4c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 13 Apr 2022 12:05:16 -0700 Subject: [PATCH 003/390] 2022 spring cmbchina solved. --- Others/2022spring-cmbchina/A/CMakeLists.txt | 6 ++ Others/2022spring-cmbchina/A/main.cpp | 39 +++++++ Others/2022spring-cmbchina/B/CMakeLists.txt | 6 ++ Others/2022spring-cmbchina/B/main.cpp | 36 +++++++ Others/2022spring-cmbchina/C/CMakeLists.txt | 6 ++ Others/2022spring-cmbchina/C/main.cpp | 106 ++++++++++++++++++++ Others/2022spring-cmbchina/D/CMakeLists.txt | 6 ++ Others/2022spring-cmbchina/D/main.cpp | 72 +++++++++++++ readme.md | 13 ++- 9 files changed, 286 insertions(+), 4 deletions(-) create mode 100644 Others/2022spring-cmbchina/A/CMakeLists.txt create mode 100644 Others/2022spring-cmbchina/A/main.cpp create mode 100644 Others/2022spring-cmbchina/B/CMakeLists.txt create mode 100644 Others/2022spring-cmbchina/B/main.cpp create mode 100644 Others/2022spring-cmbchina/C/CMakeLists.txt create mode 100644 Others/2022spring-cmbchina/C/main.cpp create mode 100644 Others/2022spring-cmbchina/D/CMakeLists.txt create mode 100644 Others/2022spring-cmbchina/D/main.cpp diff --git a/Others/2022spring-cmbchina/A/CMakeLists.txt b/Others/2022spring-cmbchina/A/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/Others/2022spring-cmbchina/A/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/Others/2022spring-cmbchina/A/main.cpp b/Others/2022spring-cmbchina/A/main.cpp new file mode 100644 index 00000000..30a304ba --- /dev/null +++ b/Others/2022spring-cmbchina/A/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode-cn.com/contest/cmbchina-2022spring/problems/fWcPGC/ +/// Author : liuyubobobo +/// Time : 2022-04-13 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string deleteText(string article, int index) { + + if(article[index] == ' ') return article; + + article = " " + article + " "; + index ++; + + int start = -1, end = -1; + for(start = index; article[start] != ' '; start --); + for(end = index; article[end] != ' '; end ++); + + article = article.substr(0, start) + " " + article.substr(end + 1); + while(!article.empty() && article[0] == ' ') article = article.substr(1); + while(!article.empty() && article.back() == ' ') article.pop_back(); + return article; + } +}; + + +int main() { + + cout << Solution().deleteText("Singing dancing in the rain", 10) << '\n'; + + return 0; +} diff --git a/Others/2022spring-cmbchina/B/CMakeLists.txt b/Others/2022spring-cmbchina/B/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/Others/2022spring-cmbchina/B/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/Others/2022spring-cmbchina/B/main.cpp b/Others/2022spring-cmbchina/B/main.cpp new file mode 100644 index 00000000..fd617358 --- /dev/null +++ b/Others/2022spring-cmbchina/B/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode-cn.com/contest/cmbchina-2022spring/problems/ReWLAw/ +/// Author : liuyubobobo +/// Time : 2022-04-13 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int numFlowers(vector>& roads) { + + int n = roads.size() + 1; + vector degrees(n); + for(const vector& road: roads){ + degrees[road[0]] ++; + degrees[road[1]] ++; + } + return *max_element(degrees.begin(), degrees.end()) + 1; + } +}; + + +int main() { + + vector> roads1 = {{0, 1}, {1, 3}, {1, 2}}; + cout << Solution().numFlowers(roads1) << '\n'; + // 4 + + return 0; +} diff --git a/Others/2022spring-cmbchina/C/CMakeLists.txt b/Others/2022spring-cmbchina/C/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/Others/2022spring-cmbchina/C/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/Others/2022spring-cmbchina/C/main.cpp b/Others/2022spring-cmbchina/C/main.cpp new file mode 100644 index 00000000..95283e13 --- /dev/null +++ b/Others/2022spring-cmbchina/C/main.cpp @@ -0,0 +1,106 @@ +/// Source : https://leetcode-cn.com/contest/cmbchina-2022spring/problems/Dk2Ytp/ +/// Author : liuyubobobo +/// Time : 2022-04-13 + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time complexity: O(V + E) +/// Space Complexity: O(V + E) +class Solution { +public: + vector lightSticks(int R, int C, vector& indices) { + + set removed_edges(indices.begin(), indices.end()); + + int n = (R + 1) * (C + 1); + vector vertex(n, false); + vector> g(n); + + for(int r = 0; r <= R; r ++){ + + for(int edge = 0; edge < C; edge ++){ + + int edge_id = r * (C + C + 1) + edge; + if(removed_edges.count(edge_id)) continue; + + int a = r * (C + 1) + edge, b = a + 1; + vertex[a] = vertex[b] = true; + g[a].push_back(b), g[b].push_back(a); + } + + if(r == R) break; + + for(int edge = 0; edge <= C; edge ++){ + int edge_id = r * (C + C + 1) + C + edge; + if(removed_edges.count(edge_id)) continue; + + int a = r * (C + 1) + edge, b = (r + 1) * (C + 1) + edge; + vertex[a] = vertex[b] = true; + g[a].push_back(b), g[b].push_back(a); + } + } + + for(int u = 0; u < n; u ++) + if(vertex[u]){ + vector visited(n, false); + dfs(g, u, visited); + + if(visited != vertex) return {}; + break; + } + + vector res; + int best = INT_MAX; + for(int u = 0; u < n; u ++) + if(vertex[u]){ + int d = bfs(n, g, u); + if(d < best) res = {u}, best = d; + else if(d == best) res.push_back(u); + } + return res; + } + +private: + int bfs(int n, const vector>& g, int s){ + + vector dis(n, -1); + queue q; + q.push(s); + dis[s] = 0; + int res = 0; + while(!q.empty()){ + int u = q.front(); q.pop(); + + for(int v: g[u]) + if(dis[v] == -1){ + dis[v] = dis[u] + 1; + res = max(res, dis[v]); + q.push(v); + } + } + return res; + } + + void dfs(const vector>& g, int u, vector& visited){ + + visited[u] = true; + for(int v: g[u]) + if(!visited[v]) dfs(g, v, visited); + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022spring-cmbchina/D/CMakeLists.txt b/Others/2022spring-cmbchina/D/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/Others/2022spring-cmbchina/D/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/Others/2022spring-cmbchina/D/main.cpp b/Others/2022spring-cmbchina/D/main.cpp new file mode 100644 index 00000000..6e6d304b --- /dev/null +++ b/Others/2022spring-cmbchina/D/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode-cn.com/contest/cmbchina-2022spring/problems/OK3hsO/ +/// Author : liuyubobobo +/// Time : 2022-04-13 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int goShopping(vector& priceA, vector& priceB) { + + int n = priceA.size(); + vector> price(n); + for(int i = 0; i < n; i ++) + price[i] = {priceB[i] * 10ll, priceA[i] * 10ll}; + + sort(price.begin(), price.end(), greater>()); + + // 2 * 4 * 3 * n + // 2: 0 - no use a discount; 1 - use a discount + // 4: last a number + // 3: last b number + vector> dp(24, vector(n, -1)); + return min(dfs(n, price, 0, 0, 0, 0, dp), dfs(n, price, 1, 0, 0, 0, dp)) / 10; + } + +private: + long long dfs(int n, const vector>& price, + int use_a, int last_a, int last_b, int index, + vector>& dp){ + + if(index == n){ + if(!use_a) return last_a == 3 ? LONG_LONG_MAX / 2 : 0; + return last_a < 3 ? LONG_LONG_MAX / 2 : 0; + } + + if(!use_a && last_a == 3) return LONG_LONG_MAX / 2; + + int state1 = use_a * 12 + last_a * 3 + last_b; + if( dp[state1][index] != -1) return dp[state1][index]; + + long long res = (last_b == 2 ? 0 : price[index].first) + dfs(n, price, use_a, last_a, (last_b + 1) % 3, index + 1, dp); + if(use_a) + res = min(res, price[index].second / 10 * 7 + dfs(n, price, use_a, min(last_a + 1, 3), last_b, index + 1, dp)); + else + res = min(res, price[index].second + dfs(n, price, use_a, min(last_a + 1, 3), last_b, index + 1, dp)); + return dp[state1][index] = res; + } +}; + +int main() { + + vector price1A = {1, 2, 5}, price1B = {2, 2, 2}; + cout << Solution().goShopping(price1A, price1B) << '\n'; + // 4 + + vector price2A = {1, 6, 1}, price2B = {2, 2, 6}; + cout << Solution().goShopping(price2A, price2B) << '\n'; + // 4 + + vector price3A = {3, 13, 5, 12}, price3B = {28, 12, 20, 7}; + cout << Solution().goShopping(price3A, price3B) << '\n'; + // 21 + + return 0; +} diff --git a/readme.md b/readme.md index c3c90dd6..4a065a7e 100644 --- a/readme.md +++ b/readme.md @@ -2090,10 +2090,15 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | -| 银联-01 | [回文链表](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/D7rekZ/) | [无] | [C++](Others/2022spring-cnunionpay/A/) | | | -| 银联-02 | [优惠活动系统](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/kDPV0f/) | [无] | [C++](Others/2022spring-cnunionpay/B/) | | | -| 银联-03 | [理财产品](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/I4mOGz/) | [无] | [C++](Others/2022spring-cnunionpay/C/) | | | -| 银联-04 | [合作开发](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/lCh58I/) | [无] | [C++](Others/2022spring-cnunionpay/D/) | | | +| | | | | | | +| 22春 招商银行-01 | [文本编辑程序设计](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/fWcPGC/) | [无] | [C++](Others/2022spring-cmbchina/A/) | | | +| 22春 招商银行-02 | [公园规划](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/ReWLAw/) | [无] | [C++](Others/2022spring-cmbchina/B/) | | | +| 22春 招商银行-03 | [点燃木棒](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/Dk2Ytp/) | [无] | [C++](Others/2022spring-cmbchina/C/) | | | +| 22春 招商银行-04 | [商店促销活动](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/OK3hsO/) | [无] | [C++](Others/2022spring-cmbchina/D/) | | | +| 22春 银联-01 | [回文链表](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/D7rekZ/) | [无] | [C++](Others/2022spring-cnunionpay/A/) | | | +| 22春 银联-02 | [优惠活动系统](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/kDPV0f/) | [无] | [C++](Others/2022spring-cnunionpay/B/) | | | +| 22春 银联-03 | [理财产品](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/I4mOGz/) | [无] | [C++](Others/2022spring-cnunionpay/C/) | | | +| 22春 银联-04 | [合作开发](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/lCh58I/) | [无] | [C++](Others/2022spring-cnunionpay/D/) | | | | | | | | | | | LCS01 | [下载插件](https://leetcode-cn.com/problems/Ju9Xwi/) | [无] | [C++](LC/LCS01/cpp-LCS01/) | | | | LCS02 | [完成一半题目](https://leetcode-cn.com/problems/WqXACV/) | [无] | [C++](LC/LCS02/cpp-LCS02/) | | | From 6ae8e5c6def500b029e537c8a5deefd3b9c665e9 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 13 Apr 2022 12:16:24 -0700 Subject: [PATCH 004/390] 2235 solved. --- .../cpp-2235/CMakeLists.txt | 6 +++++ .../2235-Add-Two-Integers/cpp-2235/main.cpp | 24 +++++++++++++++++++ readme.md | 1 + 3 files changed, 31 insertions(+) create mode 100644 2001-2500/2235-Add-Two-Integers/cpp-2235/CMakeLists.txt create mode 100644 2001-2500/2235-Add-Two-Integers/cpp-2235/main.cpp diff --git a/2001-2500/2235-Add-Two-Integers/cpp-2235/CMakeLists.txt b/2001-2500/2235-Add-Two-Integers/cpp-2235/CMakeLists.txt new file mode 100644 index 00000000..e59503a6 --- /dev/null +++ b/2001-2500/2235-Add-Two-Integers/cpp-2235/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2235) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2235 main.cpp) diff --git a/2001-2500/2235-Add-Two-Integers/cpp-2235/main.cpp b/2001-2500/2235-Add-Two-Integers/cpp-2235/main.cpp new file mode 100644 index 00000000..bb3ec453 --- /dev/null +++ b/2001-2500/2235-Add-Two-Integers/cpp-2235/main.cpp @@ -0,0 +1,24 @@ +/// Source : https://leetcode.com/problems/add-two-integers/ +/// Author : liuyubobobo +/// Time : 2022-04-13 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int sum(int num1, int num2) { + return num1 + num2; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 4a065a7e..5e76b9d5 100644 --- a/readme.md +++ b/readme.md @@ -2039,6 +2039,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2232 | [Minimize Result by Adding Parentheses to Expression](https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/) | [无] | [C++](2001-2500/2232-Minimize-Result-by-Adding-Parentheses-to-Expression/cpp-2232/) | | | | 2233 | [Maximum Product After K Increments](https://leetcode.com/problems/maximum-product-after-k-increments/) | [无] | [C++](2001-2500/2233-Maximum-Product-After-K-Increments/cpp-2233/) | | | | 2234 | [Maximum Total Beauty of the Gardens](https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/) | [无] | [C++](2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/) | | | +| 2235 | [Add Two Integers](https://leetcode.com/problems/add-two-integers/) | [无] | [C++](2001-2500/2235-Add-Two-Integers/cpp-2235/) | | | | | | | | | | ## 力扣中文站比赛 From a25a63b2ad906c4e96c3ebbf12dca407b0a1f81f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 13 Apr 2022 12:26:39 -0700 Subject: [PATCH 005/390] 2236 solved. --- .../cpp-2236/CMakeLists.txt | 6 ++++ .../cpp-2236/main.cpp | 35 +++++++++++++++++++ readme.md | 1 + 3 files changed, 42 insertions(+) create mode 100644 2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/CMakeLists.txt create mode 100644 2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/main.cpp diff --git a/2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/CMakeLists.txt b/2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/CMakeLists.txt new file mode 100644 index 00000000..73e8d34b --- /dev/null +++ b/2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2236) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2236 main.cpp) diff --git a/2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/main.cpp b/2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/main.cpp new file mode 100644 index 00000000..88756e33 --- /dev/null +++ b/2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/root-equals-sum-of-children/ +/// Author : liuyubobobo +/// Time : 2022-04-13 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(1) +/// Space Complexity: O(1) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + bool checkTree(TreeNode* root) { + return root->val == root->left->val + root->right->val; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 5e76b9d5..33fb86ff 100644 --- a/readme.md +++ b/readme.md @@ -2040,6 +2040,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2233 | [Maximum Product After K Increments](https://leetcode.com/problems/maximum-product-after-k-increments/) | [无] | [C++](2001-2500/2233-Maximum-Product-After-K-Increments/cpp-2233/) | | | | 2234 | [Maximum Total Beauty of the Gardens](https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/) | [无] | [C++](2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/) | | | | 2235 | [Add Two Integers](https://leetcode.com/problems/add-two-integers/) | [无] | [C++](2001-2500/2235-Add-Two-Integers/cpp-2235/) | | | +| 2236 | [Root Equals Sum of Children](https://leetcode.com/problems/root-equals-sum-of-children/) | [无] | [C++](2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/) | | | | | | | | | | ## 力扣中文站比赛 From 9d4b008f7fd4b31da047a25638b0341026d97d09 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 13 Apr 2022 21:25:03 -0700 Subject: [PATCH 006/390] 2237 solved. --- .../cpp-2237/CMakeLists.txt | 6 +++ .../cpp-2237/main.cpp | 39 +++++++++++++++++++ readme.md | 1 + 3 files changed, 46 insertions(+) create mode 100644 2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/CMakeLists.txt create mode 100644 2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/main.cpp diff --git a/2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/CMakeLists.txt b/2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/CMakeLists.txt new file mode 100644 index 00000000..4fe34c35 --- /dev/null +++ b/2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2237) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2237 main.cpp) diff --git a/2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/main.cpp b/2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/main.cpp new file mode 100644 index 00000000..4bc448f2 --- /dev/null +++ b/2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/count-positions-on-street-with-required-brightness/ +/// Author : liuyubobobo +/// Time : 2022-04-13 + +#include +#include + +using namespace std; + + +/// Diff Array +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int meetRequirement(int n, vector>& lights, vector& requirement) { + + vector diff(n + 1, 0); + for(const vector& light: lights){ + int pos = light[0], range = light[1]; + int l = max(0, pos - range), r = min(n - 1, pos + range); + + diff[l] += 1, diff[r + 1] -= 1; + } + + int cur = 0, res = 0; + for(int i = 0; i < n; i ++){ + cur += diff[i]; + res += (cur >= requirement[i]); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 33fb86ff..16296e07 100644 --- a/readme.md +++ b/readme.md @@ -2041,6 +2041,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2234 | [Maximum Total Beauty of the Gardens](https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/) | [无] | [C++](2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/) | | | | 2235 | [Add Two Integers](https://leetcode.com/problems/add-two-integers/) | [无] | [C++](2001-2500/2235-Add-Two-Integers/cpp-2235/) | | | | 2236 | [Root Equals Sum of Children](https://leetcode.com/problems/root-equals-sum-of-children/) | [无] | [C++](2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/) | | | +| 2237 | [Count Positions on Street With Required Brightness](https://leetcode.com/problems/count-positions-on-street-with-required-brightness/) | [无] | [C++](2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/) | | | | | | | | | | ## 力扣中文站比赛 From 0e956e10ab07b25b69ee674ad9be751643fea929 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 14 Apr 2022 18:37:05 -0700 Subject: [PATCH 007/390] 0385 solved. --- .../0385-Mini-Parser/cpp-0385/CMakeLists.txt | 6 + 0001-0500/0385-Mini-Parser/cpp-0385/main.cpp | 123 ++++++++++++++++++ readme.md | 2 +- 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0385-Mini-Parser/cpp-0385/CMakeLists.txt create mode 100644 0001-0500/0385-Mini-Parser/cpp-0385/main.cpp diff --git a/0001-0500/0385-Mini-Parser/cpp-0385/CMakeLists.txt b/0001-0500/0385-Mini-Parser/cpp-0385/CMakeLists.txt new file mode 100644 index 00000000..4f14b5ed --- /dev/null +++ b/0001-0500/0385-Mini-Parser/cpp-0385/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0385) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0385 main.cpp) diff --git a/0001-0500/0385-Mini-Parser/cpp-0385/main.cpp b/0001-0500/0385-Mini-Parser/cpp-0385/main.cpp new file mode 100644 index 00000000..9bd99bdf --- /dev/null +++ b/0001-0500/0385-Mini-Parser/cpp-0385/main.cpp @@ -0,0 +1,123 @@ +/// Source : https://leetcode.com/problems/mini-parser/ +/// Author : liuyubobobo +/// Time : 2022-04-14 + +#include +#include +#include + +using namespace std; + + +/// Recursive +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +// This is the interface that allows for creating nested lists. +// You should not implement it, or speculate about its implementation +class NestedInteger { + +private: + bool is_integer; + int x; + vector v; + +public: + // Constructor initializes an empty nested list. + NestedInteger(){} + + // Constructor initializes a single integer. + NestedInteger(int value) : is_integer(true), x(value){}; + + // Return true if this NestedInteger holds a single integer, rather than a nested list. + bool isInteger(){ + return is_integer; + }; + + // Return the single integer that this NestedInteger holds, if it holds a single integer + // The result is undefined if this NestedInteger holds a nested list + int getInteger(){ + assert(is_integer); + return x; + }; + + // Set this NestedInteger to hold a single integer. + void setInteger(int value){ + is_integer = true; + x = value; + } + + // Set this NestedInteger to hold a nested list and adds a nested integer to it. + void add(const NestedInteger &ni){ + is_integer = false; + v.push_back(ni); + } + + // Return the nested list that this NestedInteger holds, if it holds a nested list + // The result is undefined if this NestedInteger holds a single integer + const vector getList() const{ + return v; + } +}; + +class Solution { +public: + NestedInteger deserialize(string s) { + + int n = s.size(); + vector right(n, -1), stack; + for(int i = 0; i < n; i ++){ + if(s[i] == '[') stack.push_back(i); + else if(s[i] == ']'){ + assert(!stack.empty()); + int l = stack.back(); stack.pop_back(); + right[l] = i; + } + } + return dfs(s, 0, n - 1, right); + } + +private: + NestedInteger dfs(const string& s, int l, int r, const vector& right){ + + if(l + 1 == r && s[l] == '['){ + NestedInteger res; + return res; + } + + if(isdigit(s[l]) || s[l] == '-'){ + NestedInteger res; + int x = atol(s.substr(l, r - l + 1).c_str()); + res.setInteger(x); + return res; + } + + assert(s[l] == '[' && s[r] == ']'); + NestedInteger res; + for(int start = l + 1, i = l + 1; i <= r;){ + if(i == r || s[i] == ','){ + res.add(dfs(s, start, i - 1, right)); + start = i + 1; + i = start; + } + else if(s[i] == '['){ + assert(right[i] <= r && s[right[i]] == ']'); + res.add(dfs(s, i, right[i], right)); + start = right[i] + 2; + i = start; + } + else i ++; + } + return res; + } +}; + + +int main() { + + Solution().deserialize("[123,[456,[789]]]"); + + Solution().deserialize("-3"); + + return 0; +} diff --git a/readme.md b/readme.md index 16296e07..719bd970 100644 --- a/readme.md +++ b/readme.md @@ -414,7 +414,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 382 | [Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/description/) | [无] | [C++](0001-0500/0382-Linked-List-Random-Node/cpp-0382/) | | | | 383 | [Ransom Note](https://leetcode.com/problems/ransom-note/) | [solution](https://leetcode.com/problems/ransom-note/solution/) | [C++](0001-0500/0383-Ransom-Note/cpp-0383/) | | | | 384 | [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/description/) | [solution](https://leetcode.com/problems/shuffle-an-array/solution/) | [C++](0001-0500/0384-Shuffle-an-Array/cpp-0384/) | | | -| | | | | | | +| 385 | [Mini Parser](https://leetcode.com/problems/mini-parser/) | [无] | [C++](0001-0500/0385-Mini-Parser/cpp-0385/) | | | | 386 | [Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/description/) | | [C++](0001-0500/0386-Lexicographical-Numbers/cpp-0386/) | | | | 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/description/) | [solution](https://leetcode.com/problems/first-unique-character-in-a-string/solution/) | [C++](0001-0500/0387-First-Unique-Character-in-a-String/cpp-0387/) | [Java](0001-0500/0387-First-Unique-Character-in-a-String/java-0387/src/) | | | 388 | [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/description/) | | [C++](0001-0500/0388-Longest-Absolute-File-Path/cpp-0388/) | | | From e1a9ec427f1e3ec4a00c8926401e4ac7b5e3feb2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 15 Apr 2022 15:13:18 -0700 Subject: [PATCH 008/390] 0479 solved. --- .../cpp-0479/CMakeLists.txt | 6 ++ .../cpp-0479/main.cpp | 49 +++++++++++++ .../cpp-0479/main2.cpp | 73 +++++++++++++++++++ readme.md | 4 +- 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0479-Largest-Palindrome-Product/cpp-0479/CMakeLists.txt create mode 100644 0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main.cpp create mode 100644 0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main2.cpp diff --git a/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/CMakeLists.txt b/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/CMakeLists.txt new file mode 100644 index 00000000..ae545ca1 --- /dev/null +++ b/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0479) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0479 main2.cpp) diff --git a/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main.cpp b/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main.cpp new file mode 100644 index 00000000..b02dba2a --- /dev/null +++ b/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/largest-palindrome-product/ +/// Author : liuyubobobo +/// Time : 2022-04-15 + +#include +#include + +using namespace std; + + +/// Iterate Palindrome +/// Time Complexity: O(10^(2n)) in theory but in fact it is very fast +/// Space Complexity: O(n) +class Solution { +public: + int largestPalindrome(int n) { + + long long maxv = pow(10, n) - 1; + long long minv = pow(10, n - 1); + for(int a = maxv; a >= minv; a --){ + long long num = get_palindrome_num(a); + + for(long long d = maxv; d >= minv; d --){ + long long nd = num / d; + if(nd > d) break; + if(d * nd == num) return num % 1337ll; + } + } + return 9; + } + +private: + long long get_palindrome_num(int a){ + string left = to_string(a); + string right = left; + reverse(right.begin(), right.end()); + string res = left + right; + return atoll(res.c_str()); + } +}; + + +int main() { + + cout << Solution().largestPalindrome(2) << '\n'; + // 987 + + return 0; +} diff --git a/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main2.cpp b/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main2.cpp new file mode 100644 index 00000000..827be19d --- /dev/null +++ b/0001-0500/0479-Largest-Palindrome-Product/cpp-0479/main2.cpp @@ -0,0 +1,73 @@ +/// Source : https://leetcode.com/problems/largest-palindrome-product/ +/// Author : liuyubobobo +/// Time : 2022-04-15 + +#include +#include +#include +#include + +using namespace std; + + +/// Iterate Two numbers int length n +/// Time Complexity: O(10^(2n)) in theory but in fact it is very fast +/// Space Complexity: O(10^(2n)) in theory but in fact it is very small +class Solution { +public: + int largestPalindrome(int n) { + + priority_queue>> pq; + set> visited; + + long long base = pow(10, n) - 1; + + pq.push({(base - 9 + 9) * (base - 9 + 1), {base - 9 + 9, base - 9 + 1}}); + visited.insert({base - 9 + 9, base - 9 + 1}); + + pq.push({(base - 9 + 3) * (base - 9 + 3), {base - 9 + 3, base - 9 + 3}}); + visited.insert({base - 9 + 3, base - 9 + 3}); + + pq.push({(base - 9 + 7) * (base - 9 + 7), {base - 9 + 7, base - 9 + 7}}); + visited.insert({base - 9 + 7, base - 9 + 7}); + + while(!pq.empty()){ + long long num = pq.top().first; + long long a = pq.top().second.first, b = pq.top().second.second; + pq.pop(); + + if(is_palindrome(num)) return num % 1337; + + pair next1 = {max(a - 10, b), min(a - 10, b)}; + if(!visited.count(next1)){ + visited.insert(next1); + pq.push({next1.first * next1.second, next1}); + } + + pair next2 = {max(a, b - 10), min(a, b - 10)}; + if(!visited.count(next2)){ + visited.insert(next2); + pq.push({next2.first * next2.second, next2}); + } + } + return 9; + } + +private: + bool is_palindrome(long long num){ + + string s = to_string(num); + for(int i = 0, j = s.size() - 1; i < j; i ++, j --) + if(s[i] != s[j]) return false; + return true; + } +}; + + +int main() { + + cout << Solution().largestPalindrome(2) << '\n'; + // 987 + + return 0; +} diff --git a/readme.md b/readme.md index 719bd970..fe85dcd7 100644 --- a/readme.md +++ b/readme.md @@ -500,7 +500,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [solution](https://leetcode.com/problems/number-complement/solution/) | [C++](0001-0500/0476-Number-Complement/cpp-0476/) | | | | 477 | [Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [solution](https://leetcode.com/problems/total-hamming-distance/solution/) [题解](https://leetcode-cn.com/problems/total-hamming-distance/solution/yi-ming-ju-chi-zong-he-by-leetcode/) | [C++](0001-0500/477-Total-Hamming-Distance/cpp-477/) | | | | 478 | [Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/description/) | [solution](https://leetcode.com/problems/generate-random-point-in-a-circle/solution/) | [C++](0001-0500/0478-Generate-Random-Point-in-a-Circle/cpp-0478/) | | | -| | | | | | | +| 479 | [Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/) | [无] | [C++](0001-0500/0479-Largest-Palindrome-Product/cpp-0479/) | | | | 480 | [Sliding Window Median](https://leetcode.com/problems/sliding-window-median/) | [无]
[缺:双数据结构] | [C++](0001-0500/0480-Sliding-Window-Median/cpp-0480/) | | | | | | | | | | | 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [无] | [C++](0001-0500/0482-License-Key-Formatting/cpp-0482/) | | | @@ -591,6 +591,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | [solution](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/solution/)
[缺:Stack] | [C++](0501-1000/0581-Shortest-Unsorted-Continuous-Subarray/cpp-0581/) | | | | 582 | [Kill Process](https://leetcode.com/problems/kill-process/) | [solution](https://leetcode.com/problems/kill-process/solution/) | [C++](0501-1000/0582-Kill-Process/cpp-0582/) | | | | 583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/description/) | [solution](https://leetcode.com/problems/delete-operation-for-two-strings/solution/) | [C++](0501-1000/0583-Delete-Operation-for-Two-Strings/cpp-0583/) | | | +| 584 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 587 | [Erect the Fence](https://leetcode.com/problems/erect-the-fence/) | [solution](https://leetcode.com/problems/erect-the-fence/solution/) [题解](https://leetcode-cn.com/problems/erect-the-fence/solution/an-zhuang-zha-lan-by-leetcode/)
[缺:凸包解法整理] | [C++](0501-1000/0587-Erect-the-Fence/cpp-0587/) | | | | | | | | | | @@ -598,6 +599,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 590 | [N-ary Tree Postorder Transversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-postorder-traversal/solution/) | [C++](0501-1000/0590-N-ary-Tree-Postorder-Transversal/cpp-0590/) | | | | | | | | | | | 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) | [solution](https://leetcode.com/problems/longest-harmonious-subsequence/solution/) | [C++](0501-1000/0594-Longest-Harmonious-Subsequence/cpp-0594/) | | | +| 595 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii/description/) | | [C++](0501-1000/0598-Range-Addition-II/cpp-0598/) | | | | 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/explore/learn/card/hash-table/184/comparison-with-other-data-structures/1177/) | [solution](https://leetcode.com/problems/minimum-index-sum-of-two-lists/solution/) | [C++](0501-1000/0599-Minimum-Index-Sum-of-Two-Lists/cpp-0599/) | | | From d315c7956a754128c6612b691f014f9351a14c7e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 13:06:20 -0700 Subject: [PATCH 009/390] 2239 solved. --- .../cpp-2239/CMakeLists.txt | 6 ++++ .../cpp-2239/main.cpp | 32 +++++++++++++++++++ readme.md | 5 +++ 3 files changed, 43 insertions(+) create mode 100644 2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/CMakeLists.txt create mode 100644 2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/main.cpp diff --git a/2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/CMakeLists.txt b/2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/CMakeLists.txt new file mode 100644 index 00000000..ad45c5db --- /dev/null +++ b/2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2239) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2239 main.cpp) diff --git a/2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/main.cpp b/2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/main.cpp new file mode 100644 index 00000000..24ebc634 --- /dev/null +++ b/2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/find-closest-number-to-zero/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include +#include + +using namespace std; + + +/// Linear Search +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int findClosestNumber(vector& nums) { + + int best = INT_MAX, res = 0; + for(int e: nums){ + if(abs(e) < best) res = e, best = abs(e); + else if(abs(e) == best) res = max(res, e); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index fe85dcd7..43ae0cf4 100644 --- a/readme.md +++ b/readme.md @@ -233,6 +233,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [solution](https://leetcode.com/problems/reverse-bits/solution/) | [C++](0001-0500/0190-Reverse-Bits/cpp-0190/) | | | | 191 | [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/description/) | [solution](https://leetcode.com/problems/number-of-1-bits/solution/) | [C++](0001-0500/0191-Number-of-1-Bits/cpp-0191/) | | | | | | | | | | +| 196 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 198 | [House Robber](https://leetcode.com/problems/house-robber/description/) | [solution](https://leetcode.com/problems/house-robber/solution/) | [C++](0001-0500/0198-House-Robber/cpp-0198/) | [Java](0001-0500/0198-House-Robber/java-0198/src/) | | | 199 | [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) | [solution](https://leetcode.com/problems/binary-tree-right-side-view/solution/) | [C++](0001-0500/199-Binary-Tree-Right-Side-View/cpp-0199/) | | | | 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [solution](https://leetcode.com/problems/number-of-islands/solution/) | [C++](0001-0500/0200-Number-of-Islands/cpp-0200/) | [Java](0001-0500/0200-Number-of-Islands/java-0200/src/) | | @@ -618,6 +620,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 622 | [Design Circular Queue](https://leetcode.com/problems/design-circular-queue/description/) | [无] | [C++](0501-1000/0622-Design-Circular-Queue/cpp-0622/) | | | | 623 | [Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | [solution](https://leetcode.com/problems/add-one-row-to-tree/solution/) | [C++](0501-1000/0623-Add-One-Row-to-Tree/cpp-0623/) | | | | | | | | | | +| 627 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [solution](https://leetcode.com/problems/maximum-product-of-three-numbers/solution/) | [C++](0501-1000/0628-Maximum-Product-of-Three-Numbers/cpp-0628/) | | | | 629 | [K-Inverse-Pairs-Array](https://leetcode.com/problems/k-inverse-pairs-array/) | [solution](https://leetcode.com/problems/k-inverse-pairs-array/solution/)
[缺:其他 dp 优化思路] | [C++](0501-1000/0629-K-Inverse-Pairs-Array/cpp-0629/) | | | | 630 | [Course Schedule III](https://leetcode.com/problems/course-schedule-iii/) | [solution](https://leetcode.com/problems/course-schedule-iii/solution/) | [C++](0501-1000/0630-Course-Schedule-III/cpp-0630/) | | | @@ -2044,6 +2047,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2235 | [Add Two Integers](https://leetcode.com/problems/add-two-integers/) | [无] | [C++](2001-2500/2235-Add-Two-Integers/cpp-2235/) | | | | 2236 | [Root Equals Sum of Children](https://leetcode.com/problems/root-equals-sum-of-children/) | [无] | [C++](2001-2500/2236-Root-Equals-Sum-of-Children/cpp-2236/) | | | | 2237 | [Count Positions on Street With Required Brightness](https://leetcode.com/problems/count-positions-on-street-with-required-brightness/) | [无] | [C++](2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/) | | | +| 2238 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2239 | [Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero/) | [无] | [C++](2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/) | | | | | | | | | | ## 力扣中文站比赛 From bba7d04bd65180d3448e997b769b3aa8de9a82da Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 13:12:24 -0700 Subject: [PATCH 010/390] 2240 solved. --- .../cpp-2240/CMakeLists.txt | 6 ++++ .../cpp-2240/main.cpp | 29 +++++++++++++++++++ readme.md | 1 + 3 files changed, 36 insertions(+) create mode 100644 2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/CMakeLists.txt create mode 100644 2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/main.cpp diff --git a/2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/CMakeLists.txt b/2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/CMakeLists.txt new file mode 100644 index 00000000..ac581fb9 --- /dev/null +++ b/2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2240) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2240 main.cpp) diff --git a/2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/main.cpp b/2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/main.cpp new file mode 100644 index 00000000..bc2855c9 --- /dev/null +++ b/2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(total / cost1) +/// Space Complexity: O(1) +class Solution { +public: + long long waysToBuyPensPencils(int total, int cost1, int cost2) { + + long long res = 0; + for(int cnt1 = 0; cost1 * cnt1 <= total; cnt1 ++){ + res += (total - cost1 * cnt1) / cost2 + 1; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 43ae0cf4..3aac97f2 100644 --- a/readme.md +++ b/readme.md @@ -2049,6 +2049,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2237 | [Count Positions on Street With Required Brightness](https://leetcode.com/problems/count-positions-on-street-with-required-brightness/) | [无] | [C++](2001-2500/2237-Count-Positions-on-Street-With-Required-Brightness/cpp-2237/) | | | | 2238 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2239 | [Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero/) | [无] | [C++](2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/) | | | +| 2240 | [Number of Ways to Buy Pens and Pencils](https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/) | [无] | [C++](2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/) | | | | | | | | | | ## 力扣中文站比赛 From f5729e3b063ac1da7b0d4882ae983ad9966be3c4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 13:27:02 -0700 Subject: [PATCH 011/390] 2241 solved. --- .../cpp-2241/CMakeLists.txt | 6 +++ .../cpp-2241/main.cpp | 53 +++++++++++++++++++ readme.md | 1 + 3 files changed, 60 insertions(+) create mode 100644 2001-2500/2241-Design-an-ATM-Machine/cpp-2241/CMakeLists.txt create mode 100644 2001-2500/2241-Design-an-ATM-Machine/cpp-2241/main.cpp diff --git a/2001-2500/2241-Design-an-ATM-Machine/cpp-2241/CMakeLists.txt b/2001-2500/2241-Design-an-ATM-Machine/cpp-2241/CMakeLists.txt new file mode 100644 index 00000000..9b165d59 --- /dev/null +++ b/2001-2500/2241-Design-an-ATM-Machine/cpp-2241/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2241) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2241 main.cpp) diff --git a/2001-2500/2241-Design-an-ATM-Machine/cpp-2241/main.cpp b/2001-2500/2241-Design-an-ATM-Machine/cpp-2241/main.cpp new file mode 100644 index 00000000..b378477c --- /dev/null +++ b/2001-2500/2241-Design-an-ATM-Machine/cpp-2241/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/design-an-atm-machine/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(1) for every operation +/// Space Complexity: O(1) +class ATM { + +private: + const vector values = {20, 50, 100, 200, 500}; + const int dnum = 5; + vector cnt; + +public: + ATM() : cnt(dnum, 0) {} + + void deposit(vector banknotesCount) { + + assert(banknotesCount.size() == dnum); + for(int i = 0; i < dnum; i ++) + cnt[i] += banknotesCount[i]; + } + + vector withdraw(int amount) { + + vector res(dnum, 0); + for(int i = dnum - 1; i >= 0 && amount; i --){ + long long t = min(amount / values[i], cnt[i]); + cnt[i] -= t; + res[i] += t; + amount -= t * values[i]; + } + + if(amount == 0) return res; + + deposit(res); + return {-1}; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3aac97f2..b06a727e 100644 --- a/readme.md +++ b/readme.md @@ -2050,6 +2050,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2238 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2239 | [Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero/) | [无] | [C++](2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/) | | | | 2240 | [Number of Ways to Buy Pens and Pencils](https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/) | [无] | [C++](2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/) | | | +| 2241 | [Design an ATM Machine](https://leetcode.com/problems/design-an-atm-machine/) | [无] | [C++](2001-2500/2241-Design-an-ATM-Machine/cpp-2241/) | | | | | | | | | | ## 力扣中文站比赛 From 39925941660766c7a75bcbbf802e1992d4a3c751 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 21:37:22 -0700 Subject: [PATCH 012/390] 2243 solved. --- .../cpp-2243/CMakeLists.txt | 6 ++ .../cpp-2243/main.cpp | 58 +++++++++++++++++++ readme.md | 2 + 3 files changed, 66 insertions(+) create mode 100644 2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt create mode 100644 2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp diff --git a/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt new file mode 100644 index 00000000..d5d5e4ff --- /dev/null +++ b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.17) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) \ No newline at end of file diff --git a/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp new file mode 100644 index 00000000..300dcf63 --- /dev/null +++ b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp @@ -0,0 +1,58 @@ +#include +#include + +using namespace std; + +class Solution { +public: + bool containsPattern(vector& arr, int m, int k) { + + for(int start = 0; start + m < arr.size(); start ++) + if(ok(arr, start, m, k)) return true; + return false; + } + +private: + bool ok(const vector& arr, int start, int m, int k){ + + for(int i = 0; i < m; i ++) + for(int j = 0; j < k; j ++){ + if(start + j * m + i >= arr.size()) return false; + if(arr[start + i] != arr[start + j * m + i]) return false; + } + return true; + } +}; + +int main() { + + vector arr1 = {1,2,4,4,4,4}; + cout << Solution().containsPattern(arr1, 1, 3) << endl; + // 1 + + vector arr2 = {1,2,4,4,4}; + cout << Solution().containsPattern(arr2, 1, 3) << endl; + // 1 + + vector arr3 = {1,2,4,4}; + cout << Solution().containsPattern(arr3, 1, 3) << endl; + // 0 + + vector arr4 = {1,2,1,2,1,1,1,3}; + cout << Solution().containsPattern(arr4, 2, 2) << endl; + // 1 + + vector arr5 = {1,2,1,2,1,3}; + cout << Solution().containsPattern(arr5, 2, 3) << endl; + // 0 + + vector arr6 = {1,2,3,1,2}; + cout << Solution().containsPattern(arr6, 2, 2) << endl; + // 0 + + vector arr7 = {2,2,2,2}; + cout << Solution().containsPattern(arr7, 2, 3) << endl; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index b06a727e..18e82251 100644 --- a/readme.md +++ b/readme.md @@ -2052,6 +2052,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2240 | [Number of Ways to Buy Pens and Pencils](https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/) | [无] | [C++](2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/) | | | | 2241 | [Design an ATM Machine](https://leetcode.com/problems/design-an-atm-machine/) | [无] | [C++](2001-2500/2241-Design-an-ATM-Machine/cpp-2241/) | | | | | | | | | | +| 2243 | [Calculate Digit Sum of a String](https://leetcode.com/problems/calculate-digit-sum-of-a-string/) | [无] | [C++](2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/) | | | +| | | | | | | ## 力扣中文站比赛 From 77e4a8cf6ae7d38b1e3ed2fbc909a1458960ed18 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 21:46:17 -0700 Subject: [PATCH 013/390] 2243 solved. --- .../cpp-2243/CMakeLists.txt | 4 +- .../cpp-2243/main.cpp | 63 +++++++------------ 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt index d5d5e4ff..a3422f8f 100644 --- a/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt +++ b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/CMakeLists.txt @@ -1,6 +1,6 @@ -cmake_minimum_required(VERSION 3.17) +cmake_minimum_required(VERSION 3.21) project(A) set(CMAKE_CXX_STANDARD 14) -add_executable(A main.cpp) \ No newline at end of file +add_executable(A main.cpp) diff --git a/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp index 300dcf63..86817c25 100644 --- a/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp +++ b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp @@ -1,58 +1,41 @@ +/// Source : https://leetcode.com/problems/calculate-digit-sum-of-a-string/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + #include #include using namespace std; + +/// Simulation +/// Time Complexity: O(|s|) +/// Space Complexity: O(1) class Solution { public: - bool containsPattern(vector& arr, int m, int k) { + string digitSum(string s, int k) { + + while(s.size() > k){ - for(int start = 0; start + m < arr.size(); start ++) - if(ok(arr, start, m, k)) return true; - return false; + string nexts = ""; + for(int i = 0; i < s.size(); i += k) + nexts += to_string(sum(s.substr(i, k))); + s = nexts; + } + return s; } private: - bool ok(const vector& arr, int start, int m, int k){ - - for(int i = 0; i < m; i ++) - for(int j = 0; j < k; j ++){ - if(start + j * m + i >= arr.size()) return false; - if(arr[start + i] != arr[start + j * m + i]) return false; - } - return true; + int sum(const string& s){ + int res = 0; + for(int i = 0; i < s.size(); i ++) + res += (s[i] - '0'); + return res; } }; -int main() { - - vector arr1 = {1,2,4,4,4,4}; - cout << Solution().containsPattern(arr1, 1, 3) << endl; - // 1 - - vector arr2 = {1,2,4,4,4}; - cout << Solution().containsPattern(arr2, 1, 3) << endl; - // 1 - vector arr3 = {1,2,4,4}; - cout << Solution().containsPattern(arr3, 1, 3) << endl; - // 0 - - vector arr4 = {1,2,1,2,1,1,1,3}; - cout << Solution().containsPattern(arr4, 2, 2) << endl; - // 1 - - vector arr5 = {1,2,1,2,1,3}; - cout << Solution().containsPattern(arr5, 2, 3) << endl; - // 0 - - vector arr6 = {1,2,3,1,2}; - cout << Solution().containsPattern(arr6, 2, 2) << endl; - // 0 - - vector arr7 = {2,2,2,2}; - cout << Solution().containsPattern(arr7, 2, 3) << endl; - // 0 +int main() { return 0; } From 4f9afe5ac6205598824157138ee4739f952b73e5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 21:46:34 -0700 Subject: [PATCH 014/390] 2244 solved. --- .../cpp-2244/CMakeLists.txt | 6 ++++ .../cpp-2244/main.cpp | 35 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/CMakeLists.txt create mode 100644 2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/main.cpp diff --git a/2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/CMakeLists.txt b/2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/main.cpp b/2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/main.cpp new file mode 100644 index 00000000..03daf13c --- /dev/null +++ b/2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumRounds(vector& tasks) { + + map f; + for(int e: tasks) f[e] ++; + + int res = 0; + for(const pair& p: f){ + if(p.second == 1) return -1; + res += (p.second / 3 + !!(p.second % 3)); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 18e82251..3345dc03 100644 --- a/readme.md +++ b/readme.md @@ -2053,6 +2053,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2241 | [Design an ATM Machine](https://leetcode.com/problems/design-an-atm-machine/) | [无] | [C++](2001-2500/2241-Design-an-ATM-Machine/cpp-2241/) | | | | | | | | | | | 2243 | [Calculate Digit Sum of a String](https://leetcode.com/problems/calculate-digit-sum-of-a-string/) | [无] | [C++](2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/) | | | +| 2244 | [Minimum Rounds to Complete All Tasks](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/) | [无] | [C++](2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/) | | | +| 2245 | [Maximum Trailing Zeros in a Cornered Path](https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/) | [无] | [C++](2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/) | | | | | | | | | | ## 力扣中文站比赛 From 3e192e6d1f610f4944f2cf1748736d67efdeb608 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 21:46:51 -0700 Subject: [PATCH 015/390] 2245 solved. --- .../cpp-2245/CMakeLists.txt | 6 ++ .../cpp-2245/main.cpp | 78 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/CMakeLists.txt create mode 100644 2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/main.cpp diff --git a/2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/CMakeLists.txt b/2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/main.cpp b/2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/main.cpp new file mode 100644 index 00000000..3fb2df7a --- /dev/null +++ b/2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/main.cpp @@ -0,0 +1,78 @@ +/// Source : https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { +public: + int maxTrailingZeros(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + vector> two(R, vector(C, 0)), five(R, vector(C, 0)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + int num = grid[i][j]; + int two_num = 0, five_num = 0; + while(num % 2 == 0) two_num ++, num /= 2; + while(num % 5 == 0) five_num ++, num /= 5; + two[i][j] = two_num; + five[i][j] = five_num; + } + + vector> presum_row_two(R, vector(C + 1, 0)), presum_row_five(R, vector(C + 1, 0)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + presum_row_two[i][j + 1] = presum_row_two[i][j] + two[i][j]; + presum_row_five[i][j + 1] = presum_row_five[i][j] + five[i][j]; + } + + vector> presum_col_two(R + 1, vector(C, 0)), presum_col_five(R + 1, vector(C, 0)); + for(int j = 0; j < C; j ++) + for(int i = 0; i < R; i ++){ + presum_col_two[i + 1][j] = presum_col_two[i][j] + two[i][j]; + presum_col_five[i + 1][j] = presum_col_five[i][j] + five[i][j]; + } + + int res = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + int up_two = presum_col_two[i][j], up_five = presum_col_five[i][j]; + int down_two = presum_col_two[R][j] - presum_col_two[i + 1][j], down_five = presum_col_five[R][j] - presum_col_five[i + 1][j]; + int left_two = presum_row_two[i][j], left_five = presum_row_five[i][j]; + int right_two = presum_row_two[i][C] - presum_row_two[i][j + 1], right_five = presum_row_five[i][C] - presum_row_five[i][j + 1]; + + res = max(res, min(up_two + left_two + two[i][j], up_five + left_five + five[i][j])); + res = max(res, min(up_two + right_two + two[i][j], up_five + right_five + five[i][j])); + res = max(res, min(down_two + left_two + two[i][j], down_five + left_five + five[i][j])); + res = max(res, min(down_two + right_two + two[i][j], down_five + right_five + five[i][j])); + } + return res; + } +}; + + +int main() { + + vector> grid1 = {{23,17,15,3,20},{8,1,20,27,11},{9,4,6,2,21},{40,9,1,10,6},{22,7,4,5,3}}; + cout << Solution().maxTrailingZeros(grid1) << '\n'; + // 3 + + vector> grid2 = {{4,3,2},{7,6,1},{8,8,8}}; + cout << Solution().maxTrailingZeros(grid2) << '\n'; + // 0 + + vector> grid3 = {{10}, {6}, {15}}; + cout << Solution().maxTrailingZeros(grid3) << '\n'; + // 2 + + return 0; +} From 27a91b20fe53105340e4ec1c2eff4a8c65fdf058 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 21:58:04 -0700 Subject: [PATCH 016/390] 2246 solved. --- .../cpp-2246/CMakeLists.txt | 6 ++ .../cpp-2246/main.cpp | 86 +++++++++++++++++++ .../cpp-2246/main2.cpp | 71 +++++++++++++++ readme.md | 1 + 4 files changed, 164 insertions(+) create mode 100644 2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/CMakeLists.txt create mode 100644 2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main.cpp create mode 100644 2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main2.cpp diff --git a/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/CMakeLists.txt b/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/CMakeLists.txt new file mode 100644 index 00000000..2552f642 --- /dev/null +++ b/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) diff --git a/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main.cpp b/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main.cpp new file mode 100644 index 00000000..6f63cafd --- /dev/null +++ b/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main.cpp @@ -0,0 +1,86 @@ +/// Source : https://leetcode.com/problems/longest-path-with-different-adjacent-characters/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include + +using namespace std; + + +/// DFS - Two Pass +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int longestPath(vector& parent, string s) { + + int n = parent.size(); + vector> g(n); + for(int u = 1; u < n; u ++){ + int v = parent[u]; + g[u].push_back(v), g[v].push_back(u); + } + + vector dp1(n, 1); + dfs1(g, 0, -1, s, dp1); + +// for(int e: dp1) cout << e << ' '; cout << '\n'; + + int res = 1; + dfs2(g, 0, -1, dp1, s, res); + return res; + } + +private: + void dfs2(const vector>& g, int u, int p, const vector& dp, const string& s, + int& res){ + + vector len; + for(int v: g[u]){ + if(v == p) continue; + dfs2(g, v, u, dp, s, res); + if(s[u] != s[v]) + len.push_back(dp[v]); + } + + if(len.size() == 0) + res = max(res, 1); + else if(len.size() == 1) + res = max(res, len[0] + 1); + else{ + sort(len.begin(), len.end(), greater()); + res = max(res, len[0] + len[1] + 1); + } + } + + void dfs1(const vector>& g, int u, int p, const string& s, + vector& dp){ + + int res = 1; + for(int v: g[u]){ + if(v == p) continue; + + dfs1(g, v, u, s, dp); + if(s[v] != s[u]) + res = max(res, 1 + dp[v]); + } + dp[u] = res; + } +}; + + +int main() { + + vector parent1 = {-1,0,0,1,1,2}; + string s1 = "abacbe"; + cout << Solution().longestPath(parent1, s1) << '\n'; + // 3 + + vector parent2 = {-1,0,0,0}; + string s2 = "aabc"; + cout << Solution().longestPath(parent2, s2) << '\n'; + // 3 + + return 0; +} diff --git a/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main2.cpp b/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main2.cpp new file mode 100644 index 00000000..de1fd5fd --- /dev/null +++ b/2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/main2.cpp @@ -0,0 +1,71 @@ +/// Source : https://leetcode.com/problems/longest-path-with-different-adjacent-characters/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include + +using namespace std; + + +/// DFS - One Pass +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int longestPath(vector& parent, string s) { + + int n = parent.size(); + vector> g(n); + for(int u = 1; u < n; u ++){ + int v = parent[u]; + g[u].push_back(v), g[v].push_back(u); + } + + int res = 0; + dfs(g, 0, -1, s, res); + return res; + } + +private: + int dfs(const vector>& g, int u, int p, const string& s, + int& res){ + + vector len; + int maxl = 1; + for(int v: g[u]){ + if(v == p) continue; + int l = dfs(g, v, u, s, res); + if(s[u] != s[v]){ + len.push_back(l); + maxl = max(maxl, 1 + l); + } + } + + if(len.size() == 0) + res = max(res, 1); + else if(len.size() == 1) + res = max(res, len[0] + 1); + else{ + sort(len.begin(), len.end(), greater()); + res = max(res, len[0] + len[1] + 1); + } + return maxl; + } +}; + + +int main() { + + vector parent1 = {-1,0,0,1,1,2}; + string s1 = "abacbe"; + cout << Solution().longestPath(parent1, s1) << '\n'; + // 3 + + vector parent2 = {-1,0,0,0}; + string s2 = "aabc"; + cout << Solution().longestPath(parent2, s2) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index 3345dc03..2b160730 100644 --- a/readme.md +++ b/readme.md @@ -2055,6 +2055,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2243 | [Calculate Digit Sum of a String](https://leetcode.com/problems/calculate-digit-sum-of-a-string/) | [无] | [C++](2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/) | | | | 2244 | [Minimum Rounds to Complete All Tasks](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/) | [无] | [C++](2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/) | | | | 2245 | [Maximum Trailing Zeros in a Cornered Path](https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/) | [无] | [C++](2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/) | | | +| 2246 | [Longest Path With Different Adjacent Characters](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/) | [无] | [C++](2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/) | | | | | | | | | | ## 力扣中文站比赛 From 96564fc639d4896c0fca0b28d7714177a78e7305 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 23:32:59 -0700 Subject: [PATCH 017/390] 2242 solved. --- .../cpp-2242/CMakeLists.txt | 6 +++ .../cpp-2242/main.cpp | 48 +++++++++++++++++++ readme.md | 4 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/CMakeLists.txt create mode 100644 2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/main.cpp diff --git a/2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/CMakeLists.txt b/2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/CMakeLists.txt new file mode 100644 index 00000000..c3b127c5 --- /dev/null +++ b/2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2242) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2242 main.cpp) diff --git a/2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/main.cpp b/2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/main.cpp new file mode 100644 index 00000000..43d8c74b --- /dev/null +++ b/2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/main.cpp @@ -0,0 +1,48 @@ +#include +#include + +using namespace std; + + +class Solution { +public: + int maximumScore(vector& scores, vector>& edges) { + + int n = -1; + for(const vector& e: edges){ + n = max(n, e[0]); + n = max(n, e[1]); + } + n ++; + + vector>> g(n); + for(const vector& e: edges){ + int u = e[0], v = e[1]; + g[u].emplace_back(scores[v], v); + g[v].emplace_back(scores[u], u); + } + + for(int i = 0; i < n; i ++) + sort(g[i].begin(), g[i].end(), greater>()); + + int res = -1; + for(const vector& e: edges){ + int u = e[0], v = e[1]; + for(int i = 0; i < 3 && i < g[u].size(); i ++) + if(g[u][i].second != v){ + int x = g[u][i].second; + for(int j = 0; j < 3 && j < g[v].size(); j ++) + if(g[v][j].second != u && g[v][j].second != x){ + int y = g[v][j].second; + res = max(res, scores[x] + scores[u] + scores[v] + scores[y]); + } + } + } + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 2b160730..1eef90bd 100644 --- a/readme.md +++ b/readme.md @@ -1376,6 +1376,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1522 | [Diameter of N-Ary Tree](https://leetcode.com/problems/diameter-of-n-ary-tree/) | [无] | [C++](1501-2000/1522-Diameter-of-N-Ary-Tree/cpp-1522/) | | | | | | | | | | +| 1527 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [solution](https://leetcode.com/problems/kth-missing-positive-number/solution/) | [C++](1501-2000/1539-Kth-Missing-Positive-Number/cpp-1539/) | | | | | | | | | | | 1559 | [Detect Cycles in 2D Grid](https://leetcode.com/problems/detect-cycles-in-2d-grid/) | [无] | [C++](1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/) | | | @@ -2051,7 +2053,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2239 | [Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero/) | [无] | [C++](2001-2500/2239-Find-Closest-Number-to-Zero/cpp-2239/) | | | | 2240 | [Number of Ways to Buy Pens and Pencils](https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/) | [无] | [C++](2001-2500/2240-Number-of-Ways-to-Buy-Pens-and-Pencils/cpp-2240/) | | | | 2241 | [Design an ATM Machine](https://leetcode.com/problems/design-an-atm-machine/) | [无] | [C++](2001-2500/2241-Design-an-ATM-Machine/cpp-2241/) | | | -| | | | | | | +| 2242 | [Maximum Score of a Node Sequence](https://leetcode.com/problems/maximum-score-of-a-node-sequence/) | [无] | [C++](2001-2500/2242-Maximum-Score-of-a-Node-Sequence/cpp-2242/) | | | | 2243 | [Calculate Digit Sum of a String](https://leetcode.com/problems/calculate-digit-sum-of-a-string/) | [无] | [C++](2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/) | | | | 2244 | [Minimum Rounds to Complete All Tasks](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/) | [无] | [C++](2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/) | | | | 2245 | [Maximum Trailing Zeros in a Cornered Path](https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/) | [无] | [C++](2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/) | | | From 762ca70792bcad2609cc26a31b11c6d3f250ff8f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 23:44:55 -0700 Subject: [PATCH 018/390] LCP50 added. --- LC/LCP50/cpp-LCP50/CMakeLists.txt | 6 ++++++ LC/LCP50/cpp-LCP50/main.cpp | 31 +++++++++++++++++++++++++++++++ readme.md | 2 ++ 3 files changed, 39 insertions(+) create mode 100644 LC/LCP50/cpp-LCP50/CMakeLists.txt create mode 100644 LC/LCP50/cpp-LCP50/main.cpp diff --git a/LC/LCP50/cpp-LCP50/CMakeLists.txt b/LC/LCP50/cpp-LCP50/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/LC/LCP50/cpp-LCP50/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/LC/LCP50/cpp-LCP50/main.cpp b/LC/LCP50/cpp-LCP50/main.cpp new file mode 100644 index 00000000..e819803b --- /dev/null +++ b/LC/LCP50/cpp-LCP50/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode-cn.com/problems/WHnhjV/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|gem| + |op|) +/// Space Complexity: O(1) +class Solution { +public: + int giveGem(vector& gem, vector>& operations) { + + for(const vector& op: operations){ + int x = op[0], y = op[1]; + int t = gem[x] / 2; + gem[x] -= t, gem[y] += t; + } + return *max_element(gem.begin(), gem.end()) - *min_element(gem.begin(), gem.end()); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 1eef90bd..a8ec92db 100644 --- a/readme.md +++ b/readme.md @@ -2104,6 +2104,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | LCP47 | [入场安检](https://leetcode-cn.com/problems/oPs9Bm/) | [无] | [C++](LC/LCP47/cpp-LCP47/) | | | | LCP48 | [无限棋局](https://leetcode-cn.com/problems/fsa7oZ/) | [无] | [C++](LC/LCP48/cpp-LCP48/) | | | | | | | | | | +| LCP50 | [宝石补给](https://leetcode-cn.com/problems/WHnhjV/) | [无] | [C++](LC/LCP50/cpp-LCP50/) | | | +| | | | | | | ## 其他 From bd504e427f735b3d08e50dc5a86fd56e77215c04 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 16 Apr 2022 23:52:58 -0700 Subject: [PATCH 019/390] LCP 51 solved. --- LC/LCP51/cpp-LCP51/CMakeLists.txt | 6 +++ LC/LCP51/cpp-LCP51/main.cpp | 61 +++++++++++++++++++++++++++++++ readme.md | 1 + 3 files changed, 68 insertions(+) create mode 100644 LC/LCP51/cpp-LCP51/CMakeLists.txt create mode 100644 LC/LCP51/cpp-LCP51/main.cpp diff --git a/LC/LCP51/cpp-LCP51/CMakeLists.txt b/LC/LCP51/cpp-LCP51/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/LC/LCP51/cpp-LCP51/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/LC/LCP51/cpp-LCP51/main.cpp b/LC/LCP51/cpp-LCP51/main.cpp new file mode 100644 index 00000000..e8156683 --- /dev/null +++ b/LC/LCP51/cpp-LCP51/main.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode-cn.com/problems/UEcfPD/ +/// Author : liuyubobobo +/// Time : 2022-04-16 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(2^|cookbooks| * |cookbooks|) +/// Space Complexity: O(1) +class Solution { +public: + int perfectMenu(vector& materials, vector>& cookbooks, + vector>& attribute, int limit) { + + int n = cookbooks.size(); + int best_x = -1; + for(int state = 1; state < (1 << n); state ++){ + + int total_x = 0, total_y = 0; + vector total_m(5, 0); + for(int i = 0; i < n; i ++) + if(state & (1 << i)){ + total_x += attribute[i][0]; + total_y += attribute[i][1]; + add(total_m, cookbooks[i]); + } + if(total_y >= limit && greater_or_equal(materials, total_m)) + best_x = max(best_x, total_x); + } + return best_x; + } + +private: + // v += a + void add(vector& v, const vector& a){ + for(int i = 0; i < v.size(); i ++) v[i] += a[i]; + } + + bool greater_or_equal(const vector& a, const vector& b){ + for(int i = 0; i < a.size(); i ++) + if(a[i] < b[i]) return false; + return true; + } +}; + + +int main() { + + vector materials1 = {3,2,4,1,2}; + vector> cookbooks1 = {{1,1,0,1,2},{2,1,4,0,0},{3,2,4,1,0}}; + vector> attribute1 = {{3,2},{2,4},{7,6}}; + int limit1 = 5; + cout << Solution().perfectMenu(materials1, cookbooks1, attribute1, limit1) << '\n'; + // 7 + + return 0; +} diff --git a/readme.md b/readme.md index a8ec92db..089a1ca3 100644 --- a/readme.md +++ b/readme.md @@ -2105,6 +2105,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | LCP48 | [无限棋局](https://leetcode-cn.com/problems/fsa7oZ/) | [无] | [C++](LC/LCP48/cpp-LCP48/) | | | | | | | | | | | LCP50 | [宝石补给](https://leetcode-cn.com/problems/WHnhjV/) | [无] | [C++](LC/LCP50/cpp-LCP50/) | | | +| LCP51 | [烹饪料理](https://leetcode-cn.com/problems/UEcfPD/) | [无] | [C++](LC/LCP51/cpp-LCP51/) | | | | | | | | | | ## 其他 From e7f05d9af8f58381ae088de05f7a6eb1d89ce440 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 17 Apr 2022 01:03:43 -0700 Subject: [PATCH 020/390] LCP52 added. --- LC/LCP52/cpp-LCP52/CMakeLists.txt | 6 ++ LC/LCP52/cpp-LCP52/main.cpp | 145 ++++++++++++++++++++++++++++++ readme.md | 1 + 3 files changed, 152 insertions(+) create mode 100644 LC/LCP52/cpp-LCP52/CMakeLists.txt create mode 100644 LC/LCP52/cpp-LCP52/main.cpp diff --git a/LC/LCP52/cpp-LCP52/CMakeLists.txt b/LC/LCP52/cpp-LCP52/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/LC/LCP52/cpp-LCP52/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/LC/LCP52/cpp-LCP52/main.cpp b/LC/LCP52/cpp-LCP52/main.cpp new file mode 100644 index 00000000..39162dce --- /dev/null +++ b/LC/LCP52/cpp-LCP52/main.cpp @@ -0,0 +1,145 @@ +/// Source : https://leetcode-cn.com/problems/QO5KpG/ +/// Author : liuyubobobo +/// Time : 2022-04-17 + +#include +#include +#include + +using namespace std; + + +/// Using Segment Tree +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +template +class SegmentTree{ + +private: + int n; + vector tree, lazy; + T (*combine)(T a, T b); + +public: + SegmentTree(int n, T (*combine)(T a, T b)): n(n), tree(4 * n, 0), lazy(4 * n, -1){ + this->combine = combine; + } + + void set(int uL, int uR, T v){ + if(uL > uR) return; + set(0, 0, n - 1, uL, uR, v); + } + + T query(int qL, int qR){ + if(qL > qR) return 0; + return query(0, 0, n - 1, qL, qR); + } + +private: + void set(int treeID, int treeL, int treeR, int uL, int uR, T v){ + + if(uL > treeR || uR < treeL) return; + + if(uL <= treeL && treeR <= uR){ + tree[treeID] = (treeR - treeL + 1) * v; + lazy[treeID] = v; + return; + } + + if(lazy[treeID] != -1) + push(treeID, treeL, treeR); + + int mid = (treeL + treeR) / 2; + set(2 * treeID + 1, treeL, mid, uL, uR, v); + set(2 * treeID + 2, mid + 1, treeR, uL, uR, v); + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + T query(int treeID, int treeL, int treeR, int qL, int qR){ + + if(qL <= treeL && treeR <= qR) + return tree[treeID]; + + if(lazy[treeID] != -1) + push(treeID, treeL, treeR); + + int mid = (treeL + treeR) / 2; + if(qR <= mid) return query(2 * treeID + 1, treeL, mid, qL, qR); + if(qL >= mid + 1) return query(2 * treeID + 2, mid + 1, treeR, qL, qR); + + T resl = query(2 * treeID + 1, treeL, mid, qL, qR); + T resr = query(2 * treeID + 2, mid + 1, treeR, qL, qR); + T res = combine(resl, resr); + return res; + } + +private: + void push(int treeID, int treeL, int treeR){ + + if(treeL == treeR) return; + + T v = lazy[treeID]; + + int mid = (treeL + treeR) / 2; + int tn = treeR - treeL + 1, tnl = mid - treeL + 1, tnr = tn - tnl; + + tree[treeID * 2 + 1] = v * tnl; + tree[treeID * 2 + 2] = v * tnr; + + lazy[treeID * 2 + 1] = v; + lazy[treeID * 2 + 2] = v; + + lazy[treeID] = -1; + } +}; + +class Solution { + +public: + int getNumber(TreeNode* root, vector>& ops) { + + vector index2value; + init(root, index2value); + + int n = index2value.size(); +// for(int e: index2value) cout << e << ' '; cout << '\n'; + + SegmentTree seg_tree(n, [](int a, int b){return a + b;}); + + for(const vector& op: ops){ + int color = op[0], l_value = op[0], r_value = op[1]; + int l = lower_bound(index2value.begin(), index2value.end(), l_value) - index2value.begin(); + int r = (upper_bound(index2value.begin(), index2value.end(), r_value) - index2value.begin()) - 1; + +// cout << l << ' ' << r << ' ' << color << '\n'; + seg_tree.set(l, r, color); + } + return seg_tree.query(0, n - 1); + } + +private: + void init(TreeNode* node, vector& index2value){ + + if(!node) return; + + init(node->left, index2value); + index2value.push_back(node->val); + init(node->right, index2value); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 089a1ca3..01d4f8d3 100644 --- a/readme.md +++ b/readme.md @@ -2106,6 +2106,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | LCP50 | [宝石补给](https://leetcode-cn.com/problems/WHnhjV/) | [无] | [C++](LC/LCP50/cpp-LCP50/) | | | | LCP51 | [烹饪料理](https://leetcode-cn.com/problems/UEcfPD/) | [无] | [C++](LC/LCP51/cpp-LCP51/) | | | +| LCP52 | [二叉搜索树染色](https://leetcode-cn.com/problems/QO5KpG/) | [无] [缺:非线段树做法] | [C++](LC/LCP52/cpp-LCP52/) | | | | | | | | | | ## 其他 From 5d28eb60f03a652d701e76ae7ae6c47a9b4c95e6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 20 Apr 2022 21:15:14 -0700 Subject: [PATCH 021/390] 0824 solved. --- .../0824-Goat-Latin/cpp-0824/CMakeLists.txt | 6 +++ 0501-1000/0824-Goat-Latin/cpp-0824/main.cpp | 48 +++++++++++++++++++ readme.md | 8 ++-- 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 0501-1000/0824-Goat-Latin/cpp-0824/CMakeLists.txt create mode 100644 0501-1000/0824-Goat-Latin/cpp-0824/main.cpp diff --git a/0501-1000/0824-Goat-Latin/cpp-0824/CMakeLists.txt b/0501-1000/0824-Goat-Latin/cpp-0824/CMakeLists.txt new file mode 100644 index 00000000..2afd100a --- /dev/null +++ b/0501-1000/0824-Goat-Latin/cpp-0824/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0824) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0824 main.cpp) diff --git a/0501-1000/0824-Goat-Latin/cpp-0824/main.cpp b/0501-1000/0824-Goat-Latin/cpp-0824/main.cpp new file mode 100644 index 00000000..5094a988 --- /dev/null +++ b/0501-1000/0824-Goat-Latin/cpp-0824/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/goat-latin/ +/// Author : liuyubobobo +/// Time : 2022-04-20 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Compelxity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string toGoatLatin(string sentence) { + + vector v; + for(int start = 0, i = 1; i <= sentence.size(); i ++) + if(i == sentence.size() || sentence[i] == ' '){ + v.push_back(sentence.substr(start, i - start)); + start = i + 1; + i = start; + } + + set vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; + + string res = ""; + for(int i = 0; i < v.size(); i ++){ + if(vowels.count(v[i][0])) + res += v[i] + "ma"; + else + res += v[i].substr(1) + v[i][0] + "ma"; + + res += string(i + 1, 'a'); + res += ' '; + } + res.pop_back(); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 01d4f8d3..53d6de77 100644 --- a/readme.md +++ b/readme.md @@ -234,7 +234,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 191 | [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/description/) | [solution](https://leetcode.com/problems/number-of-1-bits/solution/) | [C++](0001-0500/0191-Number-of-1-Bits/cpp-0191/) | | | | | | | | | | | 196 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| | | | | | | +| 197 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 198 | [House Robber](https://leetcode.com/problems/house-robber/description/) | [solution](https://leetcode.com/problems/house-robber/solution/) | [C++](0001-0500/0198-House-Robber/cpp-0198/) | [Java](0001-0500/0198-House-Robber/java-0198/src/) | | | 199 | [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) | [solution](https://leetcode.com/problems/binary-tree-right-side-view/solution/) | [C++](0001-0500/199-Binary-Tree-Right-Side-View/cpp-0199/) | | | | 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [solution](https://leetcode.com/problems/number-of-islands/solution/) | [C++](0001-0500/0200-Number-of-Islands/cpp-0200/) | [Java](0001-0500/0200-Number-of-Islands/java-0200/src/) | | @@ -609,7 +609,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | [solution](https://leetcode.com/problems/can-place-flowers/solution/) | [C++](0501-1000/0605-Can-Place-Flowers/cpp-0605/) | | | | 606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/) | [solution](https://leetcode.com/problems/construct-string-from-binary-tree/solution/) | [C++](0501-1000/0606-Construct-String-from-Binary-Tree/cpp-0606/) | | | -| | | | | | | +| 607 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 608 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 609 | [Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/) | [solution](https://leetcode.com/problems/find-duplicate-file-in-system/solution/) | [C++](0501-1000/0609-Find-Duplicate-File-in-System/cpp-0609/) | | | | | | | | | | | 611 | [Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/) | [solution](https://leetcode.com/problems/valid-triangle-number/solution/) | [C++](0501-1000/0611-Valid-Triangle-Number/cpp-0611/) | | | @@ -790,7 +791,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/description/) | [solution](https://leetcode.com/problems/most-common-word/solution/) | [C++](0501-1000/0819-Most-Common-Word/cpp-0819/) | | | | | | | | | | | 823 | [Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors/) | [solution](https://leetcode.com/problems/binary-trees-with-factors/solution/) | [C++](0501-1000/0823-Binary-Trees-With-Factors/cpp-0823/) | | | -| | | | | | | +| 824 | [Goat Latin](https://leetcode.com/problems/goat-latin/) | [无] | [C++](0501-1000/0824-Goat-Latin/cpp-0824/) | | | | 825 | [Friends Of Appropriate Ages](https://leetcode.com/problems/friends-of-appropriate-ages/) | [无] | [C++](0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/) | | | | | | | | | | | 827 | [Making A Large Island](https://leetcode.com/problems/making-a-large-island/) | [无] | [C++](0501-1000/0827-Making-A-Large-Island/cpp-0827/) | | | @@ -1079,6 +1080,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1145 | [Binary Tree Coloring Game](https://leetcode.com/problems/binary-tree-coloring-game/) | [无] | [C++](1001-1500/1145-Binary-Tree-Coloring-Game/cpp-1145/) | | | | 1146 | [Snapshot Array](https://leetcode.com/problems/snapshot-array/) | [无] | [C++](1001-1500/1146-Snapshot-Array/cpp-1146/) | | | | 1147 | [Longest Chunked Palindrome Decomposition](https://leetcode.com/problems/longest-chunked-palindrome-decomposition/) | [无] | [C++](1001-1500/1147-Longest-Chunked-Palindrome-Decomposition/cpp-1147/) | | | +| 1148 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1150 | [Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/is-a-a-majority-element/) | [无] | [C++](1001-1500/1150-Check-If-a-Number-Is-Majority-Element-in-a-Sorted-Array/cpp-1150/) | | | | 1151 | [Minimum Swaps to Group All 1's Together](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together/) | [无] | [C++](1001-1500/1151-Minimum-Swaps-to-Group-All-1s-Together/cpp-1151/) | | | From ce92985b265d29dcecdaf7ba84b23700ad52e034 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 21 Apr 2022 17:34:57 -0700 Subject: [PATCH 022/390] 1166 updated. --- .../1166-Design-File-System/cpp-1166/main.cpp | 15 +++++++++------ readme.md | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/1001-1500/1166-Design-File-System/cpp-1166/main.cpp b/1001-1500/1166-Design-File-System/cpp-1166/main.cpp index 8ab7327c..a96380e6 100644 --- a/1001-1500/1166-Design-File-System/cpp-1166/main.cpp +++ b/1001-1500/1166-Design-File-System/cpp-1166/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/design-file-system/ /// Author : liuyubobobo /// Time : 2019-08-24 +/// Updated: 2022-04-21 #include #include @@ -11,8 +12,8 @@ using namespace std; /// Using HashMap -/// Time Complexity: create: O(|path|) -/// get: O(1) +/// Time Complexity: createPath: O(|path|) +/// get: O(|path|) /// Space Complexity: O(|query| * |path|) class FileSystem { @@ -24,7 +25,9 @@ class FileSystem { map[""] = -1; } - bool create(string path, int value) { + bool createPath(string path, int value) { + + if(map.count(path)) return false; int last = path.rfind('/'); if(!map.count(path.substr(0, last))) return false; @@ -42,10 +45,10 @@ class FileSystem { int main() { FileSystem fs; - cout << fs.create("/leet", 1) << endl; - cout << fs.create("/leet/code", 2) << endl; + cout << fs.createPath("/leet", 1) << endl; + cout << fs.createPath("/leet/code", 2) << endl; cout << fs.get("/leet/code") << endl; - cout << fs.create("/c/d", 1) << endl; // false + cout << fs.createPath("/c/d", 1) << endl; // false cout << fs.get("/c") << endl; // -1 return 0; diff --git a/readme.md b/readme.md index 53d6de77..b75ec539 100644 --- a/readme.md +++ b/readme.md @@ -533,6 +533,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [无] | [C++](0501-1000/0509-Fibonacci-Number/cpp-0509/) | | | | | | | | | | +| 511 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 516 | [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [无] | [C++](0501-1000/0516-Longest-Palindromic-Subsequence/cpp-0516/) | | | | 517 | [Super Washing Machines](https://leetcode.com/problems/super-washing-machines/) | [solution](https://leetcode.com/problems/super-washing-machines/solution/) | [C++](0501-1000/0517-Super-Washing-Machines/cpp-0517/) | | | | 518 | [Coin Change 2](https://leetcode.com/problems/coin-change-2/description/) | [无] | [C++](0501-1000/0518-Coin-Change-2/cpp-0518/) | | | @@ -595,6 +597,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/description/) | [solution](https://leetcode.com/problems/delete-operation-for-two-strings/solution/) | [C++](0501-1000/0583-Delete-Operation-for-Two-Strings/cpp-0583/) | | | | 584 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 586 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 587 | [Erect the Fence](https://leetcode.com/problems/erect-the-fence/) | [solution](https://leetcode.com/problems/erect-the-fence/solution/) [题解](https://leetcode-cn.com/problems/erect-the-fence/solution/an-zhuang-zha-lan-by-leetcode/)
[缺:凸包解法整理] | [C++](0501-1000/0587-Erect-the-Fence/cpp-0587/) | | | | | | | | | | | 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-preorder-traversal/solution/) | [C++](0501-1000/0589-N-ary-Tree-Preorder-Traversal/cpp-0589/) | | | @@ -1074,6 +1077,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1138 | [Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/) | [无] | [C++](1001-1500/1138-Alphabet-Board-Path/cpp-1138/) | | | | 1139 | [Largest 1-Bordered Square](https://leetcode.com/problems/largest-1-bordered-square/) | [无] | [C++](1001-1500/1139-Largest-1-Bordered-Square/cpp-1139/) | | | | 1140 | [Stone Game II](https://leetcode.com/problems/stone-game-ii/) | [无] | [C++](1001-1500/1140-Stone-Game-II/cpp-1140/) | | | +| 1141 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [无] | [C++](1001-1500/1143-Longest-Common-Subsequence/cpp-1143/) | | | | 1144 | [Decrease Elements To Make Array Zigzag](https://leetcode.com/problems/decrease-elements-to-make-array-zigzag/) | [无] | [C++](1001-1500/1144-Decrease-Elements-To-Make-Array-Zigzag/cpp-1144/) | | | From 26f4bc85e88053a408152f00119da4a82faf8b87 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 21 Apr 2022 18:07:21 -0700 Subject: [PATCH 023/390] 0396 solved. --- .../cpp-0396/CMakeLists.txt | 6 +++ .../0396-Rotate-Function/cpp-0396/main.cpp | 44 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0396-Rotate-Function/cpp-0396/CMakeLists.txt create mode 100644 0001-0500/0396-Rotate-Function/cpp-0396/main.cpp diff --git a/0001-0500/0396-Rotate-Function/cpp-0396/CMakeLists.txt b/0001-0500/0396-Rotate-Function/cpp-0396/CMakeLists.txt new file mode 100644 index 00000000..5c6b2f59 --- /dev/null +++ b/0001-0500/0396-Rotate-Function/cpp-0396/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0396) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0396 main.cpp) diff --git a/0001-0500/0396-Rotate-Function/cpp-0396/main.cpp b/0001-0500/0396-Rotate-Function/cpp-0396/main.cpp new file mode 100644 index 00000000..bd225b42 --- /dev/null +++ b/0001-0500/0396-Rotate-Function/cpp-0396/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/rotate-function/ +/// Author : liuyubobobo +/// Time : 2022-04-21 + +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int maxRotateFunction(vector& nums) { + + int n = nums.size(); + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + nums[i]; + + int cur = 0; + for(int i = 0; i < n; i ++) cur += i * nums[i]; + + int res = cur; + for(int i = 1; i < n; i ++){ + cur -= presum[n] - presum[i]; + cur -= presum[i - 1]; + cur += (n - 1) * nums[i - 1]; + res = max(res, cur); + } + return res; + } +}; + + +int main() { + + vector nums1 = {4, 3, 2, 6}; + cout << Solution().maxRotateFunction(nums1) << '\n'; + // 26 + + return 0; +} diff --git a/readme.md b/readme.md index b75ec539..855fff9e 100644 --- a/readme.md +++ b/readme.md @@ -427,7 +427,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 393 | [UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) | [solution](https://leetcode.com/problems/utf-8-validation/solution/) | [C++](0001-0500/0393-UTF-8-Validation/cpp-0393/) | | | | 394 | [Decode String](https://leetcode.com/problems/decode-string/description/) | [无] | [C++](0001-0500/0394-Decode-String/cpp-0394/) | | | | 395 | [Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [solution](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/solution/)
[缺:滑动窗口] | [C++](0001-0500/0395-Longest-Substring-with-At-Least-K-Repeating-Characters/cpp-0395/) | | | -| | | | | | | +| 396 | [Rotate Function](https://leetcode.com/problems/rotate-function/) | [无] | [C++](0001-0500/0396-Rotate-Function/cpp-0396/) | | | | 397 | [Integer Replacement](https://leetcode.com/problems/integer-replacement/) | [无] | [C++](0001-0500/0397-Integer-Replacement/cpp-0397/) | | | | 398 | [Random Pick Index](https://leetcode.com/problems/random-pick-index/description/) | [无] | [C++](0001-0500/0398-Random-Pick-Index/cpp-0398/) | | | | 399 | [Evaluate Division](https://leetcode.com/problems/evaluate-division/) | [solution](https://leetcode.com/problems/evaluate-division/solution/)
[缺:UF] | [C++](0001-0500/0399-Evaluate-Division/cpp-0399/) | | | From c1d5dd5ff109ce0e750c61215ddb69bdfd56a4cf Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 22 Apr 2022 12:58:42 -0700 Subject: [PATCH 024/390] 0587 codes updated. --- .../cpp-0587/CMakeLists.txt | 2 +- .../0587-Erect-the-Fence/cpp-0587/main.cpp | 98 +++++++++---------- .../0587-Erect-the-Fence/cpp-0587/main2.cpp | 56 ++++++----- .../0587-Erect-the-Fence/cpp-0587/main3.cpp | 65 ++++++------ .../0587-Erect-the-Fence/cpp-0587/main4.cpp | 79 --------------- 5 files changed, 115 insertions(+), 185 deletions(-) delete mode 100644 0501-1000/0587-Erect-the-Fence/cpp-0587/main4.cpp diff --git a/0501-1000/0587-Erect-the-Fence/cpp-0587/CMakeLists.txt b/0501-1000/0587-Erect-the-Fence/cpp-0587/CMakeLists.txt index ce4f8117..edf32f40 100644 --- a/0501-1000/0587-Erect-the-Fence/cpp-0587/CMakeLists.txt +++ b/0501-1000/0587-Erect-the-Fence/cpp-0587/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0587) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_0587 main4.cpp) \ No newline at end of file +add_executable(cpp_0587 main3.cpp) \ No newline at end of file diff --git a/0501-1000/0587-Erect-the-Fence/cpp-0587/main.cpp b/0501-1000/0587-Erect-the-Fence/cpp-0587/main.cpp index c3894dae..7ab9a7f5 100644 --- a/0501-1000/0587-Erect-the-Fence/cpp-0587/main.cpp +++ b/0501-1000/0587-Erect-the-Fence/cpp-0587/main.cpp @@ -8,9 +8,28 @@ using namespace std; -/// Jarvis Algorithm -/// Time Complexity: O(n*H) +/// Graham Scan - One Pass +/// Time Complexity: O(nlogn + n) /// Space Complexity: O(1) + +vector start; + +int cross_value(const vector& p1, const vector& p2, const vector& p3){ + int a = p2[0] - p1[0], b = p2[1] - p1[1]; + int c = p3[0] - p2[0], d = p3[1] - p2[1]; + return a * d - b * c; +} + +int distance_square(const vector& p1, const vector& p2){ + return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); +} + +bool cmp(const vector& p1, const vector& p2){ + int det = cross_value(start, p1, p2); + if(det == 0) return distance_square(start, p1) < distance_square(start, p2); + return det > 0; +} + class Solution { public: vector> outerTrees(vector>& points) { @@ -22,53 +41,28 @@ class Solution { if(points[i][0] < points[leftmost][0] || (points[i][0] == points[leftmost][0] && points[i][1] < points[leftmost][1])) leftmost = i; - - int p = leftmost; - vector res = {p}; - while(true){ - int next = (res.back() + 1) % points.size(); - for(int i = 0; i < points.size(); i ++) - if(i != res.back() && i != next) - if(cross_value(points[res.back()], points[next], points[i]) < 0) - next = i; - - for(int i = 0; i < points.size(); i ++) - if(i != res.back() && i != next && in_between(points[p], points[i], points[next])) - res.push_back(i); - - if(next == leftmost) break; - res.push_back(next); - p = next; + swap(points[0], points[leftmost]); + start = points[0]; + + sort(points.begin() + 1, points.end(), cmp); + +// for(const vector& p: points) +// cout << "(" << p[0] << "," << p[1] << ") "; cout << endl; + + for(int i = points.size() - 2; i >= 0; i --) + if(cross_value(points[0], points.back(), points[i]) != 0){ + for(int l = i + 1, r = points.size() - 1; l < r; l ++, r --) + swap(points[l], points[r]); + break; + } + + vector> res = {points[0], points[1]}; + for(int i = 2; i < points.size(); i ++){ + while(res.size() >= 2 && cross_value(res[res.size() - 2], res.back(), points[i]) < 0) + res.pop_back(); + res.push_back(points[i]); } - - vector> ret; - for(int i: res) ret.push_back(points[i]); - return ret; - } - -private: - // see if x in between of p1 and p2 - bool in_between(const vector& p1, const vector& x, const vector& p2){ - - if(x[0] == p1[0] && x[1] == p1[1]) return false; - if(x[0] == p2[0] && x[1] == p2[1]) return false; - if(cross_value(p1, x, p2) != 0) return false; - - int a = p1[0], b = p2[0]; - if(a > b) swap(a, b); - if(x[0] < a || x[0] > b) return false; - - a = p1[1], b = p2[1]; - if(a > b) swap(a, b); - if(x[1] < a || x[1] > b) return false; - - return true; - } - - int cross_value(const vector& p1, const vector& p2, const vector& p3){ - int a = p2[0] - p1[0], b = p2[1] - p1[1]; - int c = p3[0] - p2[0], d = p3[1] - p2[1]; - return a * d - b * c; + return res; } }; @@ -77,14 +71,10 @@ void print_vec(const vector>& vec){ for(const vector& p: vec) cout << "(" << p[0] << "," << p[1] << ")"; cout << endl; } - int main() { -// vector> points1 = {{1,1},{2,2},{2,0},{2,4},{3,3},{4,2}}; -// print_vec(Solution().outerTrees(points1)); - - vector> points2 = {{0,0},{0,1},{0,2},{1,2},{2,2},{3,2},{3,1},{3,0},{2,0}}; - print_vec(Solution().outerTrees(points2)); + vector> points = {{1,1},{2,2},{2,0},{2,4},{3,3},{4,2}}; + print_vec(Solution().outerTrees(points)); return 0; } diff --git a/0501-1000/0587-Erect-the-Fence/cpp-0587/main2.cpp b/0501-1000/0587-Erect-the-Fence/cpp-0587/main2.cpp index 9495c990..780a5240 100644 --- a/0501-1000/0587-Erect-the-Fence/cpp-0587/main2.cpp +++ b/0501-1000/0587-Erect-the-Fence/cpp-0587/main2.cpp @@ -8,8 +8,9 @@ using namespace std; -/// Graham Scan - Two Pass -/// Time Complexity: O(nlogn + 2n) +/// Graham Scan - One Pass +/// Using (0, 0) as the base to avoid free functions +/// Time Complexity: O(nlogn + n) /// Space Complexity: O(1) class Solution { public: @@ -17,38 +18,50 @@ class Solution { if(points.size() <= 3) return points; - sort(points.begin(), points.end(), + int leftmost = 0; + for(int i = 1; i < points.size(); i ++) + if(points[i][0] < points[leftmost][0] || + (points[i][0] == points[leftmost][0] && points[i][1] < points[leftmost][1])) + leftmost = i; + swap(points[0], points[leftmost]); + + vector start = points[0]; + for(vector& p: points) p[0] -= start[0], p[1] -= start[1]; + + sort(points.begin() + 1, points.end(), [](const vector& p1, const vector& p2){ - return p1[0] == p2[0] ? p1[1] < p2[1] : p1[0] < p2[0]; + + int det = p1[0] * (p2[1] - p1[1]) - (p2[0] - p1[0]) * p1[1]; + if(det == 0) return p1[0] * p1[0] + p1[1] * p1[1] < p2[0] * p2[0] + p2[1] * p2[1]; + return det > 0; }); - vector> res; - for(vector>::iterator iter = points.begin(); iter != points.end(); iter ++){ - while(res.size() >= 2){ - int det = cross_value(res[res.size() - 2], res.back(), *iter); - if(det < 0) - res.pop_back(); - else break; +// for(const vector& p: points) +// cout << "(" << p[0] + start[0] << "," << p[1] + start[1] << ") "; cout << endl; + + for(int i = points.size() - 2; i >= 0; i --) + if(cross_value(points[0], points.back(), points[i]) != 0){ + for(int l = i + 1, r = points.size() - 1; l < r; l ++, r --) + swap(points[l], points[r]); + break; } - res.push_back(*iter); - } - vector>::reverse_iterator riter = points.rbegin(); - for(riter ++; riter != points.rend(); riter ++){ - while(res.size() >= 2 && cross_value(res[res.size() - 2], res.back(), *riter) < 0) + vector> res = {points[0], points[1]}; + for(int i = 2; i < points.size(); i ++){ + while(res.size() >= 2 && cross_value(res[res.size() - 2], res.back(), points[i]) < 0) res.pop_back(); - res.push_back(*riter); + res.push_back(points[i]); } + for(vector& p: res) p[0] += start[0], p[1] += start[1]; - res.pop_back(); return res; } private: int cross_value(const vector& p1, const vector& p2, const vector& p3){ - int a = p2[0] - p1[0], b = p2[1] - p1[1]; - int c = p3[0] - p2[0], d = p3[1] - p2[1]; - return a * d - b * c; +// int a = p2[0] - p1[0], b = p2[1] - p1[1]; +// int c = p3[0] - p2[0], d = p3[1] - p2[1]; + return (p2[0] - p1[0]) * (p3[1] - p2[1]) - (p2[1] - p1[1]) * (p3[0] - p2[0]); } }; @@ -57,7 +70,6 @@ void print_vec(const vector>& vec){ for(const vector& p: vec) cout << "(" << p[0] << "," << p[1] << ")"; cout << endl; } - int main() { vector> points = {{1,1},{2,2},{2,0},{2,4},{3,3},{4,2}}; diff --git a/0501-1000/0587-Erect-the-Fence/cpp-0587/main3.cpp b/0501-1000/0587-Erect-the-Fence/cpp-0587/main3.cpp index 7ab9a7f5..a81d0f53 100644 --- a/0501-1000/0587-Erect-the-Fence/cpp-0587/main3.cpp +++ b/0501-1000/0587-Erect-the-Fence/cpp-0587/main3.cpp @@ -1,6 +1,6 @@ /// Source : https://leetcode.com/problems/erect-the-fence/ /// Author : liuyubobobo -/// Time : 2020-05-01 +/// Time : 2022-04-22 #include #include @@ -8,46 +8,34 @@ using namespace std; -/// Graham Scan - One Pass +/// Graham Scan +/// Using my template /// Time Complexity: O(nlogn + n) /// Space Complexity: O(1) - -vector start; - -int cross_value(const vector& p1, const vector& p2, const vector& p3){ - int a = p2[0] - p1[0], b = p2[1] - p1[1]; - int c = p3[0] - p2[0], d = p3[1] - p2[1]; - return a * d - b * c; -} - -int distance_square(const vector& p1, const vector& p2){ - return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); -} - -bool cmp(const vector& p1, const vector& p2){ - int det = cross_value(start, p1, p2); - if(det == 0) return distance_square(start, p1) < distance_square(start, p2); - return det > 0; -} - -class Solution { +template +class ConvexHull { public: - vector> outerTrees(vector>& points) { + vector> solve(vector>& points) { if(points.size() <= 3) return points; int leftmost = 0; - for(int i = 1; i < points.size(); i ++) + for(int i = 1; i < points.size(); i ++){ if(points[i][0] < points[leftmost][0] || (points[i][0] == points[leftmost][0] && points[i][1] < points[leftmost][1])) leftmost = i; + } + swap(points[0], points[leftmost]); - start = points[0]; - sort(points.begin() + 1, points.end(), cmp); + vector start = points[0]; + for(vector& p: points) p[0] -= start[0], p[1] -= start[1]; -// for(const vector& p: points) -// cout << "(" << p[0] << "," << p[1] << ") "; cout << endl; + sort(points.begin() + 1, points.end(), [](const vector& p1, const vector& p2){ + T det = p1[0] * (p2[1] - p1[1]) - (p2[0] - p1[0]) * p1[1]; + if(det == 0) return p1[0] * p1[0] + p1[1] * p1[1] < p2[0] * p2[0] + p2[1] * p2[1]; + return det > 0; + }); for(int i = points.size() - 2; i >= 0; i --) if(cross_value(points[0], points.back(), points[i]) != 0){ @@ -56,14 +44,33 @@ class Solution { break; } - vector> res = {points[0], points[1]}; + vector> res = {points[0], points[1]}; for(int i = 2; i < points.size(); i ++){ while(res.size() >= 2 && cross_value(res[res.size() - 2], res.back(), points[i]) < 0) res.pop_back(); res.push_back(points[i]); } + + for(vector& p: res) p[0] += start[0], p[1] += start[1]; + return res; } + +private: + T cross_value(const vector& p1, const vector& p2, const vector& p3){ + // int a = p2[0] - p1[0], b = p2[1] - p1[1]; + // int c = p3[0] - p2[0], d = p3[1] - p2[1]; + return (p2[0] - p1[0]) * (p3[1] - p2[1]) - (p2[1] - p1[1]) * (p3[0] - p2[0]); + } +}; + +class Solution { +public: + vector> outerTrees(vector>& points) { + + ConvexHull solver; + return solver.solve(points); + } }; diff --git a/0501-1000/0587-Erect-the-Fence/cpp-0587/main4.cpp b/0501-1000/0587-Erect-the-Fence/cpp-0587/main4.cpp deleted file mode 100644 index 4be049dc..00000000 --- a/0501-1000/0587-Erect-the-Fence/cpp-0587/main4.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/// Source : https://leetcode.com/problems/erect-the-fence/ -/// Author : liuyubobobo -/// Time : 2020-05-01 - -#include -#include - -using namespace std; - - -/// Graham Scan - One Pass -/// Using (0, 0) as the base to avoid free functions -/// Time Complexity: O(nlogn + n) -/// Space Complexity: O(1) -class Solution { -public: - vector> outerTrees(vector>& points) { - - if(points.size() <= 3) return points; - - int leftmost = 0; - for(int i = 1; i < points.size(); i ++) - if(points[i][0] < points[leftmost][0] || - (points[i][0] == points[leftmost][0] && points[i][1] < points[leftmost][1])) - leftmost = i; - swap(points[0], points[leftmost]); - - vector start = points[0]; - for(vector& p: points) p[0] -= start[0], p[1] -= start[1]; - - sort(points.begin() + 1, points.end(), - [](const vector& p1, const vector& p2){ - - int det = p1[0] * (p2[1] - p1[1]) - (p2[0] - p1[0]) * p1[1]; - if(det == 0) return p1[0] * p1[0] + p1[1] * p1[1] < p2[0] * p2[0] + p2[1] * p2[1]; - return det > 0; - }); - -// for(const vector& p: points) -// cout << "(" << p[0] + start[0] << "," << p[1] + start[1] << ") "; cout << endl; - - for(int i = points.size() - 2; i >= 0; i --) - if(cross_value(points[0], points.back(), points[i]) != 0){ - for(int l = i + 1, r = points.size() - 1; l < r; l ++, r --) - swap(points[l], points[r]); - break; - } - - vector> res = {points[0], points[1]}; - for(int i = 2; i < points.size(); i ++){ - while(res.size() >= 2 && cross_value(res[res.size() - 2], res.back(), points[i]) < 0) - res.pop_back(); - res.push_back(points[i]); - } - for(vector& p: res) p[0] += start[0], p[1] += start[1]; - - return res; - } - -private: - int cross_value(const vector& p1, const vector& p2, const vector& p3){ -// int a = p2[0] - p1[0], b = p2[1] - p1[1]; -// int c = p3[0] - p2[0], d = p3[1] - p2[1]; - return (p2[0] - p1[0]) * (p3[1] - p2[1]) - (p2[1] - p1[1]) * (p3[0] - p2[0]); - } -}; - - -void print_vec(const vector>& vec){ - for(const vector& p: vec) cout << "(" << p[0] << "," << p[1] << ")"; cout << endl; -} - -int main() { - - vector> points = {{1,1},{2,2},{2,0},{2,4},{3,3},{4,2}}; - print_vec(Solution().outerTrees(points)); - - return 0; -} From 4c6aaa069f372890c9f040e9c40d2e707675ad44 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 22 Apr 2022 15:52:07 -0700 Subject: [PATCH 025/390] 2247 solved. --- .../cpp-2247/CMakeLists.txt | 6 ++ .../cpp-2247/main.cpp | 66 +++++++++++++++++++ readme.md | 1 + 3 files changed, 73 insertions(+) create mode 100644 2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/CMakeLists.txt create mode 100644 2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/main.cpp diff --git a/2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/CMakeLists.txt b/2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/CMakeLists.txt new file mode 100644 index 00000000..79f82a6b --- /dev/null +++ b/2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2247) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2247 main.cpp) diff --git a/2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/main.cpp b/2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/main.cpp new file mode 100644 index 00000000..5451c683 --- /dev/null +++ b/2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/maximum-cost-of-trip-with-k-highways/ +/// Author : liuyubobobo +/// Time : 2022-04-22 + +#include +#include + +using namespace std; + + +/// State Compression Memoization +/// Time Complexity: O(2^n * n^2) +/// Space Complexity: O(2^n * n) +class Solution { +public: + int maximumCost(int n, vector>& highways, int k) { + + vector>> g(n); + for(const vector& e: highways){ + int u = e[0], v = e[1], w = e[2]; + g[u].push_back({v, w}); + g[v].push_back({u, w}); + } + + vector> dp(1 << n, vector(n, INT_MIN)); + int res = INT_MIN / 2; + for(int s = 0; s < n; s ++) + res = max(res, dfs(g, 1 << s, s, k, dp)); + return res < 0 ? -1 : res; + } + +private: + int dfs(const vector>>& g, int state, int u, int k, + vector>& dp){ + + int v_num = __builtin_popcount(state); + if(v_num == k + 1) return 0; + if(dp[state][u] != INT_MIN) return dp[state][u]; + + int res = INT_MIN / 2; + for(const pair& p: g[u]){ + int v = p.first, w = p.second; + if((state & (1 << v)) == 0) + res = max(res, w + dfs(g, state | (1 << v), v, k, dp)); + } + return dp[state][u] = res; + } +}; + + +int main() { + + vector> highways1 = {{0,1,4},{2,1,3},{1,4,11},{3,2,3},{3,4,2}}; + cout << Solution().maximumCost(5, highways1, 3) << '\n'; + // 17 + + vector> highways2 = {{0,1,3},{2,3,2}}; + cout << Solution().maximumCost(4, highways2, 2) << '\n'; + // -1 + + vector> highways3 = {{0,1,0}}; + cout << Solution().maximumCost(2, highways3, 1) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index 855fff9e..71672eac 100644 --- a/readme.md +++ b/readme.md @@ -2064,6 +2064,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2244 | [Minimum Rounds to Complete All Tasks](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/) | [无] | [C++](2001-2500/2244-Minimum-Rounds-to-Complete-All-Tasks/cpp-2244/) | | | | 2245 | [Maximum Trailing Zeros in a Cornered Path](https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/) | [无] | [C++](2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/) | | | | 2246 | [Longest Path With Different Adjacent Characters](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/) | [无] | [C++](2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/) | | | +| 2247 | [Maximum Cost of Trip With K Highways](https://leetcode.com/problems/maximum-cost-of-trip-with-k-highways/) | [无] | [C++](2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/) | | | | | | | | | | ## 力扣中文站比赛 From 46f3545b5f268aa3240fa04ec71c4e806e3b74e6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Apr 2022 22:07:05 -0700 Subject: [PATCH 026/390] contest 290 added. --- .../cpp-2248/CMakeLists.txt | 6 +++ .../cpp-2248/main.cpp | 38 ++++++++++++++ .../cpp-2249/CMakeLists.txt | 6 +++ .../cpp-2249/main.cpp | 46 ++++++++++++++++ .../cpp-2250/CMakeLists.txt | 6 +++ .../cpp-2250/main.cpp | 52 +++++++++++++++++++ .../cpp-2251/CMakeLists.txt | 6 +++ .../cpp-2251/main.cpp | 50 ++++++++++++++++++ readme.md | 8 +++ 9 files changed, 218 insertions(+) create mode 100644 2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/CMakeLists.txt create mode 100644 2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/main.cpp create mode 100644 2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/CMakeLists.txt create mode 100644 2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/main.cpp create mode 100644 2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/CMakeLists.txt create mode 100644 2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/main.cpp create mode 100644 2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/CMakeLists.txt create mode 100644 2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/main.cpp diff --git a/2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/CMakeLists.txt b/2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/main.cpp b/2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/main.cpp new file mode 100644 index 00000000..e5793e40 --- /dev/null +++ b/2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/intersection-of-multiple-arrays/ +/// Author : liuyubobobo +/// Time : 2022-04-23 + +#include +#include +#include + +using namespace std; + + +/// Using Set +/// Time Complexity: O(sum(nums[i][j])) +/// Space Complexity: O(min(nums[i])) +class Solution { +public: + vector intersection(vector>& nums) { + + set res_set(nums[0].begin(), nums[0].end()); + for(int i = 1; i < nums.size(); i ++) + res_set = intersect(res_set, nums[i]); + return vector(res_set.begin(), res_set.end()); + } + +private: + set intersect(const set& s, const vector& v){ + + set res; + for(int e: v) if(s.count(e)) res.insert(e); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/CMakeLists.txt b/2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/main.cpp b/2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/main.cpp new file mode 100644 index 00000000..cf70d4c4 --- /dev/null +++ b/2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/count-lattice-points-inside-a-circle/ +/// Author : liuyubobobo +/// Time : 2022-04-23 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(x_range * y_range * |circles|) +/// Space Compelxity: O(1) +class Solution { +public: + int countLatticePoints(vector>& circles) { + + int minx = INT_MAX, maxx = INT_MIN, miny = INT_MAX, maxy = INT_MIN; + for(const vector& circle: circles){ + int x = circle[0], y = circle[1], r = circle[2]; + minx = min(minx, x - r), maxx = max(maxx, x + r); + miny = min(miny, y - r), maxy = max(maxy, y + r); + } + + int res = 0; + for(int x = minx; x <= maxx; x ++) + for(int y = miny; y <= maxy; y ++){ + bool ok = false; + for(const vector& circle: circles){ + int x0 = circle[0], y0 = circle[1], r0 = circle[2]; + if((x - x0) * (x - x0) + (y - y0) * (y - y0) <= r0 * r0){ + ok = true; + break; + } + } + res += ok; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/CMakeLists.txt b/2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/main.cpp b/2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/main.cpp new file mode 100644 index 00000000..320e95f9 --- /dev/null +++ b/2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/count-number-of-rectangles-containing-each-point/ +/// Author : liuyubobobo +/// Time : 2022-04-23 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(|rec|log|rec| + |point|log|rec|) +/// Space Complexity: O(|rec|) +class Solution { +public: + vector countRectangles(vector>& rectangles, vector>& points) { + + vector> h_rec(101); + for(const vector& rec: rectangles){ + int l = rec[0], h = rec[1]; + h_rec[h].push_back(l); + } + + for(int i = 0; i <= 100; i ++) sort(h_rec[i].begin(), h_rec[i].end()); + + vector res(points.size()); + for(int i = 0; i < points.size(); i ++){ + int x = points[i][0], y = points[i][1]; + int tres = 0; + for(int h = y; h <= 100; h ++){ + if(h_rec.empty()) continue; + auto iter = lower_bound(h_rec[h].begin(), h_rec[h].end(), x); + if(iter != h_rec[h].end()){ + int index = iter - h_rec[h].begin(); + tres += h_rec[h].size() - index; + } + } + res[i] = tres; + } + return res; + } +}; + + +int main() { + + vector> rectangles1 = {{1, 2}, {2, 3}, {2, 5}}; + vector> points1 = {{2, 1}, {1, 4}}; + Solution().countRectangles(rectangles1, points1); + + return 0; +} diff --git a/2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/CMakeLists.txt b/2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/main.cpp b/2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/main.cpp new file mode 100644 index 00000000..004e4781 --- /dev/null +++ b/2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/number-of-flowers-in-full-bloom/ +/// Author : liuyubobobo +/// Time : 2022-04-23 + +#include +#include +#include + +using namespace std; + + +/// Sweep line +/// Time Complexity: O(|2 * flowers + persons| * log|2 * flowers + persons|) +/// Space Complexity: O(|2 * flowers + persons|) +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& persons) { + + vector> events; + // event flower: start_time 0 0 + // end_time 0 1 + // person query_time 1 index + + for(const vector& flower: flowers){ + int start = flower[0], end = flower[1]; + events.push_back({start, 0, 0}); + events.push_back({end + 1, 0, 1}); + } + for(int i = 0; i < persons.size(); i ++) + events.push_back({persons[i], 1, i}); + + sort(events.begin(), events.end()); + vector res(persons.size(), 0); + int cur = 0; + for(const vector& event: events){ + int type = event[1]; + if(type == 0){ + cur += (event[2] == 0 ? 1 : -1); + } + else res[event[2]] = cur; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 71672eac..a7d514de 100644 --- a/readme.md +++ b/readme.md @@ -1016,6 +1016,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [无] | [C++](1001-1500/1047-Remove-All-Adjacent-Duplicates-In-String/cpp-1047/) | | | | | | | | | | | 1049 | [Last Stone Weight II](https://leetcode.com/problems/last-stone-weight-ii/) | [无] | [C++](1001-1500/1049-Last-Stone-Weight-II/cpp-1049/) | | | +| 1050 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1052 | [Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner/) | [无] | [C++](1001-1500/1052-Grumpy-Bookstore-Owner/cpp-1052/) | | | | | | | | | | @@ -1032,6 +1033,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1080 | [Insufficient Nodes in Root to Leaf Paths](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/) | [无] | [C++](1001-1500/1080-Insufficient-Nodes-in-Root-to-Leaf-Paths/cpp-1080/) | | | | 1081 | [Smallest Subsequence of Distinct Characters](https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/) | [无] | [C++](1001-1500/1081-Smallest-Subsequence-of-Distinct-Characters/cpp-1081/) | | | | | | | | | | +| 1084 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 1087 | [Brace Expansion](https://leetcode.com/problems/brace-expansion/) | [无] | [C++](1001-1500/1087-Brace-Expansion/cpp-1087/) | | | | | | | | | | | 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [无] | [C++](1001-1500/1089-Duplicate-Zeros/cpp-1089/) | | | @@ -1094,6 +1097,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1155 | [Number of Dice Rolls With Target Sum](https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/) | [无]
[缺:空间优化] | [C++](1001-1500/1155-Number-of-Dice-Rolls-With-Target-Sum/cpp-1155/) | | | | 1156 | [Swap For Longest Repeated Character Substring](https://leetcode.com/problems/swap-for-maximum-repeated-substring/) | [无] | [C++](1001-1500/1156-Swap-For-Longest-Repeated-Character-Substring/cpp-1156/) | | | | 1157 | [Online Majority Element In Subarray](https://leetcode.com/problems/online-majority-element-in-subarray/) | [无] | [C++](1001-1500/1157-Online-Majority-Element-In-Subarray/cpp-1157/) | | | +| 1158 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1160 | [Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/) | [无] | [C++](1001-1500/1160-Find-Words-That-Can-Be-Formed-by-Characters/cpp-1160/) | | | | 1161 | [Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) | [无] | [C++](1001-1500/1161-Maximum-Level-Sum-of-a-Binary-Tree/cpp-1161/) | | | @@ -2065,6 +2069,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2245 | [Maximum Trailing Zeros in a Cornered Path](https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/) | [无] | [C++](2001-2500/2245-Maximum-Trailing-Zeros-in-a-Cornered-Path/cpp-2245/) | | | | 2246 | [Longest Path With Different Adjacent Characters](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/) | [无] | [C++](2001-2500/2246-Longest-Path-With-Different-Adjacent-Characters/cpp-2246/) | | | | 2247 | [Maximum Cost of Trip With K Highways](https://leetcode.com/problems/maximum-cost-of-trip-with-k-highways/) | [无] | [C++](2001-2500/2247-Maximum-Cost-of-Trip-With-K-Highways/cpp-2247/) | | | +| 2248 | [Intersection of Multiple Arrays](https://leetcode.com/problems/intersection-of-multiple-arrays/) | [无] | [C++](2001-2500/2248-Intersection-of-Multiple-Arrays/cpp-2248/) | | | +| 2249 | [Count Lattice Points Inside a Circle](https://leetcode.com/problems/count-lattice-points-inside-a-circle/) | [无] | [C++](2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/) | | | +| 2250 | [Count Number of Rectangles Containing Each Point](https://leetcode.com/problems/count-number-of-rectangles-containing-each-point/) | [无] | [C++](2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/) | | | +| 2251 | [Number of Flowers in Full Bloom](https://leetcode.com/problems/number-of-flowers-in-full-bloom/) | [无] | [C++](2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/) | | | | | | | | | | ## 力扣中文站比赛 From 95f745f6cd0599ab9a0a186373c8e9a091a248b8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Apr 2022 22:11:16 -0700 Subject: [PATCH 027/390] LCP55 added. --- LC/LCP55/cpp-LCP55/CMakeLists.txt | 6 ++++++ LC/LCP55/cpp-LCP55/main.cpp | 31 +++++++++++++++++++++++++++++++ readme.md | 2 ++ 3 files changed, 39 insertions(+) create mode 100644 LC/LCP55/cpp-LCP55/CMakeLists.txt create mode 100644 LC/LCP55/cpp-LCP55/main.cpp diff --git a/LC/LCP55/cpp-LCP55/CMakeLists.txt b/LC/LCP55/cpp-LCP55/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/LC/LCP55/cpp-LCP55/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/LC/LCP55/cpp-LCP55/main.cpp b/LC/LCP55/cpp-LCP55/main.cpp new file mode 100644 index 00000000..e78090e6 --- /dev/null +++ b/LC/LCP55/cpp-LCP55/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode-cn.com/problems/PTXy4P/ +/// Author : liuyubobobo +/// Time : 2022-04-23 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|fruits|) +/// Space Complexity: O(1) +class Solution { +public: + int getMinimumTime(vector& time, vector>& fruits, int limit) { + + int res = 0; + for(const vector& fruit: fruits){ + int type = fruit[0], num = fruit[1]; + res += (num / limit + !!(num % limit)) * time[type]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a7d514de..7bb02fe0 100644 --- a/readme.md +++ b/readme.md @@ -2123,6 +2123,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | LCP51 | [烹饪料理](https://leetcode-cn.com/problems/UEcfPD/) | [无] | [C++](LC/LCP51/cpp-LCP51/) | | | | LCP52 | [二叉搜索树染色](https://leetcode-cn.com/problems/QO5KpG/) | [无] [缺:非线段树做法] | [C++](LC/LCP52/cpp-LCP52/) | | | | | | | | | | +| LCP55 | [采集果实](https://leetcode-cn.com/problems/PTXy4P/) | [无] | [C++](LC/LCP55/cpp-LCP55/) | | | +| | | | | | | ## 其他 From 056a00019766de105068397618fd0ecc0e452563 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Apr 2022 22:14:31 -0700 Subject: [PATCH 028/390] LCP56 added. --- LC/LCP56/cpp-LCP56/CMakeLists.txt | 6 +++ LC/LCP56/cpp-LCP56/main.cpp | 77 +++++++++++++++++++++++++++++++ readme.md | 1 + 3 files changed, 84 insertions(+) create mode 100644 LC/LCP56/cpp-LCP56/CMakeLists.txt create mode 100644 LC/LCP56/cpp-LCP56/main.cpp diff --git a/LC/LCP56/cpp-LCP56/CMakeLists.txt b/LC/LCP56/cpp-LCP56/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/LC/LCP56/cpp-LCP56/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/LC/LCP56/cpp-LCP56/main.cpp b/LC/LCP56/cpp-LCP56/main.cpp new file mode 100644 index 00000000..3cb2ed35 --- /dev/null +++ b/LC/LCP56/cpp-LCP56/main.cpp @@ -0,0 +1,77 @@ +/// Source : https://leetcode-cn.com/problems/6UEx57/ +/// Author : liuyubobobo +/// Time : 2022-04-23 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// 0-1 BFS +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { + +private: + const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + const string ds = "^v<>"; + int R, C; + +public: + int conveyorBelt(vector& matrix, vector& start, vector& end) { + + R = matrix.size(), C = matrix[0].size(); + + vector>> g(R * C); + for(int cx = 0; cx < R; cx ++) + for(int cy = 0; cy < C; cy ++){ + int od = ds.find(matrix[cx][cy]), cur = cx * C + cy; + assert(od != string::npos); + + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1], next = nx * C + ny; + if(in_area(nx, ny)) g[cur].push_back({next, (d == od ? 0 : 1)}); + } + } + return bfs(R * C, g, start[0] * C + start[1], end[0] * C + end[1]); + } + +private: + int bfs(int n, const vector>>& g, int s, int t){ + + vector dis(n, INT_MAX / 2); + vector visited(n, false); + deque q; + q.push_front(s); + dis[s] = 0; + while(!q.empty()){ + int u = q.front(); q.pop_front(); + if(visited[u]) continue; + visited[u] = true; + + for(const pair& p: g[u]){ + int v = p.first, w = p.second; + if(!visited[v] && w + dis[u] < dis[v]){ + dis[v] = dis[u] + w; + if(w) q.push_back(v); + else q.push_front(v); + } + } + } + return dis[t]; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 7bb02fe0..ec8970e8 100644 --- a/readme.md +++ b/readme.md @@ -2124,6 +2124,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | LCP52 | [二叉搜索树染色](https://leetcode-cn.com/problems/QO5KpG/) | [无] [缺:非线段树做法] | [C++](LC/LCP52/cpp-LCP52/) | | | | | | | | | | | LCP55 | [采集果实](https://leetcode-cn.com/problems/PTXy4P/) | [无] | [C++](LC/LCP55/cpp-LCP55/) | | | +| LCP56 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LC/LCP56/cpp-LCP56/) | | | | | | | | | | ## 其他 From de8336751f8e01cef8f2775e54ceba6a7832de5f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Apr 2022 22:19:45 -0700 Subject: [PATCH 029/390] LCP57 added. --- LC/LCP57/cpp-LCP57/CMakeLists.txt | 6 +++ LC/LCP57/cpp-LCP57/main.cpp | 68 +++++++++++++++++++++++++++++++ readme.md | 1 + 3 files changed, 75 insertions(+) create mode 100644 LC/LCP57/cpp-LCP57/CMakeLists.txt create mode 100644 LC/LCP57/cpp-LCP57/main.cpp diff --git a/LC/LCP57/cpp-LCP57/CMakeLists.txt b/LC/LCP57/cpp-LCP57/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/LC/LCP57/cpp-LCP57/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/LC/LCP57/cpp-LCP57/main.cpp b/LC/LCP57/cpp-LCP57/main.cpp new file mode 100644 index 00000000..0e42445c --- /dev/null +++ b/LC/LCP57/cpp-LCP57/main.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode-cn.com/problems/ZbAuEH/ +/// Author : liuyubobobo +/// Time : 2022-04-23 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Compelxity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const vector>> next ={ + {{0}, {1, 3}, {2, 4, 6}, {5, 7}, {8}}, + {{1}, {0, 2, 4}, {3, 5, 7}, {6, 8}}, + {{2}, {1, 5}, {0, 4, 8}, {3, 7}, {6}}, + {{3}, {0, 4, 6}, {1, 5, 7}, {2, 8}}, + {{4}, {1, 3, 5, 7}, {0, 2, 6, 8}}, + {{5}, {2, 4, 8}, {1, 3, 7}, {0, 6}}, + {{6}, {3, 7}, {0, 4, 8}, {1, 5}, {2}}, + {{7}, {4, 6, 8}, {1, 3, 5}, {0, 2}}, + {{8}, {5, 7}, {2, 4, 6}, {1, 3}, {0}} + }; + +public: + int getMaximumNumber(vector>& moles) { + + int n = moles.size(); + vector> data(n); // t, pos + for(int i = 0; i < n; i ++) + data[i] = {moles[i][0], moles[i][1] * 3 + moles[i][2]}; + sort(data.begin(), data.end()); + + vector> dp(9, vector(n, -1)); + int res = 0; + for(int t = 0; t <= data[0].first && t < next[4].size(); t ++) + for(int pos: next[4][t]) + res = max(res, dfs(n, data, pos, 0, dp)); + return res; + } + +private: + int dfs(int n, const vector>& data, int pos, int index, + vector>& dp){ + + if(index == n - 1) + return pos == data[index].second; + if(dp[pos][index] != -1) return dp[pos][index]; + + int T = data[index + 1].first - data[index].first; + int res = 0; + for(int t = 0; t <= T && t < next[pos].size(); t ++) + for(int next_pos: next[pos][t]) + res = max(res, (data[index].second == pos) + dfs(n, data, next_pos, index + 1, dp)); + return dp[pos][index] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ec8970e8..b33d93d0 100644 --- a/readme.md +++ b/readme.md @@ -2125,6 +2125,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | LCP55 | [采集果实](https://leetcode-cn.com/problems/PTXy4P/) | [无] | [C++](LC/LCP55/cpp-LCP55/) | | | | LCP56 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LC/LCP56/cpp-LCP56/) | | | +| LCP57 | [打地鼠](https://leetcode-cn.com/problems/ZbAuEH/) | [无] | [C++](LC/LCP57/cpp-LCP57/) | | | | | | | | | | ## 其他 From 283e857924eb5cd0287d23baf49798cbbb793eac Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 25 Apr 2022 16:38:05 -0700 Subject: [PATCH 030/390] readme updated. --- readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readme.md b/readme.md index b33d93d0..d910f7aa 100644 --- a/readme.md +++ b/readme.md @@ -1112,6 +1112,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1170 | [Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/) | [无] | [C++](1001-1500/1170-Compare-Strings-by-Frequency-of-the-Smallest-Character/cpp-1170/) | | | | 1171 | [Remove Zero Sum Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/) | [无] | [C++](1001-1500/1171-Remove-Zero-Sum-Consecutive-Nodes-from-Linked-List/cpp-1171/) | | | | 1172 | [Dinner Plate Stacks](https://leetcode.com/problems/dinner-plate-stacks/) | [无] | [C++](1001-1500/1172-Dinner-Plate-Stacks/cpp-1172/) | | | +| 1173 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1175 | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements/) | [无] | [C++](1001-1500/1175-Prime-Arrangements/) | | | | 1176 | [Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/) | [无] | [C++](1001-1500/1176-Diet-Plan-Performance/cpp-1176/) | | | @@ -1145,6 +1146,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1208 | [Get Equal Substrings Within Budget](https://leetcode.com/problems/get-equal-substrings-within-budget/) | [无] | [C++](1001-1500/1208-Get-Equal-Substrings-Within-Budget/cpp-1208/) | | | | 1209 | [Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/) | [无] | [C++](1001-1500/1209-Remove-All-Adjacent-Duplicates-in-String-II/cpp-1209/) | | | | 1210 | [Minimum Moves to Reach Target with Rotations](https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/) | [无] | [C++](1001-1500/1210-Minimum-Moves-to-Reach-Target-with-Rotations/cpp-1210/) | | | +| 1211 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1213 | [Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/) | [无] | [C++](1001-1500/1213-Intersection-of-Three-Sorted-Arrays/cpp-1213/) | | | | 1214 | [Two Sum BSTs](https://leetcode.com/problems/two-sum-bsts/) | [无] | [C++](1001-1500/1214-Two-Sum-BSTs/cpp-1214/) | | | @@ -1180,6 +1182,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1248 | [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [无] | [C++](1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/) | [Java](1001-1500/1248-Count-Number-of-Nice-Subarrays/java-1248/) | | | 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [solution](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/solution/) | [C++](1001-1500/1249-Minimum-Remove-to-Make-Valid-Parentheses/cpp-1249/) | | | | | | | | | | +| 1251 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [solution](https://leetcode.com/problems/shift-2d-grid/solution/) | [C++](1001-1500/1260-Shift-2D-Grid/cpp-1260/) | | | | 1261 | [Find Elements in a Contaminated Binary Tree](https://leetcode.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binary-tree/) | [C++](1001-1500/1261-Find-Elements-in-a-Contaminated-Binary-Tree/cpp-1261/) | | | | | 1262 | [Greatest Sum Divisible by Three](https://leetcode.com/problems/greatest-sum-divisible-by-three/) | [无] | [C++](1001-1500/1262-Greatest-Sum-Divisible-by-Three/cpp-1262/) | | | From 2cc4e24558f4ea692dade6860af4ca177551b3f2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 28 Apr 2022 10:55:15 -0700 Subject: [PATCH 031/390] 0427 solved. --- .../cpp-0427/CMakeLists.txt | 6 ++ .../cpp-0427/main.cpp | 91 +++++++++++++++++++ readme.md | 9 +- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0427-Construct-Quad-Tree/cpp-0427/CMakeLists.txt create mode 100644 0001-0500/0427-Construct-Quad-Tree/cpp-0427/main.cpp diff --git a/0001-0500/0427-Construct-Quad-Tree/cpp-0427/CMakeLists.txt b/0001-0500/0427-Construct-Quad-Tree/cpp-0427/CMakeLists.txt new file mode 100644 index 00000000..6cd09ad7 --- /dev/null +++ b/0001-0500/0427-Construct-Quad-Tree/cpp-0427/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0427) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0427 main.cpp) diff --git a/0001-0500/0427-Construct-Quad-Tree/cpp-0427/main.cpp b/0001-0500/0427-Construct-Quad-Tree/cpp-0427/main.cpp new file mode 100644 index 00000000..5c5d034e --- /dev/null +++ b/0001-0500/0427-Construct-Quad-Tree/cpp-0427/main.cpp @@ -0,0 +1,91 @@ +/// Source : https://leetcode.com/problems/construct-quad-tree/ +/// Author : liuyubobobo +/// Time : 2022-04-28 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(N^2) +/// Space Complexity: O(N^2) + +// Definition for a QuadTree node. +class Node { +public: + bool val; + bool isLeaf; + Node* topLeft; + Node* topRight; + Node* bottomLeft; + Node* bottomRight; + + Node() { + val = false; + isLeaf = false; + topLeft = NULL; + topRight = NULL; + bottomLeft = NULL; + bottomRight = NULL; + } + + Node(bool _val, bool _isLeaf) { + val = _val; + isLeaf = _isLeaf; + topLeft = NULL; + topRight = NULL; + bottomLeft = NULL; + bottomRight = NULL; + } + + Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) { + val = _val; + isLeaf = _isLeaf; + topLeft = _topLeft; + topRight = _topRight; + bottomLeft = _bottomLeft; + bottomRight = _bottomRight; + } +}; + +class Solution { +public: + Node* construct(vector>& grid) { + + int N = grid.size(); + vector> presum(N + 1, vector(N + 1, 0)); + for(int i = 0; i < N; i ++) + for(int j = 0; j < N; j ++) + presum[i + 1][j + 1] = presum[i + 1][j] + presum[i][j + 1] - presum[i][j] + grid[i][j]; + + return construct(grid, presum, 0, 0, N - 1, N - 1); + } + +private: + Node* construct(const vector>& grid, const vector>& presum, + int x1, int y1, int x2, int y2){ + + int value = presum[x2 + 1][y2 + 1] - presum[x1][y2 + 1] - presum[x2 + 1][y1] + presum[x1][y1]; + int area = (y2 - y1 + 1) * (x2 - x1 + 1); + if(value == area || value == 0) + return new Node(value, true); + + int N = (y2 - y1 + 1); + Node* node0 = construct(grid, presum, x1, y1, x1 + N / 2 - 1, y1 + N / 2 - 1); + Node* node1 = construct(grid, presum, x1, y1 + N / 2, x1 + N / 2 - 1, y1 + N - 1); + Node* node2 = construct(grid, presum, x1 + N / 2, y1, x1 + N - 1, y1 + N / 2 - 1); + Node* node3 = construct(grid, presum, x1 + N / 2, y1 + N / 2, x1 + N - 1, y1 + N - 1); + return new Node(value, false, node0, node1, node2, node3); + } +}; + + +int main() { + + vector> g1 = {{0, 1}, {1, 0}}; + Solution().construct(g1); + + return 0; +} diff --git a/readme.md b/readme.md index d910f7aa..d8a44bf9 100644 --- a/readme.md +++ b/readme.md @@ -457,6 +457,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 424 | [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | [无] | [C++](0001-0500/0424-Longest-Repeating-Character-Replacement/cpp-0424/) | | | | | | | | | | | 426 | [Convert Binary Search Tree to Sorted Doubly Linked List](https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/) | [solution](https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/solution/) | [C++](0001-0500/0426-Convert-Binary-Search-Tree-to-Sorted-Doubly-Linked-List/cpp-0426/) | | | +| 427 | [Construct Quad Tree](https://leetcode.com/problems/construct-quad-tree/) | [无] | [C++](0001-0500/0427-Construct-Quad-Tree/cpp-0427/) | | | | | | | | | | | 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [无] | [C++](0001-0500/0429-N-ary-Tree-Level-Order-Traversal/cpp-0429/) | | | | 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/) | [无] | [C++](0001-0500/0430-Flatten-a-Multilevel-Doubly-Linked-List/cpp-0430/) | | | @@ -586,6 +587,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 566 | [Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/) | [solution](https://leetcode.com/problems/reshape-the-matrix/solution/) | [C++](0501-1000/0566-Reshape-the-Matrix/cpp-0566/) | | | | 567 | [Permutation in String](https://leetcode.com/problems/permutation-in-string/) | [solution](https://leetcode.com/problems/permutation-in-string/solution/) [题解](https://leetcode-cn.com/problems/permutation-in-string/solution/zi-fu-chuan-de-pai-lie-by-leetcode/) | [C++](0501-1000/0567-Permutation-in-String/cpp-0567/) | | | | | | | | | | +| 570 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [solution](https://leetcode.com/problems/subtree-of-another-tree/solution/) | [C++](0501-1000/0572-Subtree-of-Another-Tree/cpp-0572/) | | | | 573 | [Squirrel Simulation](https://leetcode.com/problems/squirrel-simulation/) |[solution](https://leetcode.com/problems/squirrel-simulation/solution/) | [C++](0501-1000/0573-Squirrel-Simulation/cpp-0573/) | | | | | | | | | | @@ -620,6 +623,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C++](0501-1000/0617-Merge-Two-Binary-Trees/cpp-0617/) | | | | | | | | | | | +| 619 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 621 | [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | [solution](https://leetcode.com/problems/task-scheduler/solution/) | [C++](0501-1000/0621-Task-Scheduler/cpp-0621/) | | | | 622 | [Design Circular Queue](https://leetcode.com/problems/design-circular-queue/description/) | [无] | [C++](0501-1000/0622-Design-Circular-Queue/cpp-0622/) | | | | 623 | [Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | [solution](https://leetcode.com/problems/add-one-row-to-tree/solution/) | [C++](0501-1000/0623-Add-One-Row-to-Tree/cpp-0623/) | | | @@ -1059,6 +1064,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1109 | [Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [无] | [C++](1001-1500/1109-Corporate-Flight-Bookings/cpp-1009/) | | | | 1110 | [Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/) | [无] | [C++](1001-1500/1110-Delete-Nodes-And-Return-Forest/cpp-1110/) | | | | 1111 | [Maximum Nesting Depth of Two Valid Parentheses Strings](https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/) | [无] | [C++](1001-1500/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/) | | | +| 1112 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1118 | [Number of Days in a Month](https://leetcode.com/problems/number-of-days-in-a-month/) | [无] | [C++](1001-1500/1118-Number-of-Days-in-a-Month/cpp-1118/) | | | | 1119 | [Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/) | [无] | [C++](1001-1500/1119-Remove-Vowels-from-a-String/cpp-1119/) | | | @@ -1188,6 +1194,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1261 | [Find Elements in a Contaminated Binary Tree](https://leetcode.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binary-tree/) | [C++](1001-1500/1261-Find-Elements-in-a-Contaminated-Binary-Tree/cpp-1261/) | | | | | 1262 | [Greatest Sum Divisible by Three](https://leetcode.com/problems/greatest-sum-divisible-by-three/) | [无] | [C++](1001-1500/1262-Greatest-Sum-Divisible-by-Three/cpp-1262/) | | | | 1263 | [Minimum Moves to Move a Box to Their Target Location](https://leetcode.com/problems/minimum-moves-to-move-a-box-to-their-target-location/) | [无] | [C++](1001-1500/1263-Minimum-Moves-to-Move-a-Box-to-Their-Target-Location/cpp-1263/) | | | +| 1264 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1275 | [Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/) | [无] | [C++](1001-1500/1275-Find-Winner-on-a-Tic-Tac-Toe-Game/cpp-1275/) | | | | | | | | | | @@ -1204,7 +1211,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1293 | [Shortest Path in a Grid with Obstacles Elimination](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/) | [solution](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/) | [C++](1001-1500/1293-Shortest-Path-in-a-Grid-with-Obstacles-Elimination/cpp-1293/) | | | | | | | | | | | 1302 | [Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/) | [solution](https://leetcode.com/problems/deepest-leaves-sum/solution/) | [C++](1302-Deepest-Leaves-Sum/cpp-1302/) | | | -| | | | | | | +| 1303 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 1304 | [Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/) | [无] | [C++](1001-1500/1304-Find-N-Unique-Integers-Sum-up-to-Zero/cpp-1304/) | | | | 1305 | [All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/) | [无] | [C++](1001-1500/1305-All-Elements-in-Two-Binary-Search-Trees/cpp-1305/) | | | | 1306 | [Jump Game III](https://leetcode.com/problems/jump-game-iii/) | [无] | [C++](1001-1500/1306-Jump-Game-III/cpp-1306/) | | | From a0ad45166c7fbde051a389330f50247644cfcd24 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 29 Apr 2022 10:51:21 -0700 Subject: [PATCH 032/390] 2254 solved. --- .../cpp-2254/CMakeLists.txt | 6 ++ .../cpp-2254/main.cpp | 95 +++++++++++++++++++ readme.md | 5 + 3 files changed, 106 insertions(+) create mode 100644 2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/CMakeLists.txt create mode 100644 2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/main.cpp diff --git a/2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/CMakeLists.txt b/2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/CMakeLists.txt new file mode 100644 index 00000000..9bcef8ac --- /dev/null +++ b/2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2254) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2254 main.cpp) diff --git a/2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/main.cpp b/2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/main.cpp new file mode 100644 index 00000000..ad01ddd3 --- /dev/null +++ b/2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/main.cpp @@ -0,0 +1,95 @@ +/// Source : https://leetcode.com/problems/design-video-sharing-platform/ +/// Author : liuyubobobo +/// Time : 2022-04-29 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Priority Queue to maintain available ids +/// Time Complexity: init: O(1) +/// others: O(logn) +/// Space Complexity: O(n) +class VideoSharingPlatform { + +private: + int MAX_ID = 128; + const int D = 128; + priority_queue, greater> available_ids; + + map> videos; // video, watched, like, dislike + +public: + VideoSharingPlatform() { + for(int i = 0; i < MAX_ID; i ++) available_ids.push(i); + } + + int upload(const string& video) { + + if(available_ids.empty()){ + int start = MAX_ID; + MAX_ID += D; + for(int i = start; i < MAX_ID; i ++) available_ids.push(i); + } + + int new_id = available_ids.top(); + available_ids.pop(); + + videos[new_id] = {video, 0, 0, 0}; + return new_id; + } + + void remove(int videoId) { + if(videos.count(videoId)){ + videos.erase(videoId); + available_ids.push(videoId); + } + } + + string watch(int videoId, int startMinute, int endMinute) { + auto iter = videos.find(videoId); + if(iter != videos.end()){ + get<1>(iter->second) ++; + return get<0>(iter->second).substr(startMinute, endMinute - startMinute + 1); + } + return "-1"; + } + + void like(int videoId) { + auto iter = videos.find(videoId); + if(iter != videos.end()){ + get<2>(iter->second) ++; + } + } + + void dislike(int videoId) { + auto iter = videos.find(videoId); + if(iter != videos.end()){ + get<3>(iter->second) ++; + } + } + + vector getLikesAndDislikes(int videoId) { + auto iter = videos.find(videoId); + if(iter != videos.end()){ + return {get<2>(iter->second), get<3>(iter->second)}; + } + return {-1}; + } + + int getViews(int videoId) { + auto iter = videos.find(videoId); + if(iter != videos.end()) return get<1>(iter->second); + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d8a44bf9..25f9180f 100644 --- a/readme.md +++ b/readme.md @@ -1201,6 +1201,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1277 | [Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/) | [题解](https://leetcode-cn.com/problems/count-square-submatrices-with-all-ones/solution/tong-ji-quan-wei-1-de-zheng-fang-xing-zi-ju-zhen-2/) | [C++](1001-1500/1277-Count-Square-Submatrices-with-All-Ones/cpp-1277/) | | | | 1278 | [Palindrome Partitioning III](https://leetcode.com/problems/palindrome-partitioning-iii/) | [无] | [C++](1001-1500/1278-Palindrome-Partitioning-III/cpp-1278/) | | | | | | | | | | +| 1280 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 1286 | [Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/) | [solution](https://leetcode.com/problems/iterator-for-combination/solution/) | [C++](1001-1500/1286-Iterator-for-Combination/cpp-1286/) | | | | | | | | | | | 1288 | [Remove Covered Intervals](https://leetcode.com/problems/remove-covered-intervals/) | [solution](https://leetcode.com/problems/remove-covered-intervals/solution/) | [C++](1001-1500/1288-Remove-Covered-Intervals/cpp-1288/) | | | @@ -2084,6 +2086,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2249 | [Count Lattice Points Inside a Circle](https://leetcode.com/problems/count-lattice-points-inside-a-circle/) | [无] | [C++](2001-2500/2249-Count-Lattice-Points-Inside-a-Circle/cpp-2249/) | | | | 2250 | [Count Number of Rectangles Containing Each Point](https://leetcode.com/problems/count-number-of-rectangles-containing-each-point/) | [无] | [C++](2001-2500/2250-Count-Number-of-Rectangles-Containing-Each-Point/cpp-2250/) | | | | 2251 | [Number of Flowers in Full Bloom](https://leetcode.com/problems/number-of-flowers-in-full-bloom/) | [无] | [C++](2001-2500/2251-Number-of-Flowers-in-Full-Bloom/cpp-2251/) | | | +| 2252 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2253 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2254 | [Design Video Sharing Platform](https://leetcode.com/problems/design-video-sharing-platform/) | [无] | [C++](2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/) | | | | | | | | | | ## 力扣中文站比赛 From afc2acecd0b0c4e065e083f8db8a36a9c32e8dd2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 29 Apr 2022 11:22:41 -0700 Subject: [PATCH 033/390] 0431 solved. --- .../cpp-0431/CMakeLists.txt | 6 ++ .../cpp-0431/main.cpp | 83 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/CMakeLists.txt create mode 100644 0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/main.cpp diff --git a/0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/CMakeLists.txt b/0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/CMakeLists.txt new file mode 100644 index 00000000..f1cdc201 --- /dev/null +++ b/0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0431) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0431 main.cpp) diff --git a/0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/main.cpp b/0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/main.cpp new file mode 100644 index 00000000..5f5f1551 --- /dev/null +++ b/0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/main.cpp @@ -0,0 +1,83 @@ +/// Source : https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree/ +/// Author : liuyubobobo +/// Time : 2022-04-29 + +#include +#include + +using namespace std; + + +/// Left-Child Right-Sibling Representation +/// Time Complexity: O(n) +/// Space Complexity: O(1) + +// Definition for a Node. +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; + +///Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +class Codec { +public: + // Encodes an n-ary tree to a binary tree. + TreeNode* encode(Node* node) { + + if(!node) return nullptr; + + TreeNode* ret = new TreeNode(node->val); + + if(node->children.size() == 0) return ret; + + vector v; + for(Node* child_node: node->children) v.push_back(encode(child_node)); + + for(int i = 1; i < v.size(); i ++) v[i - 1]->right = v[i]; + ret->left = v[0]; + return ret; + } + + // Decodes your binary tree to an n-ary tree. + Node* decode(TreeNode* node) { + + if(!node) return nullptr; + + Node* ret = new Node(node->val); + if(!node->left) return ret; + + TreeNode* cur = node->left; + vector v; + while(cur){ + v.push_back(decode(cur)); + cur = cur->right; + } + ret->children = v; + return ret; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 25f9180f..d5404836 100644 --- a/readme.md +++ b/readme.md @@ -461,7 +461,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [无] | [C++](0001-0500/0429-N-ary-Tree-Level-Order-Traversal/cpp-0429/) | | | | 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/) | [无] | [C++](0001-0500/0430-Flatten-a-Multilevel-Doubly-Linked-List/cpp-0430/) | | | -| | | | | | | +| 431 | [Encode N-ary Tree to Binary Tree](https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree/) | [solution](https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree/solution/) | [C++](0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/) | | | | 432 | [All O one Data Structure](https://leetcode.com/problems/all-oone-data-structure/) | [无] [缺:O(1)] | [C++](0001-0500/0432-All-O-one-Data-Structure/cpp-0432/) | | | | | | | | | | | 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | | [C++](0001-0500/0434-Number-of-Segments-in-a-String/cpp-0434/) | | | From eb5aadddf9d7f68ccbd17e7d78fa7f36dde002a4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 30 Apr 2022 00:51:48 -0700 Subject: [PATCH 034/390] 0420 solved. --- .../cpp-0420/CMakeLists.txt | 6 + .../cpp-0420/main.cpp | 138 ++++++++++++++++++ readme.md | 2 +- 3 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0420-Strong-Password-Checker/cpp-0420/CMakeLists.txt create mode 100644 0001-0500/0420-Strong-Password-Checker/cpp-0420/main.cpp diff --git a/0001-0500/0420-Strong-Password-Checker/cpp-0420/CMakeLists.txt b/0001-0500/0420-Strong-Password-Checker/cpp-0420/CMakeLists.txt new file mode 100644 index 00000000..6581d524 --- /dev/null +++ b/0001-0500/0420-Strong-Password-Checker/cpp-0420/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0420) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0420 main.cpp) diff --git a/0001-0500/0420-Strong-Password-Checker/cpp-0420/main.cpp b/0001-0500/0420-Strong-Password-Checker/cpp-0420/main.cpp new file mode 100644 index 00000000..624e4036 --- /dev/null +++ b/0001-0500/0420-Strong-Password-Checker/cpp-0420/main.cpp @@ -0,0 +1,138 @@ +/// Source : https://leetcode.com/problems/strong-password-checker/ +/// Author : liuyubobobo +/// Time : 2022-04-30 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(|s|) +/// Space Compelxity: O(|s|) +class Solution { +public: + int strongPasswordChecker(string s) { + + int lower = 0, upper = 0, digit = 0, other = 0; + for(char c: s){ + if(islower(c)) lower ++; + else if(isupper(c)) upper ++; + else if(isdigit(c)) digit ++; + else other ++; + } + + priority_queue pq; + for(int start = 0, i = 1; i <= s.size(); i ++) + if(i == s.size() || s[start] != s[i]){ + if(i - start >= 3) pq.push(i - start); + start = i; + } + + int need_change = !lower + !upper + !digit; + int len = s.size(); + if(len < 6){ + int res = 0; + while(!pq.empty() && need_change){ + assert(pq.top() / 3 == 1); + res ++; + pq.pop(); need_change --; + len ++; + } + + res += pq.size(); len += pq.size(); + res += need_change; len += need_change; + res += max(6 - len, 0); + return res; + } + + if(len <= 20){ + return for20(pq, need_change); + } + + // len > 20 + vector v; + while(!pq.empty()) v.push_back(pq.top()), pq.pop(); + sort(v.begin(), v.end(), [](int a, int b){ + return a % 3 < b % 3; + }); + + int res = 0; + for(int i = 0; i < v.size() && len > 20; i ++){ + if(v[i] % 3 == 0){ + len --; v[i] --; res ++; + } + else if(v[i] % 3 == 1){ + len --; v[i] --; res ++; + if(len <= 20) break; + len --; v[i] --; res ++; + } + } + + for(int i = 0; i < v.size() && len > 20; i ++){ + while(v[i] > 5 && len > 20){ + int t = min(len - 20, 3); + v[i] -= t; len -= t; res += t; + } + } + + if(len == 20){ + for(int e: v) pq.push(e); + return res + for20(pq, need_change); + } + + for(int i = 0; i < v.size() && len > 20; i ++){ + int t = min(len - 20, v[i] - 2); + v[i] -= t; len -= t; res += t; + } + + if(len == 20){ + for(int e: v) pq.push(e); + return res + for20(pq, need_change); + } + + return max(len - 20, 0) + res + need_change; + } + +private: + int for20(priority_queue& pq, int need_change){ + int res = 0; + while(!pq.empty() && need_change){ + int cur = pq.top(); pq.pop(); + res ++, need_change --; + cur -= 3; + if(cur >= 3) pq.push(cur); + } + + if(need_change) res += need_change; + else{ + while(!pq.empty()){ + int cur = pq.top(); pq.pop(); + res += cur / 3; + } + } + return res; + } +}; + + +int main() { + + cout << Solution().strongPasswordChecker("a") << '\n'; + // 5 + + cout << Solution().strongPasswordChecker("aA1") << '\n'; + // 3 + + cout << Solution().strongPasswordChecker("1337C0d3") << '\n'; + // 0 + + cout << Solution().strongPasswordChecker("ABABABABABABABABABAB1") << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index d5404836..ba97be40 100644 --- a/readme.md +++ b/readme.md @@ -450,7 +450,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | [无] | | [Java](0001-0500/0417-Pacific-Atlantic-Water-Flow/java-0417/src/) | | | | | | | | | | 419 | [Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/) | [无] | [C++](0001-0500/0419-Battleships-in-a-Board/cpp-0419/) | | | -| | | | | | | +| 420 | [Strong Password Checker](https://leetcode.com/problems/strong-password-checker/) | [无] | [C++](0001-0500/0420-Strong-Password-Checker/cpp-0420/) | | | | 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [solution](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/solution/) | [C++](0001-0500/0421-Maximum-XOR-of-Two-Numbers-in-an-Array/cpp-0421/) | | | | | | | | | | | 423 | [Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/) | [solution](https://leetcode.com/problems/reconstruct-original-digits-from-english/solution/) | [C++](0001-0500/0423-Reconstruct-Original-Digits-from-English/cpp-0423/) | | | From 6774df83bc0c3fcfde795bf55e7fdfd13848dbef Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 30 Apr 2022 09:33:11 -0700 Subject: [PATCH 035/390] 2255 solved. --- .../cpp-2255/CMakeLists.txt | 6 ++++ .../cpp-2255/main.cpp | 30 +++++++++++++++++++ readme.md | 4 +++ 3 files changed, 40 insertions(+) create mode 100644 2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/CMakeLists.txt create mode 100644 2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/main.cpp diff --git a/2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/CMakeLists.txt b/2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/CMakeLists.txt new file mode 100644 index 00000000..8243a242 --- /dev/null +++ b/2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2255) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2255 main.cpp) diff --git a/2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/main.cpp b/2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/main.cpp new file mode 100644 index 00000000..cb7acfac --- /dev/null +++ b/2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/count-prefixes-of-a-given-string/ +/// Author : liuyubobobo +/// Time : 2022-04-30 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n * |s|) +/// Space Complexity: O(|s|) +class Solution { +public: + int countPrefixes(vector& words, string s) { + + int res = 0; + for(const string& word: words){ + res += (word.size() <= s.size() && s.substr(0, word.size()) == word); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ba97be40..3847c5e7 100644 --- a/readme.md +++ b/readme.md @@ -226,6 +226,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 181 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 184 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | | 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/description/) | [无] | [C++](0001-0500/0186-Reverse-Words-in-a-String-II/cpp-0186/) | | | | | | | | | | | 188 | [Best-Time-to-Buy-and-Sell-Stock-IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) | | [C++](0001-0500/0188-Best-Time-to-Buy-and-Sell-Stock-IV/cpp-0188/) | | | @@ -595,6 +597,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 575 | [Distribute Candies](https://leetcode.com/problems/distribute-candies/) | [solution](https://leetcode.com/problems/distribute-candies/solution/) | [C++](0501-1000/0575-Distribute-Candies/cpp-0575/) | | | | 576 | [Out of Boundary Paths](https://leetcode.com/problems/out-of-boundary-paths/) | [solution](https://leetcode.com/problems/out-of-boundary-paths/solution/) | [C++](0501-1000/0576-Out-of-Boundary-Paths/cpp-0576/) | | | | | | | | | | +| 580 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | [solution](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/solution/)
[缺:Stack] | [C++](0501-1000/0581-Shortest-Unsorted-Continuous-Subarray/cpp-0581/) | | | | 582 | [Kill Process](https://leetcode.com/problems/kill-process/) | [solution](https://leetcode.com/problems/kill-process/solution/) | [C++](0501-1000/0582-Kill-Process/cpp-0582/) | | | | 583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/description/) | [solution](https://leetcode.com/problems/delete-operation-for-two-strings/solution/) | [C++](0501-1000/0583-Delete-Operation-for-Two-Strings/cpp-0583/) | | | @@ -2089,6 +2092,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2252 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2253 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2254 | [Design Video Sharing Platform](https://leetcode.com/problems/design-video-sharing-platform/) | [无] | [C++](2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/) | | | +| 2255 | [Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/) | [无] | [C++](2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/) | | | | | | | | | | ## 力扣中文站比赛 From ff974909d3a17dbd2393aebdddd6d5d06ccd8b1f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 1 May 2022 18:38:59 -0700 Subject: [PATCH 036/390] 2259 solved. --- .../cpp-2259/CMakeLists.txt | 6 +++ .../cpp-2259/main.cpp | 41 +++++++++++++++++++ readme.md | 2 + 3 files changed, 49 insertions(+) create mode 100644 2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/CMakeLists.txt create mode 100644 2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/main.cpp diff --git a/2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/CMakeLists.txt b/2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/main.cpp b/2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/main.cpp new file mode 100644 index 00000000..ec2c6878 --- /dev/null +++ b/2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/ +/// Author : liuyubobobo +/// Time : 2022-04-30 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + string removeDigit(string number, char digit) { + + string res(number.size() - 1, '0'); + for(int i = 0; i < number.size(); i ++) + if(number[i] == digit){ + string cur = number.substr(0, i) + number.substr(i + 1); +// cout << cur << '\n'; + if(cur > res) res = cur; + } + return res; + } +}; + + +int main() { + + cout << Solution().removeDigit("123", '3') << '\n'; + // 12 + + cout << Solution().removeDigit("1231", '1') << '\n'; + // 231 + + cout << Solution().removeDigit("551", '5') << '\n'; + // 51 + + return 0; +} diff --git a/readme.md b/readme.md index 3847c5e7..8b3bda20 100644 --- a/readme.md +++ b/readme.md @@ -2094,6 +2094,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2254 | [Design Video Sharing Platform](https://leetcode.com/problems/design-video-sharing-platform/) | [无] | [C++](2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/) | | | | 2255 | [Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/) | [无] | [C++](2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/) | | | | | | | | | | +| 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [无] | [C++](2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/) | | | +| | | | | | | ## 力扣中文站比赛 From 2753f15ddf3b12e8d00caf85be4bfb9e39beb208 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 1 May 2022 18:42:30 -0700 Subject: [PATCH 037/390] 2260 solved. --- .../cpp-2260/CMakeLists.txt | 6 +++ .../cpp-2260/main.cpp | 52 +++++++++++++++++++ readme.md | 1 + 3 files changed, 59 insertions(+) create mode 100644 2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/CMakeLists.txt create mode 100644 2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/main.cpp diff --git a/2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/CMakeLists.txt b/2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/main.cpp b/2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/main.cpp new file mode 100644 index 00000000..58220580 --- /dev/null +++ b/2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/ +/// Author : liuyubobobo +/// Time : 2022-04-30 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumCardPickup(vector& cards) { + + map pos; + int res = INT_MAX; + for(int i = 0; i < cards.size(); i ++){ + if(pos.count(cards[i])){ + res = min(res, i - pos[cards[i]] + 1); + } + pos[cards[i]] = i; + } + return res == INT_MAX ? -1 : res; + } +}; + + +int main() { + + vector cards1 = {3,4,2,3,4,7}; + cout << Solution().minimumCardPickup(cards1) << '\n'; + // 4 + + vector cards2 = {1,0,5,3}; + cout << Solution().minimumCardPickup(cards2) << '\n'; + // -1 + + vector cards3 = {95,11,8,65,5,86,30,27,30,73,15,91,30,7,37,26,55,76,60,43,36,85,47,96,6}; + cout << Solution().minimumCardPickup(cards3) << '\n'; + // 3 + + vector cards4 = {77,10,11,51,69,83,33,94,0,42,86,41,65,40,72,8,53,31,43,22,9,94,45,80,40,0,84,34,76,28,7,79,80,93,20,82,36,74,82,89,74,77,27,54,44,93,98,44,39,74,36,9,22,57,70,98,19,68,33,68,49,86,20,50,43}; + cout << Solution().minimumCardPickup(cards4) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index 8b3bda20..126a19c7 100644 --- a/readme.md +++ b/readme.md @@ -2095,6 +2095,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2255 | [Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/) | [无] | [C++](2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/) | | | | | | | | | | | 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [无] | [C++](2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/) | | | +| 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [无] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | | | | | | | | | ## 力扣中文站比赛 From 001d6b60cbe0cc8267b70d80cd78c26560acfd61 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 1 May 2022 21:00:02 -0700 Subject: [PATCH 038/390] 2261 solved. --- .../cpp-2261/CMakeLists.txt | 6 ++ .../cpp-2261/main.cpp | 54 ++++++++++++ .../cpp-2261/main2.cpp | 65 ++++++++++++++ .../cpp-2261/main3.cpp | 88 +++++++++++++++++++ readme.md | 1 + 5 files changed, 214 insertions(+) create mode 100644 2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/CMakeLists.txt create mode 100644 2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main.cpp create mode 100644 2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main2.cpp create mode 100644 2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main3.cpp diff --git a/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/CMakeLists.txt b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/CMakeLists.txt new file mode 100644 index 00000000..e69f76e5 --- /dev/null +++ b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main3.cpp) diff --git a/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main.cpp b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main.cpp new file mode 100644 index 00000000..8ea4d0b5 --- /dev/null +++ b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/k-divisible-elements-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-04-30 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^3) +/// Space Compelxity: O(n^3) +class Solution { +public: + int countDistinct(vector& nums, int k, int p) { + + int n = nums.size(); + vector k_divisible(n, 0); + for(int i = 0; i < n; i ++) k_divisible[i] = (nums[i] % p == 0); + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + k_divisible[i]; + + vector>> res(k + 1); + for(int l = 0; l < n; l ++) + for(int r = l; r < n; r ++){ + + int t = presum[r + 1] - presum[l]; + if(t <= k) + res[t].insert(vector(nums.begin() + l, nums.begin() + (r + 1))); + else break; + } + + int ret = 0; + for(const set>& s: res) ret += s.size(); + return ret; + } +}; + + +int main() { + + vector nums1 = {2, 3, 3, 2, 2}; + cout << Solution().countDistinct(nums1, 2, 2) << '\n'; + // 11 + + vector nums2 = {1, 2, 3, 4}; + cout << Solution().countDistinct(nums2, 4, 1) << '\n'; + // 10 + + return 0; +} diff --git a/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main2.cpp b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main2.cpp new file mode 100644 index 00000000..38188f45 --- /dev/null +++ b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main2.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.com/problems/k-divisible-elements-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-05-01 + +#include +#include +#include +#include + +using namespace std; + + +/// Hash +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { + +private: + const unsigned long long B = 13331; + +public: + int countDistinct(vector& nums, int k, int p) { + + int n = nums.size(); + vector k_divisible(n, 0); + for(int i = 0; i < n; i ++) k_divisible[i] = (nums[i] % p == 0); + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + k_divisible[i]; + + vector> res(k + 1); + for(int l = 0; l < n; l ++) + for(int r = l; r < n; r ++){ + + int t = presum[r + 1] - presum[l]; + if(t <= k) res[t].insert(vector_to_hash(nums, l, r)); + else break; + } + + int ret = 0; + for(const set& s: res) ret += s.size(); + return ret; + } + +private: + unsigned long long vector_to_hash(const vector& nums, int l, int r){ + unsigned long long res = 0; + for(int i = l; i <= r; i ++) res = res * B + nums[i]; + return res; + } +}; + + +int main() { + + vector nums1 = {2, 3, 3, 2, 2}; + cout << Solution().countDistinct(nums1, 2, 2) << '\n'; + // 11 + + vector nums2 = {1, 2, 3, 4}; + cout << Solution().countDistinct(nums2, 4, 1) << '\n'; + // 10 + + return 0; +} diff --git a/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main3.cpp b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main3.cpp new file mode 100644 index 00000000..fb913a20 --- /dev/null +++ b/2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/main3.cpp @@ -0,0 +1,88 @@ +/// Source : https://leetcode.com/problems/k-divisible-elements-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-05-01 + +#include +#include +#include + +using namespace std; + + +/// Trie +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^3) +class Trie { + +private: + class Node{ + + public: + map next; + bool is_end; + + Node() : is_end(false) {}; + }; + + Node* root; + int sz; + +public: + Trie() : root(new Node()), sz(0) {} + + void insert(const vector& word, int l, int r) { + + Node* cur = root; + for(int i = l; i <= r; i ++){ + int c = word[i]; + if(!cur->next.count(c)) + cur->next[c] = new Node(); + cur = cur->next[c]; + + if(!cur->is_end) + cur->is_end = true, sz ++; + } + } + + int size(){ + return sz; + } +}; + +class Solution { + +public: + int countDistinct(vector& nums, int k, int p) { + + int n = nums.size(); + vector k_divisible(n, 0); + for(int i = 0; i < n; i ++) k_divisible[i] = (nums[i] % p == 0); + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + k_divisible[i]; + + Trie trie; + for(int l = 0; l < n; l ++){ + auto iter = upper_bound(presum.begin(), presum.end(), presum[l] + k); + int index = iter - presum.begin() - 1; + int r = index - 1; + if(l <= r) + trie.insert(nums, l, r); + } + return trie.size(); + } +}; + + +int main() { + + vector nums1 = {2, 3, 3, 2, 2}; + cout << Solution().countDistinct(nums1, 2, 2) << '\n'; + // 11 + + vector nums2 = {1, 2, 3, 4}; + cout << Solution().countDistinct(nums2, 4, 1) << '\n'; + // 10 + + return 0; +} diff --git a/readme.md b/readme.md index 126a19c7..cb64f9ae 100644 --- a/readme.md +++ b/readme.md @@ -2096,6 +2096,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [无] | [C++](2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/) | | | | 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [无] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | | +| 2261 | [K Divisible Elements Subarrays](https://leetcode.com/problems/k-divisible-elements-subarrays/) | [无] | [C++](2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/) | | | | | | | | | | ## 力扣中文站比赛 From 83cef9261aa449ba2c598f097278fb6001a03797 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 3 May 2022 00:50:08 -0700 Subject: [PATCH 039/390] 0591 solved. --- .../cpp-0591/CMakeLists.txt | 6 + .../0591-Tag-Validator/cpp-0591/main.cpp | 105 +++++++++++++++++ .../0591-Tag-Validator/cpp-0591/main2.cpp | 106 ++++++++++++++++++ readme.md | 1 + 4 files changed, 218 insertions(+) create mode 100644 0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt create mode 100644 0501-1000/0591-Tag-Validator/cpp-0591/main.cpp create mode 100644 0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp diff --git a/0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt b/0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt new file mode 100644 index 00000000..658a490f --- /dev/null +++ b/0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0591) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0591 main2.cpp) diff --git a/0501-1000/0591-Tag-Validator/cpp-0591/main.cpp b/0501-1000/0591-Tag-Validator/cpp-0591/main.cpp new file mode 100644 index 00000000..701e7a13 --- /dev/null +++ b/0501-1000/0591-Tag-Validator/cpp-0591/main.cpp @@ -0,0 +1,105 @@ +#include +#include + +using namespace std; + + +class Solution { +public: + bool isValid(string code) { + + while(true){ + int cdata_start = code.find("", cdata_start); + if(cdata_end == string::npos || cdata_end + 3 == code.size()) return false; + + code = code.substr(0, cdata_start) + code.substr(cdata_end + 3); + } + + + vector tag_stack; + int i = 0; + while(i < code.size()){ + pair tag_pos = get_tag_pos(code, i); + int tag_start = tag_pos.first, tag_end = tag_pos.second; + + if(tag_start == -1) return false; + if(i == 0 && tag_start != 0) return false; + + bool is_start_tag = true; + string tag_name = ""; + if(code[tag_start + 1] == '/'){ + is_start_tag = false; + tag_name = code.substr(tag_start + 2, tag_end - (tag_start + 2)); + } + else{ + tag_name = code.substr(tag_start + 1, tag_end - (tag_start + 1)); + } + + if(!is_valid_tag(tag_name)) return false; + + if(is_start_tag){ + tag_stack.push_back(tag_name); + i = tag_end + 1; + } + else{ + if(tag_stack.empty() || tag_stack.back() != tag_name) return false; + tag_stack.pop_back(); + i = tag_end + 1; + } + + if(i < code.size() && tag_stack.empty()) return false; + } + + return i == code.size() && tag_stack.empty(); + } + +private: + bool is_valid_tag(const string& tag){ + if(tag.empty() || tag.size() > 9) return false; + for(char c: tag) + if(!isupper(c)) return false; + return true; + } + + pair get_tag_pos(const string& s, int index){ + + int start_pos = s.find('<', index); + if(start_pos == string::npos) return {-1, -1}; + + int end_pos = s.find('>', start_pos); + if(end_pos == string::npos) return {-1, -1}; + + return {start_pos, end_pos}; + } +}; + + +int main() { + + cout << Solution().isValid("
This is the first line ]]>
") << '\n'; + // 1 + + cout << Solution().isValid("
>> ![cdata[]] ]>]]>]]>>]
") << '\n'; + // 1 + + cout << Solution().isValid(" ") << '\n'; + // 0 + + cout << Solution().isValid(" wahaha]]>") << '\n'; + // 1 + + cout << Solution().isValid(" wahaha]]>") << '\n'; + // 0 + + cout << Solution().isValid("") << '\n'; + // 0 + + cout << Solution().isValid("sometext") << '\n'; + // 0 + + return 0; +} diff --git a/0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp b/0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp new file mode 100644 index 00000000..f7eb4775 --- /dev/null +++ b/0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp @@ -0,0 +1,106 @@ +/// Source : https://leetcode.com/problems/tag-validator/ +/// Author : liuyubobobo +/// Time : 2022-05-03 + +#include +#include + +using namespace std; + + +/// Using Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + bool isValid(string code) { + + vector tag_stack; + int i = 0; + while(i < code.size()){ + pair tag_pos = get_tag_pos(code, i); + int tag_start = tag_pos.first, tag_end = tag_pos.second; + + if(tag_start == -1) return false; + if(i == 0 && tag_start != 0) return false; + + // end tag + if(code[tag_start + 1] == '/'){ + string tag_name = code.substr(tag_start + 2, tag_end - (tag_start + 2)); + if(!is_valid_tag(tag_name)) return false; + + if(tag_stack.empty() || tag_stack.back() != tag_name) return false; + tag_stack.pop_back(); + i = tag_end + 1; + } + else if(code.find("", tag_start); + if(cdata_end == string::npos || cdata_end + 3 == code.size()) return false; + + i = cdata_end + 3; + } + else{ // start tag + string tag_name = code.substr(tag_start + 1, tag_end - (tag_start + 1)); + if(!is_valid_tag(tag_name)) return false; + + tag_stack.push_back(tag_name); + i = tag_end + 1; + } + + if(i < code.size() && tag_stack.empty()) return false; + } + + return i == code.size() && tag_stack.empty(); + } + +private: + bool is_valid_tag(const string& tag){ + if(tag.empty() || tag.size() > 9) return false; + for(char c: tag) + if(!isupper(c)) return false; + return true; + } + + pair get_tag_pos(const string& s, int index){ + + int start_pos = s.find('<', index); + if(start_pos == string::npos) return {-1, -1}; + + int end_pos = s.find('>', start_pos); + if(end_pos == string::npos) return {-1, -1}; + + return {start_pos, end_pos}; + } +}; + + +int main() { + + cout << Solution().isValid("
This is the first line ]]>
") << '\n'; + // 1 + + cout << Solution().isValid("
>> ![cdata[]] ]>]]>]]>>]
") << '\n'; + // 1 + + cout << Solution().isValid(" ") << '\n'; + // 0 + + cout << Solution().isValid(" wahaha]]>") << '\n'; + // 1 + + cout << Solution().isValid(" wahaha]]>") << '\n'; + // 0 + + cout << Solution().isValid("") << '\n'; + // 0 + + cout << Solution().isValid("sometext") << '\n'; + // 0 + + cout << Solution().isValid("sometextG>") << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index cb64f9ae..a61f4456 100644 --- a/readme.md +++ b/readme.md @@ -608,6 +608,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-preorder-traversal/solution/) | [C++](0501-1000/0589-N-ary-Tree-Preorder-Traversal/cpp-0589/) | | | | 590 | [N-ary Tree Postorder Transversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-postorder-traversal/solution/) | [C++](0501-1000/0590-N-ary-Tree-Postorder-Transversal/cpp-0590/) | | | +| 591 | [Tag Validator](https://leetcode.com/problems/tag-validator/) | [solution](https://leetcode.com/problems/tag-validator/solution/) | [C++](0501-1000/0591-Tag-Validator/cpp-0591/) | | | | | | | | | | | 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) | [solution](https://leetcode.com/problems/longest-harmonious-subsequence/solution/) | [C++](0501-1000/0594-Longest-Harmonious-Subsequence/cpp-0594/) | | | | 595 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | From 2ab732c800e50622b76052b229b905cabc894e60 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 3 May 2022 00:50:47 -0700 Subject: [PATCH 040/390] 0591 codes updated. --- .../cpp-0591/CMakeLists.txt | 2 +- .../0591-Tag-Validator/cpp-0591/main.cpp | 53 ++++----- .../0591-Tag-Validator/cpp-0591/main2.cpp | 106 ------------------ 3 files changed, 28 insertions(+), 133 deletions(-) delete mode 100644 0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp diff --git a/0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt b/0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt index 658a490f..1028ffaf 100644 --- a/0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt +++ b/0501-1000/0591-Tag-Validator/cpp-0591/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0591) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_0591 main2.cpp) +add_executable(cpp_0591 main.cpp) diff --git a/0501-1000/0591-Tag-Validator/cpp-0591/main.cpp b/0501-1000/0591-Tag-Validator/cpp-0591/main.cpp index 701e7a13..f7eb4775 100644 --- a/0501-1000/0591-Tag-Validator/cpp-0591/main.cpp +++ b/0501-1000/0591-Tag-Validator/cpp-0591/main.cpp @@ -1,25 +1,20 @@ +/// Source : https://leetcode.com/problems/tag-validator/ +/// Author : liuyubobobo +/// Time : 2022-05-03 + #include #include using namespace std; +/// Using Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) class Solution { public: bool isValid(string code) { - while(true){ - int cdata_start = code.find("", cdata_start); - if(cdata_end == string::npos || cdata_end + 3 == code.size()) return false; - - code = code.substr(0, cdata_start) + code.substr(cdata_end + 3); - } - - vector tag_stack; int i = 0; while(i < code.size()){ @@ -29,25 +24,28 @@ class Solution { if(tag_start == -1) return false; if(i == 0 && tag_start != 0) return false; - bool is_start_tag = true; - string tag_name = ""; + // end tag if(code[tag_start + 1] == '/'){ - is_start_tag = false; - tag_name = code.substr(tag_start + 2, tag_end - (tag_start + 2)); - } - else{ - tag_name = code.substr(tag_start + 1, tag_end - (tag_start + 1)); + string tag_name = code.substr(tag_start + 2, tag_end - (tag_start + 2)); + if(!is_valid_tag(tag_name)) return false; + + if(tag_stack.empty() || tag_stack.back() != tag_name) return false; + tag_stack.pop_back(); + i = tag_end + 1; } + else if(code.find("", tag_start); + if(cdata_end == string::npos || cdata_end + 3 == code.size()) return false; - if(is_start_tag){ - tag_stack.push_back(tag_name); - i = tag_end + 1; + i = cdata_end + 3; } - else{ - if(tag_stack.empty() || tag_stack.back() != tag_name) return false; - tag_stack.pop_back(); + else{ // start tag + string tag_name = code.substr(tag_start + 1, tag_end - (tag_start + 1)); + if(!is_valid_tag(tag_name)) return false; + + tag_stack.push_back(tag_name); i = tag_end + 1; } @@ -101,5 +99,8 @@ int main() { cout << Solution().isValid("sometext") << '\n'; // 0 + cout << Solution().isValid("sometextG>") << '\n'; + // 0 + return 0; } diff --git a/0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp b/0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp deleted file mode 100644 index f7eb4775..00000000 --- a/0501-1000/0591-Tag-Validator/cpp-0591/main2.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/// Source : https://leetcode.com/problems/tag-validator/ -/// Author : liuyubobobo -/// Time : 2022-05-03 - -#include -#include - -using namespace std; - - -/// Using Stack -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { -public: - bool isValid(string code) { - - vector tag_stack; - int i = 0; - while(i < code.size()){ - pair tag_pos = get_tag_pos(code, i); - int tag_start = tag_pos.first, tag_end = tag_pos.second; - - if(tag_start == -1) return false; - if(i == 0 && tag_start != 0) return false; - - // end tag - if(code[tag_start + 1] == '/'){ - string tag_name = code.substr(tag_start + 2, tag_end - (tag_start + 2)); - if(!is_valid_tag(tag_name)) return false; - - if(tag_stack.empty() || tag_stack.back() != tag_name) return false; - tag_stack.pop_back(); - i = tag_end + 1; - } - else if(code.find("", tag_start); - if(cdata_end == string::npos || cdata_end + 3 == code.size()) return false; - - i = cdata_end + 3; - } - else{ // start tag - string tag_name = code.substr(tag_start + 1, tag_end - (tag_start + 1)); - if(!is_valid_tag(tag_name)) return false; - - tag_stack.push_back(tag_name); - i = tag_end + 1; - } - - if(i < code.size() && tag_stack.empty()) return false; - } - - return i == code.size() && tag_stack.empty(); - } - -private: - bool is_valid_tag(const string& tag){ - if(tag.empty() || tag.size() > 9) return false; - for(char c: tag) - if(!isupper(c)) return false; - return true; - } - - pair get_tag_pos(const string& s, int index){ - - int start_pos = s.find('<', index); - if(start_pos == string::npos) return {-1, -1}; - - int end_pos = s.find('>', start_pos); - if(end_pos == string::npos) return {-1, -1}; - - return {start_pos, end_pos}; - } -}; - - -int main() { - - cout << Solution().isValid("
This is the first line ]]>
") << '\n'; - // 1 - - cout << Solution().isValid("
>> ![cdata[]] ]>]]>]]>>]
") << '\n'; - // 1 - - cout << Solution().isValid(" ") << '\n'; - // 0 - - cout << Solution().isValid(" wahaha]]>") << '\n'; - // 1 - - cout << Solution().isValid(" wahaha]]>") << '\n'; - // 0 - - cout << Solution().isValid("") << '\n'; - // 0 - - cout << Solution().isValid("sometext") << '\n'; - // 0 - - cout << Solution().isValid("sometextG>") << '\n'; - // 0 - - return 0; -} From 36b15851d4b31ff4d594d6eef0147739dea43d1d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 3 May 2022 01:30:05 -0700 Subject: [PATCH 041/390] 2262 solved. --- .../cpp-2262/CMakeLists.txt | 6 +++ .../cpp-2262/main.cpp | 47 +++++++++++++++++++ .../cpp-2262/main2.cpp | 47 +++++++++++++++++++ readme.md | 1 + 4 files changed, 101 insertions(+) create mode 100644 2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/CMakeLists.txt create mode 100644 2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main.cpp create mode 100644 2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main2.cpp diff --git a/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/CMakeLists.txt b/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/CMakeLists.txt new file mode 100644 index 00000000..bb71ae36 --- /dev/null +++ b/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) diff --git a/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main.cpp b/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main.cpp new file mode 100644 index 00000000..df6dad2b --- /dev/null +++ b/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/total-appeal-of-a-string/ +/// Author : liuyubobobo +/// Time : 2022-04-30 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long appealSum(string s) { + + int n = s.size(); + + vector> pos(26); + for(int i = 0; i < n; i ++) + pos[s[i] - 'a'].push_back(i); + + long long res = 0; + for(int ch = 0; ch < 26; ch ++){ + vector& v = pos[ch]; + for(int l = 0; l < n; l ++){ + auto iter = lower_bound(v.begin(), v.end(), l); + if(iter != v.end()) + res += n - *iter; + } + } + return res; + } +}; + + +int main() { + + cout << Solution().appealSum("abbca") << '\n'; + // 28 + + cout << Solution().appealSum("code") << '\n'; + // 20 + + return 0; +} diff --git a/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main2.cpp b/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main2.cpp new file mode 100644 index 00000000..efd5179b --- /dev/null +++ b/2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/main2.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/total-appeal-of-a-string/ +/// Author : liuyubobobo +/// Time : 2022-05-03 + +#include +#include + +using namespace std; + + +/// Two Pointers +/// Time Complexity: O(n) +/// Space Compelxity: O(n) +class Solution { +public: + long long appealSum(string s) { + + int n = s.size(); + + vector> pos(26); + for(int i = 0; i < n; i ++) + pos[s[i] - 'a'].push_back(i); + + long long res = 0; + for(int ch = 0; ch < 26; ch ++){ + vector& v = pos[ch]; + int index = 0; + for(int l = 0; l < n && index < v.size(); l ++){ + res += n - v[index]; + if(v[index] == l) index ++; + } + } + return res; + } +}; + + +int main() { + + cout << Solution().appealSum("abbca") << '\n'; + // 28 + + cout << Solution().appealSum("code") << '\n'; + // 20 + + return 0; +} diff --git a/readme.md b/readme.md index a61f4456..6054d744 100644 --- a/readme.md +++ b/readme.md @@ -2098,6 +2098,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [无] | [C++](2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/) | | | | 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [无] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | | | 2261 | [K Divisible Elements Subarrays](https://leetcode.com/problems/k-divisible-elements-subarrays/) | [无] | [C++](2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/) | | | +| 2262 | [Total Appeal of A String](https://leetcode.com/problems/total-appeal-of-a-string/) | [无] | [C++](2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/) | | | | | | | | | | ## 力扣中文站比赛 From 98f05696627c47a1560b84f92e44b1954686defa Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 3 May 2022 01:42:24 -0700 Subject: [PATCH 042/390] 2256 solved. --- .../cpp-2256/CMakeLists.txt | 6 +++ .../cpp-2256/main.cpp | 50 +++++++++++++++++++ readme.md | 1 + 3 files changed, 57 insertions(+) create mode 100644 2001-2500/2256-Minimum-Average-Difference/cpp-2256/CMakeLists.txt create mode 100644 2001-2500/2256-Minimum-Average-Difference/cpp-2256/main.cpp diff --git a/2001-2500/2256-Minimum-Average-Difference/cpp-2256/CMakeLists.txt b/2001-2500/2256-Minimum-Average-Difference/cpp-2256/CMakeLists.txt new file mode 100644 index 00000000..4de84433 --- /dev/null +++ b/2001-2500/2256-Minimum-Average-Difference/cpp-2256/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2256) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2256 main.cpp) diff --git a/2001-2500/2256-Minimum-Average-Difference/cpp-2256/main.cpp b/2001-2500/2256-Minimum-Average-Difference/cpp-2256/main.cpp new file mode 100644 index 00000000..acdf3e54 --- /dev/null +++ b/2001-2500/2256-Minimum-Average-Difference/cpp-2256/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/minimum-average-difference/ +/// Author : liuyubobobo +/// Time : 2022-05-03 + +#include +#include +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Compelxity: O(1) +class Solution { +public: + int minimumAverageDifference(vector& nums) { + + int n = nums.size(); + + long long sum = accumulate(nums.begin(), nums.end(), 0ll); + + long long min_diff = LONG_LONG_MAX, pre = 0ll, post = sum; + int res = n - 1; + for(int i = 0; i < n; i ++){ + pre += nums[i]; + post -= nums[i]; + + long long t = abs(pre / (i + 1) - (n - i - 1 == 0 ? 0 : post / (n - i - 1))); +// cout << i << ' ' << t << '\n'; + if(t < min_diff) res = i, min_diff = t; + } + return res; + } +}; + + +int main() { + + vector nums1 = {2, 5, 3, 9, 5, 3}; + cout << Solution().minimumAverageDifference(nums1) << '\n'; + // 3 + + vector nums2 = {4, 2, 0}; + cout << Solution().minimumAverageDifference(nums2) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 6054d744..5df24453 100644 --- a/readme.md +++ b/readme.md @@ -2094,6 +2094,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2253 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2254 | [Design Video Sharing Platform](https://leetcode.com/problems/design-video-sharing-platform/) | [无] | [C++](2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/) | | | | 2255 | [Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/) | [无] | [C++](2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/) | | | +| 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [无] | [C++](2001-2500/2256-Minimum-Average-Difference/cpp-2256/) | | | | | | | | | | | 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [无] | [C++](2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/) | | | | 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [无] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | | From 1c4e9e8e3c443fe3df5f7040bfc254efc5f905f7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 3 May 2022 15:23:17 -0700 Subject: [PATCH 043/390] 2257 solved. --- .../cpp-2257/CMakeLists.txt | 6 ++ .../cpp-2257/main.cpp | 93 +++++++++++++++++++ readme.md | 1 + 3 files changed, 100 insertions(+) create mode 100644 2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/CMakeLists.txt create mode 100644 2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/main.cpp diff --git a/2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/CMakeLists.txt b/2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/CMakeLists.txt new file mode 100644 index 00000000..8d244544 --- /dev/null +++ b/2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2257) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2257 main.cpp) diff --git a/2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/main.cpp b/2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/main.cpp new file mode 100644 index 00000000..c3c63411 --- /dev/null +++ b/2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/main.cpp @@ -0,0 +1,93 @@ +/// Source : https://leetcode.com/problems/count-unguarded-cells-in-the-grid/ +/// Author : liuyubobobo +/// Time : 2022-05-03 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(m * n) +/// Space Complexity: O(m * n) +class Solution { + +private: + const int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + int R, C; + +public: + int countUnguarded(int m, int n, vector>& guards, vector>& walls) { + + R = m, C = n; + + vector g(R, string(C, '.')); + for(const vector& guard: guards) + g[guard[0]][guard[1]] = 'G'; + for(const vector& wall: walls) + g[wall[0]][wall[1]] = 'W'; + + vector>> seen(2, vector>(R, vector(C, false))); + for(int i = 0; i < R; i ++) { + for (int j = 0; j < C; j++) { + + if(g[i][j] != '.'){ + seen[0][i][j] = seen[1][i][j] = true; + } + + if (g[i][j] != 'G') continue; + assert(g[i][j] == 'G'); + for (int d = 0; d < 4; d ++) { + for (int step = 1;; step ++) { + int nx = i + dirs[d][0] * step, ny = j + dirs[d][1] * step; + if (!in_area(nx, ny) || g[nx][ny] != '.' || seen[d / 2][nx][ny]) break; + seen[d / 2][nx][ny] = true; + } + } + } + } + +// for(int i = 0; i < R; i ++) { +// for (int j = 0; j < C; j++) +// cout << seen[0][i][j] << ' '; +// cout << '\n'; +// }cout << '\n'; +// +// for(int i = 0; i < R; i ++) { +// for (int j = 0; j < C; j++) +// cout << seen[1][i][j] << ' '; +// cout << '\n'; +// }cout << '\n'; + +// for(int i = 0; i < R; i ++){ +// for(int j = 0; j < C; j ++) +// cout << (g[i][j] == '.' && !seen[0][i][j] && !seen[1][i][j]) << ' '; +// cout << '\n'; +// } + + int res = 0; + for(int i = 0; i < R; i ++) + for (int j = 0; j < C; j++) + res += (g[i][j] == '.' && !seen[0][i][j] && !seen[1][i][j]); + + return res; + } + +private: + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> guards1 = {{0,0},{1,1},{2,3}}; + vector> walls1 = {{0,1},{2,2},{1,4}}; + cout << Solution().countUnguarded(4, 6, guards1, walls1) << '\n'; + // 7 + + return 0; +} diff --git a/readme.md b/readme.md index 5df24453..b5340826 100644 --- a/readme.md +++ b/readme.md @@ -2095,6 +2095,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2254 | [Design Video Sharing Platform](https://leetcode.com/problems/design-video-sharing-platform/) | [无] | [C++](2001-2500/2254-Design-Video-Sharing-Platform/cpp-2254/) | | | | 2255 | [Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/) | [无] | [C++](2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/) | | | | 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [无] | [C++](2001-2500/2256-Minimum-Average-Difference/cpp-2256/) | | | +| 2257 | [Count Unguarded Cells in the Grid](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) | [无] | [C++](2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/) | | | | | | | | | | | 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [无] | [C++](2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/) | | | | 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [无] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | | From 7475dbae326b846eaf06ae7dfb15e1d540cad922 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 3 May 2022 21:17:54 -0700 Subject: [PATCH 044/390] 2258 solved. --- .../cpp-2258/CMakeLists.txt | 6 + .../cpp-2258/main.cpp | 122 ++++++++++++++++++ readme.md | 2 +- 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/CMakeLists.txt create mode 100644 2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/main.cpp diff --git a/2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/CMakeLists.txt b/2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/CMakeLists.txt new file mode 100644 index 00000000..cc253742 --- /dev/null +++ b/2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2258) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2258 main.cpp) diff --git a/2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/main.cpp b/2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/main.cpp new file mode 100644 index 00000000..80adba3e --- /dev/null +++ b/2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/main.cpp @@ -0,0 +1,122 @@ +/// Source : https://leetcode.com/problems/escape-the-spreading-fire/ +/// Author : liuyubobobo +/// Time : 2022-05-03 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(m * n * log(m * n)) +/// Space Complexity: O(m * n) +class Solution { + +private: + const int dirs[4][2] {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + int R, C; + +public: + int maximumMinutes(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + + vector> fire_time(R, vector(C, 2e9)); + queue q; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(grid[i][j] == 1){ + fire_time[i][j] = 0; + q.push(i * C + j); + } + + while(!q.empty()){ + int cx = q.front() / C, cy = q.front() % C, cur_time = fire_time[cx][cy]; + q.pop(); + + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && grid[nx][ny] != 2 && fire_time[nx][ny] == 2e9){ + fire_time[nx][ny] = cur_time + 1; + q.push(nx * C + ny); + } + } + } + +// for(const vector& row: fire_time){ +// for(int e: row) cout << e << ' '; +// cout << '\n'; +// } + + int l = -1, r = 1e9; + while(l < r){ + int mid = (l + r + 1) / 2; + if(ok(grid, fire_time, mid)) l = mid; + else r = mid - 1; + } + return l; + } + +private: + bool ok(const vector>& grid, const vector>& fire_time, int t){ + + if(grid[0][0] == 2 || t > fire_time[0][0]) return false; + + vector> visited(R, vector(C, -1)); + queue q; + q.push(0); + visited[0][0] = t; + + while(!q.empty()){ + int cx = q.front() / C, cy = q.front() % C, cur_t = visited[cx][cy]; + q.pop(); + + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && grid[nx][ny] != 2 && visited[nx][ny] == -1){ + + if(cur_t + 1 > fire_time[nx][ny]) continue; + if(nx == R - 1 && ny == C - 1) return true; + + if(cur_t + 1 < fire_time[nx][ny]){ + visited[nx][ny] = cur_t + 1; + q.push(nx * C + ny); + } + } + } + } + return false; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> grid1 = { + {0,2,0,0,0,0,0}, + {0,0,0,2,2,1,0}, + {0,2,0,0,1,2,0}, + {0,0,2,2,2,0,2}, + {0,0,0,0,0,0,0} + }; + cout << Solution().maximumMinutes(grid1) << '\n'; + // 3 + + vector> grid2 = { + {0,2,0,0,1}, + {0,2,0,2,2}, + {0,2,0,0,0}, + {0,0,2,2,0}, + {0,0,0,0,0} + }; + cout << Solution().maximumMinutes(grid2) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index b5340826..530a03d6 100644 --- a/readme.md +++ b/readme.md @@ -2096,7 +2096,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2255 | [Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/) | [无] | [C++](2001-2500/2255-Count-Prefixes-of-a-Given-String/cpp-2255/) | | | | 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [无] | [C++](2001-2500/2256-Minimum-Average-Difference/cpp-2256/) | | | | 2257 | [Count Unguarded Cells in the Grid](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) | [无] | [C++](2001-2500/2257-Count-Unguarded-Cells-in-the-Grid/cpp-2257/) | | | -| | | | | | | +| 2258 | [Escape the Spreading Fire](https://leetcode.com/problems/escape-the-spreading-fire/) | [无] | [C++](2001-2500/2258-Escape-the-Spreading-Fire/cpp-2258/) | | | | 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [无] | [C++](2001-2500/2259-Remove-Digit-From-Number-to-Maximize-Result/cpp-2259/) | | | | 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [无] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | | | 2261 | [K Divisible Elements Subarrays](https://leetcode.com/problems/k-divisible-elements-subarrays/) | [无] | [C++](2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/) | | | From b4cc2aa5f1f25821a2c3f162fd764e61ab22dfd4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 6 May 2022 11:08:43 -0700 Subject: [PATCH 045/390] 433 solved. --- .../cpp-0433/CMakeLists.txt | 6 ++ .../cpp-0433/main.cpp | 59 +++++++++++++++++++ readme.md | 4 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/CMakeLists.txt create mode 100644 0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/main.cpp diff --git a/0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/CMakeLists.txt b/0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/CMakeLists.txt new file mode 100644 index 00000000..1429bc97 --- /dev/null +++ b/0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0433) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0433 main.cpp) diff --git a/0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/main.cpp b/0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/main.cpp new file mode 100644 index 00000000..2d1f638c --- /dev/null +++ b/0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/minimum-genetic-mutation/ +/// Author : liuyubobobo +/// Time : 2022-05-06 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(n * |start|) +/// Space Complexity: O(n) +class Solution { + +private: + const vector gene = {'A', 'C', 'G', 'T'}; + +public: + int minMutation(string start, string end, vector& bank) { + + set bank_set(bank.begin(), bank.end()); + map dis; + + queue q; + q.push(start); + dis[start] = 0; + while(!q.empty()){ + string cur = q.front(); + int d = dis[cur]; + q.pop(); + + if(cur == end) return d; + + for(int i = 0; i < cur.size(); i ++) { + char o_gene = cur[i]; + for (int j = 0; j < 4; j++) { + cur[i] = gene[j]; + if(bank_set.count(cur) && !dis.count(cur)){ + dis[cur] = d + 1; + q.push(cur); + } + cur[i] = o_gene; + } + } + + } + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 530a03d6..11d55ebd 100644 --- a/readme.md +++ b/readme.md @@ -465,8 +465,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/) | [无] | [C++](0001-0500/0430-Flatten-a-Multilevel-Doubly-Linked-List/cpp-0430/) | | | | 431 | [Encode N-ary Tree to Binary Tree](https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree/) | [solution](https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree/solution/) | [C++](0001-0500/0431-Encode-N-ary-Tree-to-Binary-Tree/cpp-0431/) | | | | 432 | [All O one Data Structure](https://leetcode.com/problems/all-oone-data-structure/) | [无] [缺:O(1)] | [C++](0001-0500/0432-All-O-one-Data-Structure/cpp-0432/) | | | -| | | | | | | -| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | | [C++](0001-0500/0434-Number-of-Segments-in-a-String/cpp-0434/) | | | +| 433 | [Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/) | [无] | [C++](0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/) | | | +| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [无] | [C++](0001-0500/0434-Number-of-Segments-in-a-String/cpp-0434/) | | | | 435 | [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/description/) | [solution](https://leetcode.com/problems/non-overlapping-intervals/solution/) | [C++](0001-0500/0435-Non-overlapping-Intervals/cpp-0435/) | [Java](0001-0500/0435-Non-overlapping-Intervals/java-0435/src/) | | | | | | | | | | 437 | [Path Sum III](https://leetcode.com/problems/path-sum-iii/description/) | [无] | [C++](0001-0500/0437-Path-Sum-III/cpp-0437/) | [Java](0001-0500/0437-Path-Sum-III/java-0437/src/) | | From ced6dd1127a3611593c08acfaaa25595398a56a4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 7 May 2022 23:00:06 -0700 Subject: [PATCH 046/390] 2264 solved. --- .../cpp-2264/CMakeLists.txt | 6 ++++ .../cpp-2264/main.cpp | 29 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 37 insertions(+) create mode 100644 2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/CMakeLists.txt create mode 100644 2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/main.cpp diff --git a/2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/CMakeLists.txt b/2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/CMakeLists.txt new file mode 100644 index 00000000..7936f0bd --- /dev/null +++ b/2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2264) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2264 main.cpp) diff --git a/2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/main.cpp b/2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/main.cpp new file mode 100644 index 00000000..dcc0ee54 --- /dev/null +++ b/2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/largest-3-same-digit-number-in-string/ +/// Author : liuyubobobo +/// Time : 2022-05-07 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string largestGoodInteger(string num) { + + int res = -1; + for(int i = 2; i < (int)num.size(); i ++) + if(num[i - 2] == num[i - 1] && num[i - 1] == num[i]) + res = max(res, num[i] - '0'); + return res == -1 ? "" : string(3, '0' + res); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 11d55ebd..95f63510 100644 --- a/readme.md +++ b/readme.md @@ -2102,6 +2102,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2261 | [K Divisible Elements Subarrays](https://leetcode.com/problems/k-divisible-elements-subarrays/) | [无] | [C++](2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/) | | | | 2262 | [Total Appeal of A String](https://leetcode.com/problems/total-appeal-of-a-string/) | [无] | [C++](2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/) | | | | | | | | | | +| 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [无] | [C++](2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/) | | | +| | | | | | | ## 力扣中文站比赛 From 956aead0690a31f0c3c483c1c040067e673c450d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 7 May 2022 23:17:14 -0700 Subject: [PATCH 047/390] 2265 solved. --- .../cpp-2265/CMakeLists.txt | 6 ++ .../cpp-2265/main.cpp | 55 +++++++++++++++++++ readme.md | 1 + 3 files changed, 62 insertions(+) create mode 100644 2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/CMakeLists.txt create mode 100644 2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/main.cpp diff --git a/2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/CMakeLists.txt b/2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/CMakeLists.txt new file mode 100644 index 00000000..92334d44 --- /dev/null +++ b/2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2265) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2265 main.cpp) diff --git a/2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/main.cpp b/2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/main.cpp new file mode 100644 index 00000000..ae17774a --- /dev/null +++ b/2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/ +/// Author : liuyubobobo +/// Time : 2022-05-07 + +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(h) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { + +public: + int averageOfSubtree(TreeNode* root) { + + int res = 0; + dfs(root, res); + return res; + } + +private: + // sum, cnt + pair dfs(TreeNode* node, int& res){ + + if(!node) return {0, 0}; + + int sum = node->val, cnt = 1; + pair left_res = dfs(node->left, res); + pair right_res = dfs(node->right, res); + sum += left_res.first + right_res.first; + cnt += left_res.second + right_res.second; + + res += sum / cnt == node->val; + return {sum, cnt}; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 95f63510..db7278c4 100644 --- a/readme.md +++ b/readme.md @@ -2103,6 +2103,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2262 | [Total Appeal of A String](https://leetcode.com/problems/total-appeal-of-a-string/) | [无] | [C++](2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/) | | | | | | | | | | | 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [无] | [C++](2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/) | | | +| 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [无] | [C++](2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/) | | | | | | | | | | ## 力扣中文站比赛 From e1cc664fe64d00bb79228b661db2f6403222046a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 7 May 2022 23:18:29 -0700 Subject: [PATCH 048/390] 2266 solved. --- .../cpp-2266/CMakeLists.txt | 6 +++ .../cpp-2266/main.cpp | 51 +++++++++++++++++++ readme.md | 1 + 3 files changed, 58 insertions(+) create mode 100644 2001-2500/2266-Count-Number-of-Texts/cpp-2266/CMakeLists.txt create mode 100644 2001-2500/2266-Count-Number-of-Texts/cpp-2266/main.cpp diff --git a/2001-2500/2266-Count-Number-of-Texts/cpp-2266/CMakeLists.txt b/2001-2500/2266-Count-Number-of-Texts/cpp-2266/CMakeLists.txt new file mode 100644 index 00000000..b0f928fd --- /dev/null +++ b/2001-2500/2266-Count-Number-of-Texts/cpp-2266/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2266) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2266 main.cpp) diff --git a/2001-2500/2266-Count-Number-of-Texts/cpp-2266/main.cpp b/2001-2500/2266-Count-Number-of-Texts/cpp-2266/main.cpp new file mode 100644 index 00000000..c9d1b633 --- /dev/null +++ b/2001-2500/2266-Count-Number-of-Texts/cpp-2266/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/count-number-of-texts/ +/// Author : liuyubobobo +/// Time : 2022-05-07 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const vector valid = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4}; + const long long MOD = 1e9 + 7; + +public: + int countTexts(string pressedKeys) { + + int n = pressedKeys.size(); + vector dp(n, -1); + return dfs(n, pressedKeys, 0, dp); + } + +private: + long long dfs(int n, const string& s, int index, vector& dp){ + + if(index == n) return 1; + if(dp[index] != -1) return dp[index]; + + long long res = 0; + for(int i = index; i < n && s[i] == s[index] && i - index + 1 <= valid[s[index] - '0']; i ++) + res += dfs(n, s, i + 1, dp), res %= MOD; + return dp[index] = res; + } +}; + + +int main() { + + cout << Solution().countTexts("22233") << '\n'; + // 8 + + cout << Solution().countTexts("222222222222222222222222222222222222") << '\n'; + // 82876089 + + return 0; +} diff --git a/readme.md b/readme.md index db7278c4..8053bdd4 100644 --- a/readme.md +++ b/readme.md @@ -2104,6 +2104,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [无] | [C++](2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/) | | | | 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [无] | [C++](2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/) | | | +| 2266 | [Count Number of Texts](https://leetcode.com/problems/count-number-of-texts/) | [无] | [C++](2001-2500/2266-Count-Number-of-Texts/cpp-2266/) | | | | | | | | | | ## 力扣中文站比赛 From f2dc02214c425854e5a5aed504dce749d6d4614a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 7 May 2022 23:34:48 -0700 Subject: [PATCH 049/390] 2267 solved. --- .../cpp-2267/CMakeLists.txt | 6 ++ .../cpp-2267/main.cpp | 57 +++++++++++++++++++ readme.md | 1 + 3 files changed, 64 insertions(+) create mode 100644 2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/CMakeLists.txt create mode 100644 2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/main.cpp diff --git a/2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/CMakeLists.txt b/2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/CMakeLists.txt new file mode 100644 index 00000000..42565a7a --- /dev/null +++ b/2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2267) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2267 main.cpp) diff --git a/2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/main.cpp b/2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/main.cpp new file mode 100644 index 00000000..4ff5a733 --- /dev/null +++ b/2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/check-if-there-is-a-valid-parentheses-string-path/ +/// Author : liuyubobobo +/// Time : 2022-05-07 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(R * C * (R + C - 1) / 2) +/// Space Complexity: O(R * C * (R + C - 1) / 2) +class Solution { + +private: + int R, C, L; + +public: + bool hasValidPath(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + L = R + C - 1; + if(L % 2) return false; + + vector>> dp(R, vector>(C, vector(L / 2 + 1, -1))); + return dfs(grid, 0, 0, 0, dp); + } + +private: + int dfs(const vector>& g, int x, int y, int value, + vector>>& dp){ + + int next_value = value + (g[x][y] == '(' ? 1 : -1); + if(next_value < 0 || next_value > L / 2) return false; + if(x == R - 1 && y == C - 1) return next_value == 0; + + if(dp[x][y][value] != -1) return dp[x][y][value]; + + int res = 0; + if(in_area(x + 1, y) && dfs(g, x + 1, y, next_value, dp)) + res = 1; + if(!res && in_area(x, y + 1) && dfs(g, x, y + 1, next_value, dp)) + res = 1; + return dp[x][y][value] = res; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 8053bdd4..04efce7e 100644 --- a/readme.md +++ b/readme.md @@ -2105,6 +2105,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [无] | [C++](2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/) | | | | 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [无] | [C++](2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/) | | | | 2266 | [Count Number of Texts](https://leetcode.com/problems/count-number-of-texts/) | [无] | [C++](2001-2500/2266-Count-Number-of-Texts/cpp-2266/) | | | +| 2267 | [Check if There Is a Valid Parentheses String Path](https://leetcode.com/problems/check-if-there-is-a-valid-parentheses-string-path/) | [无] | [C++](2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/) | | | | | | | | | | ## 力扣中文站比赛 From f5c0a0e8bb8a2819f996402dbebc0f5e70241106 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 9 May 2022 12:54:39 -0700 Subject: [PATCH 050/390] 0417 c++ codes added. --- .../cpp-0417/CMakeLists.txt | 6 ++ .../cpp-0417/main.cpp | 63 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt create mode 100644 0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main.cpp diff --git a/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt new file mode 100644 index 00000000..055fb76d --- /dev/null +++ b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0417) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0417 main.cpp) diff --git a/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main.cpp b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main.cpp new file mode 100644 index 00000000..9f399318 --- /dev/null +++ b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/pacific-atlantic-water-flow/ +/// Author : liuyubobobo +/// Time : 2022-05-09 + +#include +#include + +using namespace std; + + +/// DFS from outside to inside +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { + +private: + const int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + int R, C; + +public: + vector> pacificAtlantic(vector>& heights) { + + R = heights.size(), C = heights[0].size(); + + vector> pacific(R, vector(C, false)); + for(int j = 0; j < C; j ++) + if(!pacific[0][j]) dfs(heights, 0, j, pacific); + for(int i = 0; i < R; i ++) + if(!pacific[i][0]) dfs(heights, i, 0, pacific); + + vector> atlantic(R, vector(C, false)); + for(int i = 0; i < R; i ++) + if(!atlantic[i][C - 1]) dfs(heights, i, C - 1, atlantic); + for(int j = 0; j < C; j ++) dfs(heights, R - 1, j, atlantic); + + vector> res; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(pacific[i][j] && atlantic[i][j]) res.push_back({i, j}); + return res; + } + +private: + void dfs(const vector>& H, int x, int y, vector>& visited){ + + visited[x][y] = true; + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny) && !visited[nx][ny] && H[x][y] <= H[nx][ny]) + dfs(H, nx, ny, visited); + } + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 04efce7e..7bb73530 100644 --- a/readme.md +++ b/readme.md @@ -449,7 +449,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [无] | [C++](0001-0500/0414-Third-Maximum-Number/cpp-0414/) | | | | 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [solution](https://leetcode.com/problems/add-strings/solution/) | [C++](0001-0500/0415-Add-Strings/cpp-0415/) | | | | 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/description/) | [无] | [C++](0001-0500/0416-Partition-Equal-Subset-Sum/cpp-0416/) | [Java](0001-0500/0416-Partition-Equal-Subset-Sum/java-0416/src/) | | -| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | [无] | | [Java](0001-0500/0417-Pacific-Atlantic-Water-Flow/java-0417/src/) | | +| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | | [C++](0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/) | [Java](0001-0500/0417-Pacific-Atlantic-Water-Flow/java-0417/src/) | | | | | | | | | | 419 | [Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/) | [无] | [C++](0001-0500/0419-Battleships-in-a-Board/cpp-0419/) | | | | 420 | [Strong Password Checker](https://leetcode.com/problems/strong-password-checker/) | [无] | [C++](0001-0500/0420-Strong-Password-Checker/cpp-0420/) | | | From f2dfd437bdde0708fec30e6461145b777f44eb41 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 10 May 2022 11:55:59 -0700 Subject: [PATCH 051/390] 0449 solved. --- .../cpp-0449/CMakeLists.txt | 6 + .../cpp-0449/main.cpp | 104 ++++++++++++++++++ readme.md | 2 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/CMakeLists.txt create mode 100644 0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/main.cpp diff --git a/0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/CMakeLists.txt b/0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/CMakeLists.txt new file mode 100644 index 00000000..62164d21 --- /dev/null +++ b/0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0449) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0449 main.cpp) diff --git a/0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/main.cpp b/0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/main.cpp new file mode 100644 index 00000000..3023e519 --- /dev/null +++ b/0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/main.cpp @@ -0,0 +1,104 @@ +/// Source : https://leetcode.com/problems/serialize-and-deserialize-bst/ +/// Author : liuyubobobo +/// Time : 2022-05-10 + +#include +#include + +using namespace std; + + +/// Same as serialize and deserialize binary tree +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +class Codec { +public: + + // Encodes a tree to a single string. + string serialize(TreeNode* root) { + + if(!root) + return "[null]"; + + string ret = "["; + dfs(root, ret); + ret.pop_back(); + return ret + "]"; + } + + // Decodes your encoded data to tree. + TreeNode* deserialize(string data) { + + vector vec = get_vector(data); + + if(vec.size() == 0 || (vec.size() == 1 && vec[0] == "null")) + return NULL; + +// for(const string& s: vec) +// cout << s << " "; +// cout << endl; + + int index = 0; + return dfs(vec, index); + } + +private: + TreeNode* dfs(const vector& vec, int& index){ + + if(index == vec.size() || vec[index] == "null") + return NULL; + + TreeNode* node = new TreeNode(atoi(vec[index].c_str())); + index ++; + node->left = dfs(vec, index); + + index ++; + node->right = dfs(vec, index); + + return node; + } + + void dfs(TreeNode* node, string& ret){ + + ret += to_string(node->val) + ","; + + if(node->left) + dfs(node->left, ret); + else + ret += "null,"; + + if(node->right) + dfs(node->right, ret); + else + ret += "null,"; + } + + vector get_vector(const string& data){ + + string s = data.substr(1, data.size() - 2) + ","; + + vector res; + int i = 0; + while(i < s.size()){ + int comma = s.find(',', i); + res.push_back(s.substr(i, comma - i)); + i = comma + 1; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 7bb73530..27d276e3 100644 --- a/readme.md +++ b/readme.md @@ -481,7 +481,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 446 | [Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/) | [solution](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/solution/) | [C++](0001-0500/0446-Arithmetic-Slices-II-Subsequence/cpp-0446/) | | | | 447 | [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/description/) | [无] | [C++](0001-0500/0447-Number-of-Boomerangs/cpp-0447/) | [Java](0001-0500/0447-Number-of-Boomerangs/java-0447/src/) | | | 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [solution](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/solution/) | [C++](0001-0500/0448-Find-All-Numbers-Disappeared-in-an-Array/cpp-0448/) | | | -| | | | | | | +| 449 | [Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/) | [solution](https://leetcode.com/problems/serialize-and-deserialize-bst/solution/) | [C++](0001-0500/0449-Serialize-and-Deserialize-BST/cpp-0449/) | | | | 450 | [Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/) | [solution](https://leetcode.com/problems/delete-node-in-a-bst/solution/) | [C++](0001-0500/0450-Delete-Node-in-a-BST/cpp-0450/) | | | | 451 | [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/description/) | [无] | [C++](0001-0500/0451-Sort-Characters-By-Frequency/cpp-0451/) | [Java](0001-0500/0451-Sort-Characters-By-Frequency/java-0451/) | | | 452 | [Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | [solution](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/solution/) | [C++](0001-0500/0452-Minimum-Number-of-Arrows-to-Burst-Balloons/cpp-0452/) | | | From 23f0293d177738b7932c06a050640d0d3fae5597 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 11 May 2022 18:05:42 -0700 Subject: [PATCH 052/390] 2268 solved. --- .../cpp-2268/CMakeLists.txt | 6 ++++ .../cpp-2268/main.cpp | 34 +++++++++++++++++++ readme.md | 1 + 3 files changed, 41 insertions(+) create mode 100644 2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/CMakeLists.txt create mode 100644 2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/main.cpp diff --git a/2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/CMakeLists.txt b/2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/CMakeLists.txt new file mode 100644 index 00000000..59e7012e --- /dev/null +++ b/2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2268) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2268 main.cpp) diff --git a/2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/main.cpp b/2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/main.cpp new file mode 100644 index 00000000..73c007e0 --- /dev/null +++ b/2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-keypresses/ +/// Author : liuyubobobo +/// Time : 2022-05-11 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumKeypresses(string s) { + + vector f(26, 0); + for(char c: s) f[c - 'a'] ++; + + sort(f.begin(), f.end(), greater()); + + int res = 0; + for(int i = 0; i < 26; i ++) + res += f[i] * (i / 9 + 1); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 27d276e3..d716bb20 100644 --- a/readme.md +++ b/readme.md @@ -2106,6 +2106,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [无] | [C++](2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/) | | | | 2266 | [Count Number of Texts](https://leetcode.com/problems/count-number-of-texts/) | [无] | [C++](2001-2500/2266-Count-Number-of-Texts/cpp-2266/) | | | | 2267 | [Check if There Is a Valid Parentheses String Path](https://leetcode.com/problems/check-if-there-is-a-valid-parentheses-string-path/) | [无] | [C++](2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/) | | | +| 2268 | [Minimum Number of Keypresses](https://leetcode.com/problems/minimum-number-of-keypresses/) | [无] | [C++](2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/) | | | | | | | | | | ## 力扣中文站比赛 From dbfc115324ec0b895c83761aea964a87b7dfd2f1 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 12 May 2022 09:30:05 -0700 Subject: [PATCH 053/390] Cracking-The-Coding-Interview 01-05 solved. --- .../01-05/cpp-01-05/CMakeLists.txt | 6 +++ .../01-05/cpp-01-05/main.cpp | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 Cracking-The-Coding-Interview/01-05/cpp-01-05/CMakeLists.txt create mode 100644 Cracking-The-Coding-Interview/01-05/cpp-01-05/main.cpp diff --git a/Cracking-The-Coding-Interview/01-05/cpp-01-05/CMakeLists.txt b/Cracking-The-Coding-Interview/01-05/cpp-01-05/CMakeLists.txt new file mode 100644 index 00000000..5741689d --- /dev/null +++ b/Cracking-The-Coding-Interview/01-05/cpp-01-05/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_01_05) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_01_05 main.cpp) diff --git a/Cracking-The-Coding-Interview/01-05/cpp-01-05/main.cpp b/Cracking-The-Coding-Interview/01-05/cpp-01-05/main.cpp new file mode 100644 index 00000000..dbdace88 --- /dev/null +++ b/Cracking-The-Coding-Interview/01-05/cpp-01-05/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.cn/problems/one-away-lcci/ +/// Author : liuyubobobo +/// Time : 2022-05-12 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + bool oneEditAway(string first, string second) { + + int n1 = first.size(), n2 = second.size(); + if(n1 == n2){ + + int change = 0; + for(int i = 0; i < n1; i ++) + change += (first[i] != second[i]); + return change <= 1; + } + + if(abs(n1 - n2) > 1) return false; + + if(n1 > n2) swap(n1, n2), swap(first, second); + + // n1 < n2; + for(int len = 0; len <= n1; len ++) + if(first.substr(0, len) == second.substr(0, len) && first.substr(len) == second.substr(len + 1)) + return true; + return false; + } +}; + + +int main() { + + return 0; +} From fe1aecbb784133171325eec5c8fa6be8255337b3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 14 May 2022 10:19:59 -0700 Subject: [PATCH 054/390] 2269 solved. --- .../cpp-2269/CMakeLists.txt | 6 ++++ .../cpp-2269/main.cpp | 31 +++++++++++++++++++ readme.md | 1 + 3 files changed, 38 insertions(+) create mode 100644 2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/CMakeLists.txt create mode 100644 2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/main.cpp diff --git a/2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/CMakeLists.txt b/2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/CMakeLists.txt new file mode 100644 index 00000000..433dcb8e --- /dev/null +++ b/2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2269) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2269 main.cpp) diff --git a/2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/main.cpp b/2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/main.cpp new file mode 100644 index 00000000..10225951 --- /dev/null +++ b/2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/find-the-k-beauty-of-a-number/ +/// Author : liuyubobobo +/// Time : 2022-05-14 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n * k) +/// Space Complexity: O(k) +class Solution { +public: + int divisorSubstrings(int num, int k) { + + string num_s = to_string(num); + int res = 0; + for(int i = 0; i + k - 1 < num_s.size(); i ++){ + int x = atoi(num_s.substr(i, k).c_str()); + if(x) res += num % x == 0; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d716bb20..f7c03f2a 100644 --- a/readme.md +++ b/readme.md @@ -2107,6 +2107,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2266 | [Count Number of Texts](https://leetcode.com/problems/count-number-of-texts/) | [无] | [C++](2001-2500/2266-Count-Number-of-Texts/cpp-2266/) | | | | 2267 | [Check if There Is a Valid Parentheses String Path](https://leetcode.com/problems/check-if-there-is-a-valid-parentheses-string-path/) | [无] | [C++](2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/) | | | | 2268 | [Minimum Number of Keypresses](https://leetcode.com/problems/minimum-number-of-keypresses/) | [无] | [C++](2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/) | | | +| 2269 | [Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | [无] | [C++](2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/) | | | | | | | | | | ## 力扣中文站比赛 From b6c098ff5c12e1c992a353a7d535aa97e0c8d2a1 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 14 May 2022 10:38:16 -0700 Subject: [PATCH 055/390] 2270 solved. --- .../cpp-2270/CMakeLists.txt | 6 ++++ .../cpp-2270/main.cpp | 36 +++++++++++++++++++ readme.md | 1 + 3 files changed, 43 insertions(+) create mode 100644 2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/CMakeLists.txt create mode 100644 2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/main.cpp diff --git a/2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/CMakeLists.txt b/2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/CMakeLists.txt new file mode 100644 index 00000000..bb0f0e81 --- /dev/null +++ b/2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2270) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2270 main.cpp) diff --git a/2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/main.cpp b/2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/main.cpp new file mode 100644 index 00000000..b913cf5f --- /dev/null +++ b/2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-split-array/ +/// Author : liuyubobobo +/// Time : 2022-05-14 + +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int waysToSplitArray(vector& nums) { + + int n = nums.size(); + + vector presum(n + 1, 0ll); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + nums[i]; + + int res = 0; + for(int len = 1; len < n; len ++){ + int l1 = 0, r1 = l1 + len - 1, l2 = l1 + len, r2 = n - 1; + res += presum[r1 + 1] - presum[l1] >= presum[r2 + 1] - presum[l2]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index f7c03f2a..07084330 100644 --- a/readme.md +++ b/readme.md @@ -2108,6 +2108,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2267 | [Check if There Is a Valid Parentheses String Path](https://leetcode.com/problems/check-if-there-is-a-valid-parentheses-string-path/) | [无] | [C++](2001-2500/2267-Check-if-There-Is-a-Valid-Parentheses-String-Path/cpp-2267/) | | | | 2268 | [Minimum Number of Keypresses](https://leetcode.com/problems/minimum-number-of-keypresses/) | [无] | [C++](2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/) | | | | 2269 | [Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | [无] | [C++](2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/) | | | +| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [无] | [C++](2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/) | | | | | | | | | | ## 力扣中文站比赛 From 2d2a33780fdbcdb76613b1062c1bc1c0c92d8557 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 14 May 2022 12:52:18 -0700 Subject: [PATCH 056/390] 2271 solved. --- .../cpp-2271/CMakeLists.txt | 6 ++ .../cpp-2271/main.cpp | 76 +++++++++++++++++++ readme.md | 1 + 3 files changed, 83 insertions(+) create mode 100644 2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/CMakeLists.txt create mode 100644 2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/main.cpp diff --git a/2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/CMakeLists.txt b/2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/CMakeLists.txt new file mode 100644 index 00000000..e0f69af1 --- /dev/null +++ b/2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2271) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2271 main.cpp) diff --git a/2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/main.cpp b/2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/main.cpp new file mode 100644 index 00000000..dd5a3816 --- /dev/null +++ b/2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/main.cpp @@ -0,0 +1,76 @@ +/// Source : https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/ +/// Author : liuyubobobo +/// Time : 2022-05-14 + +#include +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maximumWhiteTiles(vector>& tiles, int carpetLen) { + + int n = tiles.size(); + + vector> data(n); + for(int i = 0; i < n; i ++) data[i] = {tiles[i][0] - 1, tiles[i][1] - 1}; + sort(data.begin(), data.end()); + int res1 = get_res(n, data, carpetLen); + + int max_len = data.back().second + 1; + + for(pair& p: data) + p = {max_len - p.second, max_len - p.first}; + sort(data.begin(), data.end()); + int res2 = get_res(n, data, carpetLen); + + return max(res1, res2); + } + +private: + int get_res(int n, const vector>& data, int len){ + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) + presum[i + 1] = presum[i] + (data[i].second - data[i].first + 1); + + int res = 0; + for(int i = 0; i < n; i ++){ + auto iter = lower_bound(data.begin() + i, data.end(), make_pair(data[i].first + len, -1)); + iter --; + int j = iter - data.begin(); + + assert(i <= j); + + int tres = 0; + if(i <= j - 1) tres += presum[j] - presum[i]; + + int pre = data[j].first - data[i].first; + tres += min(len - pre, data[j].second - data[j].first + 1); + + res = max(res, tres); + } + return res; + } +}; + + +int main() { + + vector> tiles1 = {{1,5},{10,11},{12,18},{20,25},{30,32}}; + cout << Solution().maximumWhiteTiles(tiles1, 10) << '\n'; + // 9 + + vector> tiles2 = {{10,11},{1, 1}}; + cout << Solution().maximumWhiteTiles(tiles2, 2) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 07084330..0dc8c512 100644 --- a/readme.md +++ b/readme.md @@ -2109,6 +2109,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2268 | [Minimum Number of Keypresses](https://leetcode.com/problems/minimum-number-of-keypresses/) | [无] | [C++](2001-2500/2268-Minimum-Number-of-Keypresses/cpp-2268/) | | | | 2269 | [Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | [无] | [C++](2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/) | | | | 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [无] | [C++](2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/) | | | +| 2271 | [Maximum White Tiles Covered by a Carpet](https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/) | [无] | [C++](2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/) | | | | | | | | | | ## 力扣中文站比赛 From 45205a904d57a06b02ac6c1a7d1db716111a8e6f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 15 May 2022 01:40:23 -0700 Subject: [PATCH 057/390] 2273-2276 added. --- .../cpp-2273/CMakeLists.txt | 6 ++ .../cpp-2273/main.cpp | 36 ++++++++ .../cpp-2274/CMakeLists.txt | 6 ++ .../cpp-2274/main.cpp | 35 ++++++++ .../cpp-2275/CMakeLists.txt | 6 ++ .../cpp-2275/main.cpp | 51 ++++++++++++ .../cpp-2276/CMakeLists.txt | 6 ++ .../cpp-2276/main.cpp | 82 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 233 insertions(+) create mode 100644 2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/CMakeLists.txt create mode 100644 2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/main.cpp create mode 100644 2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/CMakeLists.txt create mode 100644 2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/main.cpp create mode 100644 2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/CMakeLists.txt create mode 100644 2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/main.cpp create mode 100644 2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/CMakeLists.txt create mode 100644 2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/main.cpp diff --git a/2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/CMakeLists.txt b/2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/main.cpp b/2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/main.cpp new file mode 100644 index 00000000..b97120d3 --- /dev/null +++ b/2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/ +/// Author : liuyubobobo +/// Time : 2022-05-15 + +#include +#include + +using namespace std; + + +/// Stack +/// Time Complexity: O(n * |word|) +/// Space Complexity: O(1) +class Solution { +public: + vector removeAnagrams(vector& words) { + + vector res; + for(const string& word: words) + if(res.empty() || !same(word, res.back())) res.push_back(word); + return res; + } + +private: + bool same(string a, string b){ + sort(a.begin(), a.end()); + sort(b.begin(), b.end()); + return a == b; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/CMakeLists.txt b/2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/main.cpp b/2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/main.cpp new file mode 100644 index 00000000..73744e11 --- /dev/null +++ b/2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/ +/// Author : liuyubobobo +/// Time : 2022-05-15 + +#include +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int maxConsecutive(int bottom, int top, vector& special) { + + special.push_back(top + 1); + sort(special.begin(), special.end()); + + int res = 0; + for(int cur = bottom, i = 0; i < special.size(); i ++){ + res = max(res, special[i] - cur); + cur = special[i] + 1; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/CMakeLists.txt b/2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/main.cpp b/2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/main.cpp new file mode 100644 index 00000000..c2cdeb0e --- /dev/null +++ b/2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/ +/// Author : liuyubobobo +/// Time : 2022-05-15 + +#include +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(nlogn(MAX_E)) +/// Space Complexity: O(1) +class Solution { +public: + int largestCombination(vector& candidates) { + + vector f(31, 0); + for(int e: candidates) calc(f, e); + + return *max_element(f.begin(), f.end()); + } + +private: + void calc(vector& f, int x){ + + int p = 0; + while(x){ + if(x & 1) f[p] ++; + x >>= 1, p ++; + } + } +}; + + +int main() { + + vector candidates1 = {16,17,71,62,12,24,14}; + cout << Solution().largestCombination(candidates1) << '\n'; + // 4 + + vector candidates2 = {8, 8}; + cout << Solution().largestCombination(candidates2) << '\n'; + // 2 + + vector candidates3 = {33,93,31,99,74,37,3,4,2,94,77,10,75,54,24,95,65,100,41,82,35,65,38,49,85,72,67,21,20,31}; + cout << Solution().largestCombination(candidates3) << '\n'; + // 18 + + return 0; +} diff --git a/2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/CMakeLists.txt b/2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/main.cpp b/2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/main.cpp new file mode 100644 index 00000000..3e66d2ac --- /dev/null +++ b/2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/main.cpp @@ -0,0 +1,82 @@ +/// Source : https://leetcode.com/problems/count-integers-in-intervals/ +/// Author : liuyubobobo +/// Time : 2022-05-15 + +#include +#include + +using namespace std; + + +/// Using Set +/// Time Complexity: init: O(1) +/// add: O(logn) in everage +/// count: O(1) +/// Space Complexity: O(n) +class CountIntervals { + +private: + set> intervals; + int num = 0; + +public: + CountIntervals() { } + + void add(int left, int right) { + + auto iter = intervals.lower_bound({left, left - 1}); + if(iter != intervals.begin()){ + iter --; + if(intersection(left, right, iter->first, iter->second)){ + left = min(left, iter->first); + right = max(right, iter->second); + + num -= (iter->second - iter->first + 1); + intervals.erase(iter); + } + } + + while(true) { + auto iter = intervals.lower_bound({left, left - 1}); + if(iter == intervals.end()) break; + + if(intersection(left, right, iter->first, iter->second)){ + left = min(left, iter->first); + right = max(right, iter->second); + + num -= (iter->second - iter->first + 1); + intervals.erase(iter); + } + else break; + } + + intervals.insert({left, right}); + num += right - left + 1; + } + + int count() { + return num; + } + +private: + bool intersection(int l1, int r1, int l2, int r2){ + if(l1 > l2) swap(l1, l2), swap(r1, r2); + return l2 <= r1; + } +}; + + +int main() { + + CountIntervals obj; + obj.add(2, 3); + cout << obj.count() << '\n'; // 2 + + obj.add(7, 10); + cout << obj.count() << '\n'; // 6 + + obj.add(5, 8); + cout << obj.count() << '\n'; // 8 + + return 0; +} diff --git a/readme.md b/readme.md index 0dc8c512..4bb82846 100644 --- a/readme.md +++ b/readme.md @@ -2111,6 +2111,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [无] | [C++](2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/) | | | | 2271 | [Maximum White Tiles Covered by a Carpet](https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/) | [无] | [C++](2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/) | | | | | | | | | | +| 2273 | [Find Resultant Array After Removing Anagrams](https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/) | [无] | [C++](2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/) | | | +| 2274 | [Maximum Consecutive Floors Without Special Floors](https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/) | [无] | [C++](2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/) | | | +| 2275 | [Largest Combination With Bitwise AND Greater Than Zero](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [无] | [C++](2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/) | | | +| 2276 | [Count Integers in Intervals](https://leetcode.com/problems/count-integers-in-intervals/) | [无] | [C++](2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/) | | | +| | | | | | | ## 力扣中文站比赛 From 2ba14f15fe33be195b82a3f14531073dcf59c3c7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 15 May 2022 10:09:24 -0700 Subject: [PATCH 058/390] Cracking-The-Coding-Interview 04-06 solved. --- .../04-06/cpp-04-06/CMakeLists.txt | 6 ++ .../04-06/cpp-04-06/main.cpp | 55 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Cracking-The-Coding-Interview/04-06/cpp-04-06/CMakeLists.txt create mode 100644 Cracking-The-Coding-Interview/04-06/cpp-04-06/main.cpp diff --git a/Cracking-The-Coding-Interview/04-06/cpp-04-06/CMakeLists.txt b/Cracking-The-Coding-Interview/04-06/cpp-04-06/CMakeLists.txt new file mode 100644 index 00000000..b344d38e --- /dev/null +++ b/Cracking-The-Coding-Interview/04-06/cpp-04-06/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_04_06) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_04_06 main.cpp) diff --git a/Cracking-The-Coding-Interview/04-06/cpp-04-06/main.cpp b/Cracking-The-Coding-Interview/04-06/cpp-04-06/main.cpp new file mode 100644 index 00000000..21a82de1 --- /dev/null +++ b/Cracking-The-Coding-Interview/04-06/cpp-04-06/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.cn/problems/successor-lcci/ +/// Author : liuyubobobo +/// Time : 2022-05-15 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +class Solution { +public: + TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { + + bool next = false; + return dfs(root, p, next); + } + +private: + TreeNode* dfs(TreeNode* node, TreeNode* target, bool& next){ + + if(node->left){ + TreeNode* ret = dfs(node->left, target, next); + if(ret != nullptr) return ret; + } + + if(next) return node; + if(node == target) next = true; + + if(node->right){ + TreeNode* ret = dfs(node->right, target, next); + if(ret != nullptr) return ret; + } + + return nullptr; + } +}; + + +int main() { + + return 0; +} From 730dc124ca0aeaa9e76a4e4be1d4e8033f71c74c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 19 May 2022 10:06:53 -0700 Subject: [PATCH 059/390] 0436 solved. --- .../cpp-0436/CMakeLists.txt | 6 +++ .../cpp-0436/main.cpp | 48 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0436-Find-Right-Interval/cpp-0436/CMakeLists.txt create mode 100644 0001-0500/0436-Find-Right-Interval/cpp-0436/main.cpp diff --git a/0001-0500/0436-Find-Right-Interval/cpp-0436/CMakeLists.txt b/0001-0500/0436-Find-Right-Interval/cpp-0436/CMakeLists.txt new file mode 100644 index 00000000..adbc37ea --- /dev/null +++ b/0001-0500/0436-Find-Right-Interval/cpp-0436/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0436) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0436 main.cpp) diff --git a/0001-0500/0436-Find-Right-Interval/cpp-0436/main.cpp b/0001-0500/0436-Find-Right-Interval/cpp-0436/main.cpp new file mode 100644 index 00000000..ae275895 --- /dev/null +++ b/0001-0500/0436-Find-Right-Interval/cpp-0436/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/find-right-interval/ +/// Author : liuyubobobo +/// Time : 2022-05-19 + +#include +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector findRightInterval(vector>& intervals) { + + int n = intervals.size(); + vector, int>> data(n); + for(int i = 0; i < n; i ++) + data[i] = {{intervals[i][0], intervals[i][1]}, i}; + sort(data.begin(), data.end()); + + vector res(n, -1); + for(int i = 0; i < n; i ++){ + int start = data[i].first.first, end = data[i].first.second, index = data[i].second; + auto iter = lower_bound(data.begin(), data.end(), make_pair(make_pair(end, INT_MIN), INT_MIN)); + if(iter != data.end()) res[index] = iter->second; + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector> intervals1 = {{3, 4}, {2, 3}, {1, 2}}; + print_vec(Solution().findRightInterval(intervals1)); + // -1 0 1 + + return 0; +} diff --git a/readme.md b/readme.md index 4bb82846..86d03428 100644 --- a/readme.md +++ b/readme.md @@ -468,7 +468,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 433 | [Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/) | [无] | [C++](0001-0500/0433-Minimum-Genetic-Mutation/cpp-0433/) | | | | 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [无] | [C++](0001-0500/0434-Number-of-Segments-in-a-String/cpp-0434/) | | | | 435 | [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/description/) | [solution](https://leetcode.com/problems/non-overlapping-intervals/solution/) | [C++](0001-0500/0435-Non-overlapping-Intervals/cpp-0435/) | [Java](0001-0500/0435-Non-overlapping-Intervals/java-0435/src/) | | -| | | | | | | +| 436 | [Find Right Interval](https://leetcode.com/problems/find-right-interval/) | [无] | [C++](0001-0500/0436-Find-Right-Interval/cpp-0436/) | | | | 437 | [Path Sum III](https://leetcode.com/problems/path-sum-iii/description/) | [无] | [C++](0001-0500/0437-Path-Sum-III/cpp-0437/) | [Java](0001-0500/0437-Path-Sum-III/java-0437/src/) | | | 438 | [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/description/) | [无] | [C++](0001-0500/0438-Find-All-Anagrams-in-a-String/cpp-0438/) | | | | 439 | [Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/) | [无] | [C++](0001-0500/0439-Ternary-Expression-Parser/cpp-0439/) | | | From b71975c218320cb7aa5d714ba01a93b332da1603 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 20 May 2022 21:39:14 -0700 Subject: [PATCH 060/390] 2277 solved. --- .../cpp-2277/CMakeLists.txt | 6 ++ .../cpp-2277/main.cpp | 96 +++++++++++++++++++ readme.md | 1 + 3 files changed, 103 insertions(+) create mode 100644 2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/CMakeLists.txt create mode 100644 2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/main.cpp diff --git a/2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/CMakeLists.txt b/2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/CMakeLists.txt new file mode 100644 index 00000000..c8cc37a6 --- /dev/null +++ b/2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2277) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2277 main.cpp) diff --git a/2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/main.cpp b/2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/main.cpp new file mode 100644 index 00000000..e7656ed9 --- /dev/null +++ b/2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/main.cpp @@ -0,0 +1,96 @@ +/// Source : https://leetcode.com/problems/closest-node-to-path-in-tree/ +/// Author : liuyubobobo +/// Time : 2022-05-20 + +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(q * n * logn) +/// Space Complexity: O(n) +class Solution { +public: + vector closestNode(int n, vector>& edges, vector>& query) { + + vector> g(n); + for(const vector& edge: edges){ + int u = edge[0], v = edge[1]; + g[u].push_back(v), g[v].push_back(u); + } + + vector res; + for(const vector& q: query){ + int start = q[0], end = q[1], node = q[2]; + + set path = get_path(n, g, start, end); + res.push_back(get_res(n, g, node, path)); + } + return res; + } + +private: + int get_res(int n, const vector>& g, int s, const set& t_set){ + + if(t_set.size() == 1) return *t_set.begin(); + + vector visited(n, false); + queue q; + q.push(s); + while(!q.empty()){ + int u = q.front(); q.pop(); + if(t_set.count(u)) return u; + + for(int v: g[u]) + if(!visited[v]){ + visited[v] = true; + q.push(v); + } + } + assert(false); + return -1; + } + + set get_path(int n, const vector>& g, int s, int t){ + + if(s == t) return {s}; + + vector pre(n, -1); + queue q; + q.push(s); + pre[s] = s; + while(!q.empty()){ + int u = q.front(); q.pop(); + if(u == t) break; + + for(int v: g[u]) + if(pre[v] == -1){ + pre[v] = u; + q.push(v); + } + } + + set path; + int cur = t; + while(pre[cur] != cur){ + path.insert(cur); + cur = pre[cur]; + } + path.insert(cur); + + return path; + } +}; + + +int main() { + + + return 0; +} diff --git a/readme.md b/readme.md index 86d03428..52e46abf 100644 --- a/readme.md +++ b/readme.md @@ -2115,6 +2115,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2274 | [Maximum Consecutive Floors Without Special Floors](https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/) | [无] | [C++](2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/) | | | | 2275 | [Largest Combination With Bitwise AND Greater Than Zero](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [无] | [C++](2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/) | | | | 2276 | [Count Integers in Intervals](https://leetcode.com/problems/count-integers-in-intervals/) | [无] | [C++](2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/) | | | +| 2277 | [Closest Node to Path in Tree](https://leetcode.com/problems/closest-node-to-path-in-tree/) | [无] | [C++](2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/) | | | | | | | | | | ## 力扣中文站比赛 From 78fba0add4e8ee70b61fb8749544cfac763e6f50 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 21 May 2022 21:33:32 -0700 Subject: [PATCH 061/390] 2278 - 2281 solved. --- .../cpp-2278/CMakeLists.txt | 6 + .../cpp-2278/main.cpp | 28 ++++ .../cpp-2279/CMakeLists.txt | 6 + .../cpp-2279/main.cpp | 40 +++++ .../cpp-2280/CMakeLists.txt | 6 + .../cpp-2280/main.cpp | 49 ++++++ .../cpp-2281/CMakeLists.txt | 6 + .../cpp-2281/main.cpp | 142 ++++++++++++++++++ readme.md | 4 + 9 files changed, 287 insertions(+) create mode 100644 2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/CMakeLists.txt create mode 100644 2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/main.cpp create mode 100644 2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/CMakeLists.txt create mode 100644 2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/main.cpp create mode 100644 2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/CMakeLists.txt create mode 100644 2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/main.cpp create mode 100644 2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/CMakeLists.txt create mode 100644 2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/main.cpp diff --git a/2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/CMakeLists.txt b/2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/main.cpp b/2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/main.cpp new file mode 100644 index 00000000..46806ede --- /dev/null +++ b/2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/main.cpp @@ -0,0 +1,28 @@ +/// Source : https://leetcode.com/problems/percentage-of-letter-in-string/ +/// Author : liuyubobobo +/// Time : 2022-05-21 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int percentageLetter(string s, char letter) { + + int k = 0; + for(char c: s) k += c == letter; + + return k * 100 / s.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/CMakeLists.txt b/2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/main.cpp b/2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/main.cpp new file mode 100644 index 00000000..1608687c --- /dev/null +++ b/2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/ +/// Author : liuyubobobo +/// Time : 2022-05-21 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int maximumBags(vector& capacity, vector& rocks, int additionalRocks) { + + int n = capacity.size(); + for(int i = 0; i < n; i ++) + capacity[i] -= rocks[i]; + + sort(capacity.begin(), capacity.end()); + for(int i = 0; i < n; i ++) + if(capacity[i] <= additionalRocks){ + additionalRocks -= capacity[i]; + capacity[i] = 0; + } + + int res = 0; + for(int e: capacity) res += e == 0; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/CMakeLists.txt b/2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/main.cpp b/2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/main.cpp new file mode 100644 index 00000000..6855a1de --- /dev/null +++ b/2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/ +/// Author : liuyubobobo +/// Time : 2022-05-21 + +#include +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int minimumLines(vector>& stockPrices) { + + sort(stockPrices.begin(), stockPrices.end()); + + int n = stockPrices.size(); + + vector> k; + for(int i = 1; i < n; i ++){ + int x0 = stockPrices[i - 1][0], y0 = stockPrices[i - 1][1]; + int x1 = stockPrices[i][0], y1 = stockPrices[i][1]; + int a = y1 - y0, b = x1 - x0; + + int g = gcd(min(abs(a), abs(b)), max(abs(a), abs(b))); + a /= g, b /= g; + + pair p = {a, b}; + if(k.empty() || k.back() != p) k.push_back(p); + } + return k.size(); + } + +private: + int gcd(int a, int b){ + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/CMakeLists.txt b/2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/main.cpp b/2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/main.cpp new file mode 100644 index 00000000..20cfdbb5 --- /dev/null +++ b/2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/main.cpp @@ -0,0 +1,142 @@ +/// Source : https://leetcode.com/problems/sum-of-total-strength-of-wizards/ +/// Author : liuyubobobo +/// Time : 2022-05-21 + +#include +#include + +using namespace std; + + +/// Counting, Segment Tree + Presum +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class SegmentTree{ + +private: + int n; + vector data, tree; + +public: + SegmentTree(const vector& data): n(data.size()), data(data), tree(4 * n, 0){ + buildSegTree(0, 0, n - 1); + } + + T query(int l, int r){ +// assert(l <= r); +// assert(0 <= l && l < n); +// assert(0 <= r && r < n); + return query(0, 0, n - 1, l, r); + } + +private: + void buildSegTree(int treeID, int l, int r){ + + if(l == r){ + tree[treeID] = l; + return; + } + + int mid = (l + r) / 2; + buildSegTree(treeID * 2 + 1, l, mid); + buildSegTree(treeID * 2 + 2, mid + 1, r); + tree[treeID] = data[tree[treeID * 2 + 1]] < data[tree[treeID * 2 + 2]] ? tree[treeID * 2 + 1] : tree[treeID * 2 + 2]; + return; + } + + T query(int treeID, int l, int r, int ql, int qr){ + + if(ql == l && qr == r) + return tree[treeID]; + + int mid = (l + r) / 2; + if(qr <= mid) return query(treeID * 2 + 1, l, mid, ql, qr); + else if(ql > mid) return query(treeID * 2 + 2, mid + 1, r, ql, qr); + + T resl = query(treeID * 2 + 1, l, mid, ql, mid); + T resr = query(treeID * 2 + 2, mid + 1, r, mid + 1, qr); + return data[resl] < data[resr] ? resl : resr; + } +}; + +class Solution { + +private: + const long long MOD = 1e9 + 7; + vector presum, presum_sum, sufsum_sum; + SegmentTree *seg_tree; + +public: + int totalStrength(vector& strength) { + + int n = strength.size(); + + presum.assign(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + strength[i]; + + presum_sum.assign(n + 2, 0); + for(int i = 0; i < n; i ++) + presum_sum[i + 1] = ((long long)strength[i] * (i + 1) % MOD + presum_sum[i] ) % MOD; + + sufsum_sum.assign(n + 2, 0); + for(int i = n - 1; i >= 0; i --) + sufsum_sum[i + 1] = ((long long)strength[i] * (n - i) % MOD + sufsum_sum[i + 2]) % MOD; + + seg_tree = new SegmentTree(strength); + return dfs(n, strength, 0, n - 1); + } + +private: + long long dfs(int n, const vector& v, int l, int r){ + + if(l > r) return 0; + if(l == r) return (long long)v[l] * v[l] % MOD; + + int min_index = seg_tree->query(l, r); + long long sum = get_total_sum(n, l, r, min_index); + + long long res = sum * v[min_index] % MOD; + res = (res + dfs(n, v, l, min_index - 1)) % MOD; + res = (res + dfs(n, v, min_index + 1, r)) % MOD; + return res; + } + + long long get_total_sum(int n, int l, int r, int k){ + + long long total_left = (presum_sum[k + 1] - presum_sum[l] + MOD) % MOD; + total_left -= (presum[k + 1] - presum[l] + MOD) % MOD * l % MOD; + total_left = (total_left + MOD) % MOD; + + if(k < r){ + long long additional_right = (sufsum_sum[k + 2] - sufsum_sum[r + 2] + MOD) % MOD; + additional_right -= (presum[r + 1] - presum[k + 1] + MOD) % MOD * (n - r - 1) % MOD; + additional_right = (additional_right + MOD) % MOD; + + long long left_cnt = k - l + 1; + long long right_cnt = r - k; + + additional_right = additional_right * left_cnt % MOD; + + total_left = total_left * (right_cnt + 1) % MOD; + total_left += additional_right; + total_left %= MOD; + } + + return total_left; + } +}; + + +int main() { + + vector strength1 = {1, 3, 1, 2}; + cout << Solution().totalStrength(strength1) << '\n'; + // 44 + + vector strength2 = {5, 4, 6}; + cout << Solution().totalStrength(strength2) << '\n'; + // 213 + + return 0; +} diff --git a/readme.md b/readme.md index 52e46abf..392c299c 100644 --- a/readme.md +++ b/readme.md @@ -2116,6 +2116,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2275 | [Largest Combination With Bitwise AND Greater Than Zero](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [无] | [C++](2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/) | | | | 2276 | [Count Integers in Intervals](https://leetcode.com/problems/count-integers-in-intervals/) | [无] | [C++](2001-2500/2276-Count-Integers-in-Intervals/cpp-2276/) | | | | 2277 | [Closest Node to Path in Tree](https://leetcode.com/problems/closest-node-to-path-in-tree/) | [无] | [C++](2001-2500/2277-Closest-Node-to-Path-in-Tree/cpp-2277/) | | | +| 2278 | [Percentage of Letter in String](https://leetcode.com/problems/percentage-of-letter-in-string/) | [无] | [C++](2001-2500/2278-Percentage-of-Letter-in-String/cpp-2278/) | | | +| 2279 | [Maximum Bags With Full Capacity of Rocks](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/) | [无] | [C++](2001-2500/2279-Maximum-Bags-With-Full-Capacity-of-Rocks/cpp-2279/) | | | +| 2280 | [Minimum Lines to Represent a Line Chart](https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/) | [无] | [C++](2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/) | | | +| 2281 | [Sum of Total Strength of Wizards](https://leetcode.com/problems/sum-of-total-strength-of-wizards/) | [无] | [C++](2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/) | | | | | | | | | | ## 力扣中文站比赛 From 0577dd3f7b499e29ce9db347e815c67ef18a9e0d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 24 May 2022 11:56:43 -0700 Subject: [PATCH 062/390] 0467 solved. --- .../cpp-0467/CMakeLists.txt | 6 +++ .../cpp-0467/main.cpp | 46 +++++++++++++++++++ readme.md | 4 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/CMakeLists.txt create mode 100644 0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/main.cpp diff --git a/0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/CMakeLists.txt b/0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/CMakeLists.txt new file mode 100644 index 00000000..d1f5331f --- /dev/null +++ b/0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0467) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0467 main.cpp) diff --git a/0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/main.cpp b/0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/main.cpp new file mode 100644 index 00000000..c0070e26 --- /dev/null +++ b/0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/unique-substrings-in-wraparound-string/ +/// Author : liuyubobobo +/// Time : 2022-05-24 + +#include +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int findSubstringInWraproundString(string p) { + + vector len(26, 0); + for(int start = 0, i = 1; i <= p.size(); i ++) + if(i == p.size() || ((p[start] - 'a') + (i - start)) % 26 != (p[i] - 'a')){ + + int l = i - start; + for(int j = start; j < i; j ++) + len[p[j] - 'a'] = max(len[p[j] - 'a'], l --); + start = i; + } + + return accumulate(len.begin(), len.end(), 0); + } +}; + + +int main() { + + cout << Solution().findSubstringInWraproundString("a") << '\n'; + // 1 + + cout << Solution().findSubstringInWraproundString("cac") << '\n'; + // 2 + + cout << Solution().findSubstringInWraproundString("zab") << '\n'; + // 6 + + return 0; +} diff --git a/readme.md b/readme.md index 392c299c..6ea12e0d 100644 --- a/readme.md +++ b/readme.md @@ -496,6 +496,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [solution](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/solution/) | [C++](0001-0500/0462-Minimum-Moves-to-Equal-Array-Elements-II/cpp-0462/) | | | | 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [solution](https://leetcode.com/problems/island-perimeter/solution/) | [C++](0001-0500/0463-Island-Perimeter/cpp-0463/) | | | | | | | | | | +| 467 | [Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/) | [无] | [C++](0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/) | | | +| | | | | | | | 470 | [Implement Rand10() Using Rand7()](https://leetcode.com/problems/implement-rand10-using-rand7/description/) | [solution](https://leetcode.com/problems/implement-rand10-using-rand7/solution/) | [C++](0001-0500/0470-Implement-Rand10-Using-Rand7/cpp-0470/) | | | | | | | | | | | 472 | [Concatenated Words](https://leetcode.com/problems/concatenated-words/) | [无] | [C++](0001-0500/0472-Matchsticks-to-Square/cpp-0472/) | | | @@ -691,7 +693,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 688 | [Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/) | [solution](https://leetcode.com/problems/knight-probability-in-chessboard/solution/) | [C++](0501-1000/0688-Knight-Probability-in-Chessboard/cpp-0688/) | | | | 689 | [Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/) | [solution](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/solution/) | [C++](0501-1000/0689-Maximum-Sum-of-3-Non-Overlapping-Subarrays/cpp-0689/)| | | | 690 | [Employee Importance](https://leetcode.com/problems/employee-importance/description/) | [solution](https://leetcode.com/problems/employee-importance/solution/) | [C++](0501-1000/0690-Employee-Importance/cpp-0690/) | | | -| | | | | | | +| 691 | [Stickers to Spell Word](https://leetcode.com/problems/stickers-to-spell-word/) | [无] | [C++](0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/) | | | | 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/description/) | [solution](https://leetcode.com/problems/top-k-frequent-words/solution/) | [C++](0501-1000/0692-Top-K-Frequent-Words/cpp-0692/) | | | | 693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/) | [无] | [C++](0501-1000/0693-Binary-Number-with-Alternating-Bits/cpp-0693/) | | | | 694 | [Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/description/) | [review: hash的方式] | [C++](0501-1000/0694-Number-of-Distinct-Islands/cpp-0694/) | | | From 42e382ef72b096d85d443ea5946e133b347e801a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 24 May 2022 11:57:05 -0700 Subject: [PATCH 063/390] 0691 solved. --- .../cpp-0691/CMakeLists.txt | 6 ++ .../cpp-0691/main.cpp | 69 +++++++++++++++++ .../cpp-0691/main2.cpp | 74 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt create mode 100644 0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp create mode 100644 0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp diff --git a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt new file mode 100644 index 00000000..16664f11 --- /dev/null +++ b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0691) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0691 main2.cpp) diff --git a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp new file mode 100644 index 00000000..129bf5f0 --- /dev/null +++ b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include + +using namespace std; + + +class Solution { +public: + int minStickers(vector& stickers, string target) { + + int n = stickers.size(); + + vector> f(n, vector(26, 0)); + for(int i = 0; i < n; i ++) + for(char c: stickers[i]) f[i][c - 'a'] ++; + + vector tf(26, 0); + for(char c: target) tf[c - 'a'] ++; + + map>, int> dp; + int res = dfs(n, f, 0, tf, dp); + return res >= INT_MAX / 2 ? -1 : res; + } + +private: + int dfs(int n, const vector>& f, int index, vector tf, + map>, int>& dp){ + + if(index == n) + return accumulate(tf.begin(), tf.end(), 0) == 0 ? 0 : INT_MAX / 2; + + pair> state = {index, tf}; + auto iter = dp.find(state); + if(iter != dp.end()) return iter->second; + + int res = dfs(n, f, index + 1, tf, dp); + + int max_need = 0; + for(int i = 0; i < 26; i ++) + if(tf[i] && f[index][i]) + max_need = max(max_need, tf[i] / f[index][i] + !!(tf[i] % f[index][i])); + + for(int k = 1; k <= max_need; k ++){ + for(int i = 0; i < 26; i ++) + tf[i] = max(0, tf[i] - f[index][i]); + res = min(res, k + dfs(n, f, index + 1, tf, dp)); + } + return dp[state] = res; + } +}; + + +int main() { + + vector stickers1 = {"with","example","science"}; + string target1 = "thehat"; + cout << Solution().minStickers(stickers1, target1) << '\n'; + // 3 + + vector stickers2 = {"notice","possible"}; + string target2 = "basicbasic"; + cout << Solution().minStickers(stickers2, target2) << '\n'; + // -1 + + return 0; +} diff --git a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp new file mode 100644 index 00000000..66a2eb5f --- /dev/null +++ b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/stickers-to-spell-word/ +/// Author : liuyubobobo +/// Time : 2022-05-24 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(C(26, 15)?) +/// Space Complexity: O(C(26, 15)?) +class Solution { +public: + int minStickers(vector& stickers, string target) { + + int n = stickers.size(); + + vector> f(n, vector(26, 0)); + for(int i = 0; i < n; i ++) + for(char c: stickers[i]) f[i][c - 'a'] ++; + + vector tf(26, 0); + for(char c: target) tf[c - 'a'] ++; + + map, int> dp; + int res = dfs(n, f, tf, dp); + return res >= INT_MAX / 2 ? -1 : res; + } + +private: + int dfs(int n, const vector>& f, const vector& tf, + map, int>& dp){ + + if(accumulate(tf.begin(), tf.end(), 0) == 0) + return 0; + + auto iter = dp.find(tf); + if(iter != dp.end()) return iter->second; + + int first_need = 0; + for(;first_need < 26 && tf[first_need] == 0; first_need ++); + + int res = INT_MAX / 2; + for(int index = 0; index < n; index ++) + if(f[index][first_need]){ + vector next_f = tf; + for(int i = 0; i < 26; i ++) + next_f[i] = max(next_f[i] - f[index][i], 0); + res = min(res, 1 + dfs(n, f, next_f, dp)); + } + return dp[tf] = res; + } +}; + + +int main() { + + vector stickers1 = {"with","example","science"}; + string target1 = "thehat"; + cout << Solution().minStickers(stickers1, target1) << '\n'; + // 3 + + vector stickers2 = {"notice","possible"}; + string target2 = "basicbasic"; + cout << Solution().minStickers(stickers2, target2) << '\n'; + // -1 + + return 0; +} From 7ee4e59c99a85b03a2d8587bcd9c7ed04a7bf1d9 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 24 May 2022 11:57:57 -0700 Subject: [PATCH 064/390] 0691 solved. --- .../cpp-0691/CMakeLists.txt | 2 +- .../cpp-0691/main.cpp | 47 ++++++------ .../cpp-0691/main2.cpp | 74 ------------------- 3 files changed, 27 insertions(+), 96 deletions(-) delete mode 100644 0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp diff --git a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt index 16664f11..fb9992e5 100644 --- a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt +++ b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0691) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_0691 main2.cpp) +add_executable(cpp_0691 main.cpp) diff --git a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp index 129bf5f0..66a2eb5f 100644 --- a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp +++ b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.cpp @@ -1,3 +1,7 @@ +/// Source : https://leetcode.com/problems/stickers-to-spell-word/ +/// Author : liuyubobobo +/// Time : 2022-05-24 + #include #include #include @@ -7,6 +11,9 @@ using namespace std; +/// Memoization +/// Time Complexity: O(C(26, 15)?) +/// Space Complexity: O(C(26, 15)?) class Solution { public: int minStickers(vector& stickers, string target) { @@ -20,35 +27,33 @@ class Solution { vector tf(26, 0); for(char c: target) tf[c - 'a'] ++; - map>, int> dp; - int res = dfs(n, f, 0, tf, dp); + map, int> dp; + int res = dfs(n, f, tf, dp); return res >= INT_MAX / 2 ? -1 : res; } private: - int dfs(int n, const vector>& f, int index, vector tf, - map>, int>& dp){ + int dfs(int n, const vector>& f, const vector& tf, + map, int>& dp){ - if(index == n) - return accumulate(tf.begin(), tf.end(), 0) == 0 ? 0 : INT_MAX / 2; + if(accumulate(tf.begin(), tf.end(), 0) == 0) + return 0; - pair> state = {index, tf}; - auto iter = dp.find(state); + auto iter = dp.find(tf); if(iter != dp.end()) return iter->second; - int res = dfs(n, f, index + 1, tf, dp); - - int max_need = 0; - for(int i = 0; i < 26; i ++) - if(tf[i] && f[index][i]) - max_need = max(max_need, tf[i] / f[index][i] + !!(tf[i] % f[index][i])); - - for(int k = 1; k <= max_need; k ++){ - for(int i = 0; i < 26; i ++) - tf[i] = max(0, tf[i] - f[index][i]); - res = min(res, k + dfs(n, f, index + 1, tf, dp)); - } - return dp[state] = res; + int first_need = 0; + for(;first_need < 26 && tf[first_need] == 0; first_need ++); + + int res = INT_MAX / 2; + for(int index = 0; index < n; index ++) + if(f[index][first_need]){ + vector next_f = tf; + for(int i = 0; i < 26; i ++) + next_f[i] = max(next_f[i] - f[index][i], 0); + res = min(res, 1 + dfs(n, f, next_f, dp)); + } + return dp[tf] = res; } }; diff --git a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp deleted file mode 100644 index 66a2eb5f..00000000 --- a/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main2.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/// Source : https://leetcode.com/problems/stickers-to-spell-word/ -/// Author : liuyubobobo -/// Time : 2022-05-24 - -#include -#include -#include -#include -#include - -using namespace std; - - -/// Memoization -/// Time Complexity: O(C(26, 15)?) -/// Space Complexity: O(C(26, 15)?) -class Solution { -public: - int minStickers(vector& stickers, string target) { - - int n = stickers.size(); - - vector> f(n, vector(26, 0)); - for(int i = 0; i < n; i ++) - for(char c: stickers[i]) f[i][c - 'a'] ++; - - vector tf(26, 0); - for(char c: target) tf[c - 'a'] ++; - - map, int> dp; - int res = dfs(n, f, tf, dp); - return res >= INT_MAX / 2 ? -1 : res; - } - -private: - int dfs(int n, const vector>& f, const vector& tf, - map, int>& dp){ - - if(accumulate(tf.begin(), tf.end(), 0) == 0) - return 0; - - auto iter = dp.find(tf); - if(iter != dp.end()) return iter->second; - - int first_need = 0; - for(;first_need < 26 && tf[first_need] == 0; first_need ++); - - int res = INT_MAX / 2; - for(int index = 0; index < n; index ++) - if(f[index][first_need]){ - vector next_f = tf; - for(int i = 0; i < 26; i ++) - next_f[i] = max(next_f[i] - f[index][i], 0); - res = min(res, 1 + dfs(n, f, next_f, dp)); - } - return dp[tf] = res; - } -}; - - -int main() { - - vector stickers1 = {"with","example","science"}; - string target1 = "thehat"; - cout << Solution().minStickers(stickers1, target1) << '\n'; - // 3 - - vector stickers2 = {"notice","possible"}; - string target2 = "basicbasic"; - cout << Solution().minStickers(stickers2, target2) << '\n'; - // -1 - - return 0; -} From 37b95fe0038a246964302cef91afb588189d7453 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 24 May 2022 13:30:40 -0700 Subject: [PATCH 065/390] 0464 solved. --- .../0464-Can-I-Win/cpp-0464/CMakeLists.txt | 6 ++ 0001-0500/0464-Can-I-Win/cpp-0464/main.cpp | 87 +++++++++++++++++++ readme.md | 1 + 3 files changed, 94 insertions(+) create mode 100644 0001-0500/0464-Can-I-Win/cpp-0464/CMakeLists.txt create mode 100644 0001-0500/0464-Can-I-Win/cpp-0464/main.cpp diff --git a/0001-0500/0464-Can-I-Win/cpp-0464/CMakeLists.txt b/0001-0500/0464-Can-I-Win/cpp-0464/CMakeLists.txt new file mode 100644 index 00000000..2af1584f --- /dev/null +++ b/0001-0500/0464-Can-I-Win/cpp-0464/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0464) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0464 main.cpp) diff --git a/0001-0500/0464-Can-I-Win/cpp-0464/main.cpp b/0001-0500/0464-Can-I-Win/cpp-0464/main.cpp new file mode 100644 index 00000000..91a2d2be --- /dev/null +++ b/0001-0500/0464-Can-I-Win/cpp-0464/main.cpp @@ -0,0 +1,87 @@ +/// Source : https://leetcode.com/problems/can-i-win/ +/// Author : liuyubobobo +/// Time : 2022-05-24 + +#include +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(2^M * M * desiredTotal) +/// Space Complexity: O(2^M * desiredTotal) +class Solution { +public: + bool canIWin(int maxChoosableInteger, int desiredTotal) { + + if(desiredTotal == 0) return true; + + int total = (maxChoosableInteger + 1) * maxChoosableInteger / 2; + + // -1: not calculated + // 0: can not get + // 1: lose + // 2: win + unordered_map dp; + return win(desiredTotal, (1 << maxChoosableInteger) - 1, maxChoosableInteger, total, dp) == 2; + } + +private: + int win(int need, int state, int M, int total, unordered_map& dp){ + + if(need <= 0) return 1; + if(total < need) return 0; + + long long hash = (1ll << M) * need + state; + auto iter = dp.find(hash); + if(iter != dp.end()) return iter->second; + + int w = 0, all = 0; + for(int i = M; i >= 1; i --) + if((state & (1 << (i - 1)))){ + all ++; + int tres = win(need - i, state - (1 << (i - 1)), M, total - i, dp); + if(tres == 1) return dp[hash] = 2; + + w += (tres == 2); + } + + return dp[hash] = (all && w == all); + } +}; + + +int main() { + + cout << Solution().canIWin(10, 11) << '\n'; + // 0 + + cout << Solution().canIWin(10, 0) << '\n'; + // 1 + + cout << Solution().canIWin(10, 1) << '\n'; + // 1 + + cout << Solution().canIWin(5, 50) << '\n'; + // 0 + + cout << Solution().canIWin(20, 209) << '\n'; + // 0 + + cout << Solution().canIWin(20, 300) << '\n'; + // 0 + + cout << Solution().canIWin(12, 49) << '\n'; + // 1 + + cout << Solution().canIWin(6, 16) << '\n'; + // 1 + + cout << Solution().canIWin(20, 152) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index 6ea12e0d..767b3924 100644 --- a/readme.md +++ b/readme.md @@ -495,6 +495,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [solution](https://leetcode.com/problems/hamming-distance/solution/) | [C++](0001-0500/0461-Hamming-Distance/cpp-0461/) | | | | 462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [solution](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/solution/) | [C++](0001-0500/0462-Minimum-Moves-to-Equal-Array-Elements-II/cpp-0462/) | | | | 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [solution](https://leetcode.com/problems/island-perimeter/solution/) | [C++](0001-0500/0463-Island-Perimeter/cpp-0463/) | | | +| 464 | [Can I Win](https://leetcode.com/problems/can-i-win/) | [无] | [C++](0001-0500/0464-Can-I-Win/cpp-0464/) | | | | | | | | | | | 467 | [Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/) | [无] | [C++](0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/) | | | | | | | | | | From a0f02459398d88ff507774dd6a00049c24ab4965 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 24 May 2022 14:15:50 -0700 Subject: [PATCH 066/390] 0456 new algo updated. --- .../0456-132-Pattern/cpp-0456/CMakeLists.txt | 2 +- 0001-0500/0456-132-Pattern/cpp-0456/main.cpp | 45 ++++++++-------- 0001-0500/0456-132-Pattern/cpp-0456/main2.cpp | 51 ------------------- readme.md | 2 +- 4 files changed, 27 insertions(+), 73 deletions(-) delete mode 100644 0001-0500/0456-132-Pattern/cpp-0456/main2.cpp diff --git a/0001-0500/0456-132-Pattern/cpp-0456/CMakeLists.txt b/0001-0500/0456-132-Pattern/cpp-0456/CMakeLists.txt index 932473d7..4b38ad49 100644 --- a/0001-0500/0456-132-Pattern/cpp-0456/CMakeLists.txt +++ b/0001-0500/0456-132-Pattern/cpp-0456/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0456) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_0456 main2.cpp) \ No newline at end of file +add_executable(cpp_0456 main.cpp) \ No newline at end of file diff --git a/0001-0500/0456-132-Pattern/cpp-0456/main.cpp b/0001-0500/0456-132-Pattern/cpp-0456/main.cpp index 30667507..69be15bf 100644 --- a/0001-0500/0456-132-Pattern/cpp-0456/main.cpp +++ b/0001-0500/0456-132-Pattern/cpp-0456/main.cpp @@ -1,40 +1,37 @@ /// Source : https://leetcode.com/problems/132-pattern/ /// Author : liuyubobobo /// Time : 2021-03-23 +/// Updated: 2022-05-24 #include #include -#include +#include using namespace std; -/// Brute Force to check the middle -/// Time Complexity: O(n^2) -/// Space Complexity: O(1) +/// Reverse to find 32-pattern first and greedily check 1-part +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) class Solution { public: bool find132pattern(vector& nums) { int n = nums.size(); - for(int i = 0; i < n; i ++){ + if(n <= 2) return false; - int leftmin = INT_MAX; - for(int j = 0; j < i; j ++) - if(nums[j] < nums[i] && nums[j] < leftmin) - leftmin = nums[j]; + set> s; + s.insert(nums[n - 1]); + s.insert(nums[n - 2]); + int t = nums[n - 2] > nums[n - 1] ? nums[n - 1] : INT_MIN; - if(leftmin == INT_MAX) continue; + for(int i = n - 3; i >= 0; i --){ + if(nums[i] < t) return true; - int rightmax = INT_MIN; - for(int j = i + 1; j < n; j ++) - if(nums[j] < nums[i] && nums[j] > rightmax) - rightmax = nums[j]; - - if(rightmax == INT_MIN) continue; - - if(leftmin < rightmax) - return true; + auto iter = s.upper_bound(nums[i]); + if(iter != s.end()) + t = max(t, *iter); + s.insert(nums[i]); } return false; @@ -44,9 +41,17 @@ class Solution { int main() { - vector nums1 = {1, 0, 1, -4, -3}; + vector nums1 = {1, 2, 3, 4}; cout << Solution().find132pattern(nums1) << endl; // 0 + vector nums2 = {1, 0, 1, -4, -3}; + cout << Solution().find132pattern(nums2) << endl; + // 0 + + vector nums3 = {1, 3, 2, 4, 5}; + cout << Solution().find132pattern(nums3) << endl; + // 1 + return 0; } diff --git a/0001-0500/0456-132-Pattern/cpp-0456/main2.cpp b/0001-0500/0456-132-Pattern/cpp-0456/main2.cpp deleted file mode 100644 index 514d5d3b..00000000 --- a/0001-0500/0456-132-Pattern/cpp-0456/main2.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/// Source : https://leetcode.com/problems/132-pattern/ -/// Author : liuyubobobo -/// Time : 2021-03-23 - -#include -#include -#include - -using namespace std; - - -/// Better Brute Force -/// Record min value during the scan -/// Time Complexity: O(n^2) -/// Space Complexity: O(1) -class Solution { -public: - bool find132pattern(vector& nums) { - - int n = nums.size(), leftmin = INT_MAX; - for(int i = 0; i < n; i ++){ - - if(nums[i] > leftmin){ - - int rightmax = INT_MIN; - for(int j = i + 1; j < n; j ++) - if(nums[j] < nums[i] && nums[j] > rightmax) - rightmax = nums[j]; - - if(rightmax == INT_MIN) continue; - - if(leftmin < rightmax) - return true; - } - - leftmin = min(leftmin, nums[i]); - } - - return false; - } -}; - - -int main() { - - vector nums1 = {1, 0, 1, -4, -3}; - cout << Solution().find132pattern(nums1) << endl; - // 0 - - return 0; -} diff --git a/readme.md b/readme.md index 767b3924..05060268 100644 --- a/readme.md +++ b/readme.md @@ -488,7 +488,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 453 | [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) | [solution](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/solution/) | [C++](0001-0500/0453-Minimum-Moves-to-Equal-Array-Elements/cpp-0453/) | | | | 454 | [4Sum II](https://leetcode.com/problems/4sum-ii/description/) | [无] | [C++](0001-0500/0454-4Sum-II/cpp-0454/) | [Java](0001-0500/0454-4Sum-II/java-0454/src/) | | | 455 | [Assign Cookies](https://leetcode.com/problems/assign-cookies/description/) | [无] | [C++](0001-0500/0455-Assign-Cookies/cpp-0455/) | [Java](0001-0500/0455-Assign-Cookies/java-0455/src/) | | -| 456 | [132 Pattern](https://leetcode.com/problems/132-pattern/) | [solution](https://leetcode.com/problems/132-pattern/solution/)
[缺:O(n), O(nlogn) 算法] | [C++](0001-0500/0456-132-Pattern/cpp-0456/) | | | +| 456 | [132 Pattern](https://leetcode.com/problems/132-pattern/) | [solution](https://leetcode.com/problems/132-pattern/solution/)
[缺:O(n) 单调栈算法] | [C++](0001-0500/0456-132-Pattern/cpp-0456/) | | | | 457 | [Circular Array Loop](https://leetcode.com/problems/circular-array-loop/) | [无] | [C++](0001-0500/0457-Circular-Array-Loop/cpp-0457/) | | | | 458 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | [solution](https://leetcode.com/problems/poor-pigs/solution/) | [C++](0001-0500/0458-Poor-Pigs/) | | | | | | | | | | From d6c45d70512e7a6f86ea0bdce59f449344118617 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 24 May 2022 14:54:37 -0700 Subject: [PATCH 067/390] 0489 solved. --- .../cpp-0489/CMakeLists.txt | 6 ++ .../0489-Robot-Room-Cleaner/cpp-0489/main.cpp | 81 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0489-Robot-Room-Cleaner/cpp-0489/CMakeLists.txt create mode 100644 0001-0500/0489-Robot-Room-Cleaner/cpp-0489/main.cpp diff --git a/0001-0500/0489-Robot-Room-Cleaner/cpp-0489/CMakeLists.txt b/0001-0500/0489-Robot-Room-Cleaner/cpp-0489/CMakeLists.txt new file mode 100644 index 00000000..b754329a --- /dev/null +++ b/0001-0500/0489-Robot-Room-Cleaner/cpp-0489/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0489) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0489 main.cpp) diff --git a/0001-0500/0489-Robot-Room-Cleaner/cpp-0489/main.cpp b/0001-0500/0489-Robot-Room-Cleaner/cpp-0489/main.cpp new file mode 100644 index 00000000..8856f127 --- /dev/null +++ b/0001-0500/0489-Robot-Room-Cleaner/cpp-0489/main.cpp @@ -0,0 +1,81 @@ +/// Source : https://leetcode.com/problems/robot-room-cleaner/ +/// Author : liuyubobobo +/// Time : 2022-05-24 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(m * n) +/// Space Complexity: O(m * n) + +/// This is the robot's control interface. +/// You should not implement it, or speculate about its implementation +class Robot { +public: + // Returns true if the cell in front is open and robot moves into the cell. + // Returns false if the cell in front is blocked and robot stays in the current cell. + bool move(); + + // Robot will stay in the same cell after calling turnLeft/turnRight. + // Each turn will be 90 degrees. + void turnLeft(); + void turnRight(); + + // Clean the current cell. + void clean(); +}; + +class Solution { + +private: + set> visited; + const int dirs[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + +public: + void cleanRoom(Robot& robot) { + + int d = 0; + dfs(robot, 0, 0, d, visited); + } + +private: + void dfs(Robot& robot, int x, int y, int& d, set>& visited){ + + robot.clean(); + visited.insert({x, y}); + + robot.turnLeft(); + d = (d - 1 + 4) % 4; + + int L = ((x == 0 && y == 0) ? 4 : 3); + for(int i = 0; i < L; i ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(visited.count({nx, ny})){ + robot.turnRight(); + d = (d + 1) % 4; + } + else if(!robot.move()){ + robot.turnRight(); + d = (d + 1) % 4; + } + else{ + dfs(robot, nx, ny, d, visited); + robot.turnLeft(); + d = (d - 1 + 4) % 4; + } + } + + robot.move(); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 05060268..605cc04f 100644 --- a/readme.md +++ b/readme.md @@ -517,7 +517,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/description/) | [无] | [C++](0001-0500/0485-Max-Consecutive-Ones/cpp-0485/) | | | | | | | | | | | 488 | [Zuma Game](https://leetcode.com/problems/zuma-game/) | [无] | [C++](0001-0500/0488-Zuma-Game/cpp-0488/) | | | -| | | | | | | +| 489 | [Robot Room Cleaner](https://leetcode.com/problems/robot-room-cleaner/) | [solution](https://leetcode.com/problems/robot-room-cleaner/solution/) | [C++](0001-0500/0489-Robot-Room-Cleaner/cpp-0489/) | | | | 490 | [The Maze](https://leetcode.com/problems/the-maze/description/) | [solution](https://leetcode.com/problems/the-maze/solution/) | [C++](0001-0500/0490-The-Maze/cpp-0490/) | | | | | | | | | | | 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/) | [无] | [C++](0001-0500/0492-Construct-the-Rectangle/cpp-0492/) | | | From f7ddf274a3d228ebdb273f750c7b33be63b17b7f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 25 May 2022 09:31:27 -0700 Subject: [PATCH 068/390] 0699 codes updated. --- .../0699-Falling-Squares/cpp-0699/main.cpp | 39 ++++++++---------- .../0699-Falling-Squares/cpp-0699/main2.cpp | 40 ++++++++----------- 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/0501-1000/0699-Falling-Squares/cpp-0699/main.cpp b/0501-1000/0699-Falling-Squares/cpp-0699/main.cpp index b2ec486e..562c5929 100644 --- a/0501-1000/0699-Falling-Squares/cpp-0699/main.cpp +++ b/0501-1000/0699-Falling-Squares/cpp-0699/main.cpp @@ -1,27 +1,29 @@ /// Source : https://leetcode.com/contest/leetcode-weekly-contest-54/problems/falling-squares/ /// Author : liuyubobobo /// Time : 2017-10-21 +/// Updated: 2022-05-25 #include #include using namespace std; + /// Using heights to record all the falling squares /// Time Complexity: O(len(position)^2) /// Space Complexity: O(len(position)) class Solution { public: - vector fallingSquares(vector>& positions) { + vector fallingSquares(vector>& positions) { int n = positions.size(); vector heights(n, 0); for(int i = 0 ; i < positions.size() ; i ++){ - heights[i] = positions[i].second; + heights[i] = positions[i][1]; for(int j = 0 ; j < i ; j ++) if(intersection(positions[j], positions[i])) - heights[i] = max(heights[i], heights[j] + positions[i].second); + heights[i] = max(heights[i], heights[j] + positions[i][1]); } vector res(n, 0); @@ -33,11 +35,11 @@ class Solution { } private: - bool intersection(const pair& a, const pair& b){ - int l1 = a.first; - int r1 = a.first + a.second - 1; - int l2 = b.first; - int r2 = b.first + b.second - 1; + bool intersection(const vector& a, const vector& b){ + int l1 = a[0]; + int r1 = a[0] + a[1] - 1; + int l2 = b[0]; + int r2 = b[0] + b[1] - 1; if(l1 > r2 || l2 > r1) return false; @@ -47,28 +49,19 @@ class Solution { }; -void printVec(const vector& vec){ - - for(int i = 0 ; i < vec.size() ; i ++) - cout << vec[i] << ((i == vec.size() - 1) ? '\n' : ' '); +void print_vec(const vector& vec){ + for(int e: vec) cout << e << ' '; cout << '\n'; } - int main() { - vector> va; - va.push_back(make_pair(1, 2)); - va.push_back(make_pair(2, 3)); - va.push_back(make_pair(6, 1)); + vector> va = {{1, 2}, {2, 3}, {6, 1}}; vector res1 = Solution().fallingSquares(va); - printVec(res1); - + print_vec(res1); - vector> vb; - vb.push_back(make_pair(100, 100)); - vb.push_back(make_pair(200, 100)); + vector> vb = {{100, 100}, {200, 100}}; vector res2 = Solution().fallingSquares(vb); - printVec(res2); + print_vec(res2); return 0; } \ No newline at end of file diff --git a/0501-1000/0699-Falling-Squares/cpp-0699/main2.cpp b/0501-1000/0699-Falling-Squares/cpp-0699/main2.cpp index 184f9edd..66fef0c0 100644 --- a/0501-1000/0699-Falling-Squares/cpp-0699/main2.cpp +++ b/0501-1000/0699-Falling-Squares/cpp-0699/main2.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/contest/leetcode-weekly-contest-54/problems/falling-squares/ /// Author : liuyubobobo /// Time : 2017-10-28 +/// Uopdted: 2022-05-25 #include #include @@ -10,19 +11,20 @@ using namespace std; + /// Coordinates compression and simulation /// Time Complexity: O(len(position)^2) /// Space Complexity: O(len(position)) class Solution { public: - vector fallingSquares(vector>& positions) { + vector fallingSquares(vector>& positions) { int n = positions.size(); set unique_pos; - for(pair position: positions){ - unique_pos.insert(position.first); - unique_pos.insert(position.first + position.second - 1); + for(const vector& position: positions){ + unique_pos.insert(position[0]); + unique_pos.insert(position[0] + position[1] - 1); } map indexes; // pos -> index @@ -35,17 +37,17 @@ class Solution { assert(indexes.size() == pos.size()); vector heights(indexes.size(), 0); vector res; - for(pair position: positions){ + for(const vector& position: positions){ - int startIndex = indexes[position.first]; - int rightBound = position.first + position.second - 1; + int startIndex = indexes[position[0]]; + int rightBound = position[0] + position[1] - 1; int best = 0; for(int i = startIndex ; i < pos.size() && pos[i] <= rightBound ; i ++) best = max(best, heights[i]); for(int i = startIndex ; i < pos.size() && pos[i] <= rightBound ; i ++) - heights[i] = best + position.second; + heights[i] = best + position[1]; best = 0; for(int i = 0 ; i < heights.size() ; i ++) @@ -56,32 +58,22 @@ class Solution { return res; } - }; -void printVec(const vector& vec){ - - for(int i = 0 ; i < vec.size() ; i ++) - cout << vec[i] << ((i == vec.size() - 1) ? '\n' : ' '); +void print_vec(const vector& vec){ + for(int e: vec) cout << e << ' '; cout << '\n'; } - int main() { - vector> va; - va.push_back(make_pair(1, 2)); - va.push_back(make_pair(2, 3)); - va.push_back(make_pair(6, 1)); + vector> va = {{1, 2}, {2, 3}, {6, 1}}; vector res1 = Solution().fallingSquares(va); - printVec(res1); - + print_vec(res1); - vector> vb; - vb.push_back(make_pair(100, 100)); - vb.push_back(make_pair(200, 100)); + vector> vb = {{100, 100}, {200, 100}}; vector res2 = Solution().fallingSquares(vb); - printVec(res2); + print_vec(res2); return 0; } \ No newline at end of file From 4888c49c580bd9ffbf8c7846348ebafa7d14c2a0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 26 May 2022 10:01:09 -0700 Subject: [PATCH 069/390] Cracking The Coding Interview 17-11 solved. --- .../17-11/cpp-17-11/CMakeLists.txt | 6 +++ .../17-11/cpp-17-11/main.cpp | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Cracking-The-Coding-Interview/17-11/cpp-17-11/CMakeLists.txt create mode 100644 Cracking-The-Coding-Interview/17-11/cpp-17-11/main.cpp diff --git a/Cracking-The-Coding-Interview/17-11/cpp-17-11/CMakeLists.txt b/Cracking-The-Coding-Interview/17-11/cpp-17-11/CMakeLists.txt new file mode 100644 index 00000000..7be0d539 --- /dev/null +++ b/Cracking-The-Coding-Interview/17-11/cpp-17-11/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_17_11) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_17_11 main.cpp) diff --git a/Cracking-The-Coding-Interview/17-11/cpp-17-11/main.cpp b/Cracking-The-Coding-Interview/17-11/cpp-17-11/main.cpp new file mode 100644 index 00000000..bf070010 --- /dev/null +++ b/Cracking-The-Coding-Interview/17-11/cpp-17-11/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.cn/problems/find-closest-lcci/ +/// Author : liuyubobobo +/// Time : 2022-05-26 + +#include +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int findClosest(vector& words, string word1, string word2) { + + vector pos1, pos2; + for(int i = 0; i < words.size(); i ++) + if(words[i] == word1) pos1.push_back(i); + else if(words[i] == word2) pos2.push_back(i); + + if(pos2.size() > pos1.size()) swap(pos1, pos2); + + int res = INT_MAX; + for(int p: pos2){ + auto iter = lower_bound(pos1.begin(), pos1.end(), p); + if(iter != pos1.end()) res = min(res, *iter - p); + if(iter != pos1.begin()){ + iter --; + res = min(res, p - *iter); + } + } + return res; + } +}; + + +int main() { + + return 0; +} From 9de9c73f8833280813fb690941e02e042798b95f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 28 May 2022 11:26:36 -0700 Subject: [PATCH 070/390] 0468 solved. --- .../cpp-0468/CMakeLists.txt | 6 ++ .../cpp-0468/main.cpp | 78 +++++++++++++++++++ readme.md | 1 + 3 files changed, 85 insertions(+) create mode 100644 0001-0500/0468-Validate-IP-Address/cpp-0468/CMakeLists.txt create mode 100644 0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp diff --git a/0001-0500/0468-Validate-IP-Address/cpp-0468/CMakeLists.txt b/0001-0500/0468-Validate-IP-Address/cpp-0468/CMakeLists.txt new file mode 100644 index 00000000..04efb57c --- /dev/null +++ b/0001-0500/0468-Validate-IP-Address/cpp-0468/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0468) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0468 main.cpp) diff --git a/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp b/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp new file mode 100644 index 00000000..6916721f --- /dev/null +++ b/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; + + +class Solution { +public: + string validIPAddress(string queryIP) { + + vector v; + if(queryIP.find('.') != string:: npos) + v = split_ip(queryIP, '.'); + + if(queryIP.find(':') != string:: npos) + v = split_ip(queryIP, ':'); + + if(v.size() == 4 && isIPv4(v)) return "IPv4"; + if(v.size() == 8 && isIPv6(v)) return "IPv6"; + return "Neither"; + } + +private: + vector split_ip(const string& ip, char split_char){ + + if(ip[0] == split_char || ip.back() == split_char) return {}; + + vector v; + for(int start = 0, i = 1; i <= ip.size(); i ++) + if(i == ip.size() || ip[i] == split_char){ + v.push_back(ip.substr(start, i - start)); + start = i + 1; + i = start; + } + return v; + } + + bool isIPv4(const vector& v){ + for(const string& s: v) + if(!ipv4_seg(s)) return false; + return true; + } + + bool ipv4_seg(const string& s){ + + if(s == "0") return true; + if(s[0] == '0') return false; + if(s.size() > 3) return false; + + for(char c: s) if(!isdigit(c)) return false; + + int x = atoi(s.c_str()); + return 0 <= x && x <= 255; + } + + bool isIPv6(const vector& v){ + for(const string& s: v) + if(!ipv6_seg(s)) return false; + return true; + } + + bool ipv6_seg(const string& s){ + + if(s == "0") return true; + if(s.size() > 4) return false; + + for(char c: s) + if(!(isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'))) + return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 605cc04f..a7974061 100644 --- a/readme.md +++ b/readme.md @@ -498,6 +498,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 464 | [Can I Win](https://leetcode.com/problems/can-i-win/) | [无] | [C++](0001-0500/0464-Can-I-Win/cpp-0464/) | | | | | | | | | | | 467 | [Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/) | [无] | [C++](0001-0500/0467-Unique-Substrings-in-Wraparound-String/cpp-0467/) | | | +| 468 | [Validate IP Address](https://leetcode.com/problems/validate-ip-address/) | [无] | [C++](0001-0500/0468-Validate-IP-Address/cpp-0468/) | | | | | | | | | | | 470 | [Implement Rand10() Using Rand7()](https://leetcode.com/problems/implement-rand10-using-rand7/description/) | [solution](https://leetcode.com/problems/implement-rand10-using-rand7/solution/) | [C++](0001-0500/0470-Implement-Rand10-Using-Rand7/cpp-0470/) | | | | | | | | | | From 97afc8633714a9a12d50104f7deed328f2aad6c5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 28 May 2022 13:20:34 -0700 Subject: [PATCH 071/390] 2283 solved. --- .../cpp-0468/main.cpp | 7 +++++ .../cpp-2283/CMakeLists.txt | 6 ++++ .../cpp-2283/main.cpp | 31 +++++++++++++++++++ readme.md | 2 ++ 4 files changed, 46 insertions(+) create mode 100644 2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/CMakeLists.txt create mode 100644 2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/main.cpp diff --git a/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp b/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp index 6916721f..66079cfe 100644 --- a/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp +++ b/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp @@ -1,9 +1,16 @@ +/// Source : https://leetcode.com/problems/validate-ip-address/ +/// Author : liuyubobobo +/// Time : 2021-05-28 + #include #include using namespace std; +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) class Solution { public: string validIPAddress(string queryIP) { diff --git a/2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/CMakeLists.txt b/2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/CMakeLists.txt new file mode 100644 index 00000000..e878956b --- /dev/null +++ b/2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2283) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2283 main.cpp) diff --git a/2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/main.cpp b/2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/main.cpp new file mode 100644 index 00000000..6e1ee80c --- /dev/null +++ b/2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/ +/// Author : liuyubobobo +/// Time : 2021-05-28 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool digitCount(string num) { + + vector f(10, 0); + for(char c: num) f[c - '0'] ++; + + for(int i = 0; i < num.size(); i ++) + if(num[i] - '0' != f[i]) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a7974061..bbe12930 100644 --- a/readme.md +++ b/readme.md @@ -2125,6 +2125,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2280 | [Minimum Lines to Represent a Line Chart](https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/) | [无] | [C++](2001-2500/2280-Minimum-Lines-to-Represent-a-Line-Chart/cpp-2280/) | | | | 2281 | [Sum of Total Strength of Wizards](https://leetcode.com/problems/sum-of-total-strength-of-wizards/) | [无] | [C++](2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/) | | | | | | | | | | +| 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/) | [无] | [C++](2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/) | | | +| | | | | | | ## 力扣中文站比赛 From 580c8f6bababd03717a076b29fc04298a12e752b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 28 May 2022 14:37:01 -0700 Subject: [PATCH 072/390] 2284 solved. --- .../cpp-2284/CMakeLists.txt | 6 +++ .../cpp-2284/main.cpp | 52 +++++++++++++++++++ readme.md | 3 ++ 3 files changed, 61 insertions(+) create mode 100644 2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/CMakeLists.txt create mode 100644 2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/main.cpp diff --git a/2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/CMakeLists.txt b/2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/CMakeLists.txt new file mode 100644 index 00000000..47d416b2 --- /dev/null +++ b/2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2284) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2284 main.cpp) diff --git a/2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/main.cpp b/2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/main.cpp new file mode 100644 index 00000000..0859311e --- /dev/null +++ b/2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/sender-with-largest-word-count/ +/// Author : liuyubobobo +/// Time : 2022-05-28 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(nlogn * |message|) +/// Space Complexity: O(n) +class Solution { +public: + string largestWordCount(vector& messages, vector& senders) { + + int n = messages.size(); + + map> cnt; + for(int i = 0; i < n; i ++){ + string name = senders[i]; + cnt[name] += get_cnt(messages[i]); + } + + int largest = -1; + string res = ""; + for(const pair& p: cnt) + if(p.second > largest) res = p.first, largest = p.second; + return res; + } + +private: + int get_cnt(const string& s){ + + int res = 0; + for(int start = 0, i = 1; i <= s.size(); i ++) + if(i == s.size() || s[i] == ' '){ + res ++; + start = i + 1; + i = start; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index bbe12930..ee9e194e 100644 --- a/readme.md +++ b/readme.md @@ -2126,6 +2126,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2281 | [Sum of Total Strength of Wizards](https://leetcode.com/problems/sum-of-total-strength-of-wizards/) | [无] | [C++](2001-2500/2281-Sum-of-Total-Strength-of-Wizards/cpp-2281/) | | | | | | | | | | | 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/) | [无] | [C++](2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/) | | | +| 2284 | [Sender With Largest Word Count](https://leetcode.com/problems/sender-with-largest-word-count/) | [无] | [C++](2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/) | | | +| | | | | | | +| | | | | | | | | | | | | | ## 力扣中文站比赛 From f279c6364439f270643f86ac1ac709a325a4b657 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 28 May 2022 14:38:02 -0700 Subject: [PATCH 073/390] 2285 solved. --- .../cpp-2285/CMakeLists.txt | 6 ++++ .../cpp-2285/main.cpp | 34 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/CMakeLists.txt create mode 100644 2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/main.cpp diff --git a/2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/CMakeLists.txt b/2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/CMakeLists.txt new file mode 100644 index 00000000..33894595 --- /dev/null +++ b/2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2285) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2285 main.cpp) diff --git a/2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/main.cpp b/2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/main.cpp new file mode 100644 index 00000000..0b0c9acb --- /dev/null +++ b/2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/maximum-total-importance-of-roads/ +/// Author : liuyubobobo +/// Time : 2022-05-28 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long maximumImportance(int n, vector>& roads) { + + vector degrees(n, 0); + for(const vector& road: roads) + degrees[road[0]] ++, degrees[road[1]] ++; + + sort(degrees.begin(), degrees.end(), greater()); + long long res = 0; + for(int i = 0; i < n; i ++) + res += degrees[i] * (n - i); + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ee9e194e..e80ec6c5 100644 --- a/readme.md +++ b/readme.md @@ -2127,7 +2127,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/) | [无] | [C++](2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/) | | | | 2284 | [Sender With Largest Word Count](https://leetcode.com/problems/sender-with-largest-word-count/) | [无] | [C++](2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/) | | | -| | | | | | | +| 2285 | [Maximum Total Importance of Roads](https://leetcode.com/problems/maximum-total-importance-of-roads/) | [无] | [C++](2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/) | | | | | | | | | | | | | | | | | From b60c17fc26e988c47be261e888d18e092549dd26 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 28 May 2022 14:39:22 -0700 Subject: [PATCH 074/390] 2286 solved. --- .../cpp-2286/CMakeLists.txt | 6 + .../cpp-2286/main.cpp | 178 ++++++++++++++++++ readme.md | 2 +- 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/CMakeLists.txt create mode 100644 2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/main.cpp diff --git a/2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/CMakeLists.txt b/2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/CMakeLists.txt new file mode 100644 index 00000000..efee9583 --- /dev/null +++ b/2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2286) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2286 main.cpp) diff --git a/2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/main.cpp b/2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/main.cpp new file mode 100644 index 00000000..999482d8 --- /dev/null +++ b/2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/main.cpp @@ -0,0 +1,178 @@ +/// Source : https://leetcode.com/problems/booking-concert-tickets-in-groups/ +/// Author : liuyubobobo +/// Time : 2022-05-28 + +#include +#include +#include + +using namespace std; + + +/// Segment Tree + Binary Search +/// Time Complexity: init: O(nlogn) +/// gather: O(logn * logn) +/// scatter: O(logn) in average +/// Space Complexity: O(n) +template +class SegmentTree{ + +private: + int n; + vector data, max_tree, sum_tree; + +public: + SegmentTree(const vector& data): n(data.size()), data(data), max_tree(4 * n, 0), sum_tree(4 * n, 0){ + buildSegTree(0, 0, n - 1); + } + + void update(int index, T value){ + if(data[index] == value) return; + data[index] = value; + update(0, 0, n - 1, index, value); + } + + T query(int index){ + assert(0 <= index && index < n); + return data[index]; + } + + T query_max(int l, int r){ + if(l > r || l < 0 || l >= n || r < 0 || r >= n) return 0; + return query_max(0, 0, n - 1, l, r); + } + + T query_sum(int l, int r){ + if(l > r || l < 0 || l >= n || r < 0 || r >= n) return 0; + return query_sum(0, 0, n - 1, l, r); + } + +private: + void buildSegTree(int treeID, int l, int r){ + + if(l == r){ + max_tree[treeID] = sum_tree[treeID] = data[l]; + return; + } + + int mid = (l + r) / 2; + buildSegTree(treeID * 2 + 1, l, mid); + buildSegTree(treeID * 2 + 2, mid + 1, r); + max_tree[treeID] = max(max_tree[treeID * 2 + 1], max_tree[treeID * 2 + 2]); + sum_tree[treeID] = sum_tree[treeID * 2 + 1] + sum_tree[treeID * 2 + 2]; + return; + } + + void update(int treeID, int l, int r, int index, T value){ + + if(l == r){ + assert(l == index); + max_tree[treeID] = sum_tree[treeID] = value; + return; + } + + int mid = (l + r) / 2; + if(index <= mid) update(treeID * 2 + 1, l, mid, index, value); + else update(treeID * 2 + 2, mid + 1, r, index, value); + + max_tree[treeID] = max(max_tree[treeID * 2 + 1], max_tree[treeID * 2 + 2]); + sum_tree[treeID] = sum_tree[treeID * 2 + 1] + sum_tree[treeID * 2 + 2]; + return; + } + + T query_max(int treeID, int l, int r, int ql, int qr){ + + if(ql == l && qr == r) + return max_tree[treeID]; + + int mid = (l + r) / 2; + if(qr <= mid) return query_max(treeID * 2 + 1, l, mid, ql, qr); + else if(ql > mid) return query_max(treeID * 2 + 2, mid + 1, r, ql, qr); + + T resl = query_max(treeID * 2 + 1, l, mid, ql, mid); + T resr = query_max(treeID * 2 + 2, mid + 1, r, mid + 1, qr); + return max(resl, resr); + } + + T query_sum(int treeID, int l, int r, int ql, int qr){ + + if(ql == l && qr == r) + return sum_tree[treeID]; + + int mid = (l + r) / 2; + if(qr <= mid) return query_sum(treeID * 2 + 1, l, mid, ql, qr); + else if(ql > mid) return query_sum(treeID * 2 + 2, mid + 1, r, ql, qr); + + T resl = query_sum(treeID * 2 + 1, l, mid, ql, mid); + T resr = query_sum(treeID * 2 + 2, mid + 1, r, mid + 1, qr); + return resl + resr; + } +}; + +class BookMyShow { + +private: + int n, m, first_non_zero_index; + SegmentTree *seg_tree; + +public: + BookMyShow(int n, int m) : n(n), m(m), first_non_zero_index(0) { + seg_tree = new SegmentTree(vector(n, m)); + } + + vector gather(int k, int maxRow) { + + int l = first_non_zero_index, r = maxRow + 1; + if(l > r) return {}; + + while(l < r){ + int mid = (l + r) / 2; + if(seg_tree->query_max(l, mid) >= k) r = mid; + else l = mid + 1; + } + + if(l == maxRow + 1) return {}; + + int left = seg_tree->query(l); + seg_tree->update(l, left - k); + + while(first_non_zero_index < n && seg_tree->query(first_non_zero_index) == 0) + first_non_zero_index ++; + return {l, m - left}; + } + + bool scatter(int k, int maxRow) { + + if(seg_tree->query_sum(first_non_zero_index, maxRow) < k) return false; + + for(int i = first_non_zero_index; i <= maxRow && k; i ++){ + int v = seg_tree->query(i); + int t = min(k, v); + + k -= t; + seg_tree->update(i, v - t); + if(v - t == 0) first_non_zero_index ++; + } + return true; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + BookMyShow obj(19, 9); + print_vec(obj.gather(38, 8)); // empty + print_vec(obj.gather(27, 3)); // empty + cout << obj.scatter(36, 14) << '\n'; // 1 + cout << obj.scatter(46, 2) << '\n'; // 0 + print_vec(obj.gather(12, 5)); // empty + cout << obj.scatter(12, 12) << '\n'; // 1 + cout << obj.scatter(43, 12) << '\n'; // 1 + print_vec(obj.gather(30, 5)); // empty + + return 0; +} diff --git a/readme.md b/readme.md index e80ec6c5..3dd11a31 100644 --- a/readme.md +++ b/readme.md @@ -2128,7 +2128,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/) | [无] | [C++](2001-2500/2283-Check-if-Number-Has-Equal-Digit-Count-and-Digit-Value/cpp-2283/) | | | | 2284 | [Sender With Largest Word Count](https://leetcode.com/problems/sender-with-largest-word-count/) | [无] | [C++](2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/) | | | | 2285 | [Maximum Total Importance of Roads](https://leetcode.com/problems/maximum-total-importance-of-roads/) | [无] | [C++](2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/) | | | -| | | | | | | +| 2286 | [Booking Concert Tickets in Groups](https://leetcode.com/problems/booking-concert-tickets-in-groups/) | [无] | [C++](2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/) | | | | | | | | | | ## 力扣中文站比赛 From 4202aac4f98efdbca0d5d08b15ec629c6802a263 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 29 May 2022 10:03:54 -0700 Subject: [PATCH 075/390] 2287 added. --- .../cpp-2287/CMakeLists.txt | 6 ++++ .../cpp-2287/main.cpp | 35 +++++++++++++++++++ readme.md | 1 + 3 files changed, 42 insertions(+) create mode 100644 2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/CMakeLists.txt create mode 100644 2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/main.cpp diff --git a/2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/CMakeLists.txt b/2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/main.cpp b/2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/main.cpp new file mode 100644 index 00000000..184017fd --- /dev/null +++ b/2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/rearrange-characters-to-make-target-string/ +/// Author : liuyubobobo +/// Time : 2022-05-29 + +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int rearrangeCharacters(string s, string target) { + + vector fs(26, 0); + for(char c: s) fs[c - 'a'] ++; + + vector ft(26, 0); + for(char c: target) ft[c - 'a'] ++; + + int res = INT_MAX; + for(int i = 0; i < 26; i ++) + if(ft[i]) res = min(res, fs[i] / ft[i]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3dd11a31..dc89b22b 100644 --- a/readme.md +++ b/readme.md @@ -2129,6 +2129,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2284 | [Sender With Largest Word Count](https://leetcode.com/problems/sender-with-largest-word-count/) | [无] | [C++](2001-2500/2284-Sender-With-Largest-Word-Count/cpp-2284/) | | | | 2285 | [Maximum Total Importance of Roads](https://leetcode.com/problems/maximum-total-importance-of-roads/) | [无] | [C++](2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/) | | | | 2286 | [Booking Concert Tickets in Groups](https://leetcode.com/problems/booking-concert-tickets-in-groups/) | [无] | [C++](2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/) | | | +| 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [无] | [C++](2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/) | | | | | | | | | | ## 力扣中文站比赛 From 997a0200b5247cb47f78de02c691e00f9abe0ec4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 29 May 2022 10:08:11 -0700 Subject: [PATCH 076/390] 2288 added. --- .../cpp-2288/CMakeLists.txt | 6 ++ .../cpp-2288/main.cpp | 64 +++++++++++++++++++ readme.md | 1 + 3 files changed, 71 insertions(+) create mode 100644 2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/CMakeLists.txt create mode 100644 2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/main.cpp diff --git a/2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/CMakeLists.txt b/2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/main.cpp b/2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/main.cpp new file mode 100644 index 00000000..833abbbf --- /dev/null +++ b/2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/main.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/apply-discount-to-prices/ +/// Author : liuyubobobo +/// Time : 2022-05-29 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string discountPrices(string sentence, int discount) { + + vector words; + for(int start = 0, i = 1; i <= sentence.size(); i ++) + if(i == sentence.size() || sentence[i] == ' '){ + words.push_back(sentence.substr(start, i - start)); + start = i + 1; + i = start; + } + + for(string& word: words) + if(is_price(word)){ + long long price = atoll(word.substr(1).c_str()); + price *= (100 - discount); + + string new_s = to_string(price); + while(new_s.size() < 3) new_s = "0" + new_s; + + int len = new_s.size(); + word = "$" + new_s.substr(0, len - 2) + "." + new_s.substr(len - 2); + } + + string res = ""; + for(int i = 0; i < words.size(); i ++){ + res += words[i]; + if(i + 1 != words.size()) res += " "; + } + return res; + } + +private: + bool is_price(const string& s){ + + if(s.size() == 1) return false; + + if(s[0] != '$') return false; + + for(int i = 1; i < s.size(); i ++) + if(!isdigit(s[i])) return false; + + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index dc89b22b..fbc1cf33 100644 --- a/readme.md +++ b/readme.md @@ -2130,6 +2130,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2285 | [Maximum Total Importance of Roads](https://leetcode.com/problems/maximum-total-importance-of-roads/) | [无] | [C++](2001-2500/2285-Maximum-Total-Importance-of-Roads/cpp-2285/) | | | | 2286 | [Booking Concert Tickets in Groups](https://leetcode.com/problems/booking-concert-tickets-in-groups/) | [无] | [C++](2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/) | | | | 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [无] | [C++](2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/) | | | +| 2288 | [Apply Discount to Prices](https://leetcode.com/problems/apply-discount-to-prices/) | [无] | [C++](2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/) | | | | | | | | | | ## 力扣中文站比赛 From d65940a433a9957e4acb985ac076c3096ab1ee1b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 29 May 2022 10:12:19 -0700 Subject: [PATCH 077/390] 2290 added. --- .../cpp-2290/CMakeLists.txt | 6 ++ .../cpp-2290/main.cpp | 74 +++++++++++++++++++ readme.md | 2 + 3 files changed, 82 insertions(+) create mode 100644 2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/CMakeLists.txt create mode 100644 2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp diff --git a/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/CMakeLists.txt b/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp b/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp new file mode 100644 index 00000000..66111347 --- /dev/null +++ b/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/ +/// Author : liuyubobobo +/// Time : 2022-05-29 + +#include +#include +#include + +using namespace std; + + +/// 0-1 BFS +/// Time Complexity: O(n * m) +/// Space Complexity: O(n * m) +class Solution { + +private: + const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + int R, C; + +public: + int minimumObstacles(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + + vector>> g(R * C); + for(int x = 0; x < R; x ++) + for(int y = 0; y < C; y ++){ + int u = x * C + y; + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny)){ + int v = nx * C + ny; + if(grid[nx][ny]) + g[u].push_back({v, 1}); + else + g[u].push_back({v, 0}); + } + } + } + + vector dis(R * C, INT_MAX / 2); + vector visited(R * C, false); + deque q; + q.push_back(0); + dis[0] = 0; + while(!q.empty()){ + int u = q.front(); q.pop_front(); + if(visited[u]) continue; + + visited[u] = true; + for(const pair& p: g[u]){ + int v = p.first, w = p.second; + if(!visited[v] && dis[u] + w < dis[v]){ + dis[v] = dis[u] + w; + if(w) q.push_back(v); + else q.push_front(v); + } + } + } + return dis[R * C - 1]; + } + +private: + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index fbc1cf33..aff19ef4 100644 --- a/readme.md +++ b/readme.md @@ -2132,6 +2132,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [无] | [C++](2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/) | | | | 2288 | [Apply Discount to Prices](https://leetcode.com/problems/apply-discount-to-prices/) | [无] | [C++](2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/) | | | | | | | | | | +| 2290 | [Minimum Obstacle Removal to Reach Corner](https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/) | [无] | [C++](2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/) | | | +| | | | | | | ## 力扣中文站比赛 From f00f39a34c9c7115845ad7d6b176fd07ef6bd490 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 29 May 2022 10:51:54 -0700 Subject: [PATCH 078/390] 2290 codes updated. --- .../cpp-2263/CMakeLists.txt | 6 ++ .../cpp-2263/main.cpp | 56 +++++++++++++++ .../cpp-2272/CMakeLists.txt | 6 ++ .../cpp-2272/main.cpp | 35 +++++++++ .../cpp-2282/CMakeLists.txt | 6 ++ .../cpp-2282/main.cpp | 72 +++++++++++++++++++ .../cpp-2290/main.cpp | 27 +++---- 7 files changed, 190 insertions(+), 18 deletions(-) create mode 100644 2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt create mode 100644 2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp create mode 100644 2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt create mode 100644 2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp create mode 100644 2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt create mode 100644 2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp diff --git a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt new file mode 100644 index 00000000..3ece3dd8 --- /dev/null +++ b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2263) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2263 main.cpp) diff --git a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp new file mode 100644 index 00000000..866f9f8c --- /dev/null +++ b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp @@ -0,0 +1,56 @@ +#include +#include + +using namespace std; + + +class Solution { + +private: + const int MAX_NUM = 1000; + +public: + int convertArray(vector& nums) { + + int n = nums.size(), res = INT_MAX; + for(int i = 0; i < n; i ++){ + res = min(res, get_inc_arr_by_fix(n, nums, i)); + res = min(res, get_desc_arr_by_fix(n, nums, i)); + } + return res; + } + +private: + int get_inc_arr_by_fix(int n, vector nums, int fix_index){ + + int res = 0; + for(int i = fix_index - 1; i >= 0; i --){ + if(nums[i] > nums[i + 1]) res += nums[i] - nums[i + 1], nums[i] = nums[i + 1]; + } + for(int i = fix_index + 1; i < n; i ++){ + if(nums[i] < nums[i - 1]) res += nums[i - 1] - nums[i], nums[i] = nums[i - 1]; + } + return res; + } + + int get_desc_arr_by_fix(int n, vector nums, int fix_index){ + + int res = 0; + for(int i = fix_index - 1; i >= 0; i --){ + if(nums[i] < nums[i + 1]) res += nums[i + 1] - nums[i], nums[i] = nums[i + 1]; + } + for(int i = fix_index + 1; i < n; i ++){ + if(nums[i] > nums[i - 1]) res += nums[i] - nums[i - 1], nums[i] = nums[i - 1]; + } + return res; + } +}; + + +int main() { + + vector nums1 = {0,2,8,0,3}; + cout << Solution().convertArray(nums1) << '\n'; + // 8 + return 0; +} diff --git a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt new file mode 100644 index 00000000..ff71f363 --- /dev/null +++ b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2272) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2272 main.cpp) diff --git a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp new file mode 100644 index 00000000..8539b66e --- /dev/null +++ b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + + +class Solution { +public: + int largestVariance(string s) { + + int n = s.size(); + + int res = 0; + vector> min_d(26, vector(26, INT_MAX / 2)); + vector cur_f(26, 0); + for(int r = 0; r < n; r ++){ + cur_f[s[r] - 'a'] ++; + for(char c1 = 'a'; c1 <= 'z'; c1 ++) + for(char c2 = 'a'; c2 <= 'z'; c2 ++){ + if(c1 == c2) continue; + if(cur_f[c1] == 0 || cur) + } + } + return res; + } +}; + + +int main() { + + string s = "aababbb"; + cout << Solution().largestVariance(s) << '\n'; + + return 0; +} diff --git a/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt b/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt new file mode 100644 index 00000000..51b9a463 --- /dev/null +++ b/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2282) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2282 main.cpp) diff --git a/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp b/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp new file mode 100644 index 00000000..6a50a751 --- /dev/null +++ b/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp @@ -0,0 +1,72 @@ +#include +#include + +using namespace std; + + +class Solution { + +public: + vector> seePeople(vector>& heights) { + + int R = heights.size(), C = heights[0].size(); + + vector> res1(R, vector(C, 0)); + for(int i = 0; i < R; i ++){ + vector stack; + for(int j = 0; j < C; j ++){ + while(!stack.empty()){ + res1[i][stack.back()] ++; + if(heights[i][j] > heights[i][stack.back()]) stack.pop_back(); + else break; + } + stack.push_back(j); + } + } + + vector> res2(R, vector(C, 0)); + for(int j = 0; j < C; j ++){ + vector stack; + for(int i = 0; i < R; i ++){ + while(!stack.empty()){ + res2[stack.back()][j] ++; + if(heights[i][j] > heights[stack.back()][j]) stack.pop_back(); + else break; + } + stack.push_back(i); + } + } + + vector> res(R, vector(C, 0)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) res[i][j] = res1[i][j] + res2[i][j]; + return res; + } +}; + + +void print_matrix(const vector>& matrix){ + for(const vector& row: matrix){ + for(int e: row) cout << e << ' '; + cout << '\n'; + } +} + +int main() { + + vector> height1 = {{3, 1, 4, 2, 5}}; + print_matrix(Solution().seePeople(height1)); + // 2 1 2 1 0 + + vector> height2 = {{5, 1}, {3, 1}, {4, 1}}; + print_matrix(Solution().seePeople(height2)); + // 3 1 + // 2 1 + // 1 0 + + vector> height3 = {{4, 2, 1, 1, 3}}; + print_matrix(Solution().seePeople(height3)); + // 2 2 1 1 0 + + return 0; +} diff --git a/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp b/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp index 66111347..f9de6da0 100644 --- a/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp +++ b/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp @@ -4,6 +4,7 @@ #include #include +#include #include using namespace std; @@ -23,22 +24,6 @@ class Solution { R = grid.size(), C = grid[0].size(); - vector>> g(R * C); - for(int x = 0; x < R; x ++) - for(int y = 0; y < C; y ++){ - int u = x * C + y; - for(int d = 0; d < 4; d ++){ - int nx = x + dirs[d][0], ny = y + dirs[d][1]; - if(in_area(nx, ny)){ - int v = nx * C + ny; - if(grid[nx][ny]) - g[u].push_back({v, 1}); - else - g[u].push_back({v, 0}); - } - } - } - vector dis(R * C, INT_MAX / 2); vector visited(R * C, false); deque q; @@ -49,8 +34,14 @@ class Solution { if(visited[u]) continue; visited[u] = true; - for(const pair& p: g[u]){ - int v = p.first, w = p.second; + if(u == R * C - 1) break; + + int cx = u / C, cy = u % C; + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(!in_area(nx, ny)) continue; + + int v = nx * C + ny, w = grid[nx][ny]; if(!visited[v] && dis[u] + w < dis[v]){ dis[v] = dis[u] + w; if(w) q.push_back(v); From 2fabfb5ef1e981052be09bdd34caf958cf10cb68 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 29 May 2022 10:52:46 -0700 Subject: [PATCH 079/390] updated. --- .../cpp-2263/CMakeLists.txt | 6 -- .../cpp-2263/main.cpp | 56 --------------- .../cpp-2272/CMakeLists.txt | 6 -- .../cpp-2272/main.cpp | 35 --------- .../cpp-2282/CMakeLists.txt | 6 -- .../cpp-2282/main.cpp | 72 ------------------- 6 files changed, 181 deletions(-) delete mode 100644 2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt delete mode 100644 2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp delete mode 100644 2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt delete mode 100644 2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp delete mode 100644 2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt delete mode 100644 2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp diff --git a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt deleted file mode 100644 index 3ece3dd8..00000000 --- a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.22) -project(cpp_2263) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(cpp_2263 main.cpp) diff --git a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp deleted file mode 100644 index 866f9f8c..00000000 --- a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include - -using namespace std; - - -class Solution { - -private: - const int MAX_NUM = 1000; - -public: - int convertArray(vector& nums) { - - int n = nums.size(), res = INT_MAX; - for(int i = 0; i < n; i ++){ - res = min(res, get_inc_arr_by_fix(n, nums, i)); - res = min(res, get_desc_arr_by_fix(n, nums, i)); - } - return res; - } - -private: - int get_inc_arr_by_fix(int n, vector nums, int fix_index){ - - int res = 0; - for(int i = fix_index - 1; i >= 0; i --){ - if(nums[i] > nums[i + 1]) res += nums[i] - nums[i + 1], nums[i] = nums[i + 1]; - } - for(int i = fix_index + 1; i < n; i ++){ - if(nums[i] < nums[i - 1]) res += nums[i - 1] - nums[i], nums[i] = nums[i - 1]; - } - return res; - } - - int get_desc_arr_by_fix(int n, vector nums, int fix_index){ - - int res = 0; - for(int i = fix_index - 1; i >= 0; i --){ - if(nums[i] < nums[i + 1]) res += nums[i + 1] - nums[i], nums[i] = nums[i + 1]; - } - for(int i = fix_index + 1; i < n; i ++){ - if(nums[i] > nums[i - 1]) res += nums[i] - nums[i - 1], nums[i] = nums[i - 1]; - } - return res; - } -}; - - -int main() { - - vector nums1 = {0,2,8,0,3}; - cout << Solution().convertArray(nums1) << '\n'; - // 8 - return 0; -} diff --git a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt deleted file mode 100644 index ff71f363..00000000 --- a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.22) -project(cpp_2272) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(cpp_2272 main.cpp) diff --git a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp deleted file mode 100644 index 8539b66e..00000000 --- a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include - -using namespace std; - - -class Solution { -public: - int largestVariance(string s) { - - int n = s.size(); - - int res = 0; - vector> min_d(26, vector(26, INT_MAX / 2)); - vector cur_f(26, 0); - for(int r = 0; r < n; r ++){ - cur_f[s[r] - 'a'] ++; - for(char c1 = 'a'; c1 <= 'z'; c1 ++) - for(char c2 = 'a'; c2 <= 'z'; c2 ++){ - if(c1 == c2) continue; - if(cur_f[c1] == 0 || cur) - } - } - return res; - } -}; - - -int main() { - - string s = "aababbb"; - cout << Solution().largestVariance(s) << '\n'; - - return 0; -} diff --git a/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt b/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt deleted file mode 100644 index 51b9a463..00000000 --- a/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.22) -project(cpp_2282) - -set(CMAKE_CXX_STANDARD 14) - -add_executable(cpp_2282 main.cpp) diff --git a/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp b/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp deleted file mode 100644 index 6a50a751..00000000 --- a/2001-2500/2282-Number-of-People-That-Can-Be-Seen-in-a-Grid/cpp-2282/main.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include - -using namespace std; - - -class Solution { - -public: - vector> seePeople(vector>& heights) { - - int R = heights.size(), C = heights[0].size(); - - vector> res1(R, vector(C, 0)); - for(int i = 0; i < R; i ++){ - vector stack; - for(int j = 0; j < C; j ++){ - while(!stack.empty()){ - res1[i][stack.back()] ++; - if(heights[i][j] > heights[i][stack.back()]) stack.pop_back(); - else break; - } - stack.push_back(j); - } - } - - vector> res2(R, vector(C, 0)); - for(int j = 0; j < C; j ++){ - vector stack; - for(int i = 0; i < R; i ++){ - while(!stack.empty()){ - res2[stack.back()][j] ++; - if(heights[i][j] > heights[stack.back()][j]) stack.pop_back(); - else break; - } - stack.push_back(i); - } - } - - vector> res(R, vector(C, 0)); - for(int i = 0; i < R; i ++) - for(int j = 0; j < C; j ++) res[i][j] = res1[i][j] + res2[i][j]; - return res; - } -}; - - -void print_matrix(const vector>& matrix){ - for(const vector& row: matrix){ - for(int e: row) cout << e << ' '; - cout << '\n'; - } -} - -int main() { - - vector> height1 = {{3, 1, 4, 2, 5}}; - print_matrix(Solution().seePeople(height1)); - // 2 1 2 1 0 - - vector> height2 = {{5, 1}, {3, 1}, {4, 1}}; - print_matrix(Solution().seePeople(height2)); - // 3 1 - // 2 1 - // 1 0 - - vector> height3 = {{4, 2, 1, 1, 3}}; - print_matrix(Solution().seePeople(height3)); - // 2 2 1 1 0 - - return 0; -} From 7f263213c5bf1b20ba04e51cb33ce22229ee48d7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 30 May 2022 11:30:29 -0700 Subject: [PATCH 080/390] 0484 solved. --- .../cpp-0484/CMakeLists.txt | 6 +++ .../0484-Find-Permutation/cpp-0484/main.cpp | 47 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0484-Find-Permutation/cpp-0484/CMakeLists.txt create mode 100644 0001-0500/0484-Find-Permutation/cpp-0484/main.cpp diff --git a/0001-0500/0484-Find-Permutation/cpp-0484/CMakeLists.txt b/0001-0500/0484-Find-Permutation/cpp-0484/CMakeLists.txt new file mode 100644 index 00000000..5e038615 --- /dev/null +++ b/0001-0500/0484-Find-Permutation/cpp-0484/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0484) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0484 main.cpp) diff --git a/0001-0500/0484-Find-Permutation/cpp-0484/main.cpp b/0001-0500/0484-Find-Permutation/cpp-0484/main.cpp new file mode 100644 index 00000000..052b2ae8 --- /dev/null +++ b/0001-0500/0484-Find-Permutation/cpp-0484/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/find-permutation/ +/// Author : liuyubobobo +/// Time : 2022-05-30 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector findPermutation(string s) { + + s += "I"; + int n = s.size(); + + vector res(n); + for(int i = 1; i <= n; i ++) res[i - 1] = i; + + for(int start = 0, i = 0; i < n; i ++) + if(i == n || s[i] != s[start]){ + if(s[start] == 'D') + reverse(res.begin() + start, res.begin() + i + 1); + start = i; + i = start; + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + print_vec(Solution().findPermutation("DI")); + // 2 1 3 + + return 0; +} diff --git a/readme.md b/readme.md index aff19ef4..c2c5dcb4 100644 --- a/readme.md +++ b/readme.md @@ -514,7 +514,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [无] | [C++](0001-0500/0482-License-Key-Formatting/cpp-0482/) | | | | 483 | [Smallest Good Base](https://leetcode.com/problems/smallest-good-base/) | [无] | [C++](0001-0500/0483-Smallest-Good-Base/cpp-0483/) | | | -| | | | | | | +| 484 | [Find Permutation](https://leetcode.com/problems/find-permutation/) | [solution](https://leetcode.com/problems/find-permutation/solution/) | [C++](0001-0500/0484-Find-Permutation/cpp-0484/) | | | | 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/description/) | [无] | [C++](0001-0500/0485-Max-Consecutive-Ones/cpp-0485/) | | | | | | | | | | | 488 | [Zuma Game](https://leetcode.com/problems/zuma-game/) | [无] | [C++](0001-0500/0488-Zuma-Game/cpp-0488/) | | | From 33bc6a193e428ea902433ad3ceb8901bbf2ab86b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 30 May 2022 17:36:08 -0700 Subject: [PATCH 081/390] 1461 codes updated. --- .../cpp-1461/main2.cpp | 8 +++++++- .../cpp-1461/main3.cpp | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main2.cpp b/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main2.cpp index aa62be26..44d168bd 100644 --- a/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main2.cpp +++ b/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main2.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/ /// Author : liuyubobobo /// Time : 2020-05-30 +/// Updated: 2022-05-30 #include #include @@ -15,12 +16,14 @@ class Solution { public: bool hasAllCodes(string s, int k) { + if(k >= s.size()) return false; + int cur = 0; for(int i = 0; i < k - 1; i ++) cur = 2 * cur + (s[i] == '1'); unordered_set set; - for(int i = k - 1; i < s.size(); i ++){ + for(int i = k - 1; i < (int)s.size(); i ++){ cur = cur * 2 + (s[i] == '1'); set.insert(cur); cur &= ~(1 << (k - 1)); @@ -47,5 +50,8 @@ int main() { cout << Solution().hasAllCodes("0000000001011100", 4) << endl; // 0 + cout << Solution().hasAllCodes("0", 20) << endl; + // 0 + return 0; } diff --git a/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main3.cpp b/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main3.cpp index 3f0dee13..270ddf57 100644 --- a/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main3.cpp +++ b/1001-1500/1461-Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/cpp-1461/main3.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/ /// Author : liuyubobobo /// Time : 2020-05-30 +/// Updated: 2022-05-30 #include #include @@ -15,6 +16,8 @@ class Solution { public: bool hasAllCodes(string s, int k) { + if(k >= s.size()) return false; + int cur = 0; for(int i = 0; i < k - 1; i ++) cur = 2 * cur + (s[i] == '1'); From a5706d111bda67e2c2b6b57aa50d86fa11503ba3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 31 May 2022 19:41:08 -0700 Subject: [PATCH 082/390] 2291 solved. --- .../cpp-2291/CMakeLists.txt | 6 +++ .../cpp-2291/main.cpp | 47 +++++++++++++++++++ readme.md | 1 + 3 files changed, 54 insertions(+) create mode 100644 2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/CMakeLists.txt create mode 100644 2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/main.cpp diff --git a/2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/CMakeLists.txt b/2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/CMakeLists.txt new file mode 100644 index 00000000..cdc867b5 --- /dev/null +++ b/2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2291) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2291 main.cpp) diff --git a/2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/main.cpp b/2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/main.cpp new file mode 100644 index 00000000..0e57b8cf --- /dev/null +++ b/2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/maximum-profit-from-trading-stocks/ +/// Author : liuyubobobo +/// Time : 2022-05-31 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * budget) +/// Space Complexity: O(n + budget) +class Solution { +public: + int maximumProfit(vector& present, vector& future, int budget) { + + int n = present.size(); + + vector values, weight; + for(int i = 0; i < n; i ++) + if(future[i] > present[i] && present[i] <= budget){ + values.push_back(future[i] - present[i]); + weight.push_back(present[i]); + } + + vector dp(budget + 1, 0); + for(int i = 0; i < values.size(); i ++){ + vector tdp = dp; + for(int j = budget; j >= weight[i]; j --) + tdp[j] = max(tdp[j], tdp[j - weight[i]] + values[i]); + dp = tdp; + } + return dp.back(); + } +}; + + +int main() { + + vector present = {2,2,5}, future = {3,4,10}; + cout << Solution().maximumProfit(present, future, 6) << '\n'; + // 5 + + return 0; +} diff --git a/readme.md b/readme.md index c2c5dcb4..9110b864 100644 --- a/readme.md +++ b/readme.md @@ -2133,6 +2133,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2288 | [Apply Discount to Prices](https://leetcode.com/problems/apply-discount-to-prices/) | [无] | [C++](2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/) | | | | | | | | | | | 2290 | [Minimum Obstacle Removal to Reach Corner](https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/) | [无] | [C++](2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/) | | | +| 2291 | [Maximum Profit From Trading Stocks](https://leetcode.com/problems/maximum-profit-from-trading-stocks/) | [无] | [C++](2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/) | | | | | | | | | | ## 力扣中文站比赛 From 60c363a9fb0387852c763a2b55fa0155b8fb1afe Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 2 Jun 2022 09:58:26 -0700 Subject: [PATCH 083/390] 0829 solved. --- .../cpp-0829/CMakeLists.txt | 6 +++ .../cpp-0829/main.cpp | 49 +++++++++++++++++++ readme.md | 1 + 3 files changed, 56 insertions(+) create mode 100644 0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/CMakeLists.txt create mode 100644 0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/main.cpp diff --git a/0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/CMakeLists.txt b/0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/CMakeLists.txt new file mode 100644 index 00000000..b2b892e3 --- /dev/null +++ b/0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0829) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0829 main.cpp) diff --git a/0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/main.cpp b/0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/main.cpp new file mode 100644 index 00000000..ad78bb31 --- /dev/null +++ b/0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/consecutive-numbers-sum/ +/// Author : liuyubobobo +/// Time : 2022-06-02 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(sqrt(2 * n)) +/// Space Complexity: O(1) +class Solution { +public: + int consecutiveNumbersSum(int n) { + + int A = 2 * n, res = 0; + for(int i = 1; i * i <= A; i ++) + if(A % i == 0){ + int x = i, y = - A / i; + res += ok(x, y); + + x = -i, y = A / i; + res += ok(x, y); + } + return res; + } + +private: + bool ok(int x, int y){ + int t = x + y + 1; + return t > 0 && t % 2 == 0; + } +}; + + +int main() { + + cout << Solution().consecutiveNumbersSum(5) << '\n'; + // 2 + + cout << Solution().consecutiveNumbersSum(9) << '\n'; + // 3 + + cout << Solution().consecutiveNumbersSum(15) << '\n'; + // 4 + + return 0; +} diff --git a/readme.md b/readme.md index 9110b864..d541742a 100644 --- a/readme.md +++ b/readme.md @@ -812,6 +812,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 827 | [Making A Large Island](https://leetcode.com/problems/making-a-large-island/) | [无] | [C++](0501-1000/0827-Making-A-Large-Island/cpp-0827/) | | | | | | | | | | +| 829 | [Consecutive Numbers Sum](https://leetcode.com/problems/consecutive-numbers-sum/) | [无] | [C++](0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/) | | | | 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [solution](https://leetcode.com/problems/positions-of-large-groups/solution/) | [C++](0501-1000/0830-Positions-of-Large-Groups/cpp-0830/) | | | | | | | | | | | 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [solution](https://leetcode.com/problems/flipping-an-image/solution/) | [C++](0501-1000/0832-Flipping-an-Image/cpp-0832/) | | | From edc6230461a934d8c17a181eccafed62e3e016f3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 4 Jun 2022 16:25:05 -0700 Subject: [PATCH 084/390] 2289 solved. --- .../cpp-2289/CMakeLists.txt | 6 + .../cpp-2289/main.cpp | 113 ++++++++++++++++++ .../cpp-2289/main2.cpp | 43 +++++++ readme.md | 2 +- 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/CMakeLists.txt create mode 100644 2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main.cpp create mode 100644 2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main2.cpp diff --git a/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/CMakeLists.txt b/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/CMakeLists.txt new file mode 100644 index 00000000..a89245a8 --- /dev/null +++ b/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2289) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2289 main2.cpp) diff --git a/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main.cpp b/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main.cpp new file mode 100644 index 00000000..4eba6d36 --- /dev/null +++ b/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main.cpp @@ -0,0 +1,113 @@ +/// Source : https://leetcode.com/problems/steps-to-make-array-non-decreasing/ +/// Author : liuyubobobo +/// Time : 2022-06-04 + +#include +#include +#include + +using namespace std; + + +/// Linked List Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +template +class Node{ +public: + T value; + Node* next, *prev; + Node(T v, Node* next, Node* prev) : value(v), next(next), prev(prev){} + Node(T v) : Node(v, nullptr, nullptr){} + Node(): Node(T()){} +}; + +template +class LinkedList{ +public: + Node* head, *tail; + + LinkedList(){ + head = new Node(); + tail = new Node(); + head->next = tail; + tail->prev = head; + } + + void add(Node* pre, T v){ + Node* new_node = new Node(v); + add(pre, new_node); + } + + void add_first(T v){ + add(head, v); + } + + void add_last(T v){ + add(tail->prev, v); + } + + void add(Node* pre, Node* new_node){ + new_node->next = pre->next; + pre->next = new_node; + new_node->next->prev = new_node; + new_node->prev = pre; + } + + void add_first(Node* new_node){ + add(head, new_node); + } + + void add_last(Node* new_node){ + add(tail->prev, new_node); + } + + // return node->next + Node* remove(Node* node){ +// assert(!node); + Node* ret = node->next; + node->prev->next = node->next; + node->next->prev = node->prev; + delete node; + return ret; + } +}; + +class Solution { +public: + int totalSteps(vector& nums) { + + LinkedList linkedList; + deque*> del; + for(int i = (int)nums.size() - 1; i >= 0; i --){ + linkedList.add_first(nums[i]); + if(i - 1 >= 0 && nums[i - 1] > nums[i]) + del.push_back(linkedList.head->next); + } + + int res = 0; + while(!del.empty()){ + int n = del.size(); + for(int i = 0; i < n; i ++){ + Node* del_node = del.front(); del.pop_front(); + Node* next = linkedList.remove(del_node); + if(next != linkedList.tail && next->prev != linkedList.head && next->prev->value > next->value){ + if(del.empty() || del.back() != next) + del.push_back(next); + } + } + res ++; + } + return res; + } +}; + + +int main() { + + vector nums1 = {7, 14, 4, 14, 13, 2, 6, 13}; + cout << Solution().totalSteps(nums1) << '\n'; + // 3 + + return 0; +} diff --git a/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main2.cpp b/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main2.cpp new file mode 100644 index 00000000..b2f0f049 --- /dev/null +++ b/2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/main2.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/steps-to-make-array-non-decreasing/ +/// Author : liuyubobobo +/// Time : 2022-06-04 + +#include +#include +#include + +using namespace std; + + +/// Mono Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int totalSteps(vector& nums) { + + int n = nums.size(); + + vector dp(n, 0); + stack s; + for(int i = n - 1; i >= 0; i --){ + while(!s.empty() && nums[i] > nums[s.top()]){ + dp[i] ++; + dp[i] = max(dp[i], dp[s.top()]); + s.pop(); + } + s.push(i); + } + return *max_element(dp.begin(), dp.end()); + } +}; + + +int main() { + + vector nums1 = {7, 14, 4, 14, 13, 2, 6, 13}; + cout << Solution().totalSteps(nums1) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index d541742a..38679a3d 100644 --- a/readme.md +++ b/readme.md @@ -2132,7 +2132,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2286 | [Booking Concert Tickets in Groups](https://leetcode.com/problems/booking-concert-tickets-in-groups/) | [无] | [C++](2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/) | | | | 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [无] | [C++](2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/) | | | | 2288 | [Apply Discount to Prices](https://leetcode.com/problems/apply-discount-to-prices/) | [无] | [C++](2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/) | | | -| | | | | | | +| 2289 | [Steps to Make Array Non-decreasing](https://leetcode.com/problems/steps-to-make-array-non-decreasing/) | [无] | [C++](2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/) | | | | 2290 | [Minimum Obstacle Removal to Reach Corner](https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/) | [无] | [C++](2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/) | | | | 2291 | [Maximum Profit From Trading Stocks](https://leetcode.com/problems/maximum-profit-from-trading-stocks/) | [无] | [C++](2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/) | | | | | | | | | | From edb66d9a9ec4ba7ddfa5232f6c317e156365744e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 4 Jun 2022 21:38:15 -0700 Subject: [PATCH 085/390] 2293-2296 added. --- .../2293-Min-Max-Game/cpp-2293/CMakeLists.txt | 6 + 2001-2500/2293-Min-Max-Game/cpp-2293/main.cpp | 35 +++++ .../cpp-2294/CMakeLists.txt | 6 + .../cpp-2294/main.cpp | 34 +++++ .../cpp-2295/CMakeLists.txt | 6 + .../cpp-2295/main.cpp | 38 +++++ .../cpp-2296/CMakeLists.txt | 6 + .../cpp-2296/main.cpp | 131 ++++++++++++++++++ readme.md | 5 + 9 files changed, 267 insertions(+) create mode 100644 2001-2500/2293-Min-Max-Game/cpp-2293/CMakeLists.txt create mode 100644 2001-2500/2293-Min-Max-Game/cpp-2293/main.cpp create mode 100644 2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/CMakeLists.txt create mode 100644 2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/main.cpp create mode 100644 2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/CMakeLists.txt create mode 100644 2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/main.cpp create mode 100644 2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt create mode 100644 2001-2500/2296-Design-a-Text-Editor/cpp-2296/main.cpp diff --git a/2001-2500/2293-Min-Max-Game/cpp-2293/CMakeLists.txt b/2001-2500/2293-Min-Max-Game/cpp-2293/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2293-Min-Max-Game/cpp-2293/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2293-Min-Max-Game/cpp-2293/main.cpp b/2001-2500/2293-Min-Max-Game/cpp-2293/main.cpp new file mode 100644 index 00000000..2a17647b --- /dev/null +++ b/2001-2500/2293-Min-Max-Game/cpp-2293/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/min-max-game/ +/// Author : liuyubobobo +/// Time : 2022-06-04 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int minMaxGame(vector& nums) { + + while(nums.size() > 1){ + + vector nums2(nums.size() / 2); + for(int i = 0; i < nums2.size(); i ++) + if(i % 2 == 0) nums2[i] = min(nums[2 * i], nums[2 * i + 1]); + else nums2[i] = max(nums[2 * i], nums[2 * i + 1]); + nums = nums2; + } + return nums[0]; + } +}; + + +int main() { + + + return 0; +} diff --git a/2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/CMakeLists.txt b/2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/main.cpp b/2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/main.cpp new file mode 100644 index 00000000..a5a2d7e1 --- /dev/null +++ b/2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/ +/// Author : liuyubobobo +/// Time : 2022-06-04 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int partitionArray(vector& nums, int k) { + + sort(nums.begin(), nums.end()); + + int res = 0; + for(int start = 0, i = 1; i <= nums.size(); i ++) + if(i == nums.size() || nums[i] - nums[start] > k){ + res ++; + start = i; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/CMakeLists.txt b/2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/main.cpp b/2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/main.cpp new file mode 100644 index 00000000..5a811886 --- /dev/null +++ b/2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/replace-elements-in-an-array/ +/// Author : liuyubobobo +/// Time : 2022-06-04 + +#include +#include + +using namespace std; + + +/// Hash Table +/// Time Complexity: O(|op|) +/// Space Complexity: O(max_num) +class Solution { +public: + vector arrayChange(vector& nums, vector>& operations) { + + vector pos(1e6 + 1, -1); + for(int i = 0; i < nums.size(); i ++) + pos[nums[i]] = i; + + for(const vector& op: operations){ + int from_num = op[0], to_num = op[1]; + int from_index = pos[from_num]; + nums[from_index] = to_num; + + pos[from_num] = -1; + pos[to_num] = from_index; + } + return nums; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2296-Design-a-Text-Editor/cpp-2296/main.cpp b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/main.cpp new file mode 100644 index 00000000..f40cd365 --- /dev/null +++ b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/main.cpp @@ -0,0 +1,131 @@ +/// Source : https://leetcode.com/problems/design-a-text-editor/ +/// Author : liuyubobobo +/// Time : 2022-06-04 + +#include +#include + +using namespace std; + + +/// Using Linked List +/// Time Complexity: init: O(1) +/// add: O(|text|) +/// delete: O(k) +/// move: O(k) +/// Space Complexity: O(all_characters) +template +class Node{ +public: + T value; + Node* next, *prev; + Node(T v, Node* next, Node* prev) : value(v), next(next), prev(prev){} + Node(T v) : Node(v, nullptr, nullptr){} + Node(): Node(T()){} +}; + +template +class LinkedList{ +public: + Node* head, *tail; + + LinkedList(){ + head = new Node(); + tail = new Node(); + head->next = tail; + tail->prev = head; + } + + void add(Node* pre, T v){ + Node* new_node = new Node(v); + add(pre, new_node); + } + + void add_first(T v){ + add(head, v); + } + + void add_last(T v){ + add(tail->prev, v); + } + + void add(Node* pre, Node* new_node){ + new_node->next = pre->next; + pre->next = new_node; + new_node->next->prev = new_node; + new_node->prev = pre; + } + + void add_first(Node* new_node){ + add(head, new_node); + } + + void add_last(Node* new_node){ + add(tail->prev, new_node); + } + + Node* remove(Node* node){ +// assert(!node); + Node* ret = node->prev; + node->prev->next = node->next; + node->next->prev = node->prev; + delete node; + return ret; + } +}; + +class TextEditor { + +private: + LinkedList L; + Node* cur; + +public: + TextEditor() { + cur = L.head; + } + + void addText(string text) { + for(char c: text) + L.add(cur, c), cur = cur->next; + } + + int deleteText(int k) { + + int res = 0; + while(k -- && cur != L.head){ + cur = L.remove(cur); + res ++; + } + return res; + } + + string cursorLeft(int k) { + + while(k -- && cur != L.head) cur = cur->prev; + + string ret = ""; + Node* p = cur; + for(int i = 0; i < 10 && p != L.head; i ++) + ret += p->value, p = p->prev; + reverse(ret.begin(), ret.end()); + return ret; + } + + string cursorRight(int k) { + while(k -- && cur->next != L.tail) cur = cur->next; + + string ret = ""; + Node* p = cur; + for(int i = 0; i < 10 && p != L.head; i ++) + ret += p->value, p = p->prev; + reverse(ret.begin(), ret.end()); + return ret; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 38679a3d..c98ceb34 100644 --- a/readme.md +++ b/readme.md @@ -2135,6 +2135,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2289 | [Steps to Make Array Non-decreasing](https://leetcode.com/problems/steps-to-make-array-non-decreasing/) | [无] | [C++](2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/) | | | | 2290 | [Minimum Obstacle Removal to Reach Corner](https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/) | [无] | [C++](2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/) | | | | 2291 | [Maximum Profit From Trading Stocks](https://leetcode.com/problems/maximum-profit-from-trading-stocks/) | [无] | [C++](2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/) | | | +| 2292 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2293 | [Min Max Game](https://leetcode.com/problems/min-max-game/) | [无] | [C++](2001-2500/2293-Min-Max-Game/cpp-2293/) | | | +| 2294 | [Partition Array Such That Maximum Difference Is K](https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/) | [无] | [C++](2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/) | | | +| 2295 | [Replace Elements in an Array](https://leetcode.com/problems/replace-elements-in-an-array/) | [无] | [C++](2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/) | | | +| 2296 | [Design a Text Editor](https://leetcode.com/problems/design-a-text-editor/) | [无] | [C++](2001-2500/2296-Design-a-Text-Editor/cpp-2296/) | | | | | | | | | | ## 力扣中文站比赛 From 0f5c703df161689ec93b3a6d23a80bbda07991be Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 4 Jun 2022 22:28:29 -0700 Subject: [PATCH 086/390] 2296 new algo added. --- .../cpp-2296/CMakeLists.txt | 2 +- .../cpp-2296/main2.cpp | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2296-Design-a-Text-Editor/cpp-2296/main2.cpp diff --git a/2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt index 9a6c5c78..bb71ae36 100644 --- a/2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt +++ b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/CMakeLists.txt @@ -3,4 +3,4 @@ project(D) set(CMAKE_CXX_STANDARD 14) -add_executable(D main.cpp) +add_executable(D main2.cpp) diff --git a/2001-2500/2296-Design-a-Text-Editor/cpp-2296/main2.cpp b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/main2.cpp new file mode 100644 index 00000000..d784d00c --- /dev/null +++ b/2001-2500/2296-Design-a-Text-Editor/cpp-2296/main2.cpp @@ -0,0 +1,75 @@ +/// Source : https://leetcode.com/problems/design-a-text-editor/ +/// Author : liuyubobobo +/// Time : 2022-06-04 + +#include +#include + +using namespace std; + + +/// Using C++ list +/// Time Complexity: init: O(1) +/// add: O(|text|) +/// delete: O(k) +/// move: O(k) +/// Space Complexity: O(all_characters) +class TextEditor { + +private: + list L; + list::iterator iter = L.begin(); + +public: + TextEditor() {} + + void addText(string text) { + for(char c: text) + L.insert(iter, c); + } + + int deleteText(int k) { + + int del_len = 0; + while(k -- && iter != L.begin()){ + iter --; + iter = L.erase(iter); + del_len ++; + } + return del_len; + } + + string cursorLeft(int k) { + + while(k -- && iter != L.begin()) iter --; + + auto p = iter; + string ret = ""; + for(int i = 0; i < 10 && p != L.begin(); i ++){ + p --; + ret += *p; + } + reverse(ret.begin(), ret.end()); + return ret; + } + + string cursorRight(int k) { + + while(k -- && iter != L.end()) iter ++; + + auto p = iter; + string ret = ""; + for(int i = 0; i < 10 && p != L.begin(); i ++){ + p --; + ret += *p; + } + reverse(ret.begin(), ret.end()); + return ret; + } +}; + + +int main() { + + return 0; +} From 01ddc4b5e35be2a77e9f6815658a0f0f96a23d6d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 9 Jun 2022 13:31:52 -0700 Subject: [PATCH 087/390] 0730 solved. --- .../cpp-0730/CMakeLists.txt | 6 ++ .../cpp-0730/main.cpp | 72 +++++++++++++++++++ readme.md | 4 +- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/CMakeLists.txt create mode 100644 0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/main.cpp diff --git a/0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/CMakeLists.txt b/0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/CMakeLists.txt new file mode 100644 index 00000000..941370ff --- /dev/null +++ b/0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0730) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0730 main.cpp) diff --git a/0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/main.cpp b/0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/main.cpp new file mode 100644 index 00000000..f02d9639 --- /dev/null +++ b/0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode.com/problems/count-different-palindromic-subsequences/ +/// Author : liuyubobobo +/// Time : 2022-06-09 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int countPalindromicSubsequences(const string& s) { + + int n = s.size(); + + vector>> dp(4, vector>(n, vector(n, 0))); + for(int i = 0; i < n; i ++) dp[s[i] - 'a'][i][i] = 1; + for(int i = 0; i + 1 < n; i ++){ + if(s[i] == s[i + 1]) dp[s[i] - 'a'][i][i + 1] = 2; + else{ + dp[s[i] - 'a'][i][i + 1] = dp[s[i + 1] - 'a'][i][i + 1] = 1; + } + } + + for(int len = 3; len <= n; len ++){ + for(int l = 0; l + len - 1 < n; l ++){ + int r = l + len - 1; + + for(char border = 0; border < 4; border ++){ + if(s[l] == s[r] && s[l] == 'a' + border){ + dp[border][l][r] = 2; + for(char c = 0; c < 4; c ++) + dp[border][l][r] += dp[c][l + 1][r - 1]; + dp[border][l][r] %= MOD; + } + else{ + dp[border][l][r] = dp[border][l][r - 1] + dp[border][l + 1][r] - dp[border][l + 1][r - 1] + MOD; + dp[border][l][r] %= MOD; + } + } + } + } + + long long res = 0; + for(int border = 0; border < 4; border ++) + res += dp[border][0][n - 1]; + return res % MOD; + } +}; + + +int main() { + + cout << Solution().countPalindromicSubsequences("bccb") << '\n'; + // 6 + + cout << Solution().countPalindromicSubsequences("abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba") << '\n'; + // 104860361 + + cout << Solution().countPalindromicSubsequences("baccdabbccbdbbbcdbbcdbacdabaaddcdbcabdbddadbdaaadbbdaabbdaccdabbddaaccdbbcdbcdbbaaababdddbdccbbaddabdbdabddbaccddabaccadacddbbbdacacaaddaccbabcbcbbcbcadccbccddcddccdabdabaddacdcdcbdadabbcddacbbbbacbcadbccacddddacbdcddbddbddbcabcbdacadbdccabddaacadddbadbccdacbaabababcababccddacabadbcaacbadcbacaadabbadabaabdbcabdabaddabdcacbdbacbcdcacdcaaccadabacbadcdabddccaaacdaadacbbcadcbbccaccbbbdcbcaabbbddcdaaddbdbbdacdaabacbacdabbbdbdaaddbddadbabcdabaddbbadcaadcddcaaddcdbbbbddcccaacdbdbcccccddadbacbdbcacabddaabcbbbbcadbbcabbabcbcdccdcaddabbbddbbdadbbbdadbbdaaacbbbbdbbcadcbbcddddcacdcbddcaacbaccacaddcdacbadccaabdabadbbcccabddcbbcdaacddaacccbacccacbddbcdcdaabbbacdcbabaaaaaacdabcddbcbaccabacddaccadaaddddaaaccbccdbdcdcdbdccdcbdbddbcdbcbdaacaccdabdbabcdbdbadcbcbbaabaccbbababdbbddbcacadbaaadbcccadaddbccddbcadaccbaddcdacaabbbbdbcddacbdccddaadabbbaccddbcbacbbbcbbbbcbbccdbaccdddaddaaacdababcdabdacdbaacdadbaabcadcddccabcccadabbdbbadbcadbdbaddaabbbcdbbbddcdbddbabcdccddccbaccbabcdcddddabbabdccbaddccacdddadcaadd") << endl; + + return 0; +} diff --git a/readme.md b/readme.md index c98ceb34..855c61b2 100644 --- a/readme.md +++ b/readme.md @@ -734,7 +734,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 727 | [Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/description/) | [solution](https://leetcode.com/problems/minimum-window-subsequence/solution/) | [C++](0501-1000/cpp-Minimum-Window-Subsequence/cpp-0727/) | | | | 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/description/) | [solution](https://leetcode.com/problems/self-dividing-numbers/solution/) | [C++](0501-1000/0728-Self-Dividing-Numbers/cpp-0728/) | | | | 729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i/description/) | [solution](https://leetcode.com/problems/my-calendar-i/solution/) | [C++](0501-1000/0729-My-Calendar-I/cpp-0729/) | | | -| | | | | | | +| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences/) | [无] | [C++](0501-1000/0730-Count-Different-Palindromic-Subsequences/cpp-0730/) | | | | 731 | [My Calendar II](https://leetcode.com/problems/my-calendar-ii/description/) | [solution](https://leetcode.com/problems/my-calendar-ii/solution/) | [C++](0501-1000/0731-My-Calendar-II/cpp-0731/) | | | | 732 | [My Calendar III](https://leetcode.com/problems/my-calendar-iii/description/) | [solution](https://leetcode.com/problems/my-calendar-iii/solution/) | [C++](0501-1000/0732-My-Calendar-III/cpp-0732/) | | | | 733 | [Flood Fill](https://leetcode.com/problems/flood-fill/description/) | [solution](https://leetcode.com/problems/flood-fill/solution/) | [C++](0501-1000/0733-Flood-Fill/cpp-0733/) | | | @@ -2132,7 +2132,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2286 | [Booking Concert Tickets in Groups](https://leetcode.com/problems/booking-concert-tickets-in-groups/) | [无] | [C++](2001-2500/2286-Booking-Concert-Tickets-in-Groups/cpp-2286/) | | | | 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [无] | [C++](2001-2500/2287-Rearrange-Characters-to-Make-Target-String/cpp-2287/) | | | | 2288 | [Apply Discount to Prices](https://leetcode.com/problems/apply-discount-to-prices/) | [无] | [C++](2001-2500/2288-Apply-Discount-to-Prices/cpp-2288/) | | | -| 2289 | [Steps to Make Array Non-decreasing](https://leetcode.com/problems/steps-to-make-array-non-decreasing/) | [无] | [C++](2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/) | | | +| 2289 | [Steps to Make Array Non-decreasing](https://leetcode.com/problems/steps-to-make-array-non-decreasing/) | [无]
[缺:用 C++ STL list] | [C++](2001-2500/2289-Steps-to-Make-Array-Non-decreasing/cpp-2289/) | | | | 2290 | [Minimum Obstacle Removal to Reach Corner](https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/) | [无] | [C++](2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/) | | | | 2291 | [Maximum Profit From Trading Stocks](https://leetcode.com/problems/maximum-profit-from-trading-stocks/) | [无] | [C++](2001-2500/2291-Maximum-Profit-From-Trading-Stocks/cpp-2291/) | | | | 2292 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | From 260a4d52c5d513899c359af1607a69307d57f900 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 9 Jun 2022 16:56:14 -0700 Subject: [PATCH 088/390] 2263 solved. --- .../cpp-2263/CMakeLists.txt | 6 ++ .../cpp-2263/main.cpp | 77 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt create mode 100644 2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp diff --git a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt new file mode 100644 index 00000000..3ece3dd8 --- /dev/null +++ b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2263) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2263 main.cpp) diff --git a/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp new file mode 100644 index 00000000..be4716ad --- /dev/null +++ b/2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/main.cpp @@ -0,0 +1,77 @@ +/// Source : https://leetcode.com/problems/make-array-non-decreasing-or-non-increasing/ +/// Author : liuyubobobo +/// Time : 2022-06-09 + +#include +#include +#include + +using namespace std; + + +/// DP + Basic Optimization +/// Time Complexity: O(n * maxv) +/// Space Complexity: O(n * maxv) +class Solution { + +public: + int convertArray(vector& nums) { + + int n = nums.size(), maxv = *max_element(nums.begin(), nums.end()); + + int res1 = solve(n, nums, maxv); + + reverse(nums.begin(), nums.end()); + int res2 = solve(n, nums, maxv); + + return min(res1, res2); + } + +private: + int solve(int n, const vector& nums, int maxv){ + + vector dp(maxv + 1), premin(maxv + 1); + for(int v = 0; v <= maxv; v ++){ + dp[v] = abs(nums[0] - v); + if(v) premin[v] = min(premin[v - 1], dp[v]); + else premin[v] = dp[v]; + } + + for(int i = 1; i < n; i ++){ + vector tdp(maxv + 1); + for(int v = 0; v <= maxv; v ++) + tdp[v] = premin[v] + abs(nums[i] - v); + + premin[0] = tdp[0]; + for(int v = 1; v <= maxv; v ++) premin[v] = min(premin[v - 1], tdp[v]); + dp = tdp; + } + return *min_element(dp.begin(), dp.end()); + } +}; + + +int main() { + + vector nums1 = {3,2,4,5,0}; + cout << Solution().convertArray(nums1) << '\n'; + // 4 + + vector nums2 = {2,2,3,4}; + cout << Solution().convertArray(nums2) << '\n'; + // 0 + + vector nums3 = {0}; + cout << Solution().convertArray(nums3) << '\n'; + // 0 + + vector nums4 = {0,2,8,0,3}; + cout << Solution().convertArray(nums4) << '\n'; + // 8 + + vector nums5 = {4,2,6,7}; + cout << Solution().convertArray(nums5) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 855c61b2..5c20f416 100644 --- a/readme.md +++ b/readme.md @@ -2106,7 +2106,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [无] | [C++](2001-2500/2260-Minimum-Consecutive-Cards-to-Pick-Up/cpp-2260/) | | | | 2261 | [K Divisible Elements Subarrays](https://leetcode.com/problems/k-divisible-elements-subarrays/) | [无] | [C++](2001-2500/2261-K-Divisible-Elements-Subarrays/cpp-2261/) | | | | 2262 | [Total Appeal of A String](https://leetcode.com/problems/total-appeal-of-a-string/) | [无] | [C++](2001-2500/2262-Total-Appeal-of-A-String/cpp-2262/) | | | -| | | | | | | +| 2263 | [Make Array Non-decreasing or Non-increasing](https://leetcode.com/problems/make-array-non-decreasing-or-non-increasing/) | [无] | [C++](2001-2500/2263-Make-Array-Non-decreasing-or-Non-increasing/cpp-2263/) | | | | 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [无] | [C++](2001-2500/2264-Largest-3-Same-Digit-Number-in-String/cpp-2264/) | | | | 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [无] | [C++](2001-2500/2265-Count-Nodes-Equal-to-Average-of-Subtree/cpp-2265/) | | | | 2266 | [Count Number of Texts](https://leetcode.com/problems/count-number-of-texts/) | [无] | [C++](2001-2500/2266-Count-Number-of-Texts/cpp-2266/) | | | From 1d7e7aa8dfd59d25521654edde1d468f702c6b35 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 9 Jun 2022 17:28:25 -0700 Subject: [PATCH 089/390] 2297 solved. --- .../2297-Jump-Game-IX/cpp-2297/CMakeLists.txt | 6 ++ 2001-2500/2297-Jump-Game-IX/cpp-2297/main.cpp | 66 +++++++++++++++++++ readme.md | 1 + 3 files changed, 73 insertions(+) create mode 100644 2001-2500/2297-Jump-Game-IX/cpp-2297/CMakeLists.txt create mode 100644 2001-2500/2297-Jump-Game-IX/cpp-2297/main.cpp diff --git a/2001-2500/2297-Jump-Game-IX/cpp-2297/CMakeLists.txt b/2001-2500/2297-Jump-Game-IX/cpp-2297/CMakeLists.txt new file mode 100644 index 00000000..5b098bc9 --- /dev/null +++ b/2001-2500/2297-Jump-Game-IX/cpp-2297/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2297) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2297 main.cpp) diff --git a/2001-2500/2297-Jump-Game-IX/cpp-2297/main.cpp b/2001-2500/2297-Jump-Game-IX/cpp-2297/main.cpp new file mode 100644 index 00000000..eade4ce4 --- /dev/null +++ b/2001-2500/2297-Jump-Game-IX/cpp-2297/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/jump-game-ix/ +/// Author : liuyubobobo +/// Time : 2022-06-09 + +#include +#include +#include + +using namespace std; + + +/// Mono Stack + DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long minCost(vector& nums, vector& costs) { + + int n = nums.size(); + + vector right_larger_or_equal(n, n); + vector stack; + for(int i = n - 1; i >= 0; i --){ + while(!stack.empty() && nums[i] > nums[stack.back()]) stack.pop_back(); + if(!stack.empty()) right_larger_or_equal[i] = stack.back(); + stack.push_back(i); + } +// for(int e: right_larger_or_equal) cout << e << ' '; cout << '\n'; + + vector right_smaller(n, n); + stack.clear(); + for(int i = n - 1; i >= 0; i --){ + while(!stack.empty() && nums[i] <= nums[stack.back()]) stack.pop_back(); + if(!stack.empty()) right_smaller[i] = stack.back(); + stack.push_back(i); + } +// for(int e: right_smaller) cout << e << ' '; cout << '\n'; + + vector dp(n, LONG_LONG_MAX / 2); + dp[0] = 0; + for(int i = 0; i < n; i ++){ + int to = right_larger_or_equal[i]; + if(to < n) + dp[to] = min(dp[to], dp[i] + costs[to]); + + to = right_smaller[i]; + if(to < n) + dp[to] = min(dp[to], dp[i] + costs[to]); + } + return dp.back(); + } +}; + + +int main() { + + vector nums1 = {3, 2, 4, 4, 1}, costs1 = {3, 7, 6, 4, 2}; + cout << Solution().minCost(nums1, costs1) << '\n'; + // 8 + + vector nums2 = {0, 1, 2}, costs2 = {1, 1, 1}; + cout << Solution().minCost(nums2, costs2) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 5c20f416..be9177c2 100644 --- a/readme.md +++ b/readme.md @@ -2140,6 +2140,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2294 | [Partition Array Such That Maximum Difference Is K](https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/) | [无] | [C++](2001-2500/2294-Partition-Array-Such-That-Maximum-Difference-Is-K/cpp-2294/) | | | | 2295 | [Replace Elements in an Array](https://leetcode.com/problems/replace-elements-in-an-array/) | [无] | [C++](2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/) | | | | 2296 | [Design a Text Editor](https://leetcode.com/problems/design-a-text-editor/) | [无] | [C++](2001-2500/2296-Design-a-Text-Editor/cpp-2296/) | | | +| 2297 | [Jump Game IX](https://leetcode.com/problems/jump-game-ix/) | [无] | [C++](2001-2500/2297-Jump-Game-IX/cpp-2297/) | | | | | | | | | | ## 力扣中文站比赛 From 6582bc382e0a2c5b6f8cc8b3790eeffcee4c0cc1 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 11 Jun 2022 11:00:03 -0700 Subject: [PATCH 090/390] 2299-2302 solved. --- .../cpp-2299/CMakeLists.txt | 6 +++ .../cpp-2299/main.cpp | 48 +++++++++++++++++ .../cpp-2300/CMakeLists.txt | 6 +++ .../cpp-2300/main.cpp | 44 +++++++++++++++ .../cpp-2301/CMakeLists.txt | 6 +++ .../cpp-2301/main.cpp | 39 ++++++++++++++ .../cpp-2302/CMakeLists.txt | 6 +++ .../cpp-2302/main.cpp | 54 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 214 insertions(+) create mode 100644 2001-2500/2299-Strong-Password-Checker-II/cpp-2299/CMakeLists.txt create mode 100644 2001-2500/2299-Strong-Password-Checker-II/cpp-2299/main.cpp create mode 100644 2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/CMakeLists.txt create mode 100644 2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/main.cpp create mode 100644 2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/CMakeLists.txt create mode 100644 2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/main.cpp create mode 100644 2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/CMakeLists.txt create mode 100644 2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/main.cpp diff --git a/2001-2500/2299-Strong-Password-Checker-II/cpp-2299/CMakeLists.txt b/2001-2500/2299-Strong-Password-Checker-II/cpp-2299/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2299-Strong-Password-Checker-II/cpp-2299/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2299-Strong-Password-Checker-II/cpp-2299/main.cpp b/2001-2500/2299-Strong-Password-Checker-II/cpp-2299/main.cpp new file mode 100644 index 00000000..dee9e099 --- /dev/null +++ b/2001-2500/2299-Strong-Password-Checker-II/cpp-2299/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/strong-password-checker-ii/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool strongPasswordCheckerII(string password) { + + if(password.size() < 8) return false; + + bool lower_ok = false; + for(char c: password) if(islower(c)) lower_ok = true; + if(!lower_ok) return false; + + bool upper_ok = false; + for(char c: password) if(isupper(c)) upper_ok = true; + if(!upper_ok) return false; + + bool digit_ok = false; + for(char c: password) if(isdigit(c)) digit_ok = true; + if(!digit_ok) return false; + + bool special_ok = false; + string special = "!@#$%^&*()-+"; + for(char c: password) if(special.find(c) != string::npos) special_ok = true; + if(!special_ok) return false; + + for(int i = 1; i < password.size(); i ++) + if(password[i - 1] == password[i]) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/CMakeLists.txt b/2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/main.cpp b/2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/main.cpp new file mode 100644 index 00000000..68d51c20 --- /dev/null +++ b/2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/successful-pairs-of-spells-and-potions/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogm) +/// Space Complexity: O(1) +class Solution { +public: + vector successfulPairs(vector& spells, vector& potions, long long success) { + + vector spells_ll(spells.size()); + for(int i = 0; i < spells.size(); i ++) + spells_ll[i] = spells[i]; + + vector potions_ll(potions.size()); + for(int i = 0; i < potions.size(); i ++) + potions_ll[i] = potions[i]; + sort(potions_ll.begin(), potions_ll.end()); + + vector res(spells.size()); + for(int i = 0; i < spells.size(); i ++){ + long long energy = spells_ll[i]; + long long least_potion = success / energy; + if(least_potion * energy < success) least_potion ++; + + res[i] = potions_ll.end() - lower_bound(potions_ll.begin(), potions_ll.end(), least_potion); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/CMakeLists.txt b/2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/main.cpp b/2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/main.cpp new file mode 100644 index 00000000..8ba99bc2 --- /dev/null +++ b/2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/match-substring-after-replacement/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include + +using namespace std; + + +/// Using Map Table +/// Time Complexity: O(n * m) +/// Space Complexity: O(1) +class Solution { +public: + bool matchReplacement(string s, string sub, vector>& mappings) { + + vector> map_table(256, vector(256, false)); + for(const vector& mapping: mappings){ + char old_ch = mapping[0], new_ch = mapping[1]; + map_table[old_ch][new_ch] = true; + } + + for(int i = 0; i + sub.size() <= s.size(); i ++){ + + bool ok = true; + for(int j = 0; j < sub.size() && ok; j ++) + ok = sub[j] == s[i + j] || map_table[sub[j]][s[i + j]]; + if(ok) return true; + } + return false; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/CMakeLists.txt b/2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/main.cpp b/2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/main.cpp new file mode 100644 index 00000000..11396fe6 --- /dev/null +++ b/2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/count-subarrays-with-score-less-than-k/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long countSubarrays(vector& nums, long long k) { + + int l = 0, r = -1, n = nums.size(); + long long cur_sum = 0, res = 0; + while(l < n){ + if(r + 1 < n && 1ll * (cur_sum + nums[r + 1]) * (r + 1 - l + 1) < k){ + r ++; + cur_sum += nums[r]; + } + else{ + if(l <= r){ + res += (r - l + 1); + cur_sum -= nums[l]; + } + l ++; + r = max(r, l - 1); + } + } + return res; + } +}; + + +int main() { + + vector nums1 = {2, 1, 4, 3, 5}; + cout << Solution().countSubarrays(nums1, 10) << '\n'; + // 6 + + vector nums2 = {1, 1, 1}; + cout << Solution().countSubarrays(nums2, 5) << '\n'; + // 5 + + vector nums3 = {2, 1, 4, 3, 5}; + cout << Solution().countSubarrays(nums3, 3) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index be9177c2..98e87f7f 100644 --- a/readme.md +++ b/readme.md @@ -2142,6 +2142,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2296 | [Design a Text Editor](https://leetcode.com/problems/design-a-text-editor/) | [无] | [C++](2001-2500/2296-Design-a-Text-Editor/cpp-2296/) | | | | 2297 | [Jump Game IX](https://leetcode.com/problems/jump-game-ix/) | [无] | [C++](2001-2500/2297-Jump-Game-IX/cpp-2297/) | | | | | | | | | | +| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii/) | [无] | [C++](2001-2500/2299-Strong-Password-Checker-II/cpp-2299/) | | | +| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/) | [无] | [C++](2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/) | | | +| 2301 | [Match Substring After Replacement](https://leetcode.com/problems/match-substring-after-replacement/) | [无] | [C++](2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/) | | | +| 2302 | [Count Subarrays With Score Less Than K](https://leetcode.com/problems/count-subarrays-with-score-less-than-k/) | [无] | [C++](2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/) | | | +| | | | | | | ## 力扣中文站比赛 From 74cf562652bdaaf751d6550d9121d0e602fbdb0e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 12 Jun 2022 00:06:08 -0700 Subject: [PATCH 091/390] 2303-2306 solved. --- .../cpp-2303/CMakeLists.txt | 6 ++ .../cpp-2303/main.cpp | 41 +++++++++++++ .../cpp-2304/CMakeLists.txt | 6 ++ .../cpp-2304/main.cpp | 36 +++++++++++ .../cpp-2305/CMakeLists.txt | 6 ++ .../cpp-2305/main.cpp | 48 +++++++++++++++ .../cpp-2306/CMakeLists.txt | 6 ++ .../2306-Naming-a-Company/cpp-2306/main.cpp | 61 +++++++++++++++++++ readme.md | 6 +- 9 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/CMakeLists.txt create mode 100644 2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/main.cpp create mode 100644 2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/CMakeLists.txt create mode 100644 2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/main.cpp create mode 100644 2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/CMakeLists.txt create mode 100644 2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/main.cpp create mode 100644 2001-2500/2306-Naming-a-Company/cpp-2306/CMakeLists.txt create mode 100644 2001-2500/2306-Naming-a-Company/cpp-2306/main.cpp diff --git a/2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/CMakeLists.txt b/2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/main.cpp b/2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/main.cpp new file mode 100644 index 00000000..ab211112 --- /dev/null +++ b/2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/calculate-amount-paid-in-taxes/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + double calculateTax(vector>& brackets, int income) { + + if(income <= brackets[0][0]){ + return income * brackets[0][1] / (double)100.0; + } + + int left = income - brackets[0][0]; + double res = brackets[0][0] * brackets[0][1] / (double)100.0; + for(int i = 1; i < brackets.size() && left; i ++){ + int x = brackets[i][0] - brackets[i - 1][0]; + int t = min(x, left); + left -= t; + res += t * brackets[i][1] / (double)100.0; + } + return res; + } +}; + + +int main() { + + vector> br1 = {{3, 50}, {7, 10}, {12, 25}}; + cout << Solution().calculateTax(br1, 10) << '\n'; + + return 0; +} diff --git a/2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/CMakeLists.txt b/2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/main.cpp b/2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/main.cpp new file mode 100644 index 00000000..89862b42 --- /dev/null +++ b/2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/minimum-path-cost-in-a-grid/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * m * m) +/// Space Compelxity: O(n * m) +class Solution { +public: + int minPathCost(vector>& grid, vector>& moveCost) { + + int R = grid.size(), C = grid[0].size(); + vector> dp(R, vector(C, INT_MAX)); + for(int j = 0; j < C; j ++) dp[0][j] = grid[0][j]; + + for(int i = 0; i + 1 < R; i ++) + for(int j = 0; j < C; j ++){ + for(int k = 0; k < C; k ++) + dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + moveCost[grid[i][j]][k] + grid[i + 1][k]); + } + return *min_element(dp[R - 1].begin(), dp[R - 1].end()); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/CMakeLists.txt b/2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/main.cpp b/2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/main.cpp new file mode 100644 index 00000000..3860c524 --- /dev/null +++ b/2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/fair-distribution-of-cookies/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(k * 3^n) +/// Space Complexity: O(k * 2^n) +class Solution { +public: + int distributeCookies(vector& cookies, int k) { + + int n = cookies.size(); + + vector table(1 << n, 0); + for(int state = 1; state < (1 << n); state ++){ + int p = __builtin_ffs(state) - 1; + table[state] = table[state - (1 << p)] + cookies[p]; + } + + vector> dp(k + 1, vector(1 << n, -1)); + return dfs((1 << n) - 1, k, table, dp); + } + +private: + int dfs(int state, int k, const vector& table, vector>& dp){ + + if(k == 1) return table[state]; + if(dp[k][state] != -1) return dp[k][state]; + + int res = table[state]; + for (int s = state; s; s = (s - 1) & state) { + res = min(res, max(table[s], dfs(state - s, k - 1, table, dp))); + } + return dp[k][state] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2306-Naming-a-Company/cpp-2306/CMakeLists.txt b/2001-2500/2306-Naming-a-Company/cpp-2306/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2306-Naming-a-Company/cpp-2306/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2306-Naming-a-Company/cpp-2306/main.cpp b/2001-2500/2306-Naming-a-Company/cpp-2306/main.cpp new file mode 100644 index 00000000..ae3f2f5f --- /dev/null +++ b/2001-2500/2306-Naming-a-Company/cpp-2306/main.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode.com/problems/naming-a-company/ +/// Author : liuyubobobo +/// Time : 2022-06-11 + +#include +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long distinctNames(vector& ideas) { + + int n = ideas.size(); + + unordered_set ideas_set(ideas.begin(), ideas.end()); + + vector> can_change(26, vector(n, false)); + vector> ok(26, vector(26, 0)); + for(int i = 0; i < n; i ++){ + char o_first = ideas[i][0]; + for(char c = 'a'; c <= 'z'; c ++){ + ideas[i][0] = c; + if(c != o_first && !ideas_set.count(ideas[i])){ + can_change[c - 'a'][i] = true; + ok[o_first - 'a'][c - 'a'] ++; + } + + } + ideas[i][0] = o_first; + } + + long long res = 0; + for(int i = 0; i < n; i ++){ + char o_first = ideas[i][0]; + for(char c = 'a'; c <= 'z'; c ++) + if(can_change[c - 'a'][i]) res += ok[c - 'a'][o_first - 'a']; + } + return res; + } +}; + + +int main() { + + vector ideas1 = {"coffee","donuts","time","toffee"}; + cout << Solution().distinctNames(ideas1) << '\n'; + // 6 + + vector ideas2 = {"lack","back"}; + cout << Solution().distinctNames(ideas2) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index 98e87f7f..6b8cd25a 100644 --- a/readme.md +++ b/readme.md @@ -2141,11 +2141,15 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2295 | [Replace Elements in an Array](https://leetcode.com/problems/replace-elements-in-an-array/) | [无] | [C++](2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/) | | | | 2296 | [Design a Text Editor](https://leetcode.com/problems/design-a-text-editor/) | [无] | [C++](2001-2500/2296-Design-a-Text-Editor/cpp-2296/) | | | | 2297 | [Jump Game IX](https://leetcode.com/problems/jump-game-ix/) | [无] | [C++](2001-2500/2297-Jump-Game-IX/cpp-2297/) | | | -| | | | | | | +| 2298 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii/) | [无] | [C++](2001-2500/2299-Strong-Password-Checker-II/cpp-2299/) | | | | 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/) | [无] | [C++](2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/) | | | | 2301 | [Match Substring After Replacement](https://leetcode.com/problems/match-substring-after-replacement/) | [无] | [C++](2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/) | | | | 2302 | [Count Subarrays With Score Less Than K](https://leetcode.com/problems/count-subarrays-with-score-less-than-k/) | [无] | [C++](2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/) | | | +| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes/) | [无] | [C++](2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/) | | | +| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [无] | [C++](2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/) | | | +| 2305 | [Fair Distribution of Cookies](https://leetcode.com/problems/fair-distribution-of-cookies/) | [无] | [C++](2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/) | | | +| 2306 | [Naming a Company](https://leetcode.com/problems/naming-a-company/) | [无] | [C++](2001-2500/2306-Naming-a-Company/cpp-2306/) | | | | | | | | | | ## 力扣中文站比赛 From db4230874fd0353000e718564aa7e54bafdefa67 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 12 Jun 2022 16:24:42 -0700 Subject: [PATCH 092/390] 1051 solved. --- .../cpp-1051/CMakeLists.txt | 6 ++++ .../1051-Height-Checker/cpp-1051/main.cpp | 33 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1051-Height-Checker/cpp-1051/CMakeLists.txt create mode 100644 1001-1500/1051-Height-Checker/cpp-1051/main.cpp diff --git a/1001-1500/1051-Height-Checker/cpp-1051/CMakeLists.txt b/1001-1500/1051-Height-Checker/cpp-1051/CMakeLists.txt new file mode 100644 index 00000000..0cc7ac25 --- /dev/null +++ b/1001-1500/1051-Height-Checker/cpp-1051/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_1051) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1051 main.cpp) diff --git a/1001-1500/1051-Height-Checker/cpp-1051/main.cpp b/1001-1500/1051-Height-Checker/cpp-1051/main.cpp new file mode 100644 index 00000000..ea6d0669 --- /dev/null +++ b/1001-1500/1051-Height-Checker/cpp-1051/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/height-checker/s +/// Author : liuyubobobo +/// Time : 2022-06-12 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int heightChecker(vector& heights) { + + vector final(heights.begin(), heights.end()); + sort(final.begin(), final.end()); + + int res = 0; + for(int i = 0; i < heights.size(); i ++) + res += heights[i] != final[i]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 6b8cd25a..1b20eab1 100644 --- a/readme.md +++ b/readme.md @@ -1031,7 +1031,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1049 | [Last Stone Weight II](https://leetcode.com/problems/last-stone-weight-ii/) | [无] | [C++](1001-1500/1049-Last-Stone-Weight-II/cpp-1049/) | | | | 1050 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| | | | | | | +| 1051 | [Height Checker](https://leetcode.com/problems/height-checker/) | [无] | [C++](1001-1500/1051-Height-Checker/cpp-1051/) | | | | 1052 | [Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner/) | [无] | [C++](1001-1500/1052-Grumpy-Bookstore-Owner/cpp-1052/) | | | | | | | | | | | 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [solution](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/solution/) | [C++](1001-1500/1059-All-Paths-from-Source-Lead-to-Destination/cpp-1059/) | | | From cd6632da164222f51fa34794fe40876234302776 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 14 Jun 2022 16:08:39 -0700 Subject: [PATCH 093/390] 0719 updated. --- .../cpp-0719/main.cpp | 58 ++++++++++--------- readme.md | 2 +- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/main.cpp b/0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/main.cpp index b35b51fe..1b10ca51 100644 --- a/0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/main.cpp +++ b/0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/main.cpp @@ -1,56 +1,58 @@ /// Source : https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/ /// Author : liuyubobobo -/// Time : 2017-10-28 +/// Time : 2022-06-14 #include #include #include -#include using namespace std; -/// Time Complexity: O(nlogn + n^2 + W) -/// Space Complexity: O(W), where W = max(nums) - min(nums) + +/// Binary Search +/// Time Complexity: O(log(max_dis) * nlogn) +/// Space Complexity: O(1) class Solution { public: int smallestDistancePair(vector& nums, int k) { - int dis[1000000]; - for(int i = 0 ; i < 1000000 ; i ++) - dis[i] = 0; + int n = nums.size(); sort(nums.begin(), nums.end()); - for(int i = 0 ; i < nums.size() ; i ++) - for(int j = i + 1 ; j < nums.size() ; j ++){ - //cout << nums[j] - nums[i] << endl; - dis[nums[j]-nums[i]] ++; - } - - int index = 0; - for(int i = 0 ; i < 1000000 ; i ++){ - index += dis[i]; - if(k <= index) - return i; + int l = 0, r = nums.back() - nums[0]; + while(l < r){ + int d = (l + r) / 2; + if(dis_cnt_less_than_or_equal_to(n, nums, d) >= k) r = d; + else l = d + 1; } + return l; + } + +private: + int dis_cnt_less_than_or_equal_to(int n, const vector& nums, int d){ - return -1; + int cnt = 0; + for(int i = 0; i < n; i ++){ + auto iter = upper_bound(nums.begin(), nums.end(), nums[i] + d); + cnt += iter - nums.begin() - i - 1; + } + return cnt; } }; + int main() { - int nums1[] = {1, 3, 1}; - int k1 = 1; - vector vec1(nums1, nums1 + sizeof(nums1)/sizeof(int)); - cout << Solution().smallestDistancePair(vec1, k1) << endl; + vector nums1 = {1, 3, 1}; + cout << Solution().smallestDistancePair(nums1, 1) << endl; // 0 - // --- + vector nums2 = {1, 1, 1}; + cout << Solution().smallestDistancePair(nums2, 2) << endl; + // 0 - int nums2[] = {1, 6, 1}; - int k2 = 3; - vector vec2(nums2, nums2 + sizeof(nums2)/sizeof(int)); - cout << Solution().smallestDistancePair(vec2, k2) << endl; + vector nums3 = {1, 6, 1}; + cout << Solution().smallestDistancePair(nums3, 3) << endl; // 5 return 0; diff --git a/readme.md b/readme.md index 1b20eab1..62f3caeb 100644 --- a/readme.md +++ b/readme.md @@ -723,7 +723,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 716 | [Max Stack](https://leetcode.com/problems/max-stack/description/) | [solution](https://leetcode.com/problems/max-stack/solution/) | [C++](0501-1000/0716-Max-Stack/cpp-0716/) | | | | 717 | [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/description/) | | [C++](0501-1000/0717-1-bit-and-2-bit-Characters/cpp-0717/) | | | | 718 | [Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/) | [缺:Rolling Hash] | [C++](0501-1000/0718-Maximum-Length-of-Repeated-Subarray/cpp-0718/) | | | -| 719 | [Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/) | [缺:二分搜索] | [C++](0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/) | | | +| 719 | [Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/) | [solution](https://leetcode.com/problems/find-k-th-smallest-pair-distance/solution/) | [C++](0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/) | | | | 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/description/) | [solution](https://leetcode.com/problems/longest-word-in-dictionary/solution/) | [C++](0501-1000/0720-Longest-Word-in-Dictionary/cpp-0720/) | | | | 721 | [Accounts Merge](https://leetcode.com/problems/accounts-merge/description/) | [solution](https://leetcode.com/problems/accounts-merge/solution/) | [C++](0501-1000/0721-Accounts-Merge/cpp-0721/) | | | | 722 | [Remove Comments](https://leetcode.com/problems/remove-comments/description/) | [solution](https://leetcode.com/problems/remove-comments/solution/) | [C++](0501-1000/0722-Remove-Comments/cpp-0722/) | | | From 78eed94512fd4b77352bf4bef0082c63ac0d55da Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 14 Jun 2022 22:13:44 -0700 Subject: [PATCH 094/390] 0527 solved. --- .../cpp-0527/CMakeLists.txt | 6 ++ .../0527-Word-Abbreviation/cpp-0527/main.cpp | 90 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0527-Word-Abbreviation/cpp-0527/CMakeLists.txt create mode 100644 0501-1000/0527-Word-Abbreviation/cpp-0527/main.cpp diff --git a/0501-1000/0527-Word-Abbreviation/cpp-0527/CMakeLists.txt b/0501-1000/0527-Word-Abbreviation/cpp-0527/CMakeLists.txt new file mode 100644 index 00000000..575077dc --- /dev/null +++ b/0501-1000/0527-Word-Abbreviation/cpp-0527/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0527) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0527 main.cpp) diff --git a/0501-1000/0527-Word-Abbreviation/cpp-0527/main.cpp b/0501-1000/0527-Word-Abbreviation/cpp-0527/main.cpp new file mode 100644 index 00000000..661a29c2 --- /dev/null +++ b/0501-1000/0527-Word-Abbreviation/cpp-0527/main.cpp @@ -0,0 +1,90 @@ +/// Source : https://leetcode.com/problems/word-abbreviation/ +/// Author : liuyubobobo +/// Time : 2022-06-14 + +#include +#include +#include + +using namespace std; + + +/// Hash Map + Simulation +/// Time Complexity: O(len * 26^2 + nlogn + n * len^2) +/// Space Compelxity: O(len * 26^2 + n) +class Solution { +public: + vector wordsAbbreviation(const vector& words) { + + int n = words.size(); + + vector res(n, ""); + + vector table[26][26][401]; + for(int i = 0; i < n; i ++){ + const string& word = words[i]; + table[word[0] - 'a'][word.back() - 'a'][word.size()].push_back(i); + } + + for(int first = 0; first < 26; first ++) + for(int last = 0; last < 26; last ++) + for(int len = 2; len <= 400; len ++){ + const vector& v = table[first][last][len]; + if(v.empty()) continue; + if(v.size() == 1){ + res[v[0]] = get_abbr(words[v[0]], 0); + continue; + } + + vector> data(v.size()); + for(int i = 0; i < v.size(); i ++) data[i] = {words[v[i]], v[i]}; + sort(data.begin(), data.end()); + + for(int i = 0; i < data.size(); i ++){ + const string& word = data[i].first; + int index = data[i].second; + + for(int prefix_len = 2;; prefix_len ++){ + bool ok1 = i - 1 < 0 || word.substr(0, prefix_len) != data[i - 1].first.substr(0, prefix_len); + bool ok2 = i + 1 >= data.size() || word.substr(0, prefix_len) != data[i + 1].first.substr(0, prefix_len); + if(ok1 && ok2){ + res[index] = get_abbr(word, prefix_len - 1); + break; + } + } + assert(res[index] != ""); + } + } + return res; + } + +private: + // [0...prefix_end_index] + num + last_character + string get_abbr(const string& s, int prefix_end_index){ + + string res = s.substr(0, prefix_end_index + 1); + int len = s.size() - (prefix_end_index + 2); + if(len) res += to_string(len); + res += s.back(); + + return res.size() < s.size() ? res : s; + } +}; + + +void print_vec(const vector& res){ + for(const string& e: res) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector words1 = {"like","god","internal","me","internet","interval","intension","face","intrusion"}; + print_vec(Solution().wordsAbbreviation(words1)); + // ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"] + + vector words2 = {"abcdefg","abccefg","abcckkg"}; + print_vec(Solution().wordsAbbreviation(words2)); + // ["abcd2g","abccefg","abcckkg"] + + return 0; +} diff --git a/readme.md b/readme.md index 62f3caeb..7850144a 100644 --- a/readme.md +++ b/readme.md @@ -553,7 +553,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 524 | [Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/) | [solution](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/solution/) | [C++](0501-1000/0524-Longest-Word-in-Dictionary-through-Deleting/cpp-0524/) | | | | 525 | [Contiguous Array](https://leetcode.com/problems/contiguous-array/) | [solution](https://leetcode.com/problems/contiguous-array/solution/) [题解](https://leetcode-cn.com/problems/contiguous-array/solution/lian-xu-shu-zu-by-leetcode/) | [C++](0501-1000/0525-Contiguous-Array/cpp-0525/) | | | | 526 | [Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/) | [solution](https://leetcode.com/problems/beautiful-arrangement/solution/) | [C++](0501-1000/0526-Beautiful-Arrangement/cpp-0526/) | | | -| | | | | | | +| 527 | [Word Abbreviation](https://leetcode.com/problems/word-abbreviation/) | [solution](https://leetcode.com/problems/word-abbreviation/solution/) | [C++](0501-1000/0527-Word-Abbreviation/cpp-0527/) | | | | 528 | [Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/description/) | [solution](https://leetcode.com/problems/random-pick-with-weight/solution/) | [C++](0501-1000/0528-Random-Pick-with-Weight/cpp-0528/) | | | | 529 | [Minesweeper](https://leetcode.com/problems/minesweeper/) | [无] | [C++](0501-1000/0529-Minesweeper/cpp-0529/) | | | | 530 | [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) | [无] | | [Java](0501-1000/0530-Minimum-Absolute-Difference-in-BST/java-0530/src/) | | From 7448101c4576111503be07c4a30be7593bda7004 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 15 Jun 2022 11:06:37 -0700 Subject: [PATCH 095/390] 0561 main2 codes and comments updated. --- .../0561-Array-Partition-I/cpp-0561/main2.cpp | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/0501-1000/0561-Array-Partition-I/cpp-0561/main2.cpp b/0501-1000/0561-Array-Partition-I/cpp-0561/main2.cpp index 6b9981ea..00784112 100644 --- a/0501-1000/0561-Array-Partition-I/cpp-0561/main2.cpp +++ b/0501-1000/0561-Array-Partition-I/cpp-0561/main2.cpp @@ -1,36 +1,41 @@ /// Source : https://leetcode.com/problems/array-partition-i/solution/ /// Author : liuyubobobo /// Time : 2018-06-04 +/// Updated: 2022-06-15 #include #include using namespace std; -/// Using HashMap -/// Time Complexity: O(n) -/// Space Complexity: O(n) + +/// Counting Sort based +/// Time Complexity: O(n + (maxv - minv)) +/// Space Complexity: O(maxv - minv) class Solution { public: int arrayPairSum(vector& nums) { - int hash[20001]; - memset(hash, 0, sizeof(hash)); + int maxv = *max_element(nums.begin(), nums.end()); + int minv = *min_element(nums.begin(), nums.end()); + + vector cnt(maxv - minv + 2, 0); + int offset = -minv; for(int num: nums) - hash[num + 10000] ++; + cnt[num + offset] ++; int sum = 0; bool minus = false; - for(int i = 0 ; i <= 20000 ; i ++) - if(hash[i]){ + for(int i = 0 ; i < cnt.size(); i ++) + if(cnt[i]){ if(minus){ - hash[i] --; + cnt[i] --; minus = false; } - sum += hash[i] / 2 * (i - 10000); - if(hash[i] % 2){ - sum += (i - 10000); + sum += cnt[i] / 2 * (i - offset); + if(cnt[i] % 2){ + sum += (i - offset); minus = true; } } From c376e8e54064d087afddae602e69d52c9a3d8d4c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 18 Jun 2022 09:41:48 -0700 Subject: [PATCH 096/390] 0508 solved. --- .../cpp-0508/CMakeLists.txt | 6 ++ .../cpp-0508/main.cpp | 58 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/CMakeLists.txt create mode 100644 0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/main.cpp diff --git a/0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/CMakeLists.txt b/0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/CMakeLists.txt new file mode 100644 index 00000000..2dc9e056 --- /dev/null +++ b/0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0508) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0508 main.cpp) diff --git a/0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/main.cpp b/0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/main.cpp new file mode 100644 index 00000000..021f4992 --- /dev/null +++ b/0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/main.cpp @@ -0,0 +1,58 @@ +/// Source : https://leetcode.com/problems/most-frequent-subtree-sum/ +/// Author : liuyubobobo +/// Time : 2022-06-18 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Compelxity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector findFrequentTreeSum(TreeNode* root) { + + map f; + dfs(root, f); + + vector res; + int max_f = 0; + for(const pair& p: f) + if(p.second > max_f) res = {p.first}, max_f = p.second; + else if(p.second == max_f) res.push_back(p.first); + return res; + } + +private: + int dfs(TreeNode* node, map& f){ + + if(!node) return 0; + + int res = node->val; + res += dfs(node->left, f); + res += dfs(node->right, f); + f[res] ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 7850144a..e91c8c0b 100644 --- a/readme.md +++ b/readme.md @@ -537,7 +537,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 505 | [The Maze II](https://leetcode.com/problems/the-maze-ii/) | [solution](https://leetcode.com/problems/the-maze-ii/solution/) | [C++](0501-1000/0505-The-Maze-II/cpp-0505/) | | | | 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks/) | [无] | [C++](0501-1000/0506-Relative-Ranks/cpp-0506/) | | | | 507 | [Perfect Number](https://leetcode.com/problems/perfect-number/) | [solution](https://leetcode.com/problems/perfect-number/solution/) | [C++](0501-1000/0507-Perfect-Number/cpp-0507/) | | | -| | | | | | | +| 508 | [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/) | [无] | [C++](0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/) | | | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [无] | [C++](0501-1000/0509-Fibonacci-Number/cpp-0509/) | | | | | | | | | | | 511 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | From ecf6fc938aee7231eab195142c78ba9e99d7748b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 18 Jun 2022 13:54:11 -0700 Subject: [PATCH 097/390] 2127 comments updated. --- .../cpp-2127/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/main.cpp b/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/main.cpp index 8cb06a27..e0e8218b 100644 --- a/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/main.cpp +++ b/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/main.cpp @@ -8,7 +8,8 @@ using namespace std; -/// Graph +/// Successor Graph +/// This problem is not a very standard Successor Graph Problem, a little bit harder and Ad-Hoc /// Time Complexity: O(n) /// Space Complexity: O(n) class Solution { From ad37caaa84ae85bb2121f57ab6d1e832c1584e95 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 18 Jun 2022 21:43:03 -0700 Subject: [PATCH 098/390] 2312 solved. --- .../cpp-2312/CMakeLists.txt | 6 + .../cpp-2312/main.cpp | 108 ++++++++++++++++++ readme.md | 4 + 3 files changed, 118 insertions(+) create mode 100644 2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt create mode 100644 2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp diff --git a/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp new file mode 100644 index 00000000..fbdb28dc --- /dev/null +++ b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp @@ -0,0 +1,108 @@ +/// Source : https://leetcode.com/problems/selling-pieces-of-wood/ +/// Author : liuyubobobo +/// Time : 2022-06-18 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(m * n * n + n * m * m + m * n * (m + n)) +/// Space Complexity: O(m * n) +class Solution { +public: + long long sellingWood(int m, int n, vector>& prices) { + + // 0 - any, 1 - h is fixed, 2 - w is fixed + vector>> dp(3, vector>(m + 1, vector(n + 1, -1))); + + map> H, W; + for(const vector& price: prices){ + int h = price[0], w = price[1], v = price[2]; + H[h][w] = max(H[h][w], (long long)v); + W[w][h] = max(W[w][h], (long long)v); + } + + vector h_choices; + vector> Hdp(m + 1, vector(n + 1, 0)); + for(const pair>& p: H){ + int h = p.first; + h_choices.push_back(h); + const map& items = p.second; + + for(int W = 1; W <= n; W ++){ + for(const pair& item: items){ + int weight = item.first; + long long value = item.second; + + if(W >= weight) + Hdp[h][W] = max(Hdp[h][W], Hdp[h][W - weight] + value); + } + } + } + + vector w_choices; + vector> Wdp(n + 1, vector(m + 1, 0)); + for(const pair>& p: W){ + int w = p.first; + w_choices.push_back(w); + const map& items = p.second; + + for(int W = 1; W <= m; W ++) { + for (const pair &item: items) { + int weight = item.first; + long long value = item.second; + + if(W >= weight) + Wdp[w][W] = max(Wdp[w][W], Wdp[w][W - weight] + value); + } + } + } + + return dfs(0, m, n, h_choices, w_choices, Hdp, Wdp, dp); + } + +private: + long long dfs(int state, int m, int n, const vector& h_choices, const vector& w_choices, + const vector>& Hdp, const vector>& Wdp, + vector>>& dp){ + + if(m <= 0 || n <= 0) return 0; + if(dp[state][m][n] != -1) return dp[state][m][n]; + + if(state == 1) return dp[state][m][n] = Hdp[m][n]; + if(state == 2) return dp[state][m][n] = Wdp[n][m]; + + long long res = 0; + for(int h_option: h_choices){ + if(h_option > m) break; + res = max(res, dfs(1, h_option, n, h_choices, w_choices, Hdp, Wdp, dp) + dfs(0, m - h_option, n, h_choices, w_choices, Hdp, Wdp, dp)); + } + for(int w_option: w_choices){ + if(w_option > n) break; + res = max(res, dfs(2, m, w_option, h_choices, w_choices, Hdp, Wdp, dp) + dfs(0, m, n - w_option, h_choices, w_choices, Hdp, Wdp, dp)); + } + return dp[state][m][n] = res; + } +}; + + +int main() { + + vector> prices1 = {{1, 4, 2}, {2, 2, 7}, {2, 1, 3}}; + cout << Solution().sellingWood(3, 5, prices1) << '\n'; + // 19 + + vector> prices2 = {{3, 2, 10}, {1, 4, 2}, {4, 1, 3}}; + cout << Solution().sellingWood(4, 6, prices2) << '\n'; + // 32 + + vector> prices3 = {{4, 1, 18}, {4, 2, 5}, {1, 1, 20}, {3, 1, 21}}; + cout << Solution().sellingWood(4, 2, prices3) << '\n'; + // 160 + + return 0; +} diff --git a/readme.md b/readme.md index e91c8c0b..e1fdb98b 100644 --- a/readme.md +++ b/readme.md @@ -2151,6 +2151,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2305 | [Fair Distribution of Cookies](https://leetcode.com/problems/fair-distribution-of-cookies/) | [无] | [C++](2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/) | | | | 2306 | [Naming a Company](https://leetcode.com/problems/naming-a-company/) | [无] | [C++](2001-2500/2306-Naming-a-Company/cpp-2306/) | | | | | | | | | | +| 2308 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | +| 2312 | [Selling Pieces of Wood](https://leetcode.com/problems/selling-pieces-of-wood/) | [无] | [C++](2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/) | | | +| | | | | | | ## 力扣中文站比赛 From 01b66db3c973555e1f7eaa2a999307264432176c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 21 Jun 2022 11:02:28 -0700 Subject: [PATCH 099/390] 0513 solved. --- .../cpp-0513/CMakeLists.txt | 6 +++ .../cpp-0513/main.cpp | 51 +++++++++++++++++++ readme.md | 2 + 3 files changed, 59 insertions(+) create mode 100644 0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/CMakeLists.txt create mode 100644 0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/main.cpp diff --git a/0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/CMakeLists.txt b/0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/CMakeLists.txt new file mode 100644 index 00000000..c91381e1 --- /dev/null +++ b/0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0513) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0513 main.cpp) diff --git a/0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/main.cpp b/0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/main.cpp new file mode 100644 index 00000000..71401a90 --- /dev/null +++ b/0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/find-bottom-left-tree-value/ +/// Author : liuyubobobo +/// Time : 2022-06-21 + +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + int findBottomLeftValue(TreeNode* root) { + + int res = -1; + queue q; + q.push(root); + while(!q.empty()){ + res = q.front()->val; + queue tq; + while(!q.empty()){ + TreeNode* node = q.front(); q.pop(); + if(node->left) tq.push(node->left); + if(node->right) tq.push(node->right); + } + q = tq; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index e1fdb98b..a3b35c20 100644 --- a/readme.md +++ b/readme.md @@ -542,6 +542,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 511 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 513 | [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | [无] | [C++](0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/) | | | +| | | | | | | | 516 | [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [无] | [C++](0501-1000/0516-Longest-Palindromic-Subsequence/cpp-0516/) | | | | 517 | [Super Washing Machines](https://leetcode.com/problems/super-washing-machines/) | [solution](https://leetcode.com/problems/super-washing-machines/solution/) | [C++](0501-1000/0517-Super-Washing-Machines/cpp-0517/) | | | | 518 | [Coin Change 2](https://leetcode.com/problems/coin-change-2/description/) | [无] | [C++](0501-1000/0518-Coin-Change-2/cpp-0518/) | | | From 6c37584f016dce132872ab2bb49c57a92947b083 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 21 Jun 2022 11:19:27 -0700 Subject: [PATCH 100/390] 2312 new algo added. --- .../cpp-2312/CMakeLists.txt | 2 +- .../cpp-2312/main.cpp | 2 +- .../cpp-2312/main2.cpp | 62 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main2.cpp diff --git a/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt index 9a6c5c78..bb71ae36 100644 --- a/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt +++ b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/CMakeLists.txt @@ -3,4 +3,4 @@ project(D) set(CMAKE_CXX_STANDARD 14) -add_executable(D main.cpp) +add_executable(D main2.cpp) diff --git a/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp index fbdb28dc..90682161 100644 --- a/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp +++ b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main.cpp @@ -9,7 +9,7 @@ using namespace std; -/// DP +/// DP - Knapback /// Time Complexity: O(m * n * n + n * m * m + m * n * (m + n)) /// Space Complexity: O(m * n) class Solution { diff --git a/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main2.cpp b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main2.cpp new file mode 100644 index 00000000..6fffab9d --- /dev/null +++ b/2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/main2.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/selling-pieces-of-wood/ +/// Author : liuyubobobo +/// Time : 2022-06-21 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(m * n * (m + n)) +/// Space Complexity: O(m * n) +class Solution { +public: + long long sellingWood(int m, int n, vector>& prices) { + + vector> dp(m + 1, vector(n + 1, 0)); + vector> visited(m + 1, vector(n + 1, false)); + + for(const vector& price: prices){ + int h = price[0], w = price[1], p = price[2]; + dp[h][w] = max(dp[h][w], 1ll * p); + } + + return dfs(m, n, dp, visited); + } + +private: + long long dfs(int m, int n, vector>& dp, vector>& visited){ + + if(visited[m][n]) return dp[m][n]; + + long long res = dp[m][n]; + for(int top = 1; top <= m - 1; top ++) + res = max(res, dfs(top, n, dp, visited) + dfs(m - top, n, dp, visited)); + for(int left = 1; left <= n - 1; left ++) + res = max(res, dfs(m, left, dp, visited) + dfs(m, n - left, dp, visited)); + + visited[m][n] = true; + return dp[m][n] = res; + } +}; + + +int main() { + + vector> prices1 = {{1, 4, 2}, {2, 2, 7}, {2, 1, 3}}; + cout << Solution().sellingWood(3, 5, prices1) << '\n'; + // 19 + + vector> prices2 = {{3, 2, 10}, {1, 4, 2}, {4, 1, 3}}; + cout << Solution().sellingWood(4, 6, prices2) << '\n'; + // 32 + + vector> prices3 = {{4, 1, 18}, {4, 2, 5}, {1, 1, 20}, {3, 1, 21}}; + cout << Solution().sellingWood(4, 2, prices3) << '\n'; + // 160 + + return 0; +} From 03a9615a82d581fa0105c49a3210b5c39ce5bc12 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 21 Jun 2022 11:22:55 -0700 Subject: [PATCH 101/390] 2309 added. --- .../cpp-2309/CMakeLists.txt | 6 ++++ .../cpp-2309/main.cpp | 35 +++++++++++++++++++ readme.md | 1 + 3 files changed, 42 insertions(+) create mode 100644 2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/CMakeLists.txt create mode 100644 2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp diff --git a/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/CMakeLists.txt b/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp b/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp new file mode 100644 index 00000000..1ad42bde --- /dev/null +++ b/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/ +/// Author : liuyubobobo +/// Time : 2022-06-21 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string greatestLetter(string s) { + + vector upper(26, false), lower(26, false); + for(char c: s){ + if(isupper(c)) upper[c - 'A'] = true; + else lower[c - 'a'] = true; + } + + int res = -1; + for(int i = 0; i < 26; i ++) + if(upper[i] && lower[i]) res = i; + return res == -1 ? "" : string(1, 'A' + res); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a3b35c20..2f416aa2 100644 --- a/readme.md +++ b/readme.md @@ -2154,6 +2154,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2306 | [Naming a Company](https://leetcode.com/problems/naming-a-company/) | [无] | [C++](2001-2500/2306-Naming-a-Company/cpp-2306/) | | | | | | | | | | | 2308 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2309 | [Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/) | [无] | [C++](2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/) | | | | | | | | | | | 2312 | [Selling Pieces of Wood](https://leetcode.com/problems/selling-pieces-of-wood/) | [无] | [C++](2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/) | | | | | | | | | | From 491a3d3d9c9bea57f49f25ffbbcdf0578252d1e2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 21 Jun 2022 11:26:33 -0700 Subject: [PATCH 102/390] 2310 added. --- .../cpp-2309/main.cpp | 2 +- .../cpp-2310/CMakeLists.txt | 6 +++ .../cpp-2310/main.cpp | 42 +++++++++++++++++++ readme.md | 1 + 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/CMakeLists.txt create mode 100644 2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/main.cpp diff --git a/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp b/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp index 1ad42bde..73cfd5ad 100644 --- a/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp +++ b/2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/main.cpp @@ -1,6 +1,6 @@ /// Source : https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/ /// Author : liuyubobobo -/// Time : 2022-06-21 +/// Time : 2022-06-18 #include #include diff --git a/2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/CMakeLists.txt b/2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/main.cpp b/2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/main.cpp new file mode 100644 index 00000000..a7f269ed --- /dev/null +++ b/2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/ +/// Author : liuyubobobo +/// Time : 2022-06-18 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int minimumNumbers(int num, int k) { + + if(num == 0) return 0; + + int cnt = 1; + for(cnt = 1; cnt <= 10; cnt ++) + if((cnt * k) % 10 == num % 10) + break; + + if(cnt == 11 ||cnt * k > num) return -1; + return cnt; + } +}; + + +int main() { + + cout << Solution().minimumNumbers(58, 9) << '\n'; + // 2 + + cout << Solution().minimumNumbers(37, 2) << '\n'; + // -1 + + cout << Solution().minimumNumbers(0, 7) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index 2f416aa2..e0f770ad 100644 --- a/readme.md +++ b/readme.md @@ -2155,6 +2155,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2308 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2309 | [Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/) | [无] | [C++](2001-2500/2309-Greatest-English-Letter-in-Upper-and-Lower-Case/cpp-2309/) | | | +| 2310 | [Sum of Numbers With Units Digit K](https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/) | [无] | [C++](2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/) | | | | | | | | | | | 2312 | [Selling Pieces of Wood](https://leetcode.com/problems/selling-pieces-of-wood/) | [无] | [C++](2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/) | | | | | | | | | | From 177e4e789c5df9c80441d84f7542317b11cdd14e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 22 Jun 2022 16:07:18 -0700 Subject: [PATCH 103/390] 0030 solved. --- .../cpp-0030/CMakeLists.txt | 6 ++ .../cpp-0030/main.cpp | 81 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/CMakeLists.txt create mode 100644 0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/main.cpp diff --git a/0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/CMakeLists.txt b/0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/CMakeLists.txt new file mode 100644 index 00000000..fafca30b --- /dev/null +++ b/0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0030) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0030 main.cpp) diff --git a/0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/main.cpp b/0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/main.cpp new file mode 100644 index 00000000..3ac701b4 --- /dev/null +++ b/0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/main.cpp @@ -0,0 +1,81 @@ +/// Source : https://leetcode.com/problems/substring-with-concatenation-of-all-words/ +/// Author : liuyubobobo +/// Time : 2022-06-22 + +#include +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(word_len * |s|) +/// Space Complexity: O(|s|) +class Solution { +public: + vector findSubstring(string s, vector& words) { + + int word_len = words[0].size(); + + map words_f; + for(int i = 0; i < words.size(); i ++) words_f[words[i]] ++; + + vector res; + for(int start = 0; start < word_len; start ++){ + + vector data; + for(int i = start; i < s.size(); i += word_len) + data.push_back(s.substr(i, word_len)); + + int ok_cnt = 0, l = 0, r = -1; + map cur_f; + while(l < (int)data.size()){ + if(r + 1 < (int)data.size() && words_f.count(data[r + 1]) && cur_f[data[r + 1]] + 1 <= words_f[data[r + 1]]){ + cur_f[data[r + 1]] ++; + + if(cur_f[data[r + 1]] == words_f[data[r + 1]]){ + ok_cnt ++; + if(ok_cnt == words_f.size()) + res.push_back(start + l * word_len); + } + + r ++; + } + else{ + + if(words_f.count(data[l])){ + cur_f[data[l]] --; + if(cur_f[data[l]] + 1 == words_f[data[l]]) ok_cnt --; + } + l ++; + r = max(r, l - 1); + } + } + } + + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector words1 = {"foo", "bar"}; + print_vec(Solution().findSubstring("barfoothefoobarman", words1)); + // 0 9 + + vector words2 = {"word","good","best","word"}; + print_vec(Solution().findSubstring("wordgoodgoodgoodbestword", words2)); + // empty + + vector words3 = {"bar","foo","the"}; + print_vec(Solution().findSubstring("barfoofoobarthefoobarman", words3)); + // 6 9 12 + + return 0; +} diff --git a/readme.md b/readme.md index e0f770ad..e1161c24 100644 --- a/readme.md +++ b/readme.md @@ -76,7 +76,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 027 | [Remove Element](https://leetcode.com/problems/remove-element/description/) | [solution](https://leetcode.com/problems/remove-element/solution/) | [C++](0001-0500/0027-Remove-Element/cpp-0027/) | | | | 028 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/description/) | [无]
[缺:完整BM算法, 其他模式匹配算法] | [C++](0001-0500/0028-Implement-strStr/cpp-0028/) | | | | 029 | [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [solution](https://leetcode.com/problems/divide-two-integers/solution/)
[缺:不使用乘法加法除法] | [C++](0001-0500/0029-Divide-Two-Integers/cpp-0029/) | | | -| | | | | | | +| 030 | [Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [solution](https://leetcode.com/problems/substring-with-concatenation-of-all-words/solution/) | [C++](0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/) | | | | 031 | [Next Permutation](https://leetcode.com/problems/next-permutation/) | [solution](https://leetcode.com/problems/next-permutation/solution/) | [C++](0001-0500/0031-Next-Permutation/cpp-0031/) | | | | | | | | | | | 033 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [solution](https://leetcode.com/problems/search-in-rotated-sorted-array/solution/) | [C++](0001-0500/0033-Search-in-Rotated-Sorted-Array/cpp-0033/) | | | From b3397d0c57109199b7ff5b9b8022a87cbca14c86 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 22 Jun 2022 23:36:49 -0700 Subject: [PATCH 104/390] 2313 solved. --- .../cpp-2313/CMakeLists.txt | 6 ++ .../cpp-2313/main.cpp | 72 +++++++++++++++++++ readme.md | 1 + 3 files changed, 79 insertions(+) create mode 100644 2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/CMakeLists.txt create mode 100644 2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/main.cpp diff --git a/2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/CMakeLists.txt b/2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/CMakeLists.txt new file mode 100644 index 00000000..e3bbbc38 --- /dev/null +++ b/2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2313) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2313 main.cpp) diff --git a/2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/main.cpp b/2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/main.cpp new file mode 100644 index 00000000..b01d57b1 --- /dev/null +++ b/2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/ +/// Author : liuyubobobo +/// Time : 2022-06-22 + +#include +#include +#include +#include + +using namespace std; + + +/// Tree DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + int minimumFlips(TreeNode* root, bool result) { + + vector> dp(2); + return dfs(root, result, dp); + } + +private: + int dfs(TreeNode* node, bool result, vector>& dp){ + + if(node->left == nullptr && node->right == nullptr) + return result != node->val; + + auto iter = dp[result].find(node); + if(iter != dp[result].end()) return iter->second; + + if(node->val == 5){ + TreeNode* child = node->left ? node->left : node->right; + return dp[result][node] = dfs(child, !result, dp); + } + + TreeNode* node1 = node->left, *node2 = node->right; + int res = INT_MAX; + for(int res1 = 0; res1 <= 1; res1 ++) + for(int res2 = 0; res2 <= 1; res2 ++) + if(calc(res1, res2, node->val) == result) + res = min(res, dfs(node1, res1, dp) + dfs(node2, res2, dp)); + return dp[result][node] = res; + } + + int calc(int a, int b, int op){ + switch(op){ + case 2: return a | b; + case 3: return a & b; + case 4: return a ^ b; + default: assert(false); + } + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index e1161c24..e765eb67 100644 --- a/readme.md +++ b/readme.md @@ -2158,6 +2158,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2310 | [Sum of Numbers With Units Digit K](https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/) | [无] | [C++](2001-2500/2310-Sum-of-Numbers-With-Units-Digit-K/cpp-2310/) | | | | | | | | | | | 2312 | [Selling Pieces of Wood](https://leetcode.com/problems/selling-pieces-of-wood/) | [无] | [C++](2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/) | | | +| 2313 | [Minimum Flips in Binary Tree to Get Result](https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/) | [无] | [C++](2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/) | | | | | | | | | | ## 力扣中文站比赛 From a343ffb7a34ea0da60a755661cb32ac087a47c23 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 23 Jun 2022 11:37:57 -0700 Subject: [PATCH 105/390] 0515 solved. --- .../cpp-0515/CMakeLists.txt | 6 +++ .../cpp-0515/main.cpp | 51 +++++++++++++++++++ readme.md | 1 + 3 files changed, 58 insertions(+) create mode 100644 0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/CMakeLists.txt create mode 100644 0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/main.cpp diff --git a/0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/CMakeLists.txt b/0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/CMakeLists.txt new file mode 100644 index 00000000..b2ad5a30 --- /dev/null +++ b/0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0515) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0515 main.cpp) diff --git a/0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/main.cpp b/0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/main.cpp new file mode 100644 index 00000000..0fe4f7b8 --- /dev/null +++ b/0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/find-largest-value-in-each-tree-row/ +/// Author : liuyubobobo +/// Time : 2022-06-23 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(depth) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector largestValues(TreeNode* root) { + + vector res; + dfs(root, 0, res); + return res; + } + +private: + void dfs(TreeNode* node, int depth, vector& res){ + + if(!node) return; + + if(depth == res.size()) res.push_back(node->val); + else res[depth] = max(res[depth], node->val); + + dfs(node->left, depth + 1, res); + dfs(node->right, depth + 1, res); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index e765eb67..5c438ee5 100644 --- a/readme.md +++ b/readme.md @@ -544,6 +544,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 513 | [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | [无] | [C++](0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/) | | | | | | | | | | +| 515 | [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | [无] | [C++](0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/) | | | | 516 | [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [无] | [C++](0501-1000/0516-Longest-Palindromic-Subsequence/cpp-0516/) | | | | 517 | [Super Washing Machines](https://leetcode.com/problems/super-washing-machines/) | [solution](https://leetcode.com/problems/super-washing-machines/solution/) | [C++](0501-1000/0517-Super-Washing-Machines/cpp-0517/) | | | | 518 | [Coin Change 2](https://leetcode.com/problems/coin-change-2/description/) | [无] | [C++](0501-1000/0518-Coin-Change-2/cpp-0518/) | | | From 282cee5616f460611e205ec7b7b62c769072dd92 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 23 Jun 2022 12:49:03 -0700 Subject: [PATCH 106/390] 2022 sf tech solved. --- Others/2022-sf-tech/A/CMakeLists.txt | 6 ++ Others/2022-sf-tech/A/main.cpp | 92 +++++++++++++++++++++ Others/2022-sf-tech/B/CMakeLists.txt | 6 ++ Others/2022-sf-tech/B/main.cpp | 37 +++++++++ Others/2022-sf-tech/B/main2.cpp | 52 ++++++++++++ Others/2022-sf-tech/C/CMakeLists.txt | 6 ++ Others/2022-sf-tech/C/main.cpp | 34 ++++++++ Others/2022-sf-tech/D/CMakeLists.txt | 6 ++ Others/2022-sf-tech/D/main.cpp | 116 +++++++++++++++++++++++++++ Others/2022-sf-tech/E/CMakeLists.txt | 6 ++ Others/2022-sf-tech/E/main.cpp | 69 ++++++++++++++++ readme.md | 7 ++ 12 files changed, 437 insertions(+) create mode 100644 Others/2022-sf-tech/A/CMakeLists.txt create mode 100644 Others/2022-sf-tech/A/main.cpp create mode 100644 Others/2022-sf-tech/B/CMakeLists.txt create mode 100644 Others/2022-sf-tech/B/main.cpp create mode 100644 Others/2022-sf-tech/B/main2.cpp create mode 100644 Others/2022-sf-tech/C/CMakeLists.txt create mode 100644 Others/2022-sf-tech/C/main.cpp create mode 100644 Others/2022-sf-tech/D/CMakeLists.txt create mode 100644 Others/2022-sf-tech/D/main.cpp create mode 100644 Others/2022-sf-tech/E/CMakeLists.txt create mode 100644 Others/2022-sf-tech/E/main.cpp diff --git a/Others/2022-sf-tech/A/CMakeLists.txt b/Others/2022-sf-tech/A/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/Others/2022-sf-tech/A/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/Others/2022-sf-tech/A/main.cpp b/Others/2022-sf-tech/A/main.cpp new file mode 100644 index 00000000..84c1fd41 --- /dev/null +++ b/Others/2022-sf-tech/A/main.cpp @@ -0,0 +1,92 @@ +/// Source : https://leetcode.cn/contest/sf-tech/problems/EUpcmh/ +/// Author : liuyubobobo +/// Time : 2022-06-23 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// DFS Cycle Finding +/// Time Complexity: O(|s| + n) +/// Space Compelxity: O(n) +class Solution { +public: + bool hasCycle(string graph) { + + vector edges_str = get_edges_str(graph); + + map> g; + for(const string& edge_str: edges_str){ + int u, v; + get_uv_from_edge_str(edge_str, u, v); + g[u].insert(v); + } + + set visited; + bool has_cycle = false; + for(const pair>& p: g){ + int u = p.first; + if(!visited.count(u)){ + set stack; + has_cycle = dfs(g, u, stack, visited); + if(has_cycle) break; + } + } + return has_cycle; + } + +private: + bool dfs(const map>& g, int u, set& stack, set& visited){ + + stack.insert(u); + if(g.count(u)){ + for(int v: g.at(u)){ + if(stack.count(v)) return true; + if(!visited.count(v) && dfs(g, v, stack, visited)) return true; + } + } + stack.erase(u); + return false; + } + + void get_uv_from_edge_str(const string& e, int& u, int& v){ + + int arrow_pos = e.find("->"); + assert(arrow_pos != string::npos); + + u = atoi(e.substr(0, arrow_pos).c_str()); + v = atoi(e.substr(arrow_pos + 2).c_str()); + } + + vector get_edges_str(const string& g){ + + vector res; + for(int start = 0, i = 1; i <= g.size(); i ++) + if(i == g.size() || g[i] == ','){ + res.push_back(g.substr(start, i - start)); + start = i + 1; + i = start + 1; + } + return res; + } +}; + + +int main() { + + cout << Solution().hasCycle("1->2,2->3,3->1") << '\n'; + // 1 + + cout << Solution().hasCycle("1->4,2->5,3->6,3->7,4->8,5->8,5->9,6->9,6->11,7->11,8->12,9->12,9->13,10->13,10->14,11->10,11->14") << '\n'; + // 0 + + cout << Solution().hasCycle("1->4,2->5,3->6,3->7,4->8,5->8,5->9,6->9,6->11,7->11,8->12,9->12,9->13,10->6,10->13,10->14,11->10,11->14") << '\n'; + // 1 + + return 0; +} diff --git a/Others/2022-sf-tech/B/CMakeLists.txt b/Others/2022-sf-tech/B/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/Others/2022-sf-tech/B/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/Others/2022-sf-tech/B/main.cpp b/Others/2022-sf-tech/B/main.cpp new file mode 100644 index 00000000..c9db26c1 --- /dev/null +++ b/Others/2022-sf-tech/B/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.cn/contest/sf-tech/problems/cINqyA/ +/// Author : liuyubobobo +/// Time : 2022-06-23 + +#include +#include +#include + +using namespace std; + + +/// Backpack DP +/// Time complexity: O(C * n) +/// Space Complexity: O(C) +class Solution { +public: + int minRemainingSpace(vector& V, int C) { + + vector dp(C + 1, false); + dp[0] = true; + for(int v: V){ + for(int c = C; c >= v; c --) + if(dp[c - v]) dp[c] = true; + } + + for(int i = C; i >= 0; i --) + if(dp[i]) return C - i; + assert(false); + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-sf-tech/B/main2.cpp b/Others/2022-sf-tech/B/main2.cpp new file mode 100644 index 00000000..0a6b6fd2 --- /dev/null +++ b/Others/2022-sf-tech/B/main2.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.cn/contest/sf-tech/problems/cINqyA/ +/// Author : liuyubobobo +/// Time : 2022-06-23 + +#include +#include +#include + +using namespace std; + + +/// Meet in the middle +/// Time complexity: O(n * 2^(n / 2)) +/// Space Complexity: O(2^(n/2)) +class Solution { +public: + int minRemainingSpace(vector& V, int C) { + + int n = V.size(); + if(n == 1) return max(C - V[0], 0); + + int left_n = n / 2, right_n = n - left_n; + vector left(1 << left_n, 0), right(1 << right_n, 0); + for(int state = 1; state < (1 << left_n); state ++){ + int p = __builtin_ffs(state) - 1; + left[state] = left[state - (1 << p)] + V[p]; + } + for(int state = 1; state < (1 << right_n); state ++){ + int p = __builtin_ffs(state) - 1; + right[state] = right[state - (1 << p)] + V[left_n + p]; + } + + set left_set(left.begin(), left.end()); + set right_set(right.begin(), right.end()); + + int res = C; + for(int e1: left){ + if(e1 > C) break; + auto iter = right_set.upper_bound(C - e1); + iter --; + res = min(res, C - e1 - *iter); + } + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-sf-tech/C/CMakeLists.txt b/Others/2022-sf-tech/C/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/Others/2022-sf-tech/C/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/Others/2022-sf-tech/C/main.cpp b/Others/2022-sf-tech/C/main.cpp new file mode 100644 index 00000000..a387a4fa --- /dev/null +++ b/Others/2022-sf-tech/C/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.cn/contest/sf-tech/problems/8oimK4/ +/// Author : liuyubobobo +/// Time : 2022-06-23 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Compelxity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int findMaxCI(vector& nums) { + + int n = nums.size(); + int res = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || nums[i] <= nums[i - 1]){ + res = max(res, i - start); + start = i; + i = start; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-sf-tech/D/CMakeLists.txt b/Others/2022-sf-tech/D/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/Others/2022-sf-tech/D/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/Others/2022-sf-tech/D/main.cpp b/Others/2022-sf-tech/D/main.cpp new file mode 100644 index 00000000..4d5e2c9a --- /dev/null +++ b/Others/2022-sf-tech/D/main.cpp @@ -0,0 +1,116 @@ +/// Source : https://leetcode.cn/contest/sf-tech/problems/uWWzsv/ +/// Author : liuyubobobo +/// Time : 2022-06-23 + +#include +#include +#include + +#define X real() +#define Y imag() + +using namespace std; + + +typedef double C; +typedef complex P; + +const double EPS = 1e-6; + +/// Cross Product +/// Time Complexity: O(t) +/// Space Complexity: O(1) +C cross_product(P a, P b){ + return (conj(a) * b).Y; // a.X * b.Y - b.X * a.Y +} + +bool is_zero(C x){ + return abs(x) < EPS; +} +// 1 : left +// 0 : touch +// -1 : right +int test_point_loc(P p, P s1, P s2){ + C res = cross_product(p - s1, p - s2); + if(is_zero(res)) return 0; + return res > 0 ? 1 : -1; +} + +// 0 : on boundary +// 1 : intersection +// -1 : no intersection +int intersect(P p1, P p2, P p3, P p4){ + + C x1 = p1.X, y1 = p1.Y, x2 = p2.X, y2 = p2.Y, x3 = p3.X, y3 = p3.Y, x4 = p4.X, y4 = p4.Y; + + int loc1 = test_point_loc(p1, p3, p4), loc2 = test_point_loc(p2, p3, p4); + int loc3 = test_point_loc(p3, p1, p2), loc4 = test_point_loc(p4, p1, p2); + + if(loc1 == 0 && min(x3, x4) <= x1 && x1 <= max(x3, x4) + && min(y3, y4) <= y1 && y1 <= max(y3, y4)) return 0; + + if(loc2 == 0 && min(x3, x4) <= x2 && x2 <= max(x3, x4) + && min(y3, y4) <= y2 && y2 <= max(y3, y4)) return 0; + + if(loc3 == 0 && min(x1, x2) <= x3 && x3 <= max(x1, x2) + && min(y1, y2) <= y3 && y3 <= max(y1, y2)) return 0; + + if(loc4 == 0 && min(x1, x2) <= x4 && x4 <= max(x1, x2) + && min(y1, y2) <= y4 && y4 <= max(y1, y2)) return 0; + + if(!loc1 || !loc2 || !loc3 || !loc4) return -1; + + if(loc1 * loc2 > 0 || loc3 * loc4 > 0) return -1; + return 1; +} + +//long long gcd(long long a, long long b) +//{ +// if(a > b) swap(a, b); +// if (a == 0) return b; +// return gcd(b % a, a); +//} + +int in_polygon(int n, const vector

& polygon, P p){ + + for(int i = 1; i < n; i ++){ + int loc = test_point_loc(p, polygon[i], polygon[i - 1]); + if(loc == 0 && min(polygon[i].X, polygon[i - 1].X) <= p.X && p.X <= max(polygon[i].X, polygon[i - 1].X) + && min(polygon[i].Y, polygon[i - 1].Y) <= p.Y && p.Y <= max(polygon[i].Y, polygon[i - 1].Y)) + return 0; + } + + int loc = test_point_loc(p, polygon[n - 1], polygon[0]); + if(loc == 0 && min(polygon[n - 1].X, polygon[0].X) <= p.X && p.X <= max(polygon[n - 1].X, polygon[0].X) + && min(polygon[n - 1].Y, polygon[0].Y) <= p.Y && p.Y <= max(polygon[n - 1].Y, polygon[0].Y)) + return 0; + + // 保证 p2.X - p1.X 和 p2.Y - p1.Y 互质, + double INF = 1e9 + 7; + + P p1 = p, p2 = {p.X + INF, p.Y + 1}; + int cnt = 0; + for(int i = 1; i < n; i ++) + cnt += intersect(p1, p2, polygon[i], polygon[i - 1]) >= 0; + cnt += intersect(p1, p2, polygon[n - 1], polygon[0]) >= 0; + + return (cnt & 1) ? -1 : 1; +} + +class Solution { +public: + bool isPointInPolygon(double x, double y, vector& coords) { + + vector

polygon; + for(int i = 0; i < coords.size(); i += 2) + polygon.push_back({coords[i], coords[i + 1]}); + + P p = {x, y}; + return in_polygon(polygon.size(), polygon, p) < 0; + } +}; + +int main() { + + return 0; +} diff --git a/Others/2022-sf-tech/E/CMakeLists.txt b/Others/2022-sf-tech/E/CMakeLists.txt new file mode 100644 index 00000000..bfc48c82 --- /dev/null +++ b/Others/2022-sf-tech/E/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(E) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(E main.cpp) diff --git a/Others/2022-sf-tech/E/main.cpp b/Others/2022-sf-tech/E/main.cpp new file mode 100644 index 00000000..6b30ee89 --- /dev/null +++ b/Others/2022-sf-tech/E/main.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.cn/contest/sf-tech/problems/BN8jAm/ +/// Author : liuyubobobo +/// Time : 2022-06-23 + +#include +#include + +using namespace std; + + +/// Using UF +/// Time Complexity: O(m^2) +/// Space Complexity: O(m) +class UF{ + +private: + vector parent; + int sz; + +public: + UF(int n) : parent(n), sz(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + sz --; + } + + int get_sz(){ + return sz; + } +}; + +class Solution { +public: + bool isCompliance(vector>& distance, int n) { + + int m = distance.size(); + UF uf(m); + + for(int i = 0; i < m; i ++) + for(int j = i + 1; j < m; j ++) + if(distance[i][j] <= 2) uf.union_elements(i, j); + return uf.get_sz() <= n; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 5c438ee5..7af33813 100644 --- a/readme.md +++ b/readme.md @@ -2220,10 +2220,17 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | +| 22 顺丰01 | [顺丰鄂州枢纽运转中心环线检测](https://leetcode.cn/contest/sf-tech/problems/EUpcmh/) | [无] | [C++](Others/2022-sf-tech/A/) | | | +| 22 顺丰02 | [小哥派件装载问题](https://leetcode.cn/contest/sf-tech/problems/cINqyA/) | [无] | [C++](Others/2022-sf-tech/B/) | | | +| 22 顺丰03 | [收件节节高](https://leetcode.cn/contest/sf-tech/problems/8oimK4/) | [无] | [C++](Others/2022-sf-tech/C/) | | | +| 22 顺丰04 | [顺丰中转场车辆入场识别-电子围栏](https://leetcode.cn/contest/sf-tech/problems/uWWzsv/) | [无] | [C++](Others/2022-sf-tech/D/) | | | +| 22 顺丰05 | [慧眼神瞳](https://leetcode.cn/contest/sf-tech/problems/BN8jAm/) | [无] | [C++](Others/2022-sf-tech/E/) | | | +| | | | | | | | 22春 招商银行-01 | [文本编辑程序设计](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/fWcPGC/) | [无] | [C++](Others/2022spring-cmbchina/A/) | | | | 22春 招商银行-02 | [公园规划](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/ReWLAw/) | [无] | [C++](Others/2022spring-cmbchina/B/) | | | | 22春 招商银行-03 | [点燃木棒](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/Dk2Ytp/) | [无] | [C++](Others/2022spring-cmbchina/C/) | | | | 22春 招商银行-04 | [商店促销活动](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/OK3hsO/) | [无] | [C++](Others/2022spring-cmbchina/D/) | | | +| | | | | | | | 22春 银联-01 | [回文链表](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/D7rekZ/) | [无] | [C++](Others/2022spring-cnunionpay/A/) | | | | 22春 银联-02 | [优惠活动系统](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/kDPV0f/) | [无] | [C++](Others/2022spring-cnunionpay/B/) | | | | 22春 银联-03 | [理财产品](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/I4mOGz/) | [无] | [C++](Others/2022spring-cnunionpay/C/) | | | From 70a47183e289181411539cf87578fa9270858c67 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 25 Jun 2022 12:50:55 -0700 Subject: [PATCH 107/390] 2315-2318 solved. --- .../cpp-2315/CMakeLists.txt | 6 ++ .../2315-Count-Asterisks/cpp-2315/main.cpp | 31 +++++++++ .../cpp-2316/CMakeLists.txt | 6 ++ .../cpp-2316/main.cpp | 54 +++++++++++++++ .../cpp-2317/CMakeLists.txt | 6 ++ .../cpp-2317/main.cpp | 37 ++++++++++ .../cpp-2318/CMakeLists.txt | 6 ++ .../cpp-2318/main.cpp | 67 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 218 insertions(+) create mode 100644 2001-2500/2315-Count-Asterisks/cpp-2315/CMakeLists.txt create mode 100644 2001-2500/2315-Count-Asterisks/cpp-2315/main.cpp create mode 100644 2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/CMakeLists.txt create mode 100644 2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/main.cpp create mode 100644 2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/CMakeLists.txt create mode 100644 2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/main.cpp create mode 100644 2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/CMakeLists.txt create mode 100644 2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/main.cpp diff --git a/2001-2500/2315-Count-Asterisks/cpp-2315/CMakeLists.txt b/2001-2500/2315-Count-Asterisks/cpp-2315/CMakeLists.txt new file mode 100644 index 00000000..e4397848 --- /dev/null +++ b/2001-2500/2315-Count-Asterisks/cpp-2315/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2315) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2315 main.cpp) diff --git a/2001-2500/2315-Count-Asterisks/cpp-2315/main.cpp b/2001-2500/2315-Count-Asterisks/cpp-2315/main.cpp new file mode 100644 index 00000000..8484600c --- /dev/null +++ b/2001-2500/2315-Count-Asterisks/cpp-2315/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/count-asterisks/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int countAsterisks(string s) { + + bool out = true; + int res = 0; + for(char c: s){ + if(c == '|') out = !out; + else if(c == '*') res += out; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/CMakeLists.txt b/2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/CMakeLists.txt new file mode 100644 index 00000000..6fbd5c81 --- /dev/null +++ b/2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2316) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2316 main.cpp) diff --git a/2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/main.cpp b/2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/main.cpp new file mode 100644 index 00000000..db05639b --- /dev/null +++ b/2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include +#include + +using namespace std; + + +/// CC +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long countPairs(int n, vector>& edges) { + + vector> g(n); + for(const vector& e: edges){ + int u = e[0], v = e[1]; + g[u].push_back(v), g[v].push_back(u); + } + + vector visited(n, false); + vector cc_sz; + for(int i = 0; i < n; i ++) + if(!visited[i]){ + cc_sz.push_back(dfs(g, i, visited)); + } + + long long res = 0; + for(long long sz: cc_sz) + res += sz * (n - sz); + return res / 2; + } + +private: + int dfs(const vector>& g, int u, vector& visited){ + + visited[u] = true; + int res = 1; + for(int v: g[u]){ + if(visited[v]) continue; + res += dfs(g, v, visited); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/CMakeLists.txt b/2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/CMakeLists.txt new file mode 100644 index 00000000..8d418277 --- /dev/null +++ b/2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2317) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2317 main.cpp) diff --git a/2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/main.cpp b/2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/main.cpp new file mode 100644 index 00000000..24fb26f1 --- /dev/null +++ b/2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/maximum-xor-after-operations/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include +#include + +using namespace std; + + +/// bitwise +/// Time Compelxity: O(nlogA) +/// Space Complexity: O(1) +class Solution { +public: + int maximumXOR(vector& nums) { + + int n = nums.size(); + + vector f(31, 0); + for(int num: nums){ + for(int p = 30; p >= 0; p --) + f[p] += ((num >> p) & 1); + } + + int res = 0; + for(int p = 30; p >= 0; p --) + if(f[p]) res += (1 << p); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/CMakeLists.txt b/2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/CMakeLists.txt new file mode 100644 index 00000000..51c1cd8d --- /dev/null +++ b/2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2318) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2318 main.cpp) diff --git a/2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/main.cpp b/2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/main.cpp new file mode 100644 index 00000000..c30e3354 --- /dev/null +++ b/2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/number-of-distinct-roll-sequences/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int distinctSequences(int n) { + + if(n == 1) return 6; + + vector>> dp(7, vector>(7, vector(n, 0))); + for(int cur = 1; cur <= 6; cur ++) dp[0][cur][0] = 1; + + for(int cur = 1; cur <= 6; cur ++) + for(int pre = 1; pre <= 6; pre ++) + if(gcd(pre, cur) == 1 && cur != pre) dp[pre][cur][1] += dp[0][pre][0]; + + for(int i = 2; i < n; i ++){ + for(int cur = 1; cur <= 6; cur ++){ + for(int p1 = 1; p1 <= 6; p1 ++) + for(int p2 = 1; p2 <= 6; p2 ++){ + if(gcd(p2, cur) == 1 && cur != p2 && cur != p1) + dp[p2][cur][i] += dp[p1][p2][i - 1]; + dp[p2][cur][i] %= MOD; + } + } + } + + long long res = 0; + for(int p = 1; p <= 6; p ++) + for(int cur = 1; cur <= 6; cur ++) + res += dp[p][cur][n - 1]; + return res % MOD; + } + +private: + int gcd(int a, int b){ + if(a > b) swap(a, b); + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + cout << Solution().distinctSequences(2) << '\n'; + + cout << Solution().distinctSequences(4) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index 7af33813..45784e44 100644 --- a/readme.md +++ b/readme.md @@ -2161,6 +2161,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2312 | [Selling Pieces of Wood](https://leetcode.com/problems/selling-pieces-of-wood/) | [无] | [C++](2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/) | | | | 2313 | [Minimum Flips in Binary Tree to Get Result](https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/) | [无] | [C++](2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/) | | | | | | | | | | +| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks/) | [无] | [C++](2001-2500/2315-Count-Asterisks/cpp-2315/) | | | +| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/) | [无] | [C++](2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/) | | | +| 2317 | [Maximum XOR After Operations](https://leetcode.com/problems/maximum-xor-after-operations/) | [无] | [C++](2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/) | | | +| 2318 | [Number of Distinct Roll Sequences](https://leetcode.com/problems/number-of-distinct-roll-sequences/) | [无] | [C++](2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/) | | | +| | | | | | | ## 力扣中文站比赛 From 05f82d793bdfc3d97b601e1a10e46100002c2f72 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 27 Jun 2022 11:20:04 -0700 Subject: [PATCH 108/390] 0324 solved. --- .../cpp-0324/CMakeLists.txt | 6 ++ .../0324-Wiggle-Sort-II/cpp-0324/main.cpp | 71 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0324-Wiggle-Sort-II/cpp-0324/CMakeLists.txt create mode 100644 0001-0500/0324-Wiggle-Sort-II/cpp-0324/main.cpp diff --git a/0001-0500/0324-Wiggle-Sort-II/cpp-0324/CMakeLists.txt b/0001-0500/0324-Wiggle-Sort-II/cpp-0324/CMakeLists.txt new file mode 100644 index 00000000..bab742ff --- /dev/null +++ b/0001-0500/0324-Wiggle-Sort-II/cpp-0324/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0324) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0324 main.cpp) diff --git a/0001-0500/0324-Wiggle-Sort-II/cpp-0324/main.cpp b/0001-0500/0324-Wiggle-Sort-II/cpp-0324/main.cpp new file mode 100644 index 00000000..98cef480 --- /dev/null +++ b/0001-0500/0324-Wiggle-Sort-II/cpp-0324/main.cpp @@ -0,0 +1,71 @@ +/// Source : https://leetcode.com/problems/wiggle-sort-ii/ +/// Author : liuyubobobo +/// Time : 2022-06-27 + +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + void wiggleSort(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + vector res(n); + int index = 0; + for(int i = 0; i < n; i += 2) res[i] = nums[index ++]; + for(int i = 1; i < n; i += 2) res[i] = nums[index ++]; + + if(ok(n, res)){ + nums = res; + return; + } + + assert(n % 2 == 0); + + index = 0; + for(int i = 1; i < n; i += 2) res[i] = nums[index ++]; + for(int i = 0; i < n; i += 2) res[i] = nums[index ++]; + reverse(res.begin(), res.end()); + assert(ok(n, res)); + nums = res; + } + +private: + bool ok(int n, const vector& a){ + + for(int i = 1; i + 1 < n; i ++){ + if(i % 2 == 1){ + if(!(a[i - 1] < a[i] && a[i] > a[i + 1])) return false; + } + else{ + if(!(a[i - 1] > a[i] && a[i] < a[i + 1])) return false; + } + } + return true; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector nums1 = {4, 5, 5, 6}; + Solution().wiggleSort(nums1); + print_vec(nums1); + + return 0; +} diff --git a/readme.md b/readme.md index 45784e44..e5fe76e3 100644 --- a/readme.md +++ b/readme.md @@ -359,7 +359,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 321 | [Create Maximum Number](https://leetcode.com/problems/create-maximum-number/) | [无] | [C++](0001-0500/0321-Create-Maximum-Number/cpp-0321/) | | | | 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | [solution](https://leetcode.com/problems/coin-change/solution/) | [C++](0001-0500/0322-Coin-Change/cpp-0322/) | | | | 323 | [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [solution](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/solution/) | [C++](0001-0500/0323-Number-of-Connected-Components-in-an-Undirected-Graph/cpp-0323/) | | | -| | | | | | | +| 324 | [Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/) | [无] | [C++](0001-0500/0324-Wiggle-Sort-II/cpp-0324/) | | | | 325 | [Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/) | [solution](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solution/) | [C++](0001-0500/0325-Maximum-Size-Subarray-Sum-Equals-k/cpp-0325/) | | | | 326 | [Power of Three](https://leetcode.com/problems/power-of-three/) | [solution](https://leetcode.com/problems/power-of-three/solution/) | [C++](0001-0500/0326-Power-of-Three/cpp-0326/) | | | | | | | | | | From 369524d48527826ea7bc6d5bcfe6ee8139ea00b6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 27 Jun 2022 14:33:26 -0700 Subject: [PATCH 109/390] 2319-2322 solved. --- .../cpp-2319/CMakeLists.txt | 6 + .../cpp-2319/main.cpp | 38 ++++++ .../cpp-2320/CMakeLists.txt | 6 + .../cpp-2320/main.cpp | 45 +++++++ .../cpp-2320/main2.cpp | 37 ++++++ .../cpp-2321/CMakeLists.txt | 6 + .../cpp-2321/main.cpp | 47 ++++++++ .../cpp-2322/CMakeLists.txt | 6 + .../cpp-2322/main.cpp | 110 ++++++++++++++++++ readme.md | 4 + 10 files changed, 305 insertions(+) create mode 100644 2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/CMakeLists.txt create mode 100644 2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/main.cpp create mode 100644 2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/CMakeLists.txt create mode 100644 2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main.cpp create mode 100644 2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main2.cpp create mode 100644 2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/CMakeLists.txt create mode 100644 2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/main.cpp create mode 100644 2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/CMakeLists.txt create mode 100644 2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/main.cpp diff --git a/2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/CMakeLists.txt b/2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/main.cpp b/2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/main.cpp new file mode 100644 index 00000000..ce65fb70 --- /dev/null +++ b/2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/check-if-matrix-is-x-matrix/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + bool checkXMatrix(vector>& grid) { + + int n = grid.size(); + + for(int i = 0; i < n; i ++) + for(int j = 0; j < n; j ++){ + if(is_dia(i, j, n) && grid[i][j] == 0) return false; + if(!is_dia(i, j, n) && grid[i][j] != 0) return false; + } + return true; + } + +private: + bool is_dia(int i, int j, int n){ + return i == j || i + j == n - 1; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/CMakeLists.txt b/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/CMakeLists.txt new file mode 100644 index 00000000..6c7077c9 --- /dev/null +++ b/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main2.cpp) diff --git a/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main.cpp b/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main.cpp new file mode 100644 index 00000000..da855466 --- /dev/null +++ b/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/count-number-of-ways-to-place-houses/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int countHousePlacements(int n) { + + vector> dp(4, vector(n, 0)); + dp[0][0] = dp[1][0] = dp[2][0] = dp[3][0] = 1; + for(int i = 1; i < n; i ++){ + for(int cur_state = 0; cur_state < 4; cur_state ++){ + int cur1 = cur_state / 2, cur2 = cur_state % 2; + for(int pre_state = 0; pre_state < 4; pre_state ++){ + int pre1 = pre_state / 2, pre2 = pre_state % 2; + if((cur1 && pre1) || (cur2 && pre2)) continue; + dp[cur_state][i] += dp[pre_state][i - 1]; + } + dp[cur_state][i] %= MOD; + } + } + + long long res = 0; + for(int state = 0; state < 4; state ++) res += dp[state][n - 1]; + return res % MOD; + } +}; + +int main() { + + return 0; +} diff --git a/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main2.cpp b/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main2.cpp new file mode 100644 index 00000000..4a02ccb0 --- /dev/null +++ b/2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/main2.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/count-number-of-ways-to-place-houses/ +/// Author : liuyubobobo +/// Time : 2022-06-27 + +#include +#include + +using namespace std; + + +/// DP, just consider one line is enough +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int countHousePlacements(int n) { + + vector> dp(2, vector(n, 1)); + for(int i = 1; i < n; i ++){ + dp[0][i] = (dp[0][i - 1] + dp[1][i - 1]) % MOD; + dp[1][i] = dp[0][i - 1]; + } + + long long res = (dp[0][n - 1] + dp[1][n - 1]) % MOD; + return res * res % MOD; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/CMakeLists.txt b/2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/main.cpp b/2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/main.cpp new file mode 100644 index 00000000..f950bbe5 --- /dev/null +++ b/2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/maximum-score-of-spliced-array/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int maximumsSplicedArray(vector& nums1, vector& nums2) { + + int n = nums1.size(); + + vector presum1(n + 1, 0), presum2(n + 1, 0); + for(int i = 0; i < n; i ++){ + presum1[i + 1] = presum1[i] + nums1[i]; + presum2[i + 1] = presum2[i] + nums2[i]; + } + + vector presum12(n + 1, 0), presum21(n + 1, 0); + for(int i = 0; i <= n; i ++){ + presum12[i] = presum1[i] - presum2[i]; + presum21[i] = presum2[i] - presum1[i]; + } + + int max12 = 0, max21 = 0, res = max(presum1.back(), presum2.back()); + for(int i = 0; i < n; i ++){ + res = max(res, presum1.back() + presum21[i + 1] + max12); + res = max(res, presum2.back() + presum12[i + 1] + max21); + max12 = max(max12, presum12[i + 1]); + max21 = max(max21, presum21[i + 1]); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/CMakeLists.txt b/2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/main.cpp b/2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/main.cpp new file mode 100644 index 00000000..dd4c5686 --- /dev/null +++ b/2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/main.cpp @@ -0,0 +1,110 @@ +/// Source : https://leetcode.com/problems/minimum-score-after-removals-on-a-tree/ +/// Author : liuyubobobo +/// Time : 2022-06-25 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { +public: + int minimumScore(vector& nums, vector>& edges) { + + int n = nums.size(); + vector> tree(n); + for(const vector& e: edges){ + int u = e[0], v = e[1]; + tree[u].push_back(v), tree[v].push_back(u); + } + + vector> is_ancestor(n, vector(n, false)); + vector path; + dfs_ancestor(tree, 0, -1, path, is_ancestor); + + vector xor_subtree(n, 0); + dfs_xor_subtree(tree, 0, -1, nums, xor_subtree); + + int res = INT_MAX; + for(int i = 0; i < edges.size(); i ++){ + for(int j = i + 1; j < edges.size(); j ++){ + + int u1 = edges[i][0], v1 = edges[i][1]; + int root1 = (is_ancestor[u1][v1] ? v1 : u1); + + int xor1 = (xor_subtree[0] ^ xor_subtree[root1]); + int xor2 = xor_subtree[root1]; + int xor3 = 0; + + int u2 = edges[j][0], v2 = edges[j][1]; + int root2 = is_ancestor[u2][v2] ? v2 : u2; + + if(is_ancestor[root1][root2]){ + xor3 = xor_subtree[root2]; + xor2 ^= xor3; + } + else if(is_ancestor[root2][root1]){ + xor1 = (xor_subtree[0] ^ xor_subtree[root2]); + xor3 = xor_subtree[root2] ^ xor2; + } + else{ + assert(is_ancestor[0][root2]); + xor3 = xor_subtree[root2]; + xor1 ^= xor3; + } + + res = min(res, max3(xor1, xor2, xor3) - min3(xor1, xor2, xor3)); + } + } + return res; + } + +private: + int max3(int a, int b, int c){ + return max(a, max(b, c)); + } + + int min3(int a, int b, int c){ + return min(a, min(b, c)); + } + + void dfs_xor_subtree(const vector>& tree, int u, int p, + const vector& nums, vector& xor_subtree){ + + xor_subtree[u] ^= nums[u]; + for(int v: tree[u]){ + if(v == p) continue; + dfs_xor_subtree(tree, v, u, nums, xor_subtree); + xor_subtree[u] ^= xor_subtree[v]; + } + } + + void dfs_ancestor(const vector>& tree, int u, int p, + vector& path, vector>& is_ancestor){ + + for(int x: path) + is_ancestor[x][u] = true; + + path.push_back(u); + for(int v: tree[u]){ + if(v == p) continue; + dfs_ancestor(tree, v, u, path, is_ancestor); + } + path.pop_back(); + } +}; + + +int main() { + + vector nums2 = {5, 5, 2, 4, 4, 2}; + vector> edges2 = {{0, 1}, {1, 2}, {5, 2}, {4, 3}, {1, 3}}; + cout << Solution().minimumScore(nums2, edges2) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index e5fe76e3..2c72e763 100644 --- a/readme.md +++ b/readme.md @@ -2165,6 +2165,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/) | [无] | [C++](2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/) | | | | 2317 | [Maximum XOR After Operations](https://leetcode.com/problems/maximum-xor-after-operations/) | [无] | [C++](2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/) | | | | 2318 | [Number of Distinct Roll Sequences](https://leetcode.com/problems/number-of-distinct-roll-sequences/) | [无] | [C++](2001-2500/2318-Number-of-Distinct-Roll-Sequences/cpp-2318/) | | | +| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix/) | [无] | [C++](2001-2500/2319-Check-if-Matrix-Is-X-Matrix/cpp-2319/) | | | +| 2320 | [Count Number of Ways to Place Houses](https://leetcode.com/problems/count-number-of-ways-to-place-houses/) | [无] | [C++](2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/) | | | +| 2321 | [Maximum Score Of Spliced Array](https://leetcode.com/problems/maximum-score-of-spliced-array/) | [无] | [C++](2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/) | | | +| 2322 | [Minimum Score After Removals on a Tree](https://leetcode.com/problems/minimum-score-after-removals-on-a-tree/) | [无] | [C++](2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/) | | | | | | | | | | ## 力扣中文站比赛 From dce82fb8dd9b2137a83fc13903704e294278573f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 29 Jun 2022 12:14:48 -0700 Subject: [PATCH 110/390] 0406 solved. --- .../cpp-0406/CMakeLists.txt | 6 +++ .../cpp-0406/main.cpp | 53 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/CMakeLists.txt create mode 100644 0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/main.cpp diff --git a/0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/CMakeLists.txt b/0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/CMakeLists.txt new file mode 100644 index 00000000..fd975cf4 --- /dev/null +++ b/0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0406) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0406 main.cpp) diff --git a/0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/main.cpp b/0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/main.cpp new file mode 100644 index 00000000..021c621e --- /dev/null +++ b/0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/queue-reconstruction-by-height/ +/// Author : liuyubobobo +/// Time : 2022-06-29 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn + n^2) +/// Space Complexity: O(n) +class Solution { +public: + vector> reconstructQueue(vector>& people) { + + sort(people.begin(), people.end(), + [](const vector& p1, const vector& p2){ + if(p1[0] != p2[0]) return p1[0] > p2[0]; + return p1[1] < p2[1]; + }); + + vector> res = {people[0]}; + for(int i = 1; i < people.size(); i ++){ + + int j = 0, cnt = 0; + for(; j < (int)res.size() && cnt < people[i][1]; j ++){ + if(res[j][0] >= people[i][0]) cnt ++; + } + + auto iter = res.begin() + j; + res.insert(iter, people[i]); + } + return res; + } +}; + + +void print_vec(const vector>& v){ + for(const vector& p: v) + cout << '(' << p[0] << ',' << p[1] << ')'; + cout << '\n'; +} + +int main() { + + vector> people1 = {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}}; + print_vec(Solution().reconstructQueue(people1)); + + return 0; +} diff --git a/readme.md b/readme.md index 2c72e763..5af4bc7a 100644 --- a/readme.md +++ b/readme.md @@ -439,7 +439,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 403 | [Frog Jump](https://leetcode.com/problems/Frog-Jump/) | [solution](https://leetcode.com/problems/Frog-Jump/)
[缺:dp] | [C++](0001-0500/0403-Frog-Jump/cpp-0403/) | | | | 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [无] | [C++](0001-0500/0404-Sum-of-Left-Leaves/cpp-0404/) | | | | 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [无] | [C++](0001-0500/0405-Convert-a-Number-to-Hexadecimal/cpp-0405/) | | | -| | | | | | | +| 406 | [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [无] | [C++](0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/) | | | | 407 | [Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) | [无] | [C++](0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/) | | | | | | | | | | | 410 | [Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/) | [solution](https://leetcode.com/problems/split-array-largest-sum/solution/) [题解](https://leetcode-cn.com/problems/split-array-largest-sum/solution/fen-ge-shu-zu-de-zui-da-zhi-by-leetcode/) | [C++](0001-0500/410-Split-Array-Largest-Sum/cpp-410/) | | | From 148e8279a813d3bc653ae786f6ee14635a794f87 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 30 Jun 2022 21:00:05 -0700 Subject: [PATCH 111/390] 0241 solved. --- .../cpp-0241/CMakeLists.txt | 6 ++ .../cpp-0241/main.cpp | 76 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/CMakeLists.txt create mode 100644 0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/main.cpp diff --git a/0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/CMakeLists.txt b/0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/CMakeLists.txt new file mode 100644 index 00000000..60cd7605 --- /dev/null +++ b/0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0241) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0241 main.cpp) diff --git a/0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/main.cpp b/0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/main.cpp new file mode 100644 index 00000000..e50a987b --- /dev/null +++ b/0001-0500/0241-Different-Ways-to-Add-Parentheses/cpp-0241/main.cpp @@ -0,0 +1,76 @@ +/// Source : https://leetcode.com/problems/different-ways-to-add-parentheses/ +/// Author : liuyubobobo +/// Time : 2022-06-30 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n!) +/// Space Complexity: O(n) +class Solution { +public: + vector diffWaysToCompute(string exp) { + + vector nums; + vector ops; + for(int start = 0, i = 1; i <= exp.size(); i ++) + if(i == exp.size() || !isdigit(exp[i])){ + nums.push_back(atoi(exp.substr(start, i - start).c_str())); + + if(i < exp.size()) ops.push_back(exp[i]); + + start = i + 1; + i = start; + } + +// for(int e: nums) cout << e << ' '; cout << '\n'; +// for(char op: ops) cout << op << ' '; cout << '\n'; + + vector res = dfs(nums, ops, 0, nums.size() - 1); + return res; + } + +private: + vector dfs(const vector& nums, const vector& ops, int l, int r){ + + if(l == r) return {nums[l]}; + + vector res; + for(int left_end = l; left_end < r; left_end ++){ + vector a = dfs(nums, ops, l, left_end); + vector b = dfs(nums, ops, left_end + 1, r); + + for(int e1: a) + for(int e2: b) + res.push_back(calc(e1, e2, ops[left_end])); + } + return res; + } + + int calc(int a, int b, char op){ + if(op == '+') return a + b; + if(op == '-') return a - b; + if(op == '*') return a * b; + + assert(false); + return -1; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + print_vec(Solution().diffWaysToCompute("2-1-1")); + + print_vec(Solution().diffWaysToCompute("2*3-4*5")); + + return 0; +} diff --git a/readme.md b/readme.md index 5af4bc7a..96cdb758 100644 --- a/readme.md +++ b/readme.md @@ -280,7 +280,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [solution](https://leetcode.com/problems/product-of-array-except-self/solution/) | [C++](0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/) | | [Python](0001-0500/0238-Product-of-Array-Except-Self/py-0238/) | | 239 | [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/description/) | [solution](https://leetcode.com/problems/sliding-window-maximum/solution/) | [C++](0001-0500/0239-Sliding-Window-Maximum/cpp-0239/) | | | | 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [solution](https://leetcode.com/problems/search-a-2d-matrix-ii/solution/)
[缺:分治] | [C++](0001-0500/240-Search-a-2D-Matrix-II/cpp-240/) | | | -| | | | | | | +| 241 | [Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) | [无] | [C++](0001-0500/241-Different-Ways-to-Add-Parentheses/cpp-241/) | | | | 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [solution](https://leetcode.com/problems/valid-anagram/solution/) | [C++](0001-0500/0242-Valid-Anagram/cpp-0242/) | | | | 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [solution](https://leetcode.com/problems/shortest-word-distance/solution/) | [C++](0001-0500/0243-Shortest-Word-Distance/cpp-0243/) | | | | 244 | [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/) | [solution](https://leetcode.com/problems/shortest-word-distance-ii/solution/) | [C++](0001-0500/0244-Shortest-Word-Distance-II/cpp-0244/) | | | From 7e066b3c22fae46fcc9fc891daae2378355c6447 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 30 Jun 2022 21:49:47 -0700 Subject: [PATCH 112/390] 2323 solved. --- .../cpp-2323/CMakeLists.txt | 6 +++ .../cpp-2323/main.cpp | 48 +++++++++++++++++++ readme.md | 1 + 3 files changed, 55 insertions(+) create mode 100644 2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt create mode 100644 2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp diff --git a/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt new file mode 100644 index 00000000..29632ca0 --- /dev/null +++ b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2323) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2323 main.cpp) diff --git a/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp new file mode 100644 index 00000000..0b05a9a1 --- /dev/null +++ b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs-ii/ +/// Author : liuyubobobo +/// Time : 2022-06-30 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn + nlog(max_jobs)) +/// Space Complexity: O(1) +class Solution { +public: + int minimumTime(vector& jobs, vector& workers) { + + sort(jobs.begin(), jobs.end()); + sort(workers.begin(), workers.end()); + + int l = 1, r = 1e5; + while(l < r){ + int mid = (l + r) / 2; + if(ok(jobs, workers, mid)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(const vector& jobs, const vector& workers, int d){ + + for(int i = 0; i < workers.size(); i ++) + if(1ll * workers[i] * d < 1ll * jobs[i]) return false; + return true; + } +}; + + +int main() { + + vector jobs1 = {5, 2, 4}, workers1 = {1, 7, 5}; + cout << Solution().minimumTime(jobs1, workers1) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 96cdb758..94dfde4a 100644 --- a/readme.md +++ b/readme.md @@ -2169,6 +2169,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2320 | [Count Number of Ways to Place Houses](https://leetcode.com/problems/count-number-of-ways-to-place-houses/) | [无] | [C++](2001-2500/2320-Count-Number-of-Ways-to-Place-Houses/cpp-2320/) | | | | 2321 | [Maximum Score Of Spliced Array](https://leetcode.com/problems/maximum-score-of-spliced-array/) | [无] | [C++](2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/) | | | | 2322 | [Minimum Score After Removals on a Tree](https://leetcode.com/problems/minimum-score-after-removals-on-a-tree/) | [无] | [C++](2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/) | | | +| 2323 | [Find Minimum Time to Finish All Jobs II](https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs-ii/) | [无] | [C++](2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/) | | | | | | | | | | ## 力扣中文站比赛 From 177af591414b76e502f383db8cc0f97d83e47e4b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 1 Jul 2022 00:58:19 -0700 Subject: [PATCH 113/390] 2323 new algo added. --- .../cpp-2323/CMakeLists.txt | 2 +- .../cpp-2323/main.cpp | 2 +- .../cpp-2323/main2.cpp | 37 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main2.cpp diff --git a/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt index 29632ca0..0caaac65 100644 --- a/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt +++ b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_2323) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_2323 main.cpp) +add_executable(cpp_2323 main2.cpp) diff --git a/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp index 0b05a9a1..2fc7d43f 100644 --- a/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp +++ b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main.cpp @@ -9,7 +9,7 @@ using namespace std; -/// Binary Search +/// Binary Search + Greedy /// Time Complexity: O(nlogn + nlog(max_jobs)) /// Space Complexity: O(1) class Solution { diff --git a/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main2.cpp b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main2.cpp new file mode 100644 index 00000000..67553178 --- /dev/null +++ b/2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/main2.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs-ii/ +/// Author : liuyubobobo +/// Time : 2022-07-01 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int minimumTime(vector& jobs, vector& workers) { + + sort(jobs.begin(), jobs.end()); + sort(workers.begin(), workers.end()); + + int res = 0; + for(int i = 0; i < jobs.size(); i ++) + res = max(res, jobs[i] / workers[i] + !!(jobs[i] % workers[i])); + return res; + } +}; + + +int main() { + + vector jobs1 = {5, 2, 4}, workers1 = {1, 7, 5}; + cout << Solution().minimumTime(jobs1, workers1) << '\n'; + // 2 + + return 0; +} From 4ee4b143274330cdfbd76e684dc43be10d7e52f0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 2 Jul 2022 21:27:00 -0700 Subject: [PATCH 114/390] 2325-2328 solved. --- .../cpp-2325/CMakeLists.txt | 6 ++ .../2325-Decode-the-Message/cpp-2325/main.cpp | 39 ++++++++++ .../cpp-2326/CMakeLists.txt | 6 ++ .../2326-Spiral-Matrix-IV/cpp-2326/main.cpp | 63 ++++++++++++++++ .../cpp-2327/CMakeLists.txt | 6 ++ .../cpp-2327/main.cpp | 57 +++++++++++++++ .../cpp-2327/main2.cpp | 73 +++++++++++++++++++ .../cpp-2328/CMakeLists.txt | 6 ++ .../cpp-2328/main.cpp | 67 +++++++++++++++++ readme.md | 7 +- 10 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2325-Decode-the-Message/cpp-2325/CMakeLists.txt create mode 100644 2001-2500/2325-Decode-the-Message/cpp-2325/main.cpp create mode 100644 2001-2500/2326-Spiral-Matrix-IV/cpp-2326/CMakeLists.txt create mode 100644 2001-2500/2326-Spiral-Matrix-IV/cpp-2326/main.cpp create mode 100644 2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/CMakeLists.txt create mode 100644 2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main.cpp create mode 100644 2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main2.cpp create mode 100644 2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/CMakeLists.txt create mode 100644 2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/main.cpp diff --git a/2001-2500/2325-Decode-the-Message/cpp-2325/CMakeLists.txt b/2001-2500/2325-Decode-the-Message/cpp-2325/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2325-Decode-the-Message/cpp-2325/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2325-Decode-the-Message/cpp-2325/main.cpp b/2001-2500/2325-Decode-the-Message/cpp-2325/main.cpp new file mode 100644 index 00000000..3c09525b --- /dev/null +++ b/2001-2500/2325-Decode-the-Message/cpp-2325/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/decode-the-message/ +/// Author : liuyubobobo +/// Time : 2022-07-02 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|key| + |message|) +/// Space Complexity: O(1) +class Solution { +public: + string decodeMessage(string key, string message) { + + vector used(26, false); + vector table(26); + char ch = 'a'; + for(char c: key){ + if(c == ' ' || used[c - 'a']) continue; + table[c - 'a'] = ch ++; + used[c - 'a'] = true; + } + + for(char& c: message){ + if(c == ' ') continue; + c = table[c - 'a']; + } + return message; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2326-Spiral-Matrix-IV/cpp-2326/CMakeLists.txt b/2001-2500/2326-Spiral-Matrix-IV/cpp-2326/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2326-Spiral-Matrix-IV/cpp-2326/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2326-Spiral-Matrix-IV/cpp-2326/main.cpp b/2001-2500/2326-Spiral-Matrix-IV/cpp-2326/main.cpp new file mode 100644 index 00000000..1cc5421d --- /dev/null +++ b/2001-2500/2326-Spiral-Matrix-IV/cpp-2326/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/spiral-matrix-iv/ +/// Author : liuyubobobo +/// Time : 2022-07-02 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(m * n) +/// Space Complexity: O(1) + +/// Definition for singly-linked list. +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution { + +private: + const int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + int R, C; + +public: + vector> spiralMatrix(int R, int C, ListNode* node) { + + this->R = R, this->C = C; + vector> res(R, vector(C, -1)); + + int cx = 0, cy = -1, d = 0; + while(node){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(!in_area(nx, ny) || res[nx][ny] != -1){ + d = (d + 1) % 4; + nx = cx + dirs[d][0], ny = cy + dirs[d][1]; +// assert(in_area(nx, ny)); + } + + res[nx][ny] = node->val; + node = node->next; + cx = nx, cy = ny; + } + return res; + } + +private: + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/CMakeLists.txt b/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/CMakeLists.txt new file mode 100644 index 00000000..586665ca --- /dev/null +++ b/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main.cpp b/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main.cpp new file mode 100644 index 00000000..de612e68 --- /dev/null +++ b/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/number-of-people-aware-of-a-secret/ +/// Author : liuyubobobo +/// Time : 2022-07-02 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int peopleAwareOfSecret(int n, int delay, int forget) { + + vector pos(n, 0), neg(n, 0); + pos[0] = 1; + for(int i = 0; i < n; i ++){ + + for(int j = delay; j < forget && i + j < n; j ++){ + pos[i + j] += pos[i]; + pos[i + j] %= MOD; + } + + if(i + forget < n){ + neg[i + forget] += pos[i]; + neg[i + forget] %= MOD; + } + } + + long long sum_pos = accumulate(pos.begin(), pos.end(), 0ll); + long long sum_neg = accumulate(neg.begin(), neg.end(), 0ll); + return (sum_pos % MOD - sum_neg % MOD + MOD) % MOD; + } +}; + + +int main() { + + cout << Solution().peopleAwareOfSecret(6, 2, 4) << '\n'; + // 5 + + cout << Solution().peopleAwareOfSecret(4, 1, 3) << '\n'; + // 6 + + cout << Solution().peopleAwareOfSecret(6, 1, 2) << '\n'; + // 2 + + return 0; +} diff --git a/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main2.cpp b/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main2.cpp new file mode 100644 index 00000000..50f5d0be --- /dev/null +++ b/2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/main2.cpp @@ -0,0 +1,73 @@ +/// Source : https://leetcode.com/problems/number-of-people-aware-of-a-secret/ +/// Author : liuyubobobo +/// Time : 2022-07-02 + +#include +#include +#include + +using namespace std; + + +/// DP with diff array +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int peopleAwareOfSecret(int n, int delay, int forget) { + + vector diff_pos(n, 0), neg(n, 0); + diff_pos[0] = 1; + if(1 < n) diff_pos[1] = MOD - 1; + long long presum = 0; + for(int i = 0; i < n; i ++){ + + presum += diff_pos[i]; + presum %= MOD; + + if(i + delay < n){ + diff_pos[i + delay] += presum; + diff_pos[i + delay] %= MOD; + } + + if(i + forget < n){ + diff_pos[i + forget] -= presum; + diff_pos[i + forget] += MOD; + diff_pos[i + forget] %= MOD; + + neg[i + forget] += presum; + neg[i + forget] %= MOD; + } + } + + long long sum_pos = 0; + presum = 0; + for(int i = 0; i < n; i ++){ + presum += diff_pos[i]; + presum %= MOD; + sum_pos += presum; + } + long long sum_neg = accumulate(neg.begin(), neg.end(), 0ll); + + return (sum_pos % MOD - sum_neg % MOD + MOD) % MOD; + } +}; + + +int main() { + + cout << Solution().peopleAwareOfSecret(6, 2, 4) << '\n'; + // 5 + + cout << Solution().peopleAwareOfSecret(4, 1, 3) << '\n'; + // 6 + + cout << Solution().peopleAwareOfSecret(6, 1, 2) << '\n'; + // 2 + + return 0; +} diff --git a/2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/CMakeLists.txt b/2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/main.cpp b/2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/main.cpp new file mode 100644 index 00000000..2959ce0c --- /dev/null +++ b/2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/ +/// Author : liuyubobobo +/// Time : 2022-07-02 + +#include +#include + +using namespace std; + + +/// DAG DP +/// Time Complexity: O(m * n) +/// Space Complexity: O(m * n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + int R, C; + +public: + int countPaths(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + + vector> dp(R, vector(C, -1)); + long long res = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + res += dfs(grid, i, j, dp); + return res % MOD; + } + +private: + long long dfs(const vector>& g, int cx, int cy, + vector>& dp){ + + if(dp[cx][cy] != -1) return dp[cx][cy]; + + long long res = 1; + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(!in_area(nx, ny) || g[nx][ny] <= g[cx][cy]) continue; + res += dfs(g, nx, ny, dp); + } + + return dp[cx][cy] = res % MOD; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> grid1 = {{1, 1}, {3, 4}}; + cout << Solution().countPaths(grid1) << '\n'; + // 8 + + vector> grid2 = {{1}, {2}}; + cout << Solution().countPaths(grid2) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index 94dfde4a..2bf9fd48 100644 --- a/readme.md +++ b/readme.md @@ -2160,7 +2160,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2312 | [Selling Pieces of Wood](https://leetcode.com/problems/selling-pieces-of-wood/) | [无] | [C++](2001-2500/2312-Selling-Pieces-of-Wood/cpp-2312/) | | | | 2313 | [Minimum Flips in Binary Tree to Get Result](https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/) | [无] | [C++](2001-2500/2313-Minimum-Flips-in-Binary-Tree-to-Get-Result/cpp-2313/) | | | -| | | | | | | +| 2314 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks/) | [无] | [C++](2001-2500/2315-Count-Asterisks/cpp-2315/) | | | | 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/) | [无] | [C++](2001-2500/2316-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph/cpp-2316/) | | | | 2317 | [Maximum XOR After Operations](https://leetcode.com/problems/maximum-xor-after-operations/) | [无] | [C++](2001-2500/2317-Maximum-XOR-After-Operations/cpp-2317/) | | | @@ -2170,6 +2170,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2321 | [Maximum Score Of Spliced Array](https://leetcode.com/problems/maximum-score-of-spliced-array/) | [无] | [C++](2001-2500/2321-Maximum-Score-Of-Spliced-Array/cpp-2321/) | | | | 2322 | [Minimum Score After Removals on a Tree](https://leetcode.com/problems/minimum-score-after-removals-on-a-tree/) | [无] | [C++](2001-2500/2322-Minimum-Score-After-Removals-on-a-Tree/cpp-2322/) | | | | 2323 | [Find Minimum Time to Finish All Jobs II](https://leetcode.com/problems/find-minimum-time-to-finish-all-jobs-ii/) | [无] | [C++](2001-2500/2323-Find-Minimum-Time-to-Finish-All-Jobs-II/cpp-2323/) | | | +| 2324 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2325 | [Decode the Message](https://leetcode.com/problems/decode-the-message/) | [无] | [C++](2001-2500/2325-Decode-the-Message/cpp-2325/) | | | +| 2326 | [Spiral Matrix IV](https://leetcode.com/problems/spiral-matrix-iv/) | [无] | [C++](2001-2500/2326-Spiral-Matrix-IV/cpp-2326/) | | | +| 2327 | [Number of People Aware of a Secret](https://leetcode.com/problems/number-of-people-aware-of-a-secret/) | [无] | [C++](2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/) | | | +| 2328 | [Number of Increasing Paths in a Grid](https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/) | [无] | [C++](2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/) | | | | | | | | | | ## 力扣中文站比赛 From 10825343f14a26020bf441725873946ce2f91ed7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 8 Jul 2022 14:56:58 -0700 Subject: [PATCH 115/390] 0510 solved. --- .../cpp-0510/CMakeLists.txt | 6 ++ .../cpp-0510/main.cpp | 56 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/CMakeLists.txt create mode 100644 0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/main.cpp diff --git a/0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/CMakeLists.txt b/0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/CMakeLists.txt new file mode 100644 index 00000000..4dcc79c4 --- /dev/null +++ b/0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0510) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0510 main.cpp) diff --git a/0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/main.cpp b/0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/main.cpp new file mode 100644 index 00000000..f77f990b --- /dev/null +++ b/0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/inorder-successor-in-bst-ii/s +/// Author : liuyubobobo +/// Time : 2022-07-08 + +#include +#include + +using namespace std; + + +/// Tree Algorithm +/// Time Compelxity: O(h) +/// Space Compelxity: O(1) + +// Definition for a Node. +class Node { +public: + int val; + Node* left; + Node* right; + Node* parent; +}; + +class Solution { +public: + Node* inorderSuccessor(Node* node) { + + if(node->right){ + return minimum(node->right); + } + + Node* cur = node, *pre = node->parent; + while(pre){ + if(pre->left == cur){ + return pre; + } + else{ + cur = pre; + pre = cur->parent; + } + } + return nullptr; + } + +private: + Node* minimum(Node* node){ + while(node->left) node = node->left; + return node; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 2bf9fd48..22cb6fb6 100644 --- a/readme.md +++ b/readme.md @@ -539,7 +539,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 507 | [Perfect Number](https://leetcode.com/problems/perfect-number/) | [solution](https://leetcode.com/problems/perfect-number/solution/) | [C++](0501-1000/0507-Perfect-Number/cpp-0507/) | | | | 508 | [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/) | [无] | [C++](0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/) | | | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [无] | [C++](0501-1000/0509-Fibonacci-Number/cpp-0509/) | | | -| | | | | | | +| 510 | [Inorder Successor in BST II](https://leetcode.com/problems/inorder-successor-in-bst-ii/) | [无] | [C++](0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/) | | | | 511 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 513 | [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | [无] | [C++](0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/) | | | From 5c15b1f0c77491472b4a222f8c499d9f6ec3351d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 10 Jul 2022 15:01:46 -0700 Subject: [PATCH 116/390] 2335-2338 added. --- .../cpp-2335/CMakeLists.txt | 6 ++ .../cpp-2335/main.cpp | 41 +++++++++ .../cpp-2336/CMakeLists.txt | 6 ++ .../cpp-2336/main.cpp | 63 ++++++++++++++ .../cpp-2337/CMakeLists.txt | 6 ++ .../cpp-2337/main.cpp | 39 +++++++++ .../cpp-2338/CMakeLists.txt | 6 ++ .../cpp-2338/main.cpp | 87 +++++++++++++++++++ .../cpp-2338/main2.cpp | 79 +++++++++++++++++ readme.md | 5 ++ 10 files changed, 338 insertions(+) create mode 100644 2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/CMakeLists.txt create mode 100644 2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/main.cpp create mode 100644 2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/CMakeLists.txt create mode 100644 2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/main.cpp create mode 100644 2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/CMakeLists.txt create mode 100644 2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/main.cpp create mode 100644 2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/CMakeLists.txt create mode 100644 2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main.cpp create mode 100644 2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main2.cpp diff --git a/2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/CMakeLists.txt b/2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/main.cpp b/2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/main.cpp new file mode 100644 index 00000000..65f9fdd4 --- /dev/null +++ b/2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/ +/// Author : liuyubobobo +/// Time : 2022-07-10 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(sum(amount)) +/// Space Complexity: O(1) +class Solution { +public: + int fillCups(vector& amount) { + + priority_queue pq; + for(int i = 0; i < 3; i ++) + if(amount[i]) pq.push(amount[i]); + + int res = 0; + while(pq.size() >= 2){ + int a = pq.top(); pq.pop(); + int b = pq.top(); pq.pop(); + res ++, a --, b --; + if(a) pq.push(a); + if(b) pq.push(b); + } + + if(pq.size()) res += pq.top(); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/CMakeLists.txt b/2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/main.cpp b/2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/main.cpp new file mode 100644 index 00000000..adfd494c --- /dev/null +++ b/2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/smallest-number-in-infinite-set/ +/// Author : liuyubobobo +/// Time : 2022-06-08 + +#include +#include + +using namespace std; + + +/// Using Set +/// Time Complexity: init: O(1) +/// pop: O(logn) +/// add: O(logn) +/// Space Compelxity: O(n) +class SmallestInfiniteSet { + +private: + int start = 1; + set others; + +public: + SmallestInfiniteSet() {} + + int popSmallest() { + + if(!others.empty()){ + int ret = *others.begin(); + others.erase(others.begin()); + return ret; + } + + int ret = start; + start ++; + return ret; + } + + void addBack(int num) { + + if(others.count(num) || num >= start) return; + + if(num == start - 1){ + start --; + while(true){ + auto iter = others.find(start - 1); + if(iter != others.end()){ + others.erase(iter); + start --; + } + else break; + } + } + else{ + others.insert(num); + } + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/CMakeLists.txt b/2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/main.cpp b/2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/main.cpp new file mode 100644 index 00000000..f2b2f9df --- /dev/null +++ b/2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/move-pieces-to-obtain-a-string/ +/// Author : liuyubobobo +/// Time : 2022-07-10 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + bool canChange(string start, string target) { + + vector> pos1, pos2; + for(int i = 0; i < start.size(); i ++) + if(start[i] != '_') pos1.push_back({start[i], i}); + for(int i = 0; i < target.size(); i ++) + if(target[i] != '_') pos2.push_back({target[i], i}); + + if(pos1.size() != pos2.size()) return false; + + for(int i = 0; i < pos1.size(); i ++){ + if(pos1[i].first != pos2[i].first) return false; + if(pos1[i].first == 'L' && pos1[i].second < pos2[i].second) return false; + if(pos1[i].first == 'R' && pos1[i].second > pos2[i].second) return false; + } + return true; + } +}; + + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/CMakeLists.txt b/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/CMakeLists.txt new file mode 100644 index 00000000..bb71ae36 --- /dev/null +++ b/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) diff --git a/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main.cpp b/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main.cpp new file mode 100644 index 00000000..268cb79f --- /dev/null +++ b/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main.cpp @@ -0,0 +1,87 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-ideal-arrays/ +/// Author : liuyubobobo +/// Time : 2022-07-10 + +#include +#include + +using namespace std; + + +/// Backtrack + Math +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + vector fac, ifac; + +public: + Solution(): fac(1e4 + 1, 1), ifac(1e4 + 1){} + + int idealArrays(int n, int maxValue) { + + for(int i = 1; i <= n; i ++) + fac[i] = fac[i - 1] * i % MOD; + for(int i = 0; i <= n; i ++) + ifac[i] = quick_pow(fac[i], MOD - 2); + + vector seq; + return go(seq, n, maxValue); + } + +private: + long long go(vector& seq, int n, int maxValue){ + + long long res = 0; + if(seq.empty()){ + for(int i = 1; i <= maxValue; i ++){ + seq.push_back(i); + res += go(seq, n, maxValue); + seq.pop_back(); + } + return res % MOD; + } + + int k = seq.size() - 1; + res += fac[n - 1] * ifac[k] % MOD * ifac[n - 1 - k] % MOD; + + if(seq.size() == n) return res; + + for(int d = 2; seq.back() * d <= maxValue; d ++){ + seq.push_back(seq.back() * d); + res += go(seq, n, maxValue); + seq.pop_back(); + } + return res % MOD; + } + + long long quick_pow(long long a, long long k) { + long long res = 1ll; + while (k) { + if (k & 1) res = res * a % MOD; + a = a * a % MOD; + k >>= 1; + } + return res % MOD; + } +}; + + +int main() { + + cout << Solution().idealArrays(2, 5) << '\n'; + // 10 + + cout << Solution().idealArrays(5, 3) << '\n'; + // 11 + + cout << Solution().idealArrays(5878, 2900) << '\n'; + // 465040898 + + cout << Solution().idealArrays(1e4, 1e4) << '\n'; + // 22940607 + + return 0; +} diff --git a/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main2.cpp b/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main2.cpp new file mode 100644 index 00000000..fcb5b2ff --- /dev/null +++ b/2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/main2.cpp @@ -0,0 +1,79 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-ideal-arrays/ +/// Author : liuyubobobo +/// Time : 2022-07-10 + +#include +#include + +using namespace std; + + +/// Backtrack + Math +/// Using dp is faster than multiplication reverse in this problem +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int idealArrays(int n, int maxValue) { + + vector> dp(n + 1, vector(20, 0ll)); + dp[1][0] = dp[1][1] = 1; + for(int i = 2; i <= n; i ++){ + dp[i][0] = 1; + if(i < 20) dp[i][i] = 1; + for(int k = 1; k < min(i, 20); k ++) + dp[i][k] = (dp[i - 1][k - 1] + dp[i - 1][k]) % MOD; + } + + vector seq; + return go(seq, n, maxValue, dp); + } + +private: + long long go(vector& seq, int n, int maxValue, + const vector>& dp){ + + long long res = 0; + if(seq.empty()){ + for(int i = 1; i <= maxValue; i ++){ + seq.push_back(i); + res += go(seq, n, maxValue, dp); + seq.pop_back(); + } + return res % MOD; + } + + res += dp[n - 1][seq.size() - 1]; + + if(seq.size() == n) return res; + + for(int d = 2; seq.back() * d <= maxValue; d ++){ + seq.push_back(seq.back() * d); + res += go(seq, n, maxValue, dp); + seq.pop_back(); + } + return res % MOD; + } +}; + + +int main() { + + cout << Solution().idealArrays(2, 5) << '\n'; + // 10 + + cout << Solution().idealArrays(5, 3) << '\n'; + // 11 + + cout << Solution().idealArrays(5878, 2900) << '\n'; + // 465040898 + + cout << Solution().idealArrays(1e4, 1e4) << '\n'; + // 22940607 + + return 0; +} diff --git a/readme.md b/readme.md index 22cb6fb6..8c358ed8 100644 --- a/readme.md +++ b/readme.md @@ -2176,6 +2176,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2327 | [Number of People Aware of a Secret](https://leetcode.com/problems/number-of-people-aware-of-a-secret/) | [无] | [C++](2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/) | | | | 2328 | [Number of Increasing Paths in a Grid](https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/) | [无] | [C++](2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/) | | | | | | | | | | +| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [无] | [C++](2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/) | | | +| 2336 | [Smallest Number in Infinite Set](https://leetcode.com/problems/smallest-number-in-infinite-set/) | [无] | [C++](2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/) | | | +| 2337 | [Move Pieces to Obtain a String](https://leetcode.com/problems/move-pieces-to-obtain-a-string/) | [无] | [C++](2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/) | | | +| 2338 | [Count the Number of Ideal Arrays](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | [无] | [C++](2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/) | | | +| | | | | | | ## 力扣中文站比赛 From 0071e364a815850ef0fe0c65b1cece33e49f410b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 10 Jul 2022 15:09:27 -0700 Subject: [PATCH 117/390] 2334 added. --- .../cpp-2334/CMakeLists.txt | 6 + .../cpp-2334/main.cpp | 112 ++++++++++++++++++ readme.md | 1 + 3 files changed, 119 insertions(+) create mode 100644 2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/CMakeLists.txt create mode 100644 2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/main.cpp diff --git a/2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/CMakeLists.txt b/2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/main.cpp b/2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/main.cpp new file mode 100644 index 00000000..21e0cc6a --- /dev/null +++ b/2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/main.cpp @@ -0,0 +1,112 @@ +/// Source : https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/ +/// Author : liuyubobobo +/// Time : 2022-07-09 + +#include +#include + +using namespace std; + + +/// Divide and Conquer +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class MinIndexSegmentTree{ + +private: + int n; + vector data, tree; + +public: + MinIndexSegmentTree(const vector& data): n(data.size()), data(data), tree(4 * n, 0){ + buildSegTree(0, 0, n - 1); + } + + T query(int index){ + assert(0 <= index && index < n); + return data[index]; + } + + T query(int l, int r){ + assert(l <= r); + assert(0 <= l && l < n); + assert(0 <= r && r < n); + return query(0, 0, n - 1, l, r); + } + +private: + void buildSegTree(int treeID, int l, int r){ + + if(l == r){ + tree[treeID] = l; + return; + } + + int mid = (l + r) / 2; + buildSegTree(treeID * 2 + 1, l, mid); + buildSegTree(treeID * 2 + 2, mid + 1, r); + tree[treeID] = data[tree[treeID * 2 + 1]] < data[tree[treeID * 2 + 2]] ? tree[treeID * 2 + 1] : tree[treeID * 2 + 2]; + return; + } + + T query(int treeID, int l, int r, int ql, int qr){ + + if(ql == l && qr == r) + return tree[treeID]; + + int mid = (l + r) / 2; + if(qr <= mid) return query(treeID * 2 + 1, l, mid, ql, qr); + else if(ql > mid) return query(treeID * 2 + 2, mid + 1, r, ql, qr); + + T resl = query(treeID * 2 + 1, l, mid, ql, mid); + T resr = query(treeID * 2 + 2, mid + 1, r, mid + 1, qr); + return data[resl] < data[resr] ? resl : resr; + } +}; + +class Solution { + +private: + MinIndexSegmentTree *seg_tree; + +public: + int validSubarraySize(vector& nums, int threshold) { + + seg_tree = new MinIndexSegmentTree(nums); + + int res = solve(nums, 0, nums.size() - 1, threshold); + return res ? res : -1; + } + + int solve(const vector& nums, int l, int r, int threshold){ + + if(l > r) return 0; + if(l == r){ + return nums[l] > threshold ? 1: 0; + } + + int min_index = seg_tree->query(l, r); + int min_value = nums[min_index]; + + if(min_value > threshold / (r - l + 1)) return r - l + 1; + + int resl = solve(nums, l, min_index - 1, threshold); + if(resl) return resl; + return solve(nums, min_index + 1, r, threshold); + } +}; + + +int main() { + + vector nums1 = {1, 3, 4, 3, 1}; + cout << Solution().validSubarraySize(nums1, 6) << '\n'; + // 3 + + vector nums2 = {6,5,6,5,8}; + cout << Solution().validSubarraySize(nums2, 7) << '\n'; + // 1 + + return 0; +} diff --git a/readme.md b/readme.md index 8c358ed8..e9a5ddb0 100644 --- a/readme.md +++ b/readme.md @@ -2176,6 +2176,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2327 | [Number of People Aware of a Secret](https://leetcode.com/problems/number-of-people-aware-of-a-secret/) | [无] | [C++](2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/) | | | | 2328 | [Number of Increasing Paths in a Grid](https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/) | [无] | [C++](2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/) | | | | | | | | | | +| 2334 | [Subarray With Elements Greater Than Varying Threshold](https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/) | [无] | [C++](2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/) | | | | 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [无] | [C++](2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/) | | | | 2336 | [Smallest Number in Infinite Set](https://leetcode.com/problems/smallest-number-in-infinite-set/) | [无] | [C++](2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/) | | | | 2337 | [Move Pieces to Obtain a String](https://leetcode.com/problems/move-pieces-to-obtain-a-string/) | [无] | [C++](2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/) | | | From 394b4e17886eb80976bf33ee56f5d2a0a81849f4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 10 Jul 2022 15:14:44 -0700 Subject: [PATCH 118/390] 2333 added. --- .../cpp-2333/CMakeLists.txt | 6 ++ .../cpp-2333/main.cpp | 76 +++++++++++++++++++ readme.md | 1 + 3 files changed, 83 insertions(+) create mode 100644 2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/CMakeLists.txt create mode 100644 2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/main.cpp diff --git a/2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/CMakeLists.txt b/2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/main.cpp b/2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/main.cpp new file mode 100644 index 00000000..47aa7a91 --- /dev/null +++ b/2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/main.cpp @@ -0,0 +1,76 @@ +/// Source : https://leetcode.com/problems/minimum-sum-of-squared-difference/ +/// Author : liuyubobobo +/// Time : 2022-07-10 + +#include +#include +#include + +using namespace std; + + +/// Soring and Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long minSumSquareDiff(vector& nums1, vector& nums2, int k1, int k2) { + + int n = nums1.size(); + vector d(n); + for(int i = 0; i < n; i ++) d[i] = abs(nums1[i] - nums2[i]); + + sort(d.begin(), d.end(), greater()); + d.push_back(0); + n ++; + + long long k = k1 + k2; + + long long pre = d[0]; + int i; + for(i = 1; i < n; i ++){ + long long cur = d[i]; + long long diff = pre - cur; + if(diff * i <= k){ + k -= diff * i; + pre = cur; + } + else{ + break; + } + } + + if(i < n){ + for(int j = 0; j < i; j ++) d[j] = pre; + + for(int j = 0; j < i; j ++){ + d[j] -= k / i; + if(j < k % i) d[j] --; + } + } + else return 0; + + long long res = 0; + for(int i = 0; i < n; i ++) + res += d[i] * d[i]; + return res; + } +}; + + +int main() { + + vector nums11 = {1, 2, 3, 4}, nums12 = {2, 10, 20, 19}; + cout << Solution().minSumSquareDiff(nums11, nums12, 0, 0) << '\n'; + // 579 + + vector nums21 = {1, 4, 10, 12}, nums22 = {5, 8, 6, 9}; + cout << Solution().minSumSquareDiff(nums21, nums22, 1, 1) << '\n'; + // 43 + + vector nums31 = {19, 18, 19, 18, 18, 19, 19}, nums32 = {1, 0, 1, 0, 0, 1, 1}; + cout << Solution().minSumSquareDiff(nums31, nums32, 10, 33) << '\n'; + // 985 + + return 0; +} diff --git a/readme.md b/readme.md index e9a5ddb0..3c309314 100644 --- a/readme.md +++ b/readme.md @@ -2176,6 +2176,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2327 | [Number of People Aware of a Secret](https://leetcode.com/problems/number-of-people-aware-of-a-secret/) | [无] | [C++](2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/) | | | | 2328 | [Number of Increasing Paths in a Grid](https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/) | [无] | [C++](2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/) | | | | | | | | | | +| 2333 | [Minimum Sum of Squared Difference](https://leetcode.com/problems/minimum-sum-of-squared-difference/) | [无] | [C++](2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/) | | | | 2334 | [Subarray With Elements Greater Than Varying Threshold](https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/) | [无] | [C++](2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/) | | | | 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [无] | [C++](2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/) | | | | 2336 | [Smallest Number in Infinite Set](https://leetcode.com/problems/smallest-number-in-infinite-set/) | [无] | [C++](2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/) | | | From c635289e082841213e003faf5c560f2a630a37f2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 10 Jul 2022 15:20:07 -0700 Subject: [PATCH 119/390] 2331 added. --- .../cpp-2331/CMakeLists.txt | 6 +++ .../cpp-2331/main.cpp | 43 +++++++++++++++++++ readme.md | 3 ++ 3 files changed, 52 insertions(+) create mode 100644 2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/CMakeLists.txt create mode 100644 2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/main.cpp diff --git a/2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/CMakeLists.txt b/2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/main.cpp b/2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/main.cpp new file mode 100644 index 00000000..4cdf3bd8 --- /dev/null +++ b/2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/evaluate-boolean-binary-tree/ +/// Author : liuyubobobo +/// Time : 2022-07-09 + +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(h) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + bool evaluateTree(TreeNode* root) { + + if(root->left == nullptr || root->right == nullptr) + return root->val; + + int a = evaluateTree(root->left); + int b = evaluateTree(root->right); + + if(root->val == 2) return a | b; + return a & b; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3c309314..26b3d50e 100644 --- a/readme.md +++ b/readme.md @@ -2175,6 +2175,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2326 | [Spiral Matrix IV](https://leetcode.com/problems/spiral-matrix-iv/) | [无] | [C++](2001-2500/2326-Spiral-Matrix-IV/cpp-2326/) | | | | 2327 | [Number of People Aware of a Secret](https://leetcode.com/problems/number-of-people-aware-of-a-secret/) | [无] | [C++](2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/) | | | | 2328 | [Number of Increasing Paths in a Grid](https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/) | [无] | [C++](2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/) | | | +| 2329 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | +| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree/) | [无] | [C++](2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/) | | | | | | | | | | | 2333 | [Minimum Sum of Squared Difference](https://leetcode.com/problems/minimum-sum-of-squared-difference/) | [无] | [C++](2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/) | | | | 2334 | [Subarray With Elements Greater Than Varying Threshold](https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/) | [无] | [C++](2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/) | | | From ebfbe7dd473dff62ab3e80905bf7979b484b1653 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 10 Jul 2022 15:28:43 -0700 Subject: [PATCH 120/390] 2330 solved. --- .../cpp-2330/CMakeLists.txt | 6 ++++ .../cpp-2330/main.cpp | 29 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2330-Valid-Palindrome-IV/cpp-2330/CMakeLists.txt create mode 100644 2001-2500/2330-Valid-Palindrome-IV/cpp-2330/main.cpp diff --git a/2001-2500/2330-Valid-Palindrome-IV/cpp-2330/CMakeLists.txt b/2001-2500/2330-Valid-Palindrome-IV/cpp-2330/CMakeLists.txt new file mode 100644 index 00000000..7e1584d9 --- /dev/null +++ b/2001-2500/2330-Valid-Palindrome-IV/cpp-2330/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2330) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2330 main.cpp) diff --git a/2001-2500/2330-Valid-Palindrome-IV/cpp-2330/main.cpp b/2001-2500/2330-Valid-Palindrome-IV/cpp-2330/main.cpp new file mode 100644 index 00000000..f867a3cd --- /dev/null +++ b/2001-2500/2330-Valid-Palindrome-IV/cpp-2330/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/valid-palindrome-iv/ +/// Author : liuyubobobo +/// Time : 2022-07-10 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool makePalindrome(string s) { + + int n = s.size(), res = 0; + for(int i = 0, j = n - 1; i < j; i ++, j --) + res += s[i] != s[j]; + + return res <= 2; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 26b3d50e..4f8d3c77 100644 --- a/readme.md +++ b/readme.md @@ -2176,7 +2176,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2327 | [Number of People Aware of a Secret](https://leetcode.com/problems/number-of-people-aware-of-a-secret/) | [无] | [C++](2001-2500/2327-Number-of-People-Aware-of-a-Secret/cpp-2327/) | | | | 2328 | [Number of Increasing Paths in a Grid](https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/) | [无] | [C++](2001-2500/2328-Number-of-Increasing-Paths-in-a-Grid/cpp-2328/) | | | | 2329 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| | | | | | | +| 2330 | [Valid Palindrome IV](https://leetcode.com/problems/valid-palindrome-iv/) | [无] | [C++](2001-2500/2330-Valid-Palindrome-IV/cpp-2330/) | | | | 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree/) | [无] | [C++](2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/) | | | | | | | | | | | 2333 | [Minimum Sum of Squared Difference](https://leetcode.com/problems/minimum-sum-of-squared-difference/) | [无] | [C++](2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/) | | | From 4d5d5876fb76c25749621f9cebc14553a0bb5fab Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 11 Jul 2022 10:39:49 -0700 Subject: [PATCH 121/390] 1252 solved. --- .../cpp-1252/CMakeLists.txt | 6 ++++ .../cpp-1252/main.cpp | 36 +++++++++++++++++++ readme.md | 1 + 3 files changed, 43 insertions(+) create mode 100644 1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/CMakeLists.txt create mode 100644 1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/main.cpp diff --git a/1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/CMakeLists.txt b/1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/CMakeLists.txt new file mode 100644 index 00000000..4c5ee0e1 --- /dev/null +++ b/1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_1252) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1252 main.cpp) diff --git a/1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/main.cpp b/1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/main.cpp new file mode 100644 index 00000000..fd2fe062 --- /dev/null +++ b/1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/ +/// Author : liuyubobobo +/// Time : 2022-07-11 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(m * n * (m + n)) +/// Space Complexity: O(m * n) +class Solution { +public: + int oddCells(int R, int C, vector>& indices) { + + vector> matrix(R, vector(C, 0)); + for(const vector& e: indices){ + int r = e[0], c = e[1]; + for(int j = 0; j < C; j ++) matrix[r][j] ++; + for(int i = 0; i < R; i ++) matrix[i][c] ++; + } + + int res = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) res += matrix[i][j] % 2; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 4f8d3c77..9597755b 100644 --- a/readme.md +++ b/readme.md @@ -1201,6 +1201,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [solution](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/solution/) | [C++](1001-1500/1249-Minimum-Remove-to-Make-Valid-Parentheses/cpp-1249/) | | | | | | | | | | | 1251 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [无] | [C++](1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/) | | | | | | | | | | | 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [solution](https://leetcode.com/problems/shift-2d-grid/solution/) | [C++](1001-1500/1260-Shift-2D-Grid/cpp-1260/) | | | | 1261 | [Find Elements in a Contaminated Binary Tree](https://leetcode.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binary-tree/) | [C++](1001-1500/1261-Find-Elements-in-a-Contaminated-Binary-Tree/cpp-1261/) | | | | From 642e5ac797328ca3efb05fa70711c5e8bd1a396c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 12 Jul 2022 11:18:06 -0700 Subject: [PATCH 122/390] 2332 solved. --- .../cpp-2332/CMakeLists.txt | 6 ++ .../cpp-2332/main.cpp | 72 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/CMakeLists.txt create mode 100644 2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/main.cpp diff --git a/2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/CMakeLists.txt b/2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/CMakeLists.txt new file mode 100644 index 00000000..be95bd50 --- /dev/null +++ b/2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2332) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2332 main.cpp) diff --git a/2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/main.cpp b/2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/main.cpp new file mode 100644 index 00000000..9ec118ed --- /dev/null +++ b/2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode.com/problems/the-latest-time-to-catch-a-bus/ +/// Author : liuyubobobo +/// Time : 2022-07-12 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n + m) +/// Space Complexity: O(n) +class Solution { +public: + int latestTimeCatchTheBus(vector& buses, vector& passengers, int capacity) { + + sort(buses.begin(), buses.end()); + sort(passengers.begin(), passengers.end()); + + vector pre(passengers.size()); + for(int start = 0, i = 1; i <= passengers.size(); i ++) + if(i == passengers.size() || passengers[i] != passengers[start] + (i - start)){ + for(int j = start; j < i; j ++) pre[j] = passengers[start] - 1; + start = i; + } + + int res = 1; + int pi = 0; + for(int bi = 0; bi < buses.size(); bi ++){ + vector v; + for(; pi < passengers.size() && passengers[pi] <= buses[bi] && v.size() < capacity; pi ++) + v.push_back(pi); + + for(int i: v) + res = max(res, pre[i]); + + if(v.size() < capacity){ + if(v.empty() || passengers[v.back()] != buses[bi]) + res = max(res, buses[bi]); + } + } + return res; + } +}; + + +int main() { + + vector buses1 = {10, 20}, passengers1 = {2, 17, 18, 19}; + cout << Solution().latestTimeCatchTheBus(buses1, passengers1, 2) << '\n'; + // 16 + + vector buses2 = {20, 30, 10}, passengers2 = {19, 13, 26, 4, 25, 11, 21}; + cout << Solution().latestTimeCatchTheBus(buses2, passengers2, 2) << '\n'; + // 20 + + vector buses3 = {2}, passengers3 = {2}; + cout << Solution().latestTimeCatchTheBus(buses3, passengers3, 2) << '\n'; + // 1 + + vector buses4 = {5, 15}, passengers4 = {11, 12, 13, 14, 15}; + cout << Solution().latestTimeCatchTheBus(buses4, passengers4, 2) << '\n'; + // 10 + + vector buses5 = {7,12,15,11,14,13,5,4,2,8,9,19}, passengers5 = {2,5,10,12,8,19,9,14,4,7,15,13}; + cout << Solution().latestTimeCatchTheBus(buses5, passengers5, 2) << '\n'; + // 18 + + return 0; +} diff --git a/readme.md b/readme.md index 9597755b..f92c6e7f 100644 --- a/readme.md +++ b/readme.md @@ -2179,7 +2179,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2329 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2330 | [Valid Palindrome IV](https://leetcode.com/problems/valid-palindrome-iv/) | [无] | [C++](2001-2500/2330-Valid-Palindrome-IV/cpp-2330/) | | | | 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree/) | [无] | [C++](2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/) | | | -| | | | | | | +| 2332 | [The Latest Time to Catch a Bus](https://leetcode.com/problems/the-latest-time-to-catch-a-bus/) | [无] | [C++](2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/) | | | | 2333 | [Minimum Sum of Squared Difference](https://leetcode.com/problems/minimum-sum-of-squared-difference/) | [无] | [C++](2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/) | | | | 2334 | [Subarray With Elements Greater Than Varying Threshold](https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/) | [无] | [C++](2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/) | | | | 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [无] | [C++](2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/) | | | From 96fed65f09f8763dce2be91aa09a064bb5916f55 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 14 Jul 2022 01:36:27 -0700 Subject: [PATCH 123/390] 0745 solved. --- .../cpp-0745/CMakeLists.txt | 6 ++ .../cpp-0745/main.cpp | 86 +++++++++++++++++++ readme.md | 1 + 3 files changed, 93 insertions(+) create mode 100644 0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt create mode 100644 0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main.cpp diff --git a/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt new file mode 100644 index 00000000..c5c00dd7 --- /dev/null +++ b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0745) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0745 main.cpp) diff --git a/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main.cpp b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main.cpp new file mode 100644 index 00000000..6ed918e8 --- /dev/null +++ b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main.cpp @@ -0,0 +1,86 @@ +/// Source : https://leetcode.com/problems/prefix-and-suffix-search/ +/// Author : liuyubobobo +/// Time : 2022-07-14 + +#include +#include +#include +#include + +using namespace std; + + +/// Suffix + Prefix +/// Time Complexity: init: O(sum_of_characters) +/// query: O(|prefix| + |suffix|) +/// Space Complexity: O(sum_of_characters) +class Trie { + +private: + struct Node{ + map next; + int index = -1; + }; + Node* root; + +public: + Trie(){ + root = new Node(); + } + + /** Inserts a word into the trie. */ + void insert(const string& word, int index){ + + Node* cur = root; + bool flag = false; + for(char c: word){ + if(cur->next.find(c) == cur->next.end()){ + cur->next[c] = new Node(); + } + cur = cur->next[c]; + + if(c == '#') flag = true; + if(flag) cur->index = index; + } + } + + int get(const string& s) { + + Node* cur = root; + for(char c: s){ + if(cur->next.find(c) == cur->next.end()) + return -1; + + cur = cur->next[c]; + } + return cur->index; + } +}; + +class WordFilter { + +private: + Trie trie; + +public: + WordFilter(vector words) { + for(int i = 0 ; i < words.size() ; i ++){ + string s = words[i]; + for(int j = s.size() - 1; j >= 0; j --){ + string suf = s.substr(j); + trie.insert(suf + "#" + s, i); + } + } + + } + + int f(string prefix, string suffix) { + return trie.get(suffix + "#" + prefix); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index f92c6e7f..b9ae9b0c 100644 --- a/readme.md +++ b/readme.md @@ -750,6 +750,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 740 | [Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/) | [solution](https://leetcode.com/problems/delete-and-earn/solution/) | [C++](0501-1000/0740-Delete-and-Earn/cpp-0740/) | | | | 741 | [Cherry Pickup](https://leetcode.com/problems/cherry-pickup/description/) | [solution](https://leetcode.com/problems/cherry-pickup/solution/)
[缺:自底向上的动态规划] | [C++](0501-1000/0741-Cherry-Pickup/cpp-0741/) | | | | | | | | | | +| 745 | [Prefix and Suffix Search](https://leetcode.com/problems/prefix-and-suffix-search/) | [solution](https://leetcode.com/problems/prefix-and-suffix-search/solution/) | [C++](0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/) | | | | 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [solution](https://leetcode.com/problems/min-cost-climbing-stairs/solution/) | [C++](0501-1000/0746-Min-Cost-Climbing-Stairs/cpp-0746/) | | | | 747 | [Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/) | [solution](https://leetcode.com/problems/largest-number-at-least-twice-of-others/solution/) | [C++](0501-1000/0747-Largest-Number-At-Least-Twice-of-Others/cpp-0747/) | | | | | | | | | | From 0e5154c92ee557ccf4618467e1806bf744d727d5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 14 Jul 2022 11:43:40 -0700 Subject: [PATCH 124/390] 0745 new algo added. --- .../cpp-0745/CMakeLists.txt | 2 +- .../cpp-0745/main2.cpp | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main2.cpp diff --git a/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt index c5c00dd7..c1d818ce 100644 --- a/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt +++ b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0745) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_0745 main.cpp) +add_executable(cpp_0745 main2.cpp) diff --git a/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main2.cpp b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main2.cpp new file mode 100644 index 00000000..5c0089ad --- /dev/null +++ b/0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/main2.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/prefix-and-suffix-search/ +/// Author : liuyubobobo +/// Time : 2022-07-14 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Hash + Map +/// Time Complexity: init: O(|words| * len^2) +/// query: O(|prefix| + |suffix|) +/// Space Complexity: O(|words| * len^2) +class WordFilter { + +private: + map, int> table; + +public: + WordFilter(vector words) { + + for(int i = 0 ; i < words.size() ; i ++) + insert(words[i], i); + + } + + int f(string prefix, string suffix) { + + long long pre_hash = 0; + for(int i = 0; i < prefix.size(); i ++) + pre_hash = pre_hash * 27 + (prefix[i] - 'a' + 1); + + long long suf_hash = 0; + for(int i = suffix.size() - 1; i >= 0; i --) + suf_hash = suf_hash * 27 + (suffix[i] - 'a' + 1); + + auto iter = table.find({pre_hash, suf_hash}); + return iter == table.end() ? -1 : iter->second; + } + +private: + void insert(const string& s, int index){ + + long long pre_hash = 0; + for(int i = 0; i < s.size(); i ++){ + pre_hash = pre_hash * 27 + (s[i] - 'a' + 1); + + long long suf_hash = 0; + for(int i = s.size() - 1; i >= 0; i --){ + suf_hash = suf_hash * 27 + (s[i] - 'a' + 1); + table[{pre_hash, suf_hash}] = index; + } + } + } +}; + + +int main() { + + return 0; +} From 0b4fb495f7f422ad134e895e64d6a9485b785404 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 14 Jul 2022 14:11:54 -0700 Subject: [PATCH 125/390] 0558 solved. --- .../cpp-0558/CMakeLists.txt | 6 ++ .../cpp-0558/main.cpp | 94 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/CMakeLists.txt create mode 100644 0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/main.cpp diff --git a/0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/CMakeLists.txt b/0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/CMakeLists.txt new file mode 100644 index 00000000..802b7bb9 --- /dev/null +++ b/0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0558) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0558 main.cpp) diff --git a/0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/main.cpp b/0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/main.cpp new file mode 100644 index 00000000..4c5b4b6f --- /dev/null +++ b/0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/main.cpp @@ -0,0 +1,94 @@ +/// Source : https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/ +/// Author : liuyubobobo +/// Time : 2022-07-14 + +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(h) + +/// Definition for a QuadTree node. +class Node { +public: + bool val; + bool isLeaf; + Node* topLeft; + Node* topRight; + Node* bottomLeft; + Node* bottomRight; + + Node() { + val = false; + isLeaf = false; + topLeft = NULL; + topRight = NULL; + bottomLeft = NULL; + bottomRight = NULL; + } + + Node(bool _val, bool _isLeaf) { + val = _val; + isLeaf = _isLeaf; + topLeft = NULL; + topRight = NULL; + bottomLeft = NULL; + bottomRight = NULL; + } + + Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) { + val = _val; + isLeaf = _isLeaf; + topLeft = _topLeft; + topRight = _topRight; + bottomLeft = _bottomLeft; + bottomRight = _bottomRight; + } +}; + + +class Solution { +public: + Node* intersect(Node* quadTree1, Node* quadTree2) { + return dfs(quadTree1, quadTree2); + } + +private: + Node* dfs(Node* node1, Node* node2){ + + if(node1->isLeaf && node2->isLeaf){ + return new Node(node1->val | node2->val, true); + } + + Node* ret = new Node(0, false); + ret->topLeft = dfs(node1->topLeft ? node1->topLeft : new Node(node1->val, true), + node2->topLeft ? node2->topLeft : new Node(node2->val, true)); + ret->topRight = dfs(node1->topRight ? node1->topRight : new Node(node1->val, true), + node2->topRight ? node2->topRight : new Node(node2->val, true)); + ret->bottomLeft = dfs(node1->bottomLeft ? node1->bottomLeft : new Node(node1->val, true), + node2->bottomLeft ? node2->bottomLeft : new Node(node2->val, true)); + ret->bottomRight = dfs(node1->bottomRight ? node1->bottomRight : new Node(node1->val, true), + node2->bottomRight ? node2->bottomRight : new Node(node2->val, true)); + + if(ret->topLeft->isLeaf && ret->topRight->isLeaf && ret->bottomLeft->isLeaf && ret->bottomRight->isLeaf && + same(ret->topLeft->val, ret->topRight->val, ret->bottomLeft->val, ret->bottomRight->val)){ + ret->val = ret->topLeft->val; + ret->isLeaf = true; + ret->topLeft = ret->topRight = ret->bottomLeft = ret->bottomRight = nullptr; + } + return ret; + } + + bool same(bool a, bool b, bool c, bool d){ + return a == b && a == c && a == d; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index b9ae9b0c..77ffd0d6 100644 --- a/readme.md +++ b/readme.md @@ -585,7 +585,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 556 | [Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii/) | [solution](https://leetcode.com/problems/next-greater-element-iii/solution/)
[缺:底层实现] | [C++](0501-1000/0556-Next-Greater-Element-III/cpp-0556/) | | | | 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/description/) | [solution](https://leetcode.com/problems/reverse-words-in-a-string-iii/solution/) | [C++](0501-1000/0557-Reverse-Words-in-a-String-III/cpp-0557/) | | | -| | | | | | | +| 558 | [Logical OR of Two Binary Grids Represented as Quad-Trees](https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/) | [无] | [C++](0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/) | | | | 559 | [Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/) | [solution](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/solution/) | [C++](0501-1000/0559-Maximum-Depth-of-N-ary-Tree/cpp-0559/) | | | | 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [solution](https://leetcode.com/problems/subarray-sum-equals-k/solution/) | [C++](0501-1000/0560-Subarray-Sum-Equals-K/cpp-0560/) | | | | 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/description/) | [solution](https://leetcode.com/problems/array-partition-i/solution/) | [C++](0501-1000/0561-Array-Partition-I/cpp-0561/) | | | From 17d7c65a18ac9aac02740759531905681e56db4e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 14 Jul 2022 14:25:26 -0700 Subject: [PATCH 126/390] 2340 solved. --- .../cpp-2340/CMakeLists.txt | 6 +++ .../cpp-2340/main.cpp | 41 +++++++++++++++++++ readme.md | 2 + 3 files changed, 49 insertions(+) create mode 100644 2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/CMakeLists.txt create mode 100644 2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/main.cpp diff --git a/2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/CMakeLists.txt b/2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/CMakeLists.txt new file mode 100644 index 00000000..c26ae715 --- /dev/null +++ b/2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2340) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2340 main.cpp) diff --git a/2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/main.cpp b/2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/main.cpp new file mode 100644 index 00000000..883eb07b --- /dev/null +++ b/2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/ +/// Author : liuyubobobo +/// Time : 2022-07-14 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumSwaps(vector& nums) { + + int n = nums.size(); + + int min_v = nums[0], min_v_index = 0; + for(int i = 1; i < n; i ++) + if(nums[i] < min_v) min_v = nums[i], min_v_index = i; + + int max_v = nums[0], max_v_index = 0; + for(int i = 1; i < n; i ++) + if(nums[i] >= max_v) max_v = nums[i], max_v_index = i; + + if(min_v_index == 0 && max_v_index == n - 1) return 0; + if(min_v_index < max_v_index) return min_v_index + n - 1 - max_v_index; + return min_v_index + n - 2 - max_v_index; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 77ffd0d6..8217a15c 100644 --- a/readme.md +++ b/readme.md @@ -2187,6 +2187,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2336 | [Smallest Number in Infinite Set](https://leetcode.com/problems/smallest-number-in-infinite-set/) | [无] | [C++](2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/) | | | | 2337 | [Move Pieces to Obtain a String](https://leetcode.com/problems/move-pieces-to-obtain-a-string/) | [无] | [C++](2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/) | | | | 2338 | [Count the Number of Ideal Arrays](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | [无] | [C++](2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/) | | | +| 2339 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2340 | [Minimum Adjacent Swaps to Make a Valid Array](https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/) | [无] | [C++](2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/) | | | | | | | | | | ## 力扣中文站比赛 From f8165cabe933b240975123a37ac42c24ba4039d5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 17 Jul 2022 09:49:40 -0700 Subject: [PATCH 127/390] 2341-2344 added. --- .../cpp-2341/CMakeLists.txt | 6 ++ .../cpp-2341/main.cpp | 35 +++++++++++ .../cpp-2342/CMakeLists.txt | 6 ++ .../cpp-2342/main.cpp | 52 +++++++++++++++++ .../cpp-2342/main2.cpp | 58 +++++++++++++++++++ .../cpp-2343/CMakeLists.txt | 6 ++ .../cpp-2343/main.cpp | 42 ++++++++++++++ .../cpp-2344/CMakeLists.txt | 6 ++ .../cpp-2344/main.cpp | 40 +++++++++++++ readme.md | 4 ++ 10 files changed, 255 insertions(+) create mode 100644 2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/CMakeLists.txt create mode 100644 2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/main.cpp create mode 100644 2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/CMakeLists.txt create mode 100644 2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main.cpp create mode 100644 2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main2.cpp create mode 100644 2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/CMakeLists.txt create mode 100644 2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/main.cpp create mode 100644 2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/CMakeLists.txt create mode 100644 2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/main.cpp diff --git a/2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/CMakeLists.txt b/2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/main.cpp b/2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/main.cpp new file mode 100644 index 00000000..7b03871e --- /dev/null +++ b/2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-pairs-in-array/ +/// Author : liuyubobobo +/// Time : 2022-07-16 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector numberOfPairs(vector& nums) { + + map f; + for(int e: nums) f[e] ++; + + vector res(2, 0); + for(const pair& p: f){ + res[0] += p.second / 2; + res[1] += p.second % 2; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/CMakeLists.txt b/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/CMakeLists.txt new file mode 100644 index 00000000..6c7077c9 --- /dev/null +++ b/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main2.cpp) diff --git a/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main.cpp b/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main.cpp new file mode 100644 index 00000000..6f4b3b85 --- /dev/null +++ b/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/ +/// Author : liuyubobobo +/// Time : 2022-07-16 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maximumSum(vector& nums) { + + map> f; + for(int e: nums) f[dsum(e)].push_back(e); + + for(const pair>& p: f){ + int sum = p.first; + sort(f[sum].begin(), f[sum].end(), greater()); + } + + int res = -1; + for(const pair>& p: f){ + if(p.second.size() >= 2){ + res = max(res, p.second[0] + p.second[1]); + } + } + return res; + } + +private: + int dsum(int x){ + int res = 0; + while(x){ + res += x % 10; + x /= 10; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main2.cpp b/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main2.cpp new file mode 100644 index 00000000..1a985640 --- /dev/null +++ b/2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/main2.cpp @@ -0,0 +1,58 @@ +/// Source : https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/ +/// Author : liuyubobobo +/// Time : 2022-07-16 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Map, no sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maximumSum(vector& nums) { + + map> f; + for(int e: nums){ + int sum = dsum(e); + auto iter = f.find(sum); + if(iter == f.end()) f[sum] = {e, -1}; + else if(e > iter->second.first){ + iter->second.second = iter->second.first; + iter->second.first = e; + } + else{ + iter->second.second = max(e, iter->second.second); + } + } + + int res = -1; + for(const pair>& p: f){ + if(p.second.second != -1){ + res = max(res, p.second.first + p.second.second); + } + } + return res; + } + +private: + int dsum(int x){ + int res = 0; + while(x){ + res += x % 10; + x /= 10; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/CMakeLists.txt b/2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/main.cpp b/2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/main.cpp new file mode 100644 index 00000000..040ea414 --- /dev/null +++ b/2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/query-kth-smallest-trimmed-number/ +/// Author : liuyubobobo +/// Time : 2022-07-16 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(q * nlogn * len) +/// Space Complexity: O(n) +class Solution { +public: + vector smallestTrimmedNumbers(vector& nums, vector>& queries) { + + int n = nums.size(), len = nums[0].size(); + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i ++){ + res[i] = solve(n, len, nums, queries[i][0] - 1, queries[i][1]); + } + return res; + } + +private: + int solve(int n, int len, const vector& nums, int k, int x){ + + vector> data(n); + for(int i = 0; i < n; i ++) + data[i] = {nums[i].substr(len - x), i}; + sort(data.begin(), data.end()); + return data[k].second; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/CMakeLists.txt b/2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/main.cpp b/2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/main.cpp new file mode 100644 index 00000000..076121d4 --- /dev/null +++ b/2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/ +/// Author : liuyubobobo +/// Time : 2022-07-16 + +#include +#include +#include + +using namespace std; + + +/// Sorting and Greedy +/// Time Complexity: O(nlog(max(numsDivide)) + nlogn) +/// Space Complexity: O(log(max(numsDivide))) +class Solution { +public: + int minOperations(vector& nums, vector& numsDivide) { + + int t = numsDivide[0]; + for(int i = 1; i < numsDivide.size(); i ++) + t = gcd(min(t, numsDivide[i]), max(t, numsDivide[i])); + + sort(nums.begin(), nums.end()); + for(int i = 0; i < nums.size(); i ++) + if(t % nums[i] == 0) return i; + return -1; + } + +private: + int gcd(int a, int b){ + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 8217a15c..56fef3ce 100644 --- a/readme.md +++ b/readme.md @@ -2189,6 +2189,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2338 | [Count the Number of Ideal Arrays](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | [无] | [C++](2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/) | | | | 2339 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2340 | [Minimum Adjacent Swaps to Make a Valid Array](https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/) | [无] | [C++](2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/) | | | +| 2341 | [Maximum Number of Pairs in Array](https://leetcode.com/problems/maximum-number-of-pairs-in-array/) | [无] | [C++](2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/) | | | +| 2342 | [Max Sum of a Pair With Equal Sum of Digits](https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/) | [无] | [C++](2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/) | | | +| 2343 | [Query Kth Smallest Trimmed Number](https://leetcode.com/problems/query-kth-smallest-trimmed-number/) | [无] | [C++](2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/) | | | +| 2344 | [Minimum Deletions to Make Array Divisible](https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/) | [无] | [C++](2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/) | | | | | | | | | | ## 力扣中文站比赛 From f2a35a041ce8acedd5992baa40d64e191157e9f5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 19 Jul 2022 12:45:54 -0700 Subject: [PATCH 128/390] 0749 solved. --- .../cpp-0749/CMakeLists.txt | 6 + .../0749-Contain-Virus/cpp-0749/main.cpp | 141 ++++++++++++++++++ readme.md | 2 + 3 files changed, 149 insertions(+) create mode 100644 0501-1000/0749-Contain-Virus/cpp-0749/CMakeLists.txt create mode 100644 0501-1000/0749-Contain-Virus/cpp-0749/main.cpp diff --git a/0501-1000/0749-Contain-Virus/cpp-0749/CMakeLists.txt b/0501-1000/0749-Contain-Virus/cpp-0749/CMakeLists.txt new file mode 100644 index 00000000..a3245e37 --- /dev/null +++ b/0501-1000/0749-Contain-Virus/cpp-0749/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0749) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0749 main.cpp) diff --git a/0501-1000/0749-Contain-Virus/cpp-0749/main.cpp b/0501-1000/0749-Contain-Virus/cpp-0749/main.cpp new file mode 100644 index 00000000..b5e2c228 --- /dev/null +++ b/0501-1000/0749-Contain-Virus/cpp-0749/main.cpp @@ -0,0 +1,141 @@ +/// Source : https://leetcode.com/problems/contain-virus/ +/// Author : liuyubobobo +/// Time : 2022-07-19 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O((R * C) ^ 2) +/// Space Compelxity: O(R * C) +class Solution { + +private: + const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + int R, C; + +public: + int containVirus(vector>& isInfected) { + + R = isInfected.size(), C = isInfected[0].size(); + + // 0 - not infected, 1 - virus + int res = 0; + while(true){ + vector> visited(R, vector(C, -1)); + int cc_num = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(isInfected[i][j] == 1 && visited[i][j] == -1){ + dfs(isInfected, i, j, cc_num ++, visited); + } + + if(cc_num == 0) break; + + vector walls(cc_num, 0); + vector> threaten(cc_num); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + int cc = visited[i][j]; + if(cc == -1) continue; + + for(int d = 0; d < 4; d ++){ + int nx = i + dirs[d][0], ny = j + dirs[d][1]; + if(in_area(nx, ny) && isInfected[nx][ny] == 0) + threaten[cc].insert(nx * C + ny), walls[cc] ++; + } + } + + int max_threatens = 0, max_cc = -1; + for(int cc = 0; cc < cc_num; cc ++) + if(threaten[cc].size() > max_threatens) + max_threatens = threaten[cc].size(), max_cc = cc; + + if(max_threatens == 0) break; + + res += walls[max_cc]; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(visited[i][j] == max_cc) isInfected[i][j] = 2; + + vector> next(R, vector(C, 0)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(isInfected[i][j] != 1) continue; + for(int d = 0; d < 4; d ++){ + int nx = i + dirs[d][0], ny = j + dirs[d][1]; + if(in_area(nx, ny) && !isInfected[nx][ny]) next[nx][ny] = 1; + } + } + + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(isInfected[i][j] == 0 && next[i][j]) + isInfected[i][j] = 1; + } + return res; + } + +private: + void dfs(const vector>& isInfected, int x, int y, int cc, + vector>& visited){ + visited[x][y] = cc; + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny) && isInfected[nx][ny] == 1 && visited[nx][ny] == -1) + dfs(isInfected, nx, ny, cc, visited); + } + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> isInfected1 = { + {0,1,0,0,0,0,0,1}, + {0,1,0,0,0,0,0,1}, + {0,0,0,0,0,0,0,1}, + {0,0,0,0,0,0,0,0} + }; + cout << Solution().containVirus(isInfected1) << '\n'; + // 10 + + vector> isInfected2 = { + {1,1,1,0,0,0,0,0,0}, + {1,0,1,0,1,1,1,1,1}, + {1,1,1,0,0,0,0,0,0} + }; + cout << Solution().containVirus(isInfected2) << '\n'; + // 13 + + vector> isInfected3 = { + {1} + }; + cout << Solution().containVirus(isInfected3) << '\n'; + // 0 + + vector> isInfected4 = { + {0,1,0,1,1,1,1,1,1,0}, + {0,0,0,1,0,0,0,0,0,0}, + {0,0,1,1,1,0,0,0,1,0}, + {0,0,0,1,1,0,0,1,1,0}, + {0,1,0,0,1,0,1,1,0,1}, + {0,0,0,1,0,1,0,1,1,1}, + {0,1,0,0,1,0,0,1,1,0}, + {0,1,0,1,0,0,0,1,1,0}, + {0,1,1,0,0,1,1,0,0,1}, + {1,0,1,1,0,1,0,1,0,1} + }; + cout << Solution().containVirus(isInfected4) << '\n'; + // 38 + + return 0; +} diff --git a/readme.md b/readme.md index 56fef3ce..81763ba6 100644 --- a/readme.md +++ b/readme.md @@ -754,6 +754,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [solution](https://leetcode.com/problems/min-cost-climbing-stairs/solution/) | [C++](0501-1000/0746-Min-Cost-Climbing-Stairs/cpp-0746/) | | | | 747 | [Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/) | [solution](https://leetcode.com/problems/largest-number-at-least-twice-of-others/solution/) | [C++](0501-1000/0747-Largest-Number-At-Least-Twice-of-Others/cpp-0747/) | | | | | | | | | | +| 749 | [Contain Virus](https://leetcode.com/problems/contain-virus/) | [无] | [C++](0501-1000/0749-Contain-Virus/cpp-0749/)| | | +| | | | | | | | 752 | [Open the Lock](https://leetcode.com/problems/open-the-lock/description/) | [solution](https://leetcode.com/problems/open-the-lock/solution/) | [C++](0501-1000/0752-Open-the-Lock/cpp-0752/) | | | | 753 | [Cracking the Safe](https://leetcode.com/problems/cracking-the-safe/) | [无] | [C++](0501-1000/0753-Cracking-the-Safe/cpp-0753/) | | | | 754 | [Reach a Number](https://leetcode.com/problems/reach-a-number/) | [Solution](https://leetcode.com/problems/reach-a-number/solution/) | [C++](0501-1000/0754-Reach-a-Number/cpp-0754/) | | | From 1dbb556cd239ca2ac47551ad8546b98be239c149 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 19 Jul 2022 17:27:48 -0700 Subject: [PATCH 129/390] 2022 zj future solved. --- Others/2022-zj-future/A/CMakeLists.txt | 6 ++ Others/2022-zj-future/A/main.cpp | 33 ++++++++++ Others/2022-zj-future/B/CMakeLists.txt | 6 ++ Others/2022-zj-future/B/main.cpp | 36 +++++++++++ Others/2022-zj-future/C/CMakeLists.txt | 6 ++ Others/2022-zj-future/C/main.cpp | 64 ++++++++++++++++++++ Others/2022-zj-future/D/CMakeLists.txt | 6 ++ Others/2022-zj-future/D/main.cpp | 84 ++++++++++++++++++++++++++ readme.md | 5 ++ 9 files changed, 246 insertions(+) create mode 100644 Others/2022-zj-future/A/CMakeLists.txt create mode 100644 Others/2022-zj-future/A/main.cpp create mode 100644 Others/2022-zj-future/B/CMakeLists.txt create mode 100644 Others/2022-zj-future/B/main.cpp create mode 100644 Others/2022-zj-future/C/CMakeLists.txt create mode 100644 Others/2022-zj-future/C/main.cpp create mode 100644 Others/2022-zj-future/D/CMakeLists.txt create mode 100644 Others/2022-zj-future/D/main.cpp diff --git a/Others/2022-zj-future/A/CMakeLists.txt b/Others/2022-zj-future/A/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/Others/2022-zj-future/A/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/Others/2022-zj-future/A/main.cpp b/Others/2022-zj-future/A/main.cpp new file mode 100644 index 00000000..531029b2 --- /dev/null +++ b/Others/2022-zj-future/A/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.cn/contest/zj-future2022/problems/WYKGLO/ +/// Author : liuyubobobo +/// Time : 2022-07-08 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + bool canReceiveAllSignals(vector>& intervals) { + + sort(intervals.begin(), intervals.end()); + for(int i = 1; i < intervals.size(); i ++){ + int s1 = intervals[i - 1][0], e1 = intervals[i - 1][1]; + int s2 = intervals[i][0], e2 = intervals[i][1]; + if(s2 < e1) return false; + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-zj-future/B/CMakeLists.txt b/Others/2022-zj-future/B/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/Others/2022-zj-future/B/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/Others/2022-zj-future/B/main.cpp b/Others/2022-zj-future/B/main.cpp new file mode 100644 index 00000000..8a8842ee --- /dev/null +++ b/Others/2022-zj-future/B/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.cn/contest/zj-future2022/problems/GVbKaI/ +/// Author : liuyubobobo +/// Time : 2022-07-19 + +#include +#include +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minSwaps(vector& chess) { + + int n = chess.size(); + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + chess[i]; + + int res = INT_MAX, total = accumulate(chess.begin(), chess.end(), 0); + for(int start = 0; start + total <= n; start ++) + res = min(res, total - (presum[start + total] - presum[start])); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-zj-future/C/CMakeLists.txt b/Others/2022-zj-future/C/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/Others/2022-zj-future/C/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/Others/2022-zj-future/C/main.cpp b/Others/2022-zj-future/C/main.cpp new file mode 100644 index 00000000..05ef2ba2 --- /dev/null +++ b/Others/2022-zj-future/C/main.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.cn/contest/zj-future2022/problems/kflZMc/ +/// Author : liuyubobobo +/// Time : 2022-07-19 + +#include +#include + +using namespace std; + + +/// Sliding Window in 2D +/// Time Complexity: O(R * C) +/// Space Complexity: O(R + C) +class Solution { +public: + int buildTransferStation(vector>& area) { + + int R = area.size(), C = area[0].size(); + + vector rows(R, 0), cols(C, 0); + int total = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(area[i][j]) rows[i] ++, cols[j] ++, total ++; + + vector pre_rows(R, rows[0]), pre_cols(C, cols[0]); + for(int i = 1; i < R; i ++) pre_rows[i] = pre_rows[i - 1] + rows[i]; + for(int j = 1; j < C; j ++) pre_cols[j] = pre_cols[j - 1] + cols[j]; + + int prow = 0; + for(int i = 1; i < R; i ++) prow += rows[i] * i; + vector row_res(R, prow); + for(int i = 1; i < R; i ++){ + int crow = prow + pre_rows[i - 1] - (total - pre_rows[i - 1]); + row_res[i] = crow; + prow = crow; + } + + int pcol = 0; + for(int j = 1; j < C; j ++) pcol += cols[j] * j; + vector col_res(C, pcol); + for(int j = 1; j < C; j ++){ + int ccol = pcol + pre_cols[j - 1] - (total - pre_cols[j - 1]); + col_res[j] = ccol; + pcol = ccol; + } + + return *min_element(row_res.begin(), row_res.end()) + *min_element(col_res.begin(), col_res.end()); + } +}; + + +int main() { + + vector> area1 = { + {0, 1, 0, 0, 0}, + {0, 0, 0, 0, 1}, + {0, 0, 1, 0, 0} + }; + cout << Solution().buildTransferStation(area1) << '\n'; + // 5 + + return 0; +} diff --git a/Others/2022-zj-future/D/CMakeLists.txt b/Others/2022-zj-future/D/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/Others/2022-zj-future/D/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/Others/2022-zj-future/D/main.cpp b/Others/2022-zj-future/D/main.cpp new file mode 100644 index 00000000..81be487e --- /dev/null +++ b/Others/2022-zj-future/D/main.cpp @@ -0,0 +1,84 @@ +/// Source : https://leetcode.cn/contest/zj-future2022/problems/NBCXIp/ +/// Author : liuyubobobo +/// Time : 2022-07-19 + +#include +#include +#include + +using namespace std; + + +/// Backtrack +/// Time Complexity: O(12!) +/// Space Complexity: O(12) +class Solution { +public: + int minTransfers(vector>& distributions) { + + int max_id = -1; + for(const vector& v: distributions) + max_id = max(max_id, max(v[0], v[1])); + + vector cnt(max_id + 1, 0); + for(const vector& v: distributions){ + int from_id = v[0], to_id = v[1], e = v[2]; + cnt[from_id] -= e; + cnt[to_id] += e; + } + + vector pos, neg; + for(int i = 0; i < cnt.size(); i ++) { + if (cnt[i] > 0) pos.push_back(cnt[i]); + else if (cnt[i] < 0) neg.push_back(-cnt[i]); + } + + sort(pos.begin(), pos.end(), greater()); + sort(neg.begin(), neg.end(), greater()); + + return dfs(pos, 0, neg); + } + +private: + int dfs(vector& pos, int index, vector& neg){ + + if(index == pos.size()) return 0; + + int res = INT_MAX; + for(int& e: neg){ + if(e){ + int t = min(pos[index], e); + pos[index] -= t; + e -= t; + res = min(res, 1 + dfs(pos, index + (pos[index] == 0), neg)); + pos[index] += t; + e += t; + } + } + return res; + } +}; + + +int main() { + + vector> distrubution1 = { + {0,1,5},{2,3,1},{2,0,1},{4,0,2} + }; + cout << Solution().minTransfers(distrubution1) << '\n'; + // 4 + + vector> distrubution2 = { + {1,5,8},{8,9,8},{2,3,9},{4,3,1} + }; + cout << Solution().minTransfers(distrubution2) << '\n'; + // 4 + + vector> distrubution3 = { + {0,2,4},{1,2,4},{3,4,5} + }; + cout << Solution().minTransfers(distrubution3) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index 81763ba6..a432fbb3 100644 --- a/readme.md +++ b/readme.md @@ -2255,6 +2255,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | +| 22 zj-future01 | [信号接收](https://leetcode.cn/contest/zj-future2022/problems/WYKGLO/) | [无] | [C++](Others/2022-zj-future/A/) | | | +| 22 zj-future02 | [黑白棋游戏](https://leetcode.cn/contest/zj-future2022/problems/GVbKaI/) | [无] | [C++](Others/2022-zj-future/B/) | | | +| 22 zj-future03 | [快递中转站选址](https://leetcode.cn/contest/zj-future2022/problems/kflZMc/) | [无] | [C++](Others/2022-zj-future/C/) | | | +| 22 zj-future04 | [门店商品调配](https://leetcode.cn/contest/zj-future2022/problems/NBCXIp/) | [无] | [C++](Others/2022-zj-future/D/) | | | +| | | | | | | | 22 顺丰01 | [顺丰鄂州枢纽运转中心环线检测](https://leetcode.cn/contest/sf-tech/problems/EUpcmh/) | [无] | [C++](Others/2022-sf-tech/A/) | | | | 22 顺丰02 | [小哥派件装载问题](https://leetcode.cn/contest/sf-tech/problems/cINqyA/) | [无] | [C++](Others/2022-sf-tech/B/) | | | | 22 顺丰03 | [收件节节高](https://leetcode.cn/contest/sf-tech/problems/8oimK4/) | [无] | [C++](Others/2022-sf-tech/C/) | | | From ed1a1db33f2a7ab9e692a02fadbe4a446c02ed39 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 22 Jul 2022 12:09:23 -0700 Subject: [PATCH 130/390] 444 solved. --- .../cpp-0444/CMakeLists.txt | 6 ++ .../cpp-0444/main.cpp | 57 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0444-Sequence-Reconstruction/cpp-0444/CMakeLists.txt create mode 100644 0001-0500/0444-Sequence-Reconstruction/cpp-0444/main.cpp diff --git a/0001-0500/0444-Sequence-Reconstruction/cpp-0444/CMakeLists.txt b/0001-0500/0444-Sequence-Reconstruction/cpp-0444/CMakeLists.txt new file mode 100644 index 00000000..2b587b02 --- /dev/null +++ b/0001-0500/0444-Sequence-Reconstruction/cpp-0444/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0444) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0444 main.cpp) diff --git a/0001-0500/0444-Sequence-Reconstruction/cpp-0444/main.cpp b/0001-0500/0444-Sequence-Reconstruction/cpp-0444/main.cpp new file mode 100644 index 00000000..52aefa50 --- /dev/null +++ b/0001-0500/0444-Sequence-Reconstruction/cpp-0444/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/sequence-reconstruction/ +/// Author : liuyubobobo +/// Time : 2022-07-22 + +#include +#include +#include + +using namespace std; + + +/// Topological Sort +/// Time Complexity: O(n + m) +/// Space Complexity: O(n + m) +class Solution { +public: + bool sequenceReconstruction(vector& nums, vector>& sequences) { + + int n = nums.size(); + + vector> g(n); + for(const vector& seq: sequences){ + for(int i = 1; i < seq.size(); i ++){ + int a = seq[i - 1] - 1, b = seq[i] - 1; + g[a].push_back(b); + } + } + + vector indegrees(n, 0); + for(int u = 0; u < n; u ++) + for(int v: g[u]) indegrees[v] ++; + + queue q; + for(int u = 0; u < n; u ++) + if(indegrees[u] == 0) q.push(u); + + vector res; + while(!q.empty()){ + if(q.size() > 1) return false; + + int u = q.front(); q.pop(); + res.push_back(u + 1); + for(int v: g[u]){ + indegrees[v] --; + if(indegrees[v] == 0) q.push(v); + } + } + + return res == nums; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a432fbb3..1bde10bd 100644 --- a/readme.md +++ b/readme.md @@ -476,7 +476,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 441 | [Arranging Coins](https://leetcode.com/problems/arranging-coins/) | [solution](https://leetcode.com/problems/arranging-coins/solution/) | [C++](0001-0500/0441-Arranging-Coins/cpp-0441/) | | | | 442 | [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [solution](https://leetcode.com/problems/find-all-duplicates-in-an-array/solution/) | [C++](0001-0500/0442-Find-All-Duplicates-in-an-Array/cpp-0442/) | | | | 443 | [String Compression](https://leetcode.com/problems/string-compression/description/) | | [C++](0001-0500/0443-String-Compression/cpp-0443/) | | | -| | | | | | | +| 444 | [Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/) | [无] | [C++](0001-0500/0444-Sequence-Reconstruction/cpp-0444/) | | | | 445 | [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [无] | [C++](0001-0500/0445-Add-Two-Numbers-II/cpp-0445/) | | | | 446 | [Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/) | [solution](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/solution/) | [C++](0001-0500/0446-Arithmetic-Slices-II-Subsequence/cpp-0446/) | | | | 447 | [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/description/) | [无] | [C++](0001-0500/0447-Number-of-Boomerangs/cpp-0447/) | [Java](0001-0500/0447-Number-of-Boomerangs/java-0447/src/) | | From f78380b423de7668408c4ed79ef8c9dcdf761aa2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 22 Jul 2022 12:46:23 -0700 Subject: [PATCH 131/390] 2345 solved. --- .../cpp-2345/CMakeLists.txt | 6 ++ .../cpp-2345/main.cpp | 59 +++++++++++++++++++ readme.md | 1 + 3 files changed, 66 insertions(+) create mode 100644 2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/CMakeLists.txt create mode 100644 2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/main.cpp diff --git a/2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/CMakeLists.txt b/2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/CMakeLists.txt new file mode 100644 index 00000000..9ec33917 --- /dev/null +++ b/2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2345) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2345 main.cpp) diff --git a/2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/main.cpp b/2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/main.cpp new file mode 100644 index 00000000..72496da9 --- /dev/null +++ b/2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/finding-the-number-of-visible-mountains/ +/// Author : liuyubobobo +/// Time : 2022-07-22 + +#include +#include +#include + +using namespace std; + + +/// Points Transformation +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int visibleMountains(vector>& peaks) { + + int n = peaks.size(); + + vector> points(n); + for(int i = 0; i < n; i ++){ + int x = peaks[i][0], y = peaks[i][1]; + points[i] = {x + y, y - x}; + } + + sort(points.begin(), points.end(), greater>()); + + int res = 0, ylimit = INT_MIN; + for(int i = 0; i < n; i ++){ + int x = points[i].first, y = points[i].second; + + if(i + 1 < n && points[i + 1] == points[i]){ + ylimit = max(ylimit, y); + continue; + } + + if(i && x == points[i - 1].first) continue; + + if(y > ylimit) res ++; + ylimit = max(ylimit, y); + } + return res; + } +}; + + +int main() { + + vector> peaks1 = {{2, 2}, {6, 3}, {5, 4}}; + cout << Solution().visibleMountains(peaks1) << '\n'; + // 2 + + vector> peaks2 = {{1, 3}, {1, 3}}; + cout << Solution().visibleMountains(peaks2) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index 1bde10bd..8dfc9234 100644 --- a/readme.md +++ b/readme.md @@ -2195,6 +2195,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2342 | [Max Sum of a Pair With Equal Sum of Digits](https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/) | [无] | [C++](2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/) | | | | 2343 | [Query Kth Smallest Trimmed Number](https://leetcode.com/problems/query-kth-smallest-trimmed-number/) | [无] | [C++](2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/) | | | | 2344 | [Minimum Deletions to Make Array Divisible](https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/) | [无] | [C++](2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/) | | | +| 2345 | [Finding the Number of Visible Mountains](https://leetcode.com/problems/finding-the-number-of-visible-mountains/) | [无] | [C++](2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/) | | | | | | | | | | ## 力扣中文站比赛 From 056f8f6d89256933ae2ce029f00a0d394e908110 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Jul 2022 19:27:27 -0700 Subject: [PATCH 132/390] 2347 solved. --- .../cpp-2347/CMakeLists.txt | 6 +++ .../2347-Best-Poker-Hand/cpp-2347/main.cpp | 50 +++++++++++++++++++ readme.md | 2 + 3 files changed, 58 insertions(+) create mode 100644 2001-2500/2347-Best-Poker-Hand/cpp-2347/CMakeLists.txt create mode 100644 2001-2500/2347-Best-Poker-Hand/cpp-2347/main.cpp diff --git a/2001-2500/2347-Best-Poker-Hand/cpp-2347/CMakeLists.txt b/2001-2500/2347-Best-Poker-Hand/cpp-2347/CMakeLists.txt new file mode 100644 index 00000000..3a6d9c1a --- /dev/null +++ b/2001-2500/2347-Best-Poker-Hand/cpp-2347/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2347) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2347 main.cpp) diff --git a/2001-2500/2347-Best-Poker-Hand/cpp-2347/main.cpp b/2001-2500/2347-Best-Poker-Hand/cpp-2347/main.cpp new file mode 100644 index 00000000..d8ec8156 --- /dev/null +++ b/2001-2500/2347-Best-Poker-Hand/cpp-2347/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/best-poker-hand/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + string bestHand(vector& ranks, vector& suits) { + + if(is_flush(suits)) return "Flush"; + + sort(ranks.begin(), ranks.end()); + int max_same = get_max_same(ranks); + if(max_same >= 3) return "Three of a Kind"; + else if(max_same == 2) return "Pair"; + return "High Card"; + } + +private: + int get_max_same(const vector& data){ + + int n = data.size(), res = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || data[i] != data[start]){ + res = max(res, i - start); + start = i; + } + return res; + } + + bool is_flush(const vector& suits){ + for(int i = 1; i < suits.size(); i ++) + if(suits[i] != suits[0]) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 8dfc9234..0a2bb36d 100644 --- a/readme.md +++ b/readme.md @@ -2196,6 +2196,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2343 | [Query Kth Smallest Trimmed Number](https://leetcode.com/problems/query-kth-smallest-trimmed-number/) | [无] | [C++](2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/) | | | | 2344 | [Minimum Deletions to Make Array Divisible](https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/) | [无] | [C++](2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/) | | | | 2345 | [Finding the Number of Visible Mountains](https://leetcode.com/problems/finding-the-number-of-visible-mountains/) | [无] | [C++](2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/) | | | +| 2346 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2347 | [Best Poker Hand](https://leetcode.com/problems/best-poker-hand/) | [无] | [C++](2001-2500/2347-Best-Poker-Hand/cpp-2347/) | | | | | | | | | | ## 力扣中文站比赛 From 11bcf559296731e0feb5e0d8450e15b74171b315 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Jul 2022 20:33:06 -0700 Subject: [PATCH 133/390] 2348 solved. --- .../cpp-2348/CMakeLists.txt | 6 ++++ .../cpp-2348/main.cpp | 36 +++++++++++++++++++ readme.md | 1 + 3 files changed, 43 insertions(+) create mode 100644 2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/CMakeLists.txt create mode 100644 2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/main.cpp diff --git a/2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/CMakeLists.txt b/2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/CMakeLists.txt new file mode 100644 index 00000000..9c637ccf --- /dev/null +++ b/2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2348) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2348 main.cpp) diff --git a/2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/main.cpp b/2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/main.cpp new file mode 100644 index 00000000..ef235522 --- /dev/null +++ b/2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/number-of-zero-filled-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include + +using namespace std; + + +/// Split +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long zeroFilledSubarray(vector& nums) { + + int n = nums.size(); + long long res = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || nums[i] != nums[start]){ + if(nums[start] == 0){ + long long len = i - start; + res += (len + 1ll) * len / 2ll; + } + start = i; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 0a2bb36d..4282c649 100644 --- a/readme.md +++ b/readme.md @@ -2198,6 +2198,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2345 | [Finding the Number of Visible Mountains](https://leetcode.com/problems/finding-the-number-of-visible-mountains/) | [无] | [C++](2001-2500/2345-Finding-the-Number-of-Visible-Mountains/cpp-2345/) | | | | 2346 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2347 | [Best Poker Hand](https://leetcode.com/problems/best-poker-hand/) | [无] | [C++](2001-2500/2347-Best-Poker-Hand/cpp-2347/) | | | +| 2348 | [Number of Zero-Filled Subarrays](https://leetcode.com/problems/number-of-zero-filled-subarrays/) | [无] | [C++](2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/) | | | | | | | | | | ## 力扣中文站比赛 From fccb867f141f0acb4644905b85d769010bdf72ab Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Jul 2022 20:52:16 -0700 Subject: [PATCH 134/390] 2349 solved. --- .../cpp-2349/CMakeLists.txt | 6 +++ .../cpp-2349/main.cpp | 48 +++++++++++++++++++ readme.md | 1 + 3 files changed, 55 insertions(+) create mode 100644 2001-2500/2349-Design-a-Number-Container-System/cpp-2349/CMakeLists.txt create mode 100644 2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp diff --git a/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/CMakeLists.txt b/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/CMakeLists.txt new file mode 100644 index 00000000..60f80dba --- /dev/null +++ b/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2349) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2349 main.cpp) diff --git a/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp b/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp new file mode 100644 index 00000000..bebca607 --- /dev/null +++ b/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/design-a-number-container-system/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include +#include + +using namespace std; + + +/// Using Map +class NumberContainers { + +private: + map data; + map> num2indice; + +public: + NumberContainers() {} + + void change(int index, int number) { + + auto iter = data.find(index); + if(iter == data.end()){ + data[index] = number; + num2indice[number].insert(index); + return; + } + + int old_number = iter->second; + num2indice[old_number].erase(index); + if(num2indice[old_number].empty()) num2indice.erase(old_number); + + data[index] = number; + num2indice[number].insert(index); + } + + int find(int number) { + return num2indice[number].empty() ? -1 : *num2indice[number].begin(); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 4282c649..1136be91 100644 --- a/readme.md +++ b/readme.md @@ -2199,6 +2199,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2346 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2347 | [Best Poker Hand](https://leetcode.com/problems/best-poker-hand/) | [无] | [C++](2001-2500/2347-Best-Poker-Hand/cpp-2347/) | | | | 2348 | [Number of Zero-Filled Subarrays](https://leetcode.com/problems/number-of-zero-filled-subarrays/) | [无] | [C++](2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/) | | | +| 2349 | [Design a Number Container System](https://leetcode.com/problems/design-a-number-container-system/) | [无] | [C++](2001-2500/2349-Design-a-Number-Container-System/cpp-2349/) | | | | | | | | | | ## 力扣中文站比赛 From bb296a297da2ce678b4aa3991e1ea85b418dfe96 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Jul 2022 20:58:39 -0700 Subject: [PATCH 135/390] 2350 solved. --- .../cpp-2350/CMakeLists.txt | 6 +++ .../cpp-2350/main.cpp | 39 +++++++++++++++++++ readme.md | 1 + 3 files changed, 46 insertions(+) create mode 100644 2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/CMakeLists.txt create mode 100644 2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/main.cpp diff --git a/2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/CMakeLists.txt b/2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/CMakeLists.txt new file mode 100644 index 00000000..c7e3c759 --- /dev/null +++ b/2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2350) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2350 main.cpp) diff --git a/2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/main.cpp b/2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/main.cpp new file mode 100644 index 00000000..8bbb252d --- /dev/null +++ b/2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n + k) +/// Space Compelxity: O(k) +class Solution { +public: + int shortestSequence(vector& rolls, int k) { + + vector used(k + 1, false); + int cnt = 0, res = 0; + for(int e: rolls){ + if(!used[e]){ + used[e] = true; + cnt ++; + if(cnt == k){ + res ++; + used.assign(k + 1, false); + cnt = 0; + } + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 1136be91..d10d7800 100644 --- a/readme.md +++ b/readme.md @@ -2200,6 +2200,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2347 | [Best Poker Hand](https://leetcode.com/problems/best-poker-hand/) | [无] | [C++](2001-2500/2347-Best-Poker-Hand/cpp-2347/) | | | | 2348 | [Number of Zero-Filled Subarrays](https://leetcode.com/problems/number-of-zero-filled-subarrays/) | [无] | [C++](2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/) | | | | 2349 | [Design a Number Container System](https://leetcode.com/problems/design-a-number-container-system/) | [无] | [C++](2001-2500/2349-Design-a-Number-Container-System/cpp-2349/) | | | +| 2350 | [Shortest Impossible Sequence of Rolls](https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/) | [无] | [C++](2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/) | | | | | | | | | | ## 力扣中文站比赛 From 2a8d1e4ebb79df72da8443265c3c66946fd33659 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 23 Jul 2022 21:48:08 -0700 Subject: [PATCH 136/390] 0757 solved. --- .../cpp-0757/CMakeLists.txt | 6 +++ .../cpp-0757/main.cpp | 49 +++++++++++++++++++ readme.md | 2 + 3 files changed, 57 insertions(+) create mode 100644 0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/CMakeLists.txt create mode 100644 0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/main.cpp diff --git a/0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/CMakeLists.txt b/0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/CMakeLists.txt new file mode 100644 index 00000000..0bdb2d89 --- /dev/null +++ b/0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0757) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0757 main.cpp) diff --git a/0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/main.cpp b/0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/main.cpp new file mode 100644 index 00000000..7ea8eeb6 --- /dev/null +++ b/0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/main.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +using namespace std; + + +class Solution { +public: + int intersectionSizeTwo(vector>& intervals) { + + sort(intervals.begin(), intervals.end(), + [](const vector& interval1, const vector& interval2){ + + if(interval1[1] != interval2[1]) return interval1[1] < interval2[1]; + return interval1[0] >= interval2[0]; + }); + + int res = 2, largest1 = intervals[0][1], largest2 = intervals[0][1] - 1; + for(int i = 1; i < intervals.size(); i ++){ + int a = intervals[i][0], b = intervals[i][1]; + if(a <= largest2 && largest1 <= b){ + continue; + } + else if(largest1 >= a){ + res ++; + largest2 = largest1; + largest1 = b; + } + else{ + res += 2; + largest1 = b; + largest2 = b - 1; + } + } + return res; + } +}; + + +int main() { + + vector> intervals1 = {{1,3},{1,4},{2,5},{3,5}}; + cout << Solution().intersectionSizeTwo(intervals1) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index d10d7800..613cccaa 100644 --- a/readme.md +++ b/readme.md @@ -760,6 +760,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 753 | [Cracking the Safe](https://leetcode.com/problems/cracking-the-safe/) | [无] | [C++](0501-1000/0753-Cracking-the-Safe/cpp-0753/) | | | | 754 | [Reach a Number](https://leetcode.com/problems/reach-a-number/) | [Solution](https://leetcode.com/problems/reach-a-number/solution/) | [C++](0501-1000/0754-Reach-a-Number/cpp-0754/) | | | | | | | | | | +| 757 | [Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/) | [无] | [C++](0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/) | | | +| | | | | | | | 761 | [Special Binary String](https://leetcode.com/problems/special-binary-string/) | [solution](https://leetcode.com/problems/special-binary-string/solution/) | [C++](0501-1000/0761-Special-Binary-String/cpp-0761/) | | | | 762 | [Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/) | [无] | [C++](0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/) | | | | 763 | [Partition Labels](https://leetcode.com/problems/partition-labels/) | [无] | [C++](0501-1000/0763-Partition-Labels/cpp-0763/) | | | From f04ecb3fc8d7169af0ded803b1f09cece9c3c372 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 24 Jul 2022 00:48:22 -0700 Subject: [PATCH 137/390] 2351-2354 added. --- .../cpp-2349/main.cpp | 2 + .../cpp-2351/CMakeLists.txt | 6 ++ .../cpp-2351/main.cpp | 31 +++++++++ .../cpp-2352/CMakeLists.txt | 6 ++ .../cpp-2352/main.cpp | 36 ++++++++++ .../cpp-2353/CMakeLists.txt | 6 ++ .../cpp-2353/main.cpp | 54 +++++++++++++++ .../cpp-2354/CMakeLists.txt | 6 ++ .../cpp-2354/main.cpp | 67 +++++++++++++++++++ readme.md | 4 ++ 10 files changed, 218 insertions(+) create mode 100644 2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/CMakeLists.txt create mode 100644 2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/main.cpp create mode 100644 2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/CMakeLists.txt create mode 100644 2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/main.cpp create mode 100644 2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/CMakeLists.txt create mode 100644 2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/main.cpp create mode 100644 2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/CMakeLists.txt create mode 100644 2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/main.cpp diff --git a/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp b/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp index bebca607..ae3cf74c 100644 --- a/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp +++ b/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp @@ -10,6 +10,8 @@ using namespace std; /// Using Map +/// Time Complecity: O(logn) for every operation +/// Space Compelxity: O(n) class NumberContainers { private: diff --git a/2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/CMakeLists.txt b/2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/main.cpp b/2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/main.cpp new file mode 100644 index 00000000..a4f05879 --- /dev/null +++ b/2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/first-letter-to-appear-twice/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + char repeatedCharacter(string s) { + + vector f(26, 0); + for(char c: s){ + f[c - 'a'] ++; + if(f[c - 'a'] == 2) return c; + } + return 'a'; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/CMakeLists.txt b/2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/CMakeLists.txt new file mode 100644 index 00000000..8494c320 --- /dev/null +++ b/2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/main.cpp b/2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/main.cpp new file mode 100644 index 00000000..994b9d23 --- /dev/null +++ b/2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/equal-row-and-column-pairs/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { +public: + int equalPairs(vector>& grid) { + + int n = grid.size(); + + vector> cols(n, vector(n)); + for(int j = 0; j < n; j ++) + for(int i = 0; i < n; i ++) cols[j][i] = grid[i][j]; + + int res = 0; + for(int i = 0; i < n; i ++) + for(int j = 0; j < n; j ++) + res += grid[i] == cols[j]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/CMakeLists.txt b/2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/main.cpp b/2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/main.cpp new file mode 100644 index 00000000..4966922e --- /dev/null +++ b/2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/design-a-food-rating-system/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(logn) for every operation +/// Space Complexity: O(n) +class FoodRatings { + +private: + map>> tree; + map> food; + +public: + FoodRatings(vector& foods, vector& cuisines, vector& ratings) { + + int n = foods.size(); + for(int i = 0; i < n; i ++){ + food[foods[i]] = {cuisines[i], ratings[i]}; + tree[cuisines[i]][ratings[i]].insert(foods[i]); + } + } + + void changeRating(string food_name, int newRating) { + + pair p = food[food_name]; + string cuision = p.first; + int oldRating = p.second; + + tree[cuision][oldRating].erase(food_name); + if(tree[cuision][oldRating].empty()) tree[cuision].erase(oldRating); + tree[cuision][newRating].insert(food_name); + + food[food_name].second = newRating; + } + + string highestRated(string cuisine) { + return *(tree[cuisine].rbegin()->second.begin()); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/CMakeLists.txt b/2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/main.cpp b/2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/main.cpp new file mode 100644 index 00000000..618b64e2 --- /dev/null +++ b/2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/number-of-excellent-pairs/ +/// Author : liuyubobobo +/// Time : 2022-07-24 + +#include +#include +#include +#include + +using namespace std; + + +/// bitwise +/// Time Compelxity: O(nlogn(max_a)) +/// Space Complexity: O(n) +class Solution { +public: + long long countExcellentPairs(vector& nums, int k) { + + map ones; + for(int e: nums){ + if(ones.count(e)) continue; + ones[e] = get_ones(e); + } + + vector cnt(63, 0); + for(const pair& p: ones) + cnt[p.second] ++; + + for(int i = 61; i >= 0; i --) + cnt[i] += cnt[i + 1]; + + long long res = 0; + for(const pair& p: ones){ + int e = p.first; + int one = p.second; + + int t = max(k - one, 0); + res += cnt[t]; + } + return res; + } + +private: + int get_ones(int e){ + int res = 0; + while(e){ + res ++; + e &= (e - 1); + } + return res; + } +}; + + +int main() { + + vector nums1 = {1, 2, 3, 1}; + cout << Solution().countExcellentPairs(nums1, 3) << '\n'; + // 5 + + vector nums2 = {5, 1, 1}; + cout << Solution().countExcellentPairs(nums2, 10) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index 613cccaa..a6d53947 100644 --- a/readme.md +++ b/readme.md @@ -2203,6 +2203,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2348 | [Number of Zero-Filled Subarrays](https://leetcode.com/problems/number-of-zero-filled-subarrays/) | [无] | [C++](2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/) | | | | 2349 | [Design a Number Container System](https://leetcode.com/problems/design-a-number-container-system/) | [无] | [C++](2001-2500/2349-Design-a-Number-Container-System/cpp-2349/) | | | | 2350 | [Shortest Impossible Sequence of Rolls](https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/) | [无] | [C++](2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/) | | | +| 2351 | [First Letter to Appear Twice](https://leetcode.com/problems/first-letter-to-appear-twice/) | [无] | [C++](2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/) | | | +| 2352 | [Equal Row and Column Pairs](https://leetcode.com/problems/equal-row-and-column-pairs/) | [无] | [C++](2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/) | | | +| 2353 | [Design a Food Rating System](https://leetcode.com/problems/design-a-food-rating-system/) | [无] | [C++](2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/) | | | +| 2354 | [Number of Excellent Pairs](https://leetcode.com/problems/number-of-excellent-pairs/) | [无] | [C++](2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/) | | | | | | | | | | ## 力扣中文站比赛 From e9bc19121c2295bfba2e8ed40347540927e3f862 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 26 Jul 2022 12:17:24 -0700 Subject: [PATCH 138/390] 0592 solved. --- .../cpp-0592/CMakeLists.txt | 6 ++ .../cpp-0592/main.cpp | 66 +++++++++++++++++++ readme.md | 1 + 3 files changed, 73 insertions(+) create mode 100644 0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/CMakeLists.txt create mode 100644 0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/main.cpp diff --git a/0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/CMakeLists.txt b/0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/CMakeLists.txt new file mode 100644 index 00000000..789ca250 --- /dev/null +++ b/0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0592) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0592 main.cpp) diff --git a/0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/main.cpp b/0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/main.cpp new file mode 100644 index 00000000..1193ded4 --- /dev/null +++ b/0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/fraction-addition-and-subtraction/ +/// Author : liuyubobobo +/// Time : 2022-07-26 + +#include + +using namespace std; + + +/// String Parse +/// Time Complexity: O(|exp|) +/// Space Complexity: O(1) +class Solution { +public: + string fractionAddition(string expression) { + + if(expression[0] != '-') expression = "+" + expression; + + long long upper = 0, lower = 1; + for(int start = 0, i = 1; i <= expression.size(); i ++) + if(i == expression.size() || (expression[i] == '+' || expression[i] == '-')){ + + string f = expression.substr(start, i - start); + char op = f[0]; + + f = f.substr(1); + int div = f.find('/'); + + long long a = atoll(f.substr(0, div).c_str()); + long long b = atoll(f.substr(div + 1).c_str()); + + if(op == '+'){ + long long new_a = upper * b + a * lower; + long long new_b = b * lower; + upper = new_a, lower = new_b; + } + else{ + long long new_a = upper * b - a * lower; + long long new_b = b * lower; + upper = new_a, lower = new_b; + } + + long long g = gcd(abs(upper), abs(lower)); + upper /= g, lower /= g; + + start = i; + } + + return to_string(upper) + "/" + to_string(lower); + } + +private: + int gcd(long long a, long long b){ + + if(a > b) swap(a, b); + + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a6d53947..d717825b 100644 --- a/readme.md +++ b/readme.md @@ -616,6 +616,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-preorder-traversal/solution/) | [C++](0501-1000/0589-N-ary-Tree-Preorder-Traversal/cpp-0589/) | | | | 590 | [N-ary Tree Postorder Transversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-postorder-traversal/solution/) | [C++](0501-1000/0590-N-ary-Tree-Postorder-Transversal/cpp-0590/) | | | | 591 | [Tag Validator](https://leetcode.com/problems/tag-validator/) | [solution](https://leetcode.com/problems/tag-validator/solution/) | [C++](0501-1000/0591-Tag-Validator/cpp-0591/) | | | +| 592 | [Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/) | [无] | [C++](0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/) | | | | | | | | | | | 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) | [solution](https://leetcode.com/problems/longest-harmonious-subsequence/solution/) | [C++](0501-1000/0594-Longest-Harmonious-Subsequence/cpp-0594/) | | | | 595 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | From 9741573451c5c9e499048d7b04ec44a0765338e0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 27 Jul 2022 11:45:53 -0700 Subject: [PATCH 139/390] 1331 solved. --- .../cpp-1331/CMakeLists.txt | 6 +++ .../cpp-1331/main.cpp | 38 +++++++++++++++++++ readme.md | 1 + 3 files changed, 45 insertions(+) create mode 100644 1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/CMakeLists.txt create mode 100644 1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/main.cpp diff --git a/1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/CMakeLists.txt b/1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/CMakeLists.txt new file mode 100644 index 00000000..8c4a7b96 --- /dev/null +++ b/1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_1331) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1331 main.cpp) diff --git a/1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/main.cpp b/1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/main.cpp new file mode 100644 index 00000000..156d4d23 --- /dev/null +++ b/1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/rank-transform-of-an-array/ +/// Author : liuyubobobo +/// Time : 2022-07-27 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector arrayRankTransform(vector& arr) { + + map rank; + for(int e: arr) rank[e] = 0; + + int r = 1; + for(const pair& p: rank){ + rank[p.first] = r ++; + } + + vector res(arr.size()); + for(int i = 0; i < arr.size(); i ++) + res[i] = rank[arr[i]]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d717825b..74d1af14 100644 --- a/readme.md +++ b/readme.md @@ -1250,6 +1250,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1328 | [Break a Palindrome](https://leetcode.com/problems/break-a-palindrome/) | [solution](https://leetcode.com/problems/break-a-palindrome/solution/) | [C++](1001-1500/1328-Break-a-Palindrome/cpp-1328/) | | | | 1329 | [Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [solution](https://leetcode.com/problems/sort-the-matrix-diagonally/solution/) | [C++](1001-1500/1329-Sort-the-Matrix-Diagonally/cpp-1329/) | | | | | | | | | | +| 1331 | [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/) | [无] | [C++](1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/) | | | | 1332 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [solution](https://leetcode.com/problems/remove-palindromic-subsequences/solution/) | [C++](1001-1500/1332-Remove-Palindromic-Subsequences/cpp-1332/) | | | | 1333 | [Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [无] | [C++](1001-1500/1333-Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/cpp-1333/) | | | | 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/) | [无] | [C++](1001-1500/1334-Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance/cpp-1334/) | | | From 7692ee036c652a0dfe45eebc1459bb6ed5a2123b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 28 Jul 2022 12:10:37 -0700 Subject: [PATCH 140/390] 0593 solved. --- .../0593-Valid-Square/cpp-0593/CMakeLists.txt | 6 ++ 0501-1000/0593-Valid-Square/cpp-0593/main.cpp | 60 +++++++++++++++++++ .../0593-Valid-Square/cpp-0593/main2.cpp | 43 +++++++++++++ readme.md | 2 +- 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0593-Valid-Square/cpp-0593/CMakeLists.txt create mode 100644 0501-1000/0593-Valid-Square/cpp-0593/main.cpp create mode 100644 0501-1000/0593-Valid-Square/cpp-0593/main2.cpp diff --git a/0501-1000/0593-Valid-Square/cpp-0593/CMakeLists.txt b/0501-1000/0593-Valid-Square/cpp-0593/CMakeLists.txt new file mode 100644 index 00000000..12ea5087 --- /dev/null +++ b/0501-1000/0593-Valid-Square/cpp-0593/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0593) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0593 main2.cpp) diff --git a/0501-1000/0593-Valid-Square/cpp-0593/main.cpp b/0501-1000/0593-Valid-Square/cpp-0593/main.cpp new file mode 100644 index 00000000..c92b2d11 --- /dev/null +++ b/0501-1000/0593-Valid-Square/cpp-0593/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/valid-square/ +/// Author : liuyubobobo +/// Time : 2022-07-28 + +#include +#include +#include + +using namespace std; + + +/// Brute Force + Math +/// Time Complexity: O(4!) +/// Space Complexity: O(1) +class Solution { +public: + bool validSquare(vector& p1, vector& p2, vector& p3, vector& p4) { + + vector> p ={p1, p2, p3, p4}; + for(int i = 0; i < 4; i ++) + for(int j = i + 1; j < 4; j ++) + if(p[i] == p[j]) return false; + + sort(p.begin(), p.end()); + do{ + if(valid_square(p)) return true; + }while(next_permutation(p.begin(), p.end())); + return false; + } + +private: + bool valid_square(const vector>& p){ + + int L2 = get_L2(p[0], p[1]); + for(int i = 1; i < 4; i ++) + if(get_L2(p[i], p[(i + 1) % 4]) != L2) return false; + + for(int i = 0; i < 4; i ++) + if(dot_product(p[i], p[(i + 1) % 4], p[(i + 2) % 4])) return false; + + return true; + } + + int dot_product(const vector& p1, const vector& p2, const vector& p3){ + + int a1 = p2[0] - p1[0], b1 = p2[1] - p1[1]; + int a2 = p3[0] - p2[0], b2 = p3[1] - p2[1]; + return a1 * b1 + a2 * b2; + } + + int get_L2(const vector& p1, const vector& p2){ + return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0593-Valid-Square/cpp-0593/main2.cpp b/0501-1000/0593-Valid-Square/cpp-0593/main2.cpp new file mode 100644 index 00000000..6894f216 --- /dev/null +++ b/0501-1000/0593-Valid-Square/cpp-0593/main2.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/valid-square/ +/// Author : liuyubobobo +/// Time : 2022-07-28 + +#include +#include +#include + +using namespace std; + + +/// Only check points' distance +/// Time Complexity: O(6) +/// Space Complexity: O(1) +class Solution { +public: + bool validSquare(vector& p1, vector& p2, vector& p3, vector& p4) { + + vector> p ={p1, p2, p3, p4}; + map table; + for(int i = 0; i < 4; i ++) + for(int j = i + 1; j < 4; j ++){ + if(p[i] == p[j]) return false; + table[get_L2(p[i], p[j])] ++; + } + + if(table.size() != 2) return false; + if(table.begin()->second == 4 && table.rbegin()->second == 2 && table.begin()->first * 2 == table.rbegin()->first) + return true; + return false; + } + +private: + int get_L2(const vector& p1, const vector& p2){ + return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 74d1af14..0cca507d 100644 --- a/readme.md +++ b/readme.md @@ -617,7 +617,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 590 | [N-ary Tree Postorder Transversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [solution](https://leetcode.com/problems/n-ary-tree-postorder-traversal/solution/) | [C++](0501-1000/0590-N-ary-Tree-Postorder-Transversal/cpp-0590/) | | | | 591 | [Tag Validator](https://leetcode.com/problems/tag-validator/) | [solution](https://leetcode.com/problems/tag-validator/solution/) | [C++](0501-1000/0591-Tag-Validator/cpp-0591/) | | | | 592 | [Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/) | [无] | [C++](0501-1000/0592-Fraction-Addition-and-Subtraction/cpp-0592/) | | | -| | | | | | | +| 593 | [Valid Square](https://leetcode.com/problems/valid-square/) | [solution](https://leetcode.com/problems/valid-square/solution/) | [C++](0501-1000/0593-Valid-Square/cpp-0593/) | | | | 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) | [solution](https://leetcode.com/problems/longest-harmonious-subsequence/solution/) | [C++](0501-1000/0594-Longest-Harmonious-Subsequence/cpp-0594/) | | | | 595 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | From 79ef903f7221a33d509792a245433f4b487914d6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 30 Jul 2022 10:39:45 -0700 Subject: [PATCH 141/390] 0251 solved. --- .../cpp-0251/CMakeLists.txt | 6 ++++ .../0251-Flatten-2D-Vector/cpp-0251/main.cpp | 32 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0251-Flatten-2D-Vector/cpp-0251/CMakeLists.txt create mode 100644 0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp diff --git a/0001-0500/0251-Flatten-2D-Vector/cpp-0251/CMakeLists.txt b/0001-0500/0251-Flatten-2D-Vector/cpp-0251/CMakeLists.txt new file mode 100644 index 00000000..82cedef8 --- /dev/null +++ b/0001-0500/0251-Flatten-2D-Vector/cpp-0251/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_0251) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0251 main.cpp) diff --git a/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp b/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp new file mode 100644 index 00000000..30c6353b --- /dev/null +++ b/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + + +class Vector2D { + +private: + vector data; + int cur = -1; + +public: + Vector2D(vector>& vec){ + + for(int i = 0; i < vec.size(); i ++) + for(int e: vec[i]) data.push_back(e); + } + + int next() { + return data[++ cur]; + } + + bool hasNext() { + return cur + 1 < data.size(); + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 0cca507d..b3da2474 100644 --- a/readme.md +++ b/readme.md @@ -290,7 +290,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 248 | [Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/) | [无] | [C++](0001-0500/0248-Strobogrammatic-Number-III/cpp-0248/) | | | | 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/description/) | [无] | [C++](0001-0500/0249-Group-Shifted-Strings/cpp-0249/) | | | | 250 | [Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/description/) | [solution](https://leetcode.com/problems/count-univalue-subtrees/solution/) | [C++](0001-0500/0250-Count-Univalue-Subtrees/cpp-0250/) | | | -| | | | | | | +| 251 | [Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/) | [solution](https://leetcode.com/problems/flatten-2d-vector/solution/) | [C++](0001-0500/0251-Flatten-2D-Vector/cpp-0251/) | | | | 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/description/) | [solution](https://leetcode.com/problems/meeting-rooms/solution/) | [C++](0001-0500/0252-Meeting-Rooms/cpp-0252/) | | | | 253 | [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/description/) | [solution](https://leetcode.com/problems/meeting-rooms-ii/solution/) | [C++](0001-0500/0253-Meeting-Rooms-II/cpp-0253/) | | | | 254 | [Factor Combinations](https://leetcode.com/problems/factor-combinations/description/) | [无] | [C++](0001-0500/0254-Factor-Combinations/cpp-0254/) | | | From 4c008d65ddd8681bbe2c3c60f26845e0414ad425 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 31 Jul 2022 01:04:27 -0700 Subject: [PATCH 142/390] 2357-2360 added. --- .../cpp-2357/CMakeLists.txt | 6 ++ .../cpp-2357/main.cpp | 30 +++++++ .../cpp-2358/CMakeLists.txt | 6 ++ .../cpp-2358/main.cpp | 35 +++++++++ .../cpp-2358/main2.cpp | 37 +++++++++ .../cpp-2358/main3.cpp | 34 ++++++++ .../cpp-2359/CMakeLists.txt | 6 ++ .../cpp-2359/main.cpp | 57 ++++++++++++++ .../cpp-2360/CMakeLists.txt | 6 ++ .../cpp-2360/main.cpp | 78 +++++++++++++++++++ readme.md | 6 ++ 11 files changed, 301 insertions(+) create mode 100644 2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/CMakeLists.txt create mode 100644 2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/main.cpp create mode 100644 2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/CMakeLists.txt create mode 100644 2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main.cpp create mode 100644 2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main2.cpp create mode 100644 2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main3.cpp create mode 100644 2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/CMakeLists.txt create mode 100644 2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/main.cpp create mode 100644 2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/CMakeLists.txt create mode 100644 2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/main.cpp diff --git a/2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/CMakeLists.txt b/2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/CMakeLists.txt new file mode 100644 index 00000000..fe8bfd9f --- /dev/null +++ b/2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/main.cpp b/2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/main.cpp new file mode 100644 index 00000000..da12316b --- /dev/null +++ b/2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/ +/// Author : liuyubobobo +/// Time : 2022-07-31 + +#include +#include +#include + +using namespace std; + + +/// Using Set +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumOperations(vector& nums) { + + set s; + for(int e: nums) + if(e) s.insert(e); + return s.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/CMakeLists.txt b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/CMakeLists.txt new file mode 100644 index 00000000..8e458b10 --- /dev/null +++ b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main3.cpp) diff --git a/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main.cpp b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main.cpp new file mode 100644 index 00000000..71a4c7ee --- /dev/null +++ b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-groups-entering-a-competition/ +/// Author : liuyubobobo +/// Time : 2022-07-31 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(sqrt(n)) +/// Space Complexity: O(1) +class Solution { +public: + int maximumGroups(vector& grades) { + + int n = grades.size(); + for(int k = 1; ;k ++) + if((1 + k) * k / 2 >= n){ + return ((1 + k) * k / 2 == n) ? k : (k - 1); + } + return 0; + } +}; + + +int main() { + + vector x = {10,6,12,7,3,5}; + cout << Solution().maximumGroups(x) << '\n'; + // 3 + + return 0; +} diff --git a/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main2.cpp b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main2.cpp new file mode 100644 index 00000000..ba052441 --- /dev/null +++ b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main2.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-groups-entering-a-competition/ +/// Author : liuyubobobo +/// Time : 2022-07-31 + +#include +#include + +using namespace std; + + +/// Greedy + Binary Search +/// Time Complexity: O(log(n)) +/// Space Complexity: O(1) +class Solution { +public: + int maximumGroups(vector& grades) { + + int n = grades.size(); + int l = 1, r = n; + while(l < r){ + int mid = (l + r + 1) / 2; + if((1ll + mid) * mid / 2 <= n) l = mid; + else r = mid - 1; + } + return l; + } +}; + + +int main() { + + vector x = {10,6,12,7,3,5}; + cout << Solution().maximumGroups(x) << '\n'; + // 3 + + return 0; +} diff --git a/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main3.cpp b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main3.cpp new file mode 100644 index 00000000..1a851fa7 --- /dev/null +++ b/2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/main3.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-groups-entering-a-competition/ +/// Author : liuyubobobo +/// Time : 2022-07-31 + +#include +#include + +using namespace std; + + +/// Greedy + Math +/// Time Complexity: O(log(n)) - sqrt is not O(1) +/// Space Complexity: O(1) +class Solution { +public: + int maximumGroups(vector& grades) { + + int n = grades.size(); + // (1 + x) * x / 2 <= n + // x^2 + x - 2 * n <= 0 + + return (int)((sqrt(1.0 + 8 * n) - 1.0) / 2.0); + } +}; + + +int main() { + + vector x = {10,6,12,7,3,5}; + cout << Solution().maximumGroups(x) << '\n'; + // 3 + + return 0; +} diff --git a/2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/CMakeLists.txt b/2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/CMakeLists.txt new file mode 100644 index 00000000..830d3093 --- /dev/null +++ b/2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/main.cpp b/2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/main.cpp new file mode 100644 index 00000000..54b3fa22 --- /dev/null +++ b/2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/find-closest-node-to-given-two-nodes/ +/// Author : liuyubobobo +/// Time : 2022-07-31 + +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Compelxity: O(n) +/// Space Compelxity: O(n) +class Solution { +public: + int closestMeetingNode(vector& g, int node1, int node2) { + + int n = g.size(); + vector dis1 = bfs(n, g, node1); + vector dis2 = bfs(n, g, node2); + + int best = INT_MAX, res = -1; + for(int u = 0; u < n; u ++){ + if(dis1[u] == -1 || dis2[u] == -1) continue; + int d = max(dis1[u], dis2[u]); + if(d < best) best = d, res = u; + } + return res; + } + +private: + vector bfs(int n, const vector& g, int s){ + + vector dis(n, -1); + + queue q; + q.push(s); + dis[s] = 0; + while(!q.empty()){ + int u = q.front(); q.pop(); + int v = g[u]; + if(v == -1 || dis[v] != -1) continue; + + dis[v] = dis[u] + 1; + q.push(v); + } + return dis; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/CMakeLists.txt b/2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/CMakeLists.txt new file mode 100644 index 00000000..9a6c5c78 --- /dev/null +++ b/2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/main.cpp b/2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/main.cpp new file mode 100644 index 00000000..2e038676 --- /dev/null +++ b/2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/main.cpp @@ -0,0 +1,78 @@ +/// Source : https://leetcode.com/problems/find-closest-node-to-given-two-nodes/ +/// Author : liuyubobobo +/// Time : 2022-07-31 + +#include +#include + +using namespace std; + + +/// Find Cycle in Successor Graph +/// DFS is ok but I think using UF is more elegant:) +/// Time Compelxity: O(n) +/// Space Compelxity: O(n) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + int longestCycle(vector& g) { + + int n = g.size(); + UF uf(n); + + int res = -1; + for(int i = 0; i < n; i ++){ + + if(g[i] == -1) continue; + + if(!uf.is_connected(i, g[i])){ + uf.union_elements(i, g[i]); + continue; + } + + int tres = 1, cur = g[i]; + while(cur != i){ + tres ++; + cur = g[cur]; + } + res = max(res, tres); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index b3da2474..fdab943f 100644 --- a/readme.md +++ b/readme.md @@ -2210,6 +2210,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2353 | [Design a Food Rating System](https://leetcode.com/problems/design-a-food-rating-system/) | [无] | [C++](2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/) | | | | 2354 | [Number of Excellent Pairs](https://leetcode.com/problems/number-of-excellent-pairs/) | [无] | [C++](2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/) | | | | | | | | | | +| 2356 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2357 | [Make Array Zero by Subtracting Equal Amounts](https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/) | [无] | [C++](2001-2500/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/cpp-2357/) | | | +| 2358 | [Maximum Number of Groups Entering a Competition](https://leetcode.com/problems/maximum-number-of-groups-entering-a-competition/) | [无] | [C++](2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/) | | | +| 2359 | [Find Closest Node to Given Two Nodes](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/) | [无] | [C++](2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/) | | | +| 2360 | [Longest Cycle in a Graph](https://leetcode.com/problems/longest-cycle-in-a-graph/) | [无] | [C++](2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/) | | | +| | | | | | | ## 力扣中文站比赛 From 765bacf0f22beeefa801d4859c663146a01def70 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 31 Jul 2022 01:04:53 -0700 Subject: [PATCH 143/390] 0251 comments updated. --- 0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp b/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp index 30c6353b..a5b84be3 100644 --- a/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp +++ b/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp @@ -1,9 +1,17 @@ +/// Source : https://leetcode.com/problems/flatten-2d-vector/ +/// Author : liuyubobobo +/// Time : 2022-07-30 + #include #include using namespace std; +/// Simulation +/// Time Complexity: init: O(n) +/// query: O(1) +/// Space Complexity: O(n) class Vector2D { private: @@ -26,6 +34,7 @@ class Vector2D { } }; + int main() { return 0; From 0482b3957d3b999bc5284d2fbcd9135d931f2058 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 2 Aug 2022 04:51:12 -0700 Subject: [PATCH 144/390] 0378 codes bug fixed. --- .../cpp-0378/main2.cpp | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/0001-0500/0378-Kth-Smallest-Element-in-a-Sorted-Matrix/cpp-0378/main2.cpp b/0001-0500/0378-Kth-Smallest-Element-in-a-Sorted-Matrix/cpp-0378/main2.cpp index 667c0f44..82b80993 100644 --- a/0001-0500/0378-Kth-Smallest-Element-in-a-Sorted-Matrix/cpp-0378/main2.cpp +++ b/0001-0500/0378-Kth-Smallest-Element-in-a-Sorted-Matrix/cpp-0378/main2.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ /// Author : liuyubobobo /// Time : 2018-11-13 +/// Updated: 2022-08-02 #include #include @@ -11,6 +12,11 @@ using namespace std; /// Using Binary Search +/// pay attention to the edge case when l + h is negative, +/// normally in binary search, we are searching for index, so l and h are both non-negative +/// but when l and h can be negative, we need to do a special discuss +/// See line 36 and test case 5 for details +/// /// Time Complexity: O(m * n * log(max - min)) /// Space Complexity: O(1) class Solution { @@ -26,7 +32,9 @@ class Solution { int l = matrix[0][0], h = matrix[m - 1][n - 1]; while(l < h){ - int mid = (l + h) / 2; + int sum = l + h, mid = sum / 2; + if(sum < 0 && sum % 2) mid --; + int rank = get_rank(matrix, mid); if(rank >= k) h = mid; @@ -79,5 +87,20 @@ int main() { cout << Solution().kthSmallest(matrix4, 6) << endl; // 11 + vector> matrix5 = { + {-5, -4}, + {-5, -4} + }; + cout << Solution().kthSmallest(matrix5, 2) << endl; + // -5 + + vector> matrix6 = { + {1, 5, 9}, + {10, 11, 13}, + {12, 13, 15} + }; + cout << Solution().kthSmallest(matrix6, 8) << endl; + // 13 + return 0; } \ No newline at end of file From 7fe3bf4b543ad774a53aeb98dde43b635f8e2622 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 3 Aug 2022 18:47:32 -0700 Subject: [PATCH 145/390] 2361 solved. --- .../cpp-2361/CMakeLists.txt | 6 +++ .../cpp-2361/main.cpp | 43 +++++++++++++++++++ readme.md | 1 + 3 files changed, 50 insertions(+) create mode 100644 2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/CMakeLists.txt create mode 100644 2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/main.cpp diff --git a/2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/CMakeLists.txt b/2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/CMakeLists.txt new file mode 100644 index 00000000..ced61bf5 --- /dev/null +++ b/2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2361) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2361 main.cpp) diff --git a/2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/main.cpp b/2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/main.cpp new file mode 100644 index 00000000..8ff3fdab --- /dev/null +++ b/2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/minimum-costs-using-the-train-line/ +/// Author : liuyubobobo +/// Time : 2022-08-03 + +#include +#include + +using namespace std; + + +/// DP +/// Time Compelxity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector minimumCosts(vector& regular, vector& express, int expressCost) { + + int n = regular.size(); + vector> dp(n + 1, vector(2, 0)); + dp[0][1] = expressCost; + + vector res(n); + for(int i = 1; i <= n; i ++){ + dp[i][0] = min(dp[i - 1][0] + regular[i - 1], dp[i - 1][1] + express[i - 1]); + dp[i][1] = min(dp[i - 1][0] + regular[i - 1] + expressCost, dp[i - 1][1] + express[i - 1]); + res[i - 1] = min(dp[i][0], dp[i][1]); + } + return res; + } +}; + + +void print_vec(const vector& res){ + for(long long e: res) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector regular1 = {1, 6, 9, 5}, express1 = {5, 2, 3, 10}; + print_vec(Solution().minimumCosts(regular1, express1, 8)); + + return 0; +} diff --git a/readme.md b/readme.md index fdab943f..0ba06085 100644 --- a/readme.md +++ b/readme.md @@ -2215,6 +2215,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2358 | [Maximum Number of Groups Entering a Competition](https://leetcode.com/problems/maximum-number-of-groups-entering-a-competition/) | [无] | [C++](2001-2500/2358-Maximum-Number-of-Groups-Entering-a-Competition/cpp-2358/) | | | | 2359 | [Find Closest Node to Given Two Nodes](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/) | [无] | [C++](2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/) | | | | 2360 | [Longest Cycle in a Graph](https://leetcode.com/problems/longest-cycle-in-a-graph/) | [无] | [C++](2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/) | | | +| 2361 | [Minimum Costs Using the Train Line](https://leetcode.com/problems/minimum-costs-using-the-train-line/) | [无] | [C++](2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/) | | | | | | | | | | ## 力扣中文站比赛 From 6fd853da1c1f9728167fe960f6d71fbb15947c5a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 8 Aug 2022 22:32:00 -0700 Subject: [PATCH 146/390] 0314 solved. --- .../cpp-0314/CMakeLists.txt | 6 ++ .../cpp-0314/main.cpp | 57 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/CMakeLists.txt create mode 100644 0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/main.cpp diff --git a/0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/CMakeLists.txt b/0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/CMakeLists.txt new file mode 100644 index 00000000..71044129 --- /dev/null +++ b/0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0314) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0314 main.cpp) diff --git a/0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/main.cpp b/0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/main.cpp new file mode 100644 index 00000000..db73bd90 --- /dev/null +++ b/0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/binary-tree-vertical-order-traversal/ +/// Author : liuyubobobo +/// Time : 2022-08-08 + +#include +#include +#include +#include + +using namespace std; + + +/// BFS + Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector> verticalOrder(TreeNode* root) { + + if(root == nullptr) return {}; + + map> table; + queue> q; + q.push({root, 0}); + while(!q.empty()){ + TreeNode* node = q.front().first; + int col = q.front().second; + q.pop(); + + table[col].push_back(node->val); + if(node->left) q.push({node->left, col - 1}); + if(node->right) q.push({node->right, col + 1}); + } + + vector> res; + for(const pair>& p: table) + res.push_back(p.second); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 0ba06085..56d768bf 100644 --- a/readme.md +++ b/readme.md @@ -349,7 +349,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 312 | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [solution](https://leetcode.com/problems/burst-balloons/solution/) [题解](https://leetcode-cn.com/problems/burst-balloons/solution/chuo-qi-qiu-by-leetcode/) | [C++](0001-0500/0312-Burst-Balloons/cpp-0312/) | | | | 313 | [Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [无] | [C++](0001-0500/0313-Super-Ugly-Number/cpp-0313/) | | | -| | | | | | | +| 314 | [Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/) | [无] | [C++](0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/) | | | | 315 | [Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [无] | [C++](0001-0500/0315-Count-of-Smaller-Numbers-After-Self/cpp-0315/) | | | | 316 | [Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/) | [solution](https://leetcode.com/problems/remove-duplicate-letters/solution/) | [C++](0001-0500/0316-Remove-Duplicate-Letters/cpp-0316/) | | | | 317 | [Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/) | [solution](https://leetcode.com/problems/shortest-distance-from-all-buildings/solution/) | [C++](0001-0500/0317-Shortest-Distance-from-All-Buildings/cpp-0317/) | | | From ad1bd2a21746a9201e3c98d54bd366647e521486 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 8 Aug 2022 22:33:59 -0700 Subject: [PATCH 147/390] 2367 solved. --- .../cpp-2367/CMakeLists.txt | 6 ++++ .../cpp-2367/main.cpp | 31 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 39 insertions(+) create mode 100644 2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/CMakeLists.txt create mode 100644 2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/main.cpp diff --git a/2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/CMakeLists.txt b/2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/CMakeLists.txt new file mode 100644 index 00000000..39483f78 --- /dev/null +++ b/2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2367) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2367 main.cpp) diff --git a/2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/main.cpp b/2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/main.cpp new file mode 100644 index 00000000..34b94eff --- /dev/null +++ b/2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/number-of-arithmetic-triplets/ +/// Author : liuyubobobo +/// Time : 2022-08-07 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^3) +/// Space Complexity: O(1) +class Solution { +public: + int arithmeticTriplets(vector& nums, int diff) { + + int n = nums.size(), res = 0; + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + for(int k = j + 1; k < n; k ++) + res += (nums[k] - nums[j] == diff && nums[j] - nums[i] == diff); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 56d768bf..da18b087 100644 --- a/readme.md +++ b/readme.md @@ -2217,6 +2217,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2360 | [Longest Cycle in a Graph](https://leetcode.com/problems/longest-cycle-in-a-graph/) | [无] | [C++](2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/) | | | | 2361 | [Minimum Costs Using the Train Line](https://leetcode.com/problems/minimum-costs-using-the-train-line/) | [无] | [C++](2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/) | | | | | | | | | | +| 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | +| | | | | | | ## 力扣中文站比赛 From 3d0ef4794fc099c0a9d45ca1bacea7593d97b93d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 8 Aug 2022 23:25:38 -0700 Subject: [PATCH 148/390] 0636 solved. --- .../cpp-0636/CMakeLists.txt | 6 ++ .../cpp-0636/main.cpp | 78 +++++++++++++++++++ readme.md | 1 + 3 files changed, 85 insertions(+) create mode 100644 0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/CMakeLists.txt create mode 100644 0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/main.cpp diff --git a/0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/CMakeLists.txt b/0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/CMakeLists.txt new file mode 100644 index 00000000..52e1eff9 --- /dev/null +++ b/0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0636) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0636 main.cpp) diff --git a/0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/main.cpp b/0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/main.cpp new file mode 100644 index 00000000..da76e9f9 --- /dev/null +++ b/0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/main.cpp @@ -0,0 +1,78 @@ +/// Source : https://leetcode.com/problems/exclusive-time-of-functions/ +/// Author : liuyubobobo +/// Time : 2022-08-08 + +#include +#include +#include + +using namespace std; + + +/// Using Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector exclusiveTime(int n, vector& logs) { + + vector res(n, 0); + + vector, char>> data; // id, ts, s or e + for(const string& log: logs) + data.push_back(parse(log)); + + vector> stack; // id, ts + for(const pair, char>& e: data){ + int id = e.first.first, ts = e.first.second; + char s_or_e = e.second; + + if(s_or_e == 's'){ + if(!stack.empty()){ + res[stack.back().first] += ts - stack.back().second; + stack.back().second = -1; + } + stack.push_back({id, ts}); + } + else{ + assert(!stack.empty() && stack.back().first == id); + res[stack.back().first] += ts - stack.back().second + 1; + stack.pop_back(); + + if(!stack.empty()){ + stack.back().second = ts + 1; + } + } + } + return res; + } + +private: + pair, char> parse(const string& log){ + + int c1 = log.find(':'), c2 = log.find(':', c1 + 1); + + int id = atoi(log.substr(0, c1).c_str()); + char s_or_e =log[c1 + 1]; + int ts = atoi(log.substr(c2 + 1).c_str()); + return {{id, ts}, s_or_e}; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector logs1 = {"0:start:0","1:start:2","1:end:5","0:end:6"}; + print_vec(Solution().exclusiveTime(2, logs1)); + // 3 4 + + vector logs2 = {"0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"}; + print_vec(Solution().exclusiveTime(1, logs2)); + // 8 + + return 0; +} diff --git a/readme.md b/readme.md index da18b087..8dfd1756 100644 --- a/readme.md +++ b/readme.md @@ -649,6 +649,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 632 | [Smallest Range Covering Elements from K Lists](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/) | [solution](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/solution/) | [C++](0501-1000/0632-Smallest-Range-Covering-Elements-from-K-Lists/cpp-0632/) | | | | 633 | [Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers/) | [C++](0501-1000/0633-Sum-of-Square-Numbers/cpp-0633/) | | | | | | | | | | | +| 636 | [Exclusive Time of Functions](https://leetcode.com/problems/exclusive-time-of-functions/) | [无] | [C++](0501-1000/0636-Exclusive-Time-of-Functions/cpp-0636/) | | | | 637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) | [solution](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) | [C++](0501-1000/0637-Average-of-Levels-in-Binary-Tree/cpp-0637/) | | | | 638 | [Shopping Offers](https://leetcode.com/problems/shopping-offers/) | [solution](https://leetcode.com/problems/shopping-offers/solution/) | [C++](0501-1000/0638-Shopping-Offers/cpp-0638/) | | | | 639 | [Decode Ways II](https://leetcode.com/problems/decode-ways-ii/) | [solution](https://leetcode.com/problems/decode-ways-ii/solution/)
[缺:DP] | [C++](0501-1000/0639-Decode-Ways-II/cpp-0639/) | | | From acdd6007c9958a3925e3367d8122c86359c436a0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 02:03:55 -0700 Subject: [PATCH 149/390] 0640 solved. --- .../cpp-0640/CMakeLists.txt | 6 ++ .../0640-Solve-the-Equation/cpp-0640/main.cpp | 72 +++++++++++++++++++ readme.md | 1 + 3 files changed, 79 insertions(+) create mode 100644 0501-1000/0640-Solve-the-Equation/cpp-0640/CMakeLists.txt create mode 100644 0501-1000/0640-Solve-the-Equation/cpp-0640/main.cpp diff --git a/0501-1000/0640-Solve-the-Equation/cpp-0640/CMakeLists.txt b/0501-1000/0640-Solve-the-Equation/cpp-0640/CMakeLists.txt new file mode 100644 index 00000000..6e8e27a2 --- /dev/null +++ b/0501-1000/0640-Solve-the-Equation/cpp-0640/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0640) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0640 main.cpp) diff --git a/0501-1000/0640-Solve-the-Equation/cpp-0640/main.cpp b/0501-1000/0640-Solve-the-Equation/cpp-0640/main.cpp new file mode 100644 index 00000000..084e8df1 --- /dev/null +++ b/0501-1000/0640-Solve-the-Equation/cpp-0640/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode.com/problems/solve-the-equation/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include + +using namespace std; + + +/// Parse +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string solveEquation(string equation) { + + char equal_pos = equation.find('='); + string left = equation.substr(0, equal_pos); + string right = equation.substr(equal_pos + 1); + + vector l_res = get_res(left); +// for(int e: l_res) cout << e << ' '; cout << '\n'; + vector r_res = get_res(right); +// for(int e: r_res) cout << e << ' '; cout << '\n'; + + int k = l_res[1] - r_res[1]; + int b = r_res[0] - l_res[0]; + + if(k == 0) + return b == 0 ? "Infinite solutions" : "No solution"; + + return "x=" + to_string(b / k); + } + +private: + vector get_res(const string& s){ + + vector res(2, 0); + for(int start = 0, i = 1; i <= s.size(); i ++) + if(i == s.size() || (s[i] == '+' || s[i] == '-')){ + string t = s.substr(start, i - start); + if(t[0] == '+') t = t.substr(1); + + if(t.back() == 'x'){ + if(t == "x") res[1] += 1; + else if(t == "-x") res[1] += -1; + else res[1] += atoi(t.substr(0, t.size() - 1).c_str()); + } + else + res[0] += atoi(t.c_str()); + + start = i; + } + return res; + } +}; + + +int main() { + + cout << Solution().solveEquation("x+5-3+x=6+x-2") << '\n'; + // x = 2 + + cout << Solution().solveEquation("2x=x") << '\n'; + // x = 0 + + cout << Solution().solveEquation("-x=-1") << '\n'; + // x = 1 + + return 0; +} diff --git a/readme.md b/readme.md index 8dfd1756..d9ee83ce 100644 --- a/readme.md +++ b/readme.md @@ -653,6 +653,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) | [solution](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) | [C++](0501-1000/0637-Average-of-Levels-in-Binary-Tree/cpp-0637/) | | | | 638 | [Shopping Offers](https://leetcode.com/problems/shopping-offers/) | [solution](https://leetcode.com/problems/shopping-offers/solution/) | [C++](0501-1000/0638-Shopping-Offers/cpp-0638/) | | | | 639 | [Decode Ways II](https://leetcode.com/problems/decode-ways-ii/) | [solution](https://leetcode.com/problems/decode-ways-ii/solution/)
[缺:DP] | [C++](0501-1000/0639-Decode-Ways-II/cpp-0639/) | | | +| 640 | [Solve the Equation](https://leetcode.com/problems/solve-the-equation/) | [无] | [C++](0501-1000/0640-Solve-the-Equation/cpp-0640/) | | | | | | | | | | | 642 | [Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/) | [无] | [C++](0501-1000/0642-Design-Search-Autocomplete-System/cpp-0642/) | | | | 643 | [Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/) | [solution](https://leetcode.com/problems/maximum-average-subarray-i/solution/) | [C++](0501-1000/0643-Maximum-Average-Subarray-I/cpp-0643/) | | | From df730ec8188e4309fe379114eb8325cfe3910e45 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 02:49:43 -0700 Subject: [PATCH 150/390] 0126 codes updated. --- .../0126-Word-Ladder-II/cpp-0126/main.cpp | 72 +++++++++---------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/0001-0500/0126-Word-Ladder-II/cpp-0126/main.cpp b/0001-0500/0126-Word-Ladder-II/cpp-0126/main.cpp index a06c16d3..84ea4303 100644 --- a/0001-0500/0126-Word-Ladder-II/cpp-0126/main.cpp +++ b/0001-0500/0126-Word-Ladder-II/cpp-0126/main.cpp @@ -1,18 +1,22 @@ /// Source : https://leetcode.com/problems/word-ladder-ii/description/ /// Author : liuyubobobo /// Time : 2018-04-23 +/// Updated: 2022-08-10 #include #include -#include +#include #include -#include using namespace std; -/// BFS -/// Time Complexity: O(n*n) -/// Space Complexity: O(n) + +/// BFS + Backtrack +/// Attention: backtrack from begin to end will get TLE +/// The following solution will backtrack result in reverse (from end to begin) +/// +/// Time Complexity: O(?) +/// Space Complexity: O(?) class Solution { public: @@ -29,7 +33,7 @@ class Solution { int n = wordList.size(); // Create Graph - vector> g(n, vector()); + vector> g(n); for(int i = 0 ; i < wordList.size() ; i ++) for(int j = i + 1 ; j < wordList.size() ; j ++) if(similar(wordList[i], wordList[j])){ @@ -37,65 +41,59 @@ class Solution { g[j].push_back(i); } - unordered_map distance; - bfs(g, begin, end, distance); + vector distance = bfs(n, g, begin); + if(distance[end] == -1) return {}; vector> res; - vector tres = {begin}; - getRes(g, begin, end, distance, wordList, tres, res); + vector tres = {wordList[end]}; + get_res(g, end, begin, distance, wordList, tres, res); return res; } private: - void bfs(const vector>& g, int begin, int end, - unordered_map& distance){ + vector bfs(int n, const vector>& g, int begin){ + + vector distance(n, -1); queue q; q.push(begin); distance[begin] = 0; while(!q.empty()){ - int cur = q.front(); - q.pop(); - // assert(distance.find(cur) != distance.end()); + int cur = q.front(); q.pop(); for(int j: g[cur]) - if(distance.find(j) == distance.end()){ + if(distance[j] == -1){ distance[j] = distance[cur] + 1; q.push(j); } } + + return distance; } - void getRes(vector>& g, int cur, int end, - unordered_map& distance, - const vector& wordList, - vector& tres, vector>& res){ + void get_res(vector>& g, int cur, int end, + const vector& distance, + const vector& wordList, + vector& tres, vector>& res){ - if(tres.size() > 0 && tres[tres.size() - 1] == end){ - res.push_back(getPath(tres, wordList)); + if(tres.size() > 0 && tres[tres.size() - 1] == wordList[end]){ + res.push_back(tres); + reverse(res.back().begin(), res.back().end()); return; } for(int i: g[cur]) - if(distance[i] == distance[cur] + 1){ - tres.push_back(i); - getRes(g, i, end, distance, wordList, tres, res); + if(distance[i] == distance[cur] - 1){ + tres.push_back(wordList[i]); + get_res(g, i, end, distance, wordList, tres, res); tres.pop_back(); } return; } - vector getPath(const vector& path, - const vector& wordList){ - vector ret; - for(const int e: path) - ret.push_back(wordList[e]); - return ret; - } - bool similar(const string& word1, const string& word2){ // assert(word1 != "" && word1.size() == word2.size() && word1 != word2); @@ -112,7 +110,7 @@ class Solution { }; -void print_vector_vector(const vector>& res){ +void print_vector(const vector>& res){ for(const vector& v: res){ for(const string& e: v) cout << e << " "; @@ -127,15 +125,13 @@ int main() { string beginWord1 = "hit"; string endWord1 = "cog"; vector> res1 = Solution().findLadders(beginWord1, endWord1, vec1); - print_vector_vector(res1); - - // --- + print_vector(res1); vector vec2 = {"a","b","c"}; string beginWord2 = "a"; string endWord2 = "c"; vector> res2 = Solution().findLadders(beginWord2, endWord2, vec2); - print_vector_vector(res2); + print_vector(res2); return 0; } \ No newline at end of file From e446e67ac6928a4235ff97df7995945fc667293f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 10:35:57 -0700 Subject: [PATCH 151/390] 2368 solved. --- .../cpp-2368/CMakeLists.txt | 6 +++ .../cpp-2368/main.cpp | 50 +++++++++++++++++++ readme.md | 1 + 3 files changed, 57 insertions(+) create mode 100644 2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/CMakeLists.txt create mode 100644 2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/main.cpp diff --git a/2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/CMakeLists.txt b/2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/CMakeLists.txt new file mode 100644 index 00000000..12a01851 --- /dev/null +++ b/2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2368) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2368 main.cpp) diff --git a/2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/main.cpp b/2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/main.cpp new file mode 100644 index 00000000..3fd377e5 --- /dev/null +++ b/2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/reachable-nodes-with-restrictions/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int reachableNodes(int n, vector>& edges, vector& restricted) { + + set restricted_set(restricted.begin(), restricted.end()); + + vector> tree(n); + for(const vector& p: edges){ + int u = p[0], v = p[1]; + tree[u].push_back(v), tree[v].push_back(u); + } + + return dfs(tree, 0, -1, restricted_set); + } + +private: + int dfs(const vector>& tree, int u, int p, set& restricted_set){ + + if(restricted_set.count(u)) return 0; + + int res = 1; + for(int v: tree[u]){ + if(v == p) continue; + res += dfs(tree, v, u, restricted_set); + } + return res; + } +}; + + +int main() { + + + return 0; +} diff --git a/readme.md b/readme.md index d9ee83ce..26e6c2c2 100644 --- a/readme.md +++ b/readme.md @@ -2220,6 +2220,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2361 | [Minimum Costs Using the Train Line](https://leetcode.com/problems/minimum-costs-using-the-train-line/) | [无] | [C++](2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/) | | | | | | | | | | | 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | +| 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | [无] | [C++](2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/) | | | | | | | | | | ## 力扣中文站比赛 From 6ce4546449d79744eebf397ab38f576fb2f235f4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 10:50:16 -0700 Subject: [PATCH 152/390] 2369 solved. --- .../cpp-2369/CMakeLists.txt | 6 +++ .../cpp-2369/main.cpp | 40 +++++++++++++++++++ readme.md | 1 + 3 files changed, 47 insertions(+) create mode 100644 2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/CMakeLists.txt create mode 100644 2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/main.cpp diff --git a/2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/CMakeLists.txt b/2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/CMakeLists.txt new file mode 100644 index 00000000..d1a89ba9 --- /dev/null +++ b/2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2369) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2369 main.cpp) diff --git a/2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/main.cpp b/2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/main.cpp new file mode 100644 index 00000000..c94f1d3b --- /dev/null +++ b/2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + bool validPartition(vector& nums) { + + int n = nums.size(); + + vector dp(n + 1, false); + dp[0] = true; + dp[2] = (nums[1] == nums[0]); + + for(int i = 2; i < n; i ++){ + if(nums[i] == nums[i - 1] && dp[i - 2 + 1]) + dp[i + 1] = true; + if(nums[i] == nums[i - 1] && nums[i - 1] == nums[i - 2] && dp[i - 3 + 1]) + dp[i + 1] = true; + if(nums[i] - nums[i - 1] == 1 && nums[i - 1] - nums[i - 2] == 1 && dp[i - 3 + 1]) + dp[i + 1] = true; + } + return dp[n]; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 26e6c2c2..c66e52c4 100644 --- a/readme.md +++ b/readme.md @@ -2221,6 +2221,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | | 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | [无] | [C++](2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/) | | | +| 2369 | [Check if There is a Valid Partition For The Array](https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/) | [无] | [C++](2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/) | | | | | | | | | | ## 力扣中文站比赛 From 55182b883ba3c3b89a385280f741511d4030670d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 11:26:24 -0700 Subject: [PATCH 153/390] 2370 solved. --- .../cpp-2370/CMakeLists.txt | 6 +++ .../cpp-2370/main.cpp | 39 +++++++++++++++++++ readme.md | 1 + 3 files changed, 46 insertions(+) create mode 100644 2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/CMakeLists.txt create mode 100644 2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/main.cpp diff --git a/2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/CMakeLists.txt b/2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/CMakeLists.txt new file mode 100644 index 00000000..0215df13 --- /dev/null +++ b/2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2370) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2370 main.cpp) diff --git a/2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/main.cpp b/2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/main.cpp new file mode 100644 index 00000000..cd22d159 --- /dev/null +++ b/2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/longest-ideal-subsequence/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int longestIdealString(string s, int k) { + + vector dp(26, 0); + for(char c: s){ + int res = 0; + for(char end = max(c - k, (int)'a'); end <= min(c + k, (int)'z'); end ++) + res = max(res, dp[end - 'a'] + 1); + dp[c - 'a'] = max(dp[c - 'a'], res); + } + return *max_element(dp.begin(), dp.end()); + } +}; + + +int main() { + + cout << Solution().longestIdealString("acfgbd", 2) << '\n'; + // 4 + + cout << Solution().longestIdealString("jxhwaysa", 14) << '\n'; + // 5 + + return 0; +} diff --git a/readme.md b/readme.md index c66e52c4..caea1b19 100644 --- a/readme.md +++ b/readme.md @@ -2222,6 +2222,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | | 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | [无] | [C++](2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/) | | | | 2369 | [Check if There is a Valid Partition For The Array](https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/) | [无] | [C++](2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/) | | | +| 2370 | [Longest Ideal Subsequence](https://leetcode.com/problems/longest-ideal-subsequence/) | [无] | [C++](2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/) | | | | | | | | | | ## 力扣中文站比赛 From 4f56628d4f9f49a5edff917fb764cbf402e0daf7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 11:34:06 -0700 Subject: [PATCH 154/390] 2363 solved. --- .../cpp-2363/CMakeLists.txt | 6 +++ .../cpp-2363/main.cpp | 40 +++++++++++++++++++ readme.md | 2 + 3 files changed, 48 insertions(+) create mode 100644 2001-2500/2363-Merge-Similar-Items/cpp-2363/CMakeLists.txt create mode 100644 2001-2500/2363-Merge-Similar-Items/cpp-2363/main.cpp diff --git a/2001-2500/2363-Merge-Similar-Items/cpp-2363/CMakeLists.txt b/2001-2500/2363-Merge-Similar-Items/cpp-2363/CMakeLists.txt new file mode 100644 index 00000000..db600bb0 --- /dev/null +++ b/2001-2500/2363-Merge-Similar-Items/cpp-2363/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2363) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2363 main.cpp) diff --git a/2001-2500/2363-Merge-Similar-Items/cpp-2363/main.cpp b/2001-2500/2363-Merge-Similar-Items/cpp-2363/main.cpp new file mode 100644 index 00000000..dfeb2abb --- /dev/null +++ b/2001-2500/2363-Merge-Similar-Items/cpp-2363/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/merge-similar-items/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(|items1| + |items2|) +/// Space Complexity: O(|items1| + |items2|) +class Solution { +public: + vector> mergeSimilarItems(vector>& items1, vector>& items2) { + + map table; + for(const vector& item: items1){ + int v = item[0], w = item[1]; + table[v] += w; + } + for(const vector& item: items2){ + int v = item[0], w = item[1]; + table[v] += w; + } + + vector> res; + for(const pair& p: table) + res.push_back({p.first, p.second}); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index caea1b19..81488bb4 100644 --- a/readme.md +++ b/readme.md @@ -2218,6 +2218,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2359 | [Find Closest Node to Given Two Nodes](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/) | [无] | [C++](2001-2500/2359-Find-Closest-Node-to-Given-Two-Nodes/cpp-2359/) | | | | 2360 | [Longest Cycle in a Graph](https://leetcode.com/problems/longest-cycle-in-a-graph/) | [无] | [C++](2001-2500/2360-Longest-Cycle-in-a-Graph/cpp-2360/) | | | | 2361 | [Minimum Costs Using the Train Line](https://leetcode.com/problems/minimum-costs-using-the-train-line/) | [无] | [C++](2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/) | | | +| 2362 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2363 | [Merge Similar Items](https://leetcode.com/problems/merge-similar-items/) | [无] | [C++](2001-2500/2363-Merge-Similar-Items/cpp-2363/) | | | | | | | | | | | 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | | 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | [无] | [C++](2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/) | | | From 0821c376394400e179eba5ce05c888ab07f187b4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 11:40:37 -0700 Subject: [PATCH 155/390] 2364 solved. --- .../cpp-2364/CMakeLists.txt | 6 +++ .../cpp-2364/main.cpp | 39 +++++++++++++++++++ readme.md | 1 + 3 files changed, 46 insertions(+) create mode 100644 2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/CMakeLists.txt create mode 100644 2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/main.cpp diff --git a/2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/CMakeLists.txt b/2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/CMakeLists.txt new file mode 100644 index 00000000..df01c77a --- /dev/null +++ b/2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2364) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2364 main.cpp) diff --git a/2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/main.cpp b/2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/main.cpp new file mode 100644 index 00000000..b7a1bde0 --- /dev/null +++ b/2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/count-number-of-bad-pairs/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long countBadPairs(vector& nums) { + + int n = nums.size(); + for(int i = 0; i < n; i ++) + nums[i] -= i; + + map f; + for(int e: nums) f[e] ++; + + long long res = 1ll * n * (n - 1) / 2ll; + for(const pair& p: f){ + long long len = p.second; + res -= len * (len - 1) / 2ll; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 81488bb4..b2e7056c 100644 --- a/readme.md +++ b/readme.md @@ -2220,6 +2220,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2361 | [Minimum Costs Using the Train Line](https://leetcode.com/problems/minimum-costs-using-the-train-line/) | [无] | [C++](2001-2500/2361-Minimum-Costs-Using-the-Train-Line/cpp-2361/) | | | | 2362 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2363 | [Merge Similar Items](https://leetcode.com/problems/merge-similar-items/) | [无] | [C++](2001-2500/2363-Merge-Similar-Items/cpp-2363/) | | | +| 2364 | [Count Number of Bad Pairs](https://leetcode.com/problems/count-number-of-bad-pairs/) | [无] | [C++](2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/) | | | | | | | | | | | 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | | 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | [无] | [C++](2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/) | | | From 213bb0b376d3e2e1325ec92de6d3dc607e475676 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 12:00:36 -0700 Subject: [PATCH 156/390] 2365 solved. --- .../cpp-2365/CMakeLists.txt | 6 +++ .../2365-Task-Scheduler-II/cpp-2365/main.cpp | 45 +++++++++++++++++++ readme.md | 1 + 3 files changed, 52 insertions(+) create mode 100644 2001-2500/2365-Task-Scheduler-II/cpp-2365/CMakeLists.txt create mode 100644 2001-2500/2365-Task-Scheduler-II/cpp-2365/main.cpp diff --git a/2001-2500/2365-Task-Scheduler-II/cpp-2365/CMakeLists.txt b/2001-2500/2365-Task-Scheduler-II/cpp-2365/CMakeLists.txt new file mode 100644 index 00000000..0c690275 --- /dev/null +++ b/2001-2500/2365-Task-Scheduler-II/cpp-2365/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2365) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2365 main.cpp) diff --git a/2001-2500/2365-Task-Scheduler-II/cpp-2365/main.cpp b/2001-2500/2365-Task-Scheduler-II/cpp-2365/main.cpp new file mode 100644 index 00000000..785f1349 --- /dev/null +++ b/2001-2500/2365-Task-Scheduler-II/cpp-2365/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/task-scheduler-ii/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long taskSchedulerII(vector& tasks, int space) { + + map last; + long long cur = 1; + for(int task: tasks){ + if(!last.count(task)){ + last[task] = cur; + cur ++; + continue; + } + + long long pre = last[task]; + long long next = max(pre + space + 1, cur); + last[task] = next; + cur = next + 1; + } + return cur - 1; + } +}; + + +int main() { + + vector tasks1 = {1, 2, 1, 2, 3, 1}; + cout << Solution().taskSchedulerII(tasks1, 3) << '\n'; + // 9 + + return 0; +} diff --git a/readme.md b/readme.md index b2e7056c..00668bea 100644 --- a/readme.md +++ b/readme.md @@ -2221,6 +2221,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2362 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2363 | [Merge Similar Items](https://leetcode.com/problems/merge-similar-items/) | [无] | [C++](2001-2500/2363-Merge-Similar-Items/cpp-2363/) | | | | 2364 | [Count Number of Bad Pairs](https://leetcode.com/problems/count-number-of-bad-pairs/) | [无] | [C++](2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/) | | | +| 2365 | [Task Scheduler II](https://leetcode.com/problems/task-scheduler-ii/) | [无] | [C++](2001-2500/2365-Task-Scheduler-II/cpp-2365/) | | | | | | | | | | | 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | | 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | [无] | [C++](2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/) | | | From 783d550a373301e83f74928c2d456b8e81d26bf6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 10 Aug 2022 12:20:04 -0700 Subject: [PATCH 157/390] 2366 solved. --- .../cpp-2366/CMakeLists.txt | 6 +++ .../cpp-2366/main.cpp | 47 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/CMakeLists.txt create mode 100644 2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/main.cpp diff --git a/2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/CMakeLists.txt b/2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/CMakeLists.txt new file mode 100644 index 00000000..267c7c7b --- /dev/null +++ b/2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2366) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2366 main.cpp) diff --git a/2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/main.cpp b/2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/main.cpp new file mode 100644 index 00000000..22375802 --- /dev/null +++ b/2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/minimum-replacements-to-sort-the-array/ +/// Author : liuyubobobo +/// Time : 2022-08-10 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long minimumReplacement(vector& nums) { + + int n = nums.size(); + + long long res = 0; + int p = nums.back(); + for(int i = n - 2; i >= 0; i --){ + if(nums[i] <= p){ + p = min(p, nums[i]); + continue; + } + + int cnt = nums[i] / p; + int mod = nums[i] % p; + if(mod) cnt ++; + + res += (cnt - 1); + p = min(p, nums[i] / cnt); + } + return res; + } +}; + + +int main() { + + vector nums1 = {12,9,7,6,17,19,21}; + cout << Solution().minimumReplacement(nums1) << '\n'; + // 6 + + return 0; +} diff --git a/readme.md b/readme.md index 00668bea..d2b3164b 100644 --- a/readme.md +++ b/readme.md @@ -2222,7 +2222,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2363 | [Merge Similar Items](https://leetcode.com/problems/merge-similar-items/) | [无] | [C++](2001-2500/2363-Merge-Similar-Items/cpp-2363/) | | | | 2364 | [Count Number of Bad Pairs](https://leetcode.com/problems/count-number-of-bad-pairs/) | [无] | [C++](2001-2500/2364-Count-Number-of-Bad-Pairs/cpp-2364/) | | | | 2365 | [Task Scheduler II](https://leetcode.com/problems/task-scheduler-ii/) | [无] | [C++](2001-2500/2365-Task-Scheduler-II/cpp-2365/) | | | -| | | | | | | +| 2366 | [Minimum Replacements to Sort the Array](https://leetcode.com/problems/minimum-replacements-to-sort-the-array/) | [无] | [C++](2001-2500/2366-Minimum-Replacements-to-Sort-the-Array/cpp-2366/) | | | | 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [无] | [C++](2001-2500/2367-Number-of-Arithmetic-Triplets/cpp-2367/) | | | | 2368 | [Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/) | [无] | [C++](2001-2500/2368-Reachable-Nodes-With-Restrictions/cpp-2368/) | | | | 2369 | [Check if There is a Valid Partition For The Array](https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/) | [无] | [C++](2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/) | | | From 3798b6a67ae019cb70a6ee720b5a624457db7065 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 11 Aug 2022 10:30:31 -0700 Subject: [PATCH 158/390] 1282 solved. --- .../cpp-1282/CMakeLists.txt | 6 +++ .../cpp-1282/main.cpp | 41 +++++++++++++++++++ readme.md | 2 + 3 files changed, 49 insertions(+) create mode 100644 1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/CMakeLists.txt create mode 100644 1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/main.cpp diff --git a/1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/CMakeLists.txt b/1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/CMakeLists.txt new file mode 100644 index 00000000..86e9a69b --- /dev/null +++ b/1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_1282) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1282 main.cpp) diff --git a/1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/main.cpp b/1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/main.cpp new file mode 100644 index 00000000..18321ad5 --- /dev/null +++ b/1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/ +/// Author : liuyubobobo +/// Time : 2022-08-11 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector> groupThePeople(vector& groupSizes) { + + map> groups; + for(int i = 0; i < groupSizes.size(); i ++) + groups[groupSizes[i]].push_back(i); + + vector> res; + for(const pair>& p: groups){ + int sz = p.first; + const vector& v = p.second; + assert(v.size() % sz == 0); + + for(int i = 0; i < v.size(); i += sz) + res.push_back(vector(v.begin() + i, v.begin() + i + sz)); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d2b3164b..720c14e0 100644 --- a/readme.md +++ b/readme.md @@ -1224,6 +1224,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1280 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 1282 | [Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/) | [无] | [C++](1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/) | | | +| | | | | | | | 1286 | [Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/) | [solution](https://leetcode.com/problems/iterator-for-combination/solution/) | [C++](1001-1500/1286-Iterator-for-Combination/cpp-1286/) | | | | | | | | | | | 1288 | [Remove Covered Intervals](https://leetcode.com/problems/remove-covered-intervals/) | [solution](https://leetcode.com/problems/remove-covered-intervals/solution/) | [C++](1001-1500/1288-Remove-Covered-Intervals/cpp-1288/) | | | From 5221e6c6c610c4480135e2890d89738c07dcf646 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 13 Aug 2022 21:14:22 -0700 Subject: [PATCH 159/390] 2373-2376 added. --- .../cpp-2373/CMakeLists.txt | 6 ++ .../cpp-2373/main.cpp | 37 +++++++++++ .../cpp-2374/CMakeLists.txt | 6 ++ .../cpp-2374/main.cpp | 35 +++++++++++ .../cpp-2375/CMakeLists.txt | 6 ++ .../cpp-2375/main.cpp | 63 +++++++++++++++++++ .../cpp-2376/CMakeLists.txt | 6 ++ .../cpp-2376/main.cpp | 63 +++++++++++++++++++ readme.md | 6 ++ 9 files changed, 228 insertions(+) create mode 100644 2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/CMakeLists.txt create mode 100644 2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/main.cpp create mode 100644 2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/CMakeLists.txt create mode 100644 2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/main.cpp create mode 100644 2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/CMakeLists.txt create mode 100644 2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/main.cpp create mode 100644 2001-2500/2376-Count-Special-Integers/cpp-2376/CMakeLists.txt create mode 100644 2001-2500/2376-Count-Special-Integers/cpp-2376/main.cpp diff --git a/2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/CMakeLists.txt b/2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/main.cpp b/2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/main.cpp new file mode 100644 index 00000000..d506ef05 --- /dev/null +++ b/2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/largest-local-values-in-a-matrix/ +/// Author : liuyubobobo +/// Time : 2022-08-13 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + vector> largestLocal(vector>& grid) { + + int n = grid.size(); + + vector> res(n - 2, vector(n - 2)); + for(int i = 1; i + 1 < n; i ++) + for(int j = 1; j + 1 < n; j ++){ + int t = 0; + for(int d1 = -1; d1 <= 1; d1 ++) + for(int d2 = -1; d2 <= 1; d2 ++) + t = max(t, grid[i + d1][j + d2]); + res[i - 1][j - 1] = t; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/CMakeLists.txt b/2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/main.cpp b/2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/main.cpp new file mode 100644 index 00000000..9d9fd3ea --- /dev/null +++ b/2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/node-with-highest-edge-score/ +/// Author : liuyubobobo +/// Time : 2022-08-13 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int edgeScore(vector& edges) { + + int n = edges.size(); + vector score(n, 0); + for(int u = 0; u < n; u ++) + score[edges[u]] += u; + + long long best = score[0]; + int res = 0; + for(int u = 1; u < n; u ++) + if(score[u] > best) best = score[u], res = u; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/CMakeLists.txt b/2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/main.cpp b/2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/main.cpp new file mode 100644 index 00000000..c887b5bf --- /dev/null +++ b/2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/construct-smallest-number-from-di-string/ +/// Author : liuyubobobo +/// Time : 2022-08-13 + +#include +#include + +using namespace std; + + +/// Backtrck +/// Time Complexity: O(|pattern|!) +/// Space Complexity: O(|pattern|) +class Solution { +public: + string smallestNumber(string pattern) { + + int n = pattern.size(); + string res(n + 1, '0'); + for(int d = 1; d <= 9; d ++){ + res[0] = (char)('0' + d); + if(dfs(pattern, 0, res, 1 << d)) + break; + } + return res; + } + +private: + bool dfs(const string& pattern, int index, string& res, int state){ + + if(index == pattern.size()) return true; + + if(pattern[index] == 'I'){ + for(int d = res[index] - '0' + 1; d <= 9; d ++){ + if((state & (1 << d)) == 0){ + res[index + 1] = (char)('0' + d); + if(dfs(pattern, index + 1, res, state | (1 << d))) return true; + } + } + } + else{ + for(int d = 1; d < res[index] - '0'; d ++){ + if((state & (1 << d)) == 0){ + res[index + 1] = (char)('0' + d); + if(dfs(pattern, index + 1, res, state | (1 << d))) return true; + } + } + } + return false; + } +}; + + +int main() { + + cout << Solution().smallestNumber("IIIDIDDD") << '\n'; + // 123549876 + + cout << Solution().smallestNumber("DDD") << '\n'; + // 4321 + + return 0; +} diff --git a/2001-2500/2376-Count-Special-Integers/cpp-2376/CMakeLists.txt b/2001-2500/2376-Count-Special-Integers/cpp-2376/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2376-Count-Special-Integers/cpp-2376/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2376-Count-Special-Integers/cpp-2376/main.cpp b/2001-2500/2376-Count-Special-Integers/cpp-2376/main.cpp new file mode 100644 index 00000000..31b5297d --- /dev/null +++ b/2001-2500/2376-Count-Special-Integers/cpp-2376/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/count-special-integers/ +/// Author : liuyubobobo +/// Time : 2022-08-13 + +#include +#include + +using namespace std; + + +/// Digital DP +/// Time Complexity: O(|s| * 2^|s|) where s is the string of n +/// Space Complexity: O(|s| * 2^|s|) +class Solution { +public: + int countSpecialNumbers(int n) { + + string s = to_string(n); + + long long res = 0; + for(int len = 1; len <= s.size(); len ++){ + vector>> dp(len, vector>(2, vector(1024, -1))); + res += dfs(len == s.size() ? s : string(len, '9'), 0, false, 0, dp); + } + return res; + } + +private: + long long dfs(const string& s, int index, bool can_up, int state, + vector>>& dp){ + + if(index == s.size()) return 1ll; + if(dp[index][can_up][state] != -1) return dp[index][can_up][state]; + + long long res = 0; + if(can_up){ + for(int d = (index == 0 ? 1 : 0); d <= 9; d ++) + if((state & (1 << d)) == 0) + res += dfs(s, index + 1, can_up, state | (1 << d), dp); + } + else{ + for(int d = (index == 0 ? 1 : 0); d <= s[index] - '0'; d ++) + if((state & (1 << d)) == 0) + res += dfs(s, index + 1, d < s[index] - '0', state | (1 << d), dp); + } + return dp[index][can_up][state] = res; + } +}; + + +int main() { + + cout << Solution().countSpecialNumbers(20) << '\n'; + // 19 + + cout << Solution().countSpecialNumbers(5) << '\n'; + // 5 + + cout << Solution().countSpecialNumbers(135) << '\n'; + // 110 + + return 0; +} diff --git a/readme.md b/readme.md index 720c14e0..61c09224 100644 --- a/readme.md +++ b/readme.md @@ -2230,6 +2230,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2369 | [Check if There is a Valid Partition For The Array](https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/) | [无] | [C++](2001-2500/2369-Check-if-There-is-a-Valid-Partition-For-The-Array/cpp-2369/) | | | | 2370 | [Longest Ideal Subsequence](https://leetcode.com/problems/longest-ideal-subsequence/) | [无] | [C++](2001-2500/2370-Longest-Ideal-Subsequence/cpp-2370/) | | | | | | | | | | +| 2372 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2373 | [Largest Local Values in a Matrix](https://leetcode.com/problems/largest-local-values-in-a-matrix/) | [无] | [C++](2001-2500/2373-Largest-Local-Values-in-a-Matrix/cpp-2373/) | | | +| 2374 | [Node With Highest Edge Score](https://leetcode.com/problems/node-with-highest-edge-score/) | [无] | [C++](2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/) | | | +| 2375 | [Construct Smallest Number From DI String](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | [无] | [C++](2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/) | | | +| 2376 | [Count Special Integers](https://leetcode.com/problems/count-special-integers/) | [无] | [C++](2001-2500/2376-Count-Special-Integers/cpp-2376/) | | | +| | | | | | | ## 力扣中文站比赛 From a6255556f95c8c77fffe652324ee7b79fdd9d7c5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 14 Aug 2022 00:54:13 -0700 Subject: [PATCH 160/390] 0877 new algo added. --- .../0877-Stone-Game/cpp-0877/CMakeLists.txt | 2 +- 0501-1000/0877-Stone-Game/cpp-0877/main5.cpp | 32 ++++++++++++----- 0501-1000/0877-Stone-Game/cpp-0877/main6.cpp | 36 +++++++++++++++++++ 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 0501-1000/0877-Stone-Game/cpp-0877/main6.cpp diff --git a/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt b/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt index 45cd0786..75978cbc 100644 --- a/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt +++ b/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt @@ -3,5 +3,5 @@ project(B) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main4.cpp) +set(SOURCE_FILES main5.cpp) add_executable(B ${SOURCE_FILES}) \ No newline at end of file diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main5.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main5.cpp index 879ce308..58063c21 100644 --- a/0501-1000/0877-Stone-Game/cpp-0877/main5.cpp +++ b/0501-1000/0877-Stone-Game/cpp-0877/main5.cpp @@ -1,6 +1,6 @@ /// Source : https://leetcode.com/problems/stone-game/description/ /// Author : liuyubobobo -/// Time : 2018-08-03 +/// Time : 2022-08-14 #include #include @@ -8,17 +8,31 @@ using namespace std; -/// Mathematic, the answer will always be true! -/// Since the player can technically take all the stones from even-index piles, -/// or take all the stones from odd-index piles -/// One of the two strategy must win:) -/// -/// Time Complexity: O(1) -/// Space Complexity: O(1) +/// Memory Search - No need to have player state +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) class Solution { public: bool stoneGame(vector& piles) { - return true; + + int n = piles.size(); + vector> dp(n, vector(n, INT_MIN)); + + return play(piles, 0, n-1, dp) > 0; + } + +private: + int play(const vector& piles, int l, int r, + vector>& dp){ + + if(l == r) return piles[l]; + + if(dp[l][r] != INT_MIN) + return dp[l][r]; + + int res = max(piles[l] - play(piles, l + 1, r, dp), + piles[r] - play(piles, l, r - 1, dp)); + return dp[l][r] = res; } }; diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main6.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main6.cpp new file mode 100644 index 00000000..879ce308 --- /dev/null +++ b/0501-1000/0877-Stone-Game/cpp-0877/main6.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/stone-game/description/ +/// Author : liuyubobobo +/// Time : 2018-08-03 + +#include +#include + +using namespace std; + + +/// Mathematic, the answer will always be true! +/// Since the player can technically take all the stones from even-index piles, +/// or take all the stones from odd-index piles +/// One of the two strategy must win:) +/// +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + bool stoneGame(vector& piles) { + return true; + } +}; + + +void print_bool(bool res){ + cout << (res ? "True" : "False") << endl; +} + +int main() { + + vector piles1 = {5, 3, 4, 5}; + print_bool(Solution().stoneGame(piles1)); + + return 0; +} \ No newline at end of file From 633e8f2e2fcf67262ef791c722ce8bfc6f64d0af Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 14 Aug 2022 10:31:51 -0700 Subject: [PATCH 161/390] 0877 bug fixed. --- .../0877-Stone-Game/cpp-0877/CMakeLists.txt | 2 +- 0501-1000/0877-Stone-Game/cpp-0877/main.cpp | 40 ++++++--------- 0501-1000/0877-Stone-Game/cpp-0877/main2.cpp | 43 ++++++++++------ 0501-1000/0877-Stone-Game/cpp-0877/main3.cpp | 30 +++-------- 0501-1000/0877-Stone-Game/cpp-0877/main4.cpp | 46 ----------------- 0501-1000/0877-Stone-Game/cpp-0877/main5.cpp | 50 ------------------- 0501-1000/0877-Stone-Game/cpp-0877/main6.cpp | 36 ------------- 7 files changed, 51 insertions(+), 196 deletions(-) delete mode 100644 0501-1000/0877-Stone-Game/cpp-0877/main4.cpp delete mode 100644 0501-1000/0877-Stone-Game/cpp-0877/main5.cpp delete mode 100644 0501-1000/0877-Stone-Game/cpp-0877/main6.cpp diff --git a/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt b/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt index 75978cbc..0d12d141 100644 --- a/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt +++ b/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt @@ -3,5 +3,5 @@ project(B) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main5.cpp) +set(SOURCE_FILES main3.cpp) add_executable(B ${SOURCE_FILES}) \ No newline at end of file diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main.cpp index dd5b8ea4..734b400f 100644 --- a/0501-1000/0877-Stone-Game/cpp-0877/main.cpp +++ b/0501-1000/0877-Stone-Game/cpp-0877/main.cpp @@ -1,6 +1,6 @@ /// Source : https://leetcode.com/problems/stone-game/description/ /// Author : liuyubobobo -/// Time : 2018-07-28 +/// Time : 2022-08-14 #include #include @@ -8,7 +8,7 @@ using namespace std; -/// Memory Search +/// Memoization /// Time Complexity: O(n^2) /// Space Complexity: O(n^2) class Solution { @@ -16,33 +16,23 @@ class Solution { bool stoneGame(vector& piles) { int n = piles.size(); - vector>> dp(2, vector>(n, vector(n, INT_MIN))); + vector> dp(n, vector(n, INT_MIN)); - return play(0, piles, 0, n-1, dp) > 0; + return play(piles, 0, n-1, dp) > 0; } private: - int play(int player, const vector& piles, int l, int r, - vector>>& dp){ - - if(l == r){ - if(player == 0) - return dp[player][l][r] = piles[l]; - else - return dp[player][l][r] = -piles[l]; - } - - if(dp[player][l][r] != INT_MIN) - return dp[player][l][r]; - - int res = 0; - if(player == 0) - res = max(piles[l] + play(1 - player, piles, l + 1, r, dp), - piles[r] + play(1 - player, piles, l, r - 1, dp)); - else - res = max(-piles[l] + play(1 - player, piles, l + 1, r, dp), - -piles[r] + play(1 - player, piles, l, r - 1, dp)); - return dp[player][l][r] = res; + int play(const vector& piles, int l, int r, + vector>& dp){ + + if(l == r) return piles[l]; + + if(dp[l][r] != INT_MIN) + return dp[l][r]; + + int res = max(piles[l] - play(piles, l + 1, r, dp), + piles[r] - play(piles, l, r - 1, dp)); + return dp[l][r] = res; } }; diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main2.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main2.cpp index 029d42da..95a111ba 100644 --- a/0501-1000/0877-Stone-Game/cpp-0877/main2.cpp +++ b/0501-1000/0877-Stone-Game/cpp-0877/main2.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/stone-game/description/ /// Author : liuyubobobo -/// Time : 2018-08-02 +/// Time : 2018-07-28 +/// Updated: 2022-08-14 #include #include @@ -8,7 +9,8 @@ using namespace std; -/// Dynamic Programming +/// Memoization - using player as a state +/// The code and logic is more complex(, but might be educational) /// Time Complexity: O(n^2) /// Space Complexity: O(n^2) class Solution { @@ -16,22 +18,33 @@ class Solution { bool stoneGame(vector& piles) { int n = piles.size(); - vector>> dp(2, vector>(n, vector(n, -1))); + vector>> dp(2, vector>(n, vector(n, INT_MIN))); - for(int i = 0 ; i < n ; i ++){ - dp[0][i][i] = piles[i]; - dp[1][i][i] = -piles[i]; - } + return play(0, piles, 0, n-1, dp) > 0; + } - for(int sz = 2 ; sz <= n ; sz ++) - for(int i = 0; i + sz - 1 < n ; i ++){ - dp[0][i][i + sz - 1] = max(piles[i] + dp[1][i + 1][i + sz - 1], - piles[i + sz - 1] + dp[1][i][i + sz - 2]); - dp[1][i][i + sz - 1] = max(-piles[i] + dp[0][i + 1][i + sz - 1], - -piles[i + sz - 1] + dp[0][i][i + sz - 2]); - } +private: + int play(int player, const vector& piles, int l, int r, + vector>>& dp){ + + if(l == r){ + if(player == 0) + return dp[player][l][r] = piles[l]; + else + return dp[player][l][r] = -piles[l]; + } - return dp[0][0][n - 1]; + if(dp[player][l][r] != INT_MIN) + return dp[player][l][r]; + + int res = 0; + if(player == 0) + res = max(piles[l] + play(1 - player, piles, l + 1, r, dp), + piles[r] + play(1 - player, piles, l, r - 1, dp)); + else + res = min(-piles[l] + play(1 - player, piles, l + 1, r, dp), + -piles[r] + play(1 - player, piles, l, r - 1, dp)); + return dp[player][l][r] = res; } }; diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main3.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main3.cpp index ca3fba47..879ce308 100644 --- a/0501-1000/0877-Stone-Game/cpp-0877/main3.cpp +++ b/0501-1000/0877-Stone-Game/cpp-0877/main3.cpp @@ -8,33 +8,17 @@ using namespace std; -/// Memory Search -/// Just use 2d dp array and consider two moves by each player together:) +/// Mathematic, the answer will always be true! +/// Since the player can technically take all the stones from even-index piles, +/// or take all the stones from odd-index piles +/// One of the two strategy must win:) /// -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) +/// Time Complexity: O(1) +/// Space Complexity: O(1) class Solution { public: bool stoneGame(vector& piles) { - - int n = piles.size(); - vector> dp(n, vector(n, INT_MIN)); - - return play(piles, 0, n-1, dp) > 0; - } - -private: - int play(const vector& piles, int l, int r, vector>& dp){ - - if(l + 1 == r) - return abs(piles[l] - piles[r]); - - if(dp[l][r] != INT_MIN) - return dp[l][r]; - - return dp[l][r] = max( - abs(piles[l] - piles[l + 1]) + play(piles, l + 2, r, dp), - abs(piles[l] - piles[r]) + play(piles, l + 1, r - 1, dp)); + return true; } }; diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main4.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main4.cpp deleted file mode 100644 index f7cc9f63..00000000 --- a/0501-1000/0877-Stone-Game/cpp-0877/main4.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/// Source : https://leetcode.com/problems/stone-game/description/ -/// Author : liuyubobobo -/// Time : 2018-08-03 - -#include -#include - -using namespace std; - - -/// Dynamic Programming -/// Just use 2d dp array and consider two moves by each player together:) -/// -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) -class Solution { -public: - bool stoneGame(vector& piles) { - - int n = piles.size(); - vector> dp(n, vector(n, INT_MIN)); - - for(int i = 0; i + 1 < n; i ++ ) - dp[i][i + 1] = abs(piles[i] - piles[i + 1]); - - for(int sz = 4; sz <= n; sz ++) - for(int i = 0; i + sz - 1 < n; i ++) - dp[i][i + sz - 1] = max( - abs(piles[i] - piles[i + 1]) + dp[i + 2][i + 3], - abs(piles[i] - piles[i + sz - 1]) + dp[i + 1][i + sz - 2]); - return dp[0][n - 1]; - } -}; - - -void print_bool(bool res){ - cout << (res ? "True" : "False") << endl; -} - -int main() { - - vector piles1 = {5, 3, 4, 5}; - print_bool(Solution().stoneGame(piles1)); - - return 0; -} \ No newline at end of file diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main5.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main5.cpp deleted file mode 100644 index 58063c21..00000000 --- a/0501-1000/0877-Stone-Game/cpp-0877/main5.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/stone-game/description/ -/// Author : liuyubobobo -/// Time : 2022-08-14 - -#include -#include - -using namespace std; - - -/// Memory Search - No need to have player state -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) -class Solution { -public: - bool stoneGame(vector& piles) { - - int n = piles.size(); - vector> dp(n, vector(n, INT_MIN)); - - return play(piles, 0, n-1, dp) > 0; - } - -private: - int play(const vector& piles, int l, int r, - vector>& dp){ - - if(l == r) return piles[l]; - - if(dp[l][r] != INT_MIN) - return dp[l][r]; - - int res = max(piles[l] - play(piles, l + 1, r, dp), - piles[r] - play(piles, l, r - 1, dp)); - return dp[l][r] = res; - } -}; - - -void print_bool(bool res){ - cout << (res ? "True" : "False") << endl; -} - -int main() { - - vector piles1 = {5, 3, 4, 5}; - print_bool(Solution().stoneGame(piles1)); - - return 0; -} \ No newline at end of file diff --git a/0501-1000/0877-Stone-Game/cpp-0877/main6.cpp b/0501-1000/0877-Stone-Game/cpp-0877/main6.cpp deleted file mode 100644 index 879ce308..00000000 --- a/0501-1000/0877-Stone-Game/cpp-0877/main6.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/// Source : https://leetcode.com/problems/stone-game/description/ -/// Author : liuyubobobo -/// Time : 2018-08-03 - -#include -#include - -using namespace std; - - -/// Mathematic, the answer will always be true! -/// Since the player can technically take all the stones from even-index piles, -/// or take all the stones from odd-index piles -/// One of the two strategy must win:) -/// -/// Time Complexity: O(1) -/// Space Complexity: O(1) -class Solution { -public: - bool stoneGame(vector& piles) { - return true; - } -}; - - -void print_bool(bool res){ - cout << (res ? "True" : "False") << endl; -} - -int main() { - - vector piles1 = {5, 3, 4, 5}; - print_bool(Solution().stoneGame(piles1)); - - return 0; -} \ No newline at end of file From 3398d79c269eec0185fd29d91dcb44e20652dfa4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 19 Aug 2022 12:08:52 -0700 Subject: [PATCH 162/390] 2378 solved. --- .../cpp-2378/CMakeLists.txt | 6 ++ .../cpp-2378/main.cpp | 63 +++++++++++++++++++ readme.md | 2 + 3 files changed, 71 insertions(+) create mode 100644 2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/CMakeLists.txt create mode 100644 2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/main.cpp diff --git a/2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/CMakeLists.txt b/2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/CMakeLists.txt new file mode 100644 index 00000000..485fca8f --- /dev/null +++ b/2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2378) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2378 main.cpp) diff --git a/2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/main.cpp b/2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/main.cpp new file mode 100644 index 00000000..de88f1c6 --- /dev/null +++ b/2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/choose-edges-to-maximize-score-in-a-tree/ +/// Author : liuyubobobo +/// Time : 2022-08-19 + +#include +#include +#include +#include + +using namespace std; + + +/// Tree DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long maxScore(vector>& edges) { + + int n = edges.size(); + vector>> tree(n); + + for(int i = 1; i < n; i ++){ + int p = edges[i][0]; + long long w = edges[i][1]; + tree[p].push_back({i, w}); + } + + vector> dp(2, vector(n, -1)); + return dfs(tree, 1, 0, dp); + } + +private: + long long dfs(const vector>>& tree, + int can_choose, int u, vector>& dp){ + + if(dp[can_choose][u] != -1) return dp[can_choose][u]; + + long long sum = 0; + for (const pair &p: tree[u]) + sum += dfs(tree, 1, p.first, dp); + + if(can_choose == 0) { + return dp[can_choose][u] = sum; + } + + long long res = sum; + for(const pair& p: tree[u]){ + long long tres = sum; + tres += p.second; + tres -= dfs(tree, 1, p.first, dp); + tres += dfs(tree, 0, p.first, dp); + res = max(res, tres); + } + return dp[can_choose][u] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 61c09224..9916c994 100644 --- a/readme.md +++ b/readme.md @@ -2235,6 +2235,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2374 | [Node With Highest Edge Score](https://leetcode.com/problems/node-with-highest-edge-score/) | [无] | [C++](2001-2500/2374-Node-With-Highest-Edge-Score/cpp-2374/) | | | | 2375 | [Construct Smallest Number From DI String](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | [无] | [C++](2001-2500/2375-Construct-Smallest-Number-From-DI-String/cpp-2375/) | | | | 2376 | [Count Special Integers](https://leetcode.com/problems/count-special-integers/) | [无] | [C++](2001-2500/2376-Count-Special-Integers/cpp-2376/) | | | +| 2377 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2378 | [Choose Edges to Maximize Score in a Tree](https://leetcode.com/problems/choose-edges-to-maximize-score-in-a-tree/) | [无] | [C++](2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/) | | | | | | | | | | ## 力扣中文站比赛 From 2257aeb26f94d1bc1dd69f56cb129c9b473df45a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 19 Aug 2022 12:16:46 -0700 Subject: [PATCH 163/390] 0654 solved. --- .../cpp-0654/CMakeLists.txt | 6 +++ .../cpp-0654/main.cpp | 50 +++++++++++++++++++ readme.md | 3 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0654-Maximum-Binary-Tree/cpp-0654/CMakeLists.txt create mode 100644 0501-1000/0654-Maximum-Binary-Tree/cpp-0654/main.cpp diff --git a/0501-1000/0654-Maximum-Binary-Tree/cpp-0654/CMakeLists.txt b/0501-1000/0654-Maximum-Binary-Tree/cpp-0654/CMakeLists.txt new file mode 100644 index 00000000..c30d7ed3 --- /dev/null +++ b/0501-1000/0654-Maximum-Binary-Tree/cpp-0654/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0654) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0654 main.cpp) diff --git a/0501-1000/0654-Maximum-Binary-Tree/cpp-0654/main.cpp b/0501-1000/0654-Maximum-Binary-Tree/cpp-0654/main.cpp new file mode 100644 index 00000000..bc6d84b7 --- /dev/null +++ b/0501-1000/0654-Maximum-Binary-Tree/cpp-0654/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/maximum-binary-tree/ +/// Author : liuyubobobo +/// Time : 2022-08-19 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + TreeNode* constructMaximumBinaryTree(vector& nums) { + + int n = nums.size(); + return construct(nums, 0, n - 1); + } + +private: + TreeNode* construct(const vector& nums, int l, int r){ + + if(l > r) return nullptr; + + int max_index = max_element(nums.begin() + l, nums.begin() + (r + 1)) - nums.begin(); + TreeNode* ret = new TreeNode(nums[max_index]); + ret->left = construct(nums, l, max_index - 1); + ret->right = construct(nums, max_index + 1, r); + return ret; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9916c994..1f95b575 100644 --- a/readme.md +++ b/readme.md @@ -666,7 +666,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 650 | [2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/) | [solution](https://leetcode.com/problems/2-keys-keyboard/solution/) | [C++](0501-1000/0650-2-Keys-Keyboard/cpp-0650/) | | | | | | | | | | | 652 | [Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/description/) | [solution](https://leetcode.com/problems/find-duplicate-subtrees/solution/) | [C++](0501-1000/0652-Find-Duplicate-Subtrees/cpp-0652/) | | | -| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [C++](0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/) | | | | +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [无] | [C++](0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/) | | | +| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [solution](https://leetcode.com/problems/maximum-binary-tree/solution/) | [C++](0501-1000/0654-Maximum-Binary-Tree/cpp-0654/) | | | | | | | | | | | 658 | [Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) | [solution](https://leetcode.com/problems/find-k-closest-elements/solution/)
[缺:二分] | [C++](0501-1000/0658-Find-K-Closest-Elements/cpp-0658/) | | | | 659 | [Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [solution](https://leetcode.com/problems/split-array-into-consecutive-subsequences/solution/) | [C++](0501-1000/0659-Split-Array-into-Consecutive-Subsequences/cpp-0659/) | | | From 0b77ba30e054a7646df1d2f41d7bb05e2948c057 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 19 Aug 2022 14:53:04 -0700 Subject: [PATCH 164/390] 0417 new algo added. --- .../cpp-0417/CMakeLists.txt | 2 +- .../cpp-0417/main2.cpp | 165 ++++++++++++++++++ 2 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main2.cpp diff --git a/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt index 055fb76d..c5dc18c1 100644 --- a/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt +++ b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0417) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_0417 main.cpp) +add_executable(cpp_0417 main2.cpp) diff --git a/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main2.cpp b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main2.cpp new file mode 100644 index 00000000..bc574188 --- /dev/null +++ b/0001-0500/0417-Pacific-Atlantic-Water-Flow/cpp-0417/main2.cpp @@ -0,0 +1,165 @@ +/// Source : https://leetcode.com/problems/pacific-atlantic-water-flow/ +/// Author : liuyubobobo +/// Time : 2022-08-19 + +#include +#include +#include + +using namespace std; + + +/// DFS from inside to outside +/// Shrink vertex to remove all the cycles from the underlying search graph +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { + +private: + const int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + int R, C; + +public: + vector> pacificAtlantic(vector>& heights) { + + R = heights.size(), C = heights[0].size(); + + // uf 有 R * C 个节点。 + UF uf(R * C); + vector> visited(R, vector(C, false)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + // 缩点 + if(!visited[i][j]){ + vector cc; + dfs_cc(heights, i, j, visited, cc); + for(int i = 1; i < cc.size(); i ++) + uf.union_elements(cc[0], cc[i]); + } + } + + // 根据缩好的点,创建新图,这张图是 DAG + // 注意,这张图有 R * C + 2 个节点,索引 R * C 对应 pacific,索引 R * C + 1 对应 atlantic + vector> g(R * C + 2); + for(int x = 0; x < R; x ++) + for(int y = 0; y < C; y ++){ + + // u 是 [x, y] 缩点后对应的点编号 + int u = uf.find(x * C + y); + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(!in_area(nx, ny)) continue; + + // v 是 [nx, ny] 缩点后对应的点编号 + int v = uf.find(nx * C + ny); + if(u == v) continue; + + // 注意,此时 height 的比较,可以扔掉 = 了, + // 因为 u 和 v 不是缩点后的一个节点,所以高度肯定不等 + if(heights[x][y] > heights[nx][ny]) g[u].insert(v); + } + + if(x == 0 || y == 0) g[u].insert(R * C); + if(x == R - 1 || y == C - 1) g[u].insert(R * C + 1); + } + + // 现在,可以在 g 这张新图上,做记忆化搜索了。 + // 因为这张图上边的方向已经代表了从高的地方走向低的地方,所以不需要再使用 height 数组了。 + // 但是,因为建图的方式,我们的搜索是从高向地进行的。 + vector pacific(R * C + 2, -1), atlantic(R * C + 2, -1); + for(int u = 0; u < R * C; u ++){ + if(pacific[u] == -1) dfs(g, u, pacific, atlantic); + } + + vector> res; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + int u = uf.find(i * C + j); + if(pacific[u] == 1 && atlantic[u] == 1) + res.push_back({i, j}); + } + return res; + } + +private: + void dfs_cc(const vector>& H, int x, int y, + vector>& visited, vector& cc){ + + visited[x][y] = true; + cc.push_back(x * C + y); + + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny) && !visited[nx][ny] && H[x][y] == H[nx][ny]) + dfs_cc(H, nx, ny, visited, cc); + } + } + + void dfs(const vector>& g, int u, + vector& pacific, vector& atlantic){ + + if(u == R * C) pacific[u] = 1; + if(u == R * C + 1) atlantic[u] = 1; + + if(pacific[u] != -1) return; + + for(int v: g[u]){ + dfs(g, v, pacific, atlantic); + if(pacific[v] == 1) pacific[u] = 1; + if(atlantic[v] == 1) atlantic[u] = 1; + } + + if(pacific[u] != 1) pacific[u] = 0; + if(atlantic[u] != 1) atlantic[u] = 0; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> heights = { + {1,2,2,3,5}, + {3,2,3,4,4}, + {2,4,5,3,1}, + {6,7,1,4,5}, + {5,1,1,2,4} + }; + Solution().pacificAtlantic(heights); + + return 0; +} From 28cde481d41a0b38e32a4cd77f434be7fcfb5e4f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 20 Aug 2022 16:03:29 -0700 Subject: [PATCH 165/390] 2379-2382 solved. --- .../cpp-2379/CMakeLists.txt | 6 + .../cpp-2379/main.cpp | 36 ++++++ .../cpp-2379/main2.cpp | 37 ++++++ .../cpp-2380/CMakeLists.txt | 6 + .../cpp-2380/main.cpp | 43 ++++++ .../cpp-2381/CMakeLists.txt | 6 + .../cpp-2381/main.cpp | 46 +++++++ .../cpp-2382/CMakeLists.txt | 6 + .../cpp-2382/main.cpp | 122 ++++++++++++++++++ .../cpp-2382/main2.cpp | 113 ++++++++++++++++ readme.md | 4 + 11 files changed, 425 insertions(+) create mode 100644 2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/CMakeLists.txt create mode 100644 2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main.cpp create mode 100644 2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main2.cpp create mode 100644 2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/CMakeLists.txt create mode 100644 2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/main.cpp create mode 100644 2001-2500/2381-Shifting-Letters-II/cpp-2381/CMakeLists.txt create mode 100644 2001-2500/2381-Shifting-Letters-II/cpp-2381/main.cpp create mode 100644 2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/CMakeLists.txt create mode 100644 2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main.cpp create mode 100644 2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main2.cpp diff --git a/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/CMakeLists.txt b/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/CMakeLists.txt new file mode 100644 index 00000000..4741167f --- /dev/null +++ b/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main2.cpp) diff --git a/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main.cpp b/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main.cpp new file mode 100644 index 00000000..ac8165ff --- /dev/null +++ b/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumRecolors(string blocks, int k) { + + int n = blocks.size(); + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + (blocks[i] == 'B'); + + int res = INT_MAX; + for(int start = 0; start + k <= n; start ++){ + int b = presum[start + k] - presum[start]; + res = min(res, k - b); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main2.cpp b/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main2.cpp new file mode 100644 index 00000000..ade1d695 --- /dev/null +++ b/2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/main2.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumRecolors(string blocks, int k) { + + int n = blocks.size(); + int b = 0; + for(int i = 0; i + 1 < k; i ++) b += blocks[i] == 'B'; + + int res = INT_MAX; + for(int i = k - 1; i < n; i ++){ + b += blocks[i] == 'B'; + res = min(res, k - b); + b -= blocks[i - (k - 1)] == 'B'; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/CMakeLists.txt b/2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/main.cpp b/2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/main.cpp new file mode 100644 index 00000000..74d9f61e --- /dev/null +++ b/2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int secondsToRemoveOccurrences(string s) { + + int n = s.size(), res = 0; + while(true){ + + bool changed = false; + for(int i = 0; i + 1 < n; i ++) + if(s[i] == '0' && s[i + 1] == '1'){ + s[i] = '1', s[i + 1] = '0'; + i ++; + changed = true; + } + + if(!changed) break; + res ++; + } + return res; + } +}; + + +int main() { + + cout << Solution().secondsToRemoveOccurrences("0110101") << '\n'; + // 4 + + return 0; +} diff --git a/2001-2500/2381-Shifting-Letters-II/cpp-2381/CMakeLists.txt b/2001-2500/2381-Shifting-Letters-II/cpp-2381/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2381-Shifting-Letters-II/cpp-2381/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2381-Shifting-Letters-II/cpp-2381/main.cpp b/2001-2500/2381-Shifting-Letters-II/cpp-2381/main.cpp new file mode 100644 index 00000000..0d0c2d11 --- /dev/null +++ b/2001-2500/2381-Shifting-Letters-II/cpp-2381/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/shifting-letters-ii/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include + +using namespace std; + + +/// Diff Array +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string shiftingLetters(string s, vector>& shifts) { + + int n = s.size(); + vector diff_array(n + 1, 0); + for(const vector& shift: shifts){ + int start = shift[0], end = shift[1], d = shift[2]; + if(d == 0) d = -1; + diff_array[start] += d, diff_array[end + 1] -= d; + } + + string res = ""; + int cur = 0; + for(int i = 0; i < n; i ++){ + cur += diff_array[i]; + + int d = cur; + if(d < 0) d = d % 26 + 26; + else d %= 26; + + int c = (s[i] - 'a' + d) % 26; + res += (char)('a' + c); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/CMakeLists.txt b/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/CMakeLists.txt new file mode 100644 index 00000000..541b2eff --- /dev/null +++ b/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) diff --git a/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main.cpp b/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main.cpp new file mode 100644 index 00000000..06e1b3ff --- /dev/null +++ b/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main.cpp @@ -0,0 +1,122 @@ +/// Source : https://leetcode.com/problems/maximum-segment-sum-after-removals/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include +#include +#include + +using namespace std; + + +/// UF +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + vector maximumSegmentSum(vector& nums, vector& removeQueries) { + + int n = nums.size(); + + vector v(n); + for(int i = 0; i < n; i ++) v[i] = nums[i]; + + for(int index: removeQueries) v[index] = 0; + + UF uf(n); + map sum; + multiset sum_set; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || get_type(v[i]) != get_type(v[start])){ + if(v[start] != 0){ + long long seg_sum = 0; + for(int j = start; j < i; j ++){ + seg_sum += v[j]; + uf.union_elements(start, j); + } + sum[uf.find(start)] = seg_sum; + sum_set.insert(seg_sum); + } + start = i; + } + + vector res(n); + res[n - 1] = (sum_set.empty() ? 0 : *sum_set.rbegin()); + for(int i = n - 1; i > 0; i --){ + int index = removeQueries[i]; + long long new_seg_sum = nums[index]; + if(index - 1 >= 0 && v[index - 1] != 0){ + long long left_sum = sum[uf.find(index - 1)]; + new_seg_sum += left_sum; + sum.erase(uf.find(index - 1)); + sum_set.erase(sum_set.find(left_sum)); + uf.union_elements(index, index - 1); + } + if(index + 1 < n && v[index + 1] != 0){ + long long right_sum = sum[uf.find(index + 1)]; + new_seg_sum += right_sum; + sum.erase(uf.find(index + 1)); + sum_set.erase(sum_set.find(right_sum)); + uf.union_elements(index, index + 1); + } + + sum[uf.find(index)] = new_seg_sum; + sum_set.insert(new_seg_sum); + res[i - 1] = (sum_set.empty() ? 0 : *sum_set.rbegin()); + v[index] = nums[index]; + } + return res; + } + +private: + bool get_type(long long x){ + return x != 0; + } +}; + + +void print_vec(const vector& v){ + for(long long e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector nums1 = {1, 2, 5, 6, 1}; + vector removeQueries1 = {0, 3, 2, 4, 1}; + print_vec(Solution().maximumSegmentSum(nums1, removeQueries1)); + // 14 7 2 2 0 + + return 0; +} diff --git a/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main2.cpp b/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main2.cpp new file mode 100644 index 00000000..ed3dea5b --- /dev/null +++ b/2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/main2.cpp @@ -0,0 +1,113 @@ +/// Source : https://leetcode.com/problems/maximum-segment-sum-after-removals/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include + +using namespace std; + + +/// Same idea with main.cpp, but has some optimization +/// Such as declare sum as a vector instead of map +/// ans no need to main sum_set but just just max_sum to keep the best result +/// +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + vector maximumSegmentSum(vector& nums, vector& removeQueries) { + + int n = nums.size(); + + vector v(n); + for(int i = 0; i < n; i ++) v[i] = nums[i]; + + for(int index: removeQueries) v[index] = 0; + + UF uf(n); + vector sum(n, 0); + long long max_sum = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || (v[i] != 0) != (v[start] != 0)){ + if(v[start] != 0){ + long long seg_sum = 0; + for(int j = start; j < i; j ++){ + seg_sum += v[j]; + uf.union_elements(start, j); + } + sum[uf.find(start)] = seg_sum; + max_sum = max(max_sum, seg_sum); + } + start = i; + } + + vector res(n, max_sum); + for(int i = n - 1; i > 0; i --){ + int index = removeQueries[i]; + long long new_seg_sum = nums[index]; + if(index - 1 >= 0 && v[index - 1] != 0){ + long long left_sum = sum[uf.find(index - 1)]; + new_seg_sum += left_sum; + uf.union_elements(index, index - 1); + } + if(index + 1 < n && v[index + 1] != 0){ + long long right_sum = sum[uf.find(index + 1)]; + new_seg_sum += right_sum; + uf.union_elements(index, index + 1); + } + + sum[uf.find(index)] = new_seg_sum; + max_sum = max(max_sum, new_seg_sum); + res[i - 1] = max_sum; + v[index] = nums[index]; + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(long long e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector nums1 = {1, 2, 5, 6, 1}; + vector removeQueries1 = {0, 3, 2, 4, 1}; + print_vec(Solution().maximumSegmentSum(nums1, removeQueries1)); + // 14 7 2 2 0 + + return 0; +} diff --git a/readme.md b/readme.md index 1f95b575..95b7c69b 100644 --- a/readme.md +++ b/readme.md @@ -2238,6 +2238,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2376 | [Count Special Integers](https://leetcode.com/problems/count-special-integers/) | [无] | [C++](2001-2500/2376-Count-Special-Integers/cpp-2376/) | | | | 2377 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2378 | [Choose Edges to Maximize Score in a Tree](https://leetcode.com/problems/choose-edges-to-maximize-score-in-a-tree/) | [无] | [C++](2001-2500/2378-Choose-Edges-to-Maximize-Score-in-a-Tree/cpp-2378/) | | | +| 2379 | [Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/) | [无] | [C++](2001-2500/2379-Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/cpp-2379/) | | | +| 2380 | [Time Needed to Rearrange a Binary String](https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/) | [无] | [C++](2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/) | | | +| 2381 | [Shifting Letters II](https://leetcode.com/problems/shifting-letters-ii/) | [无] | [C++](2001-2500/2381-Shifting-Letters-II/cpp-2381/) | | | +| 2382 | [Maximum Segment Sum After Removals](https://leetcode.com/problems/maximum-segment-sum-after-removals/) | [无] | [C++](2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/) | | | | | | | | | | ## 力扣中文站比赛 From cda60f7ac203370d851c4273bdde4ffa156a6222 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 21 Aug 2022 22:17:47 -0700 Subject: [PATCH 166/390] 0655 solved. --- .../cpp-0655/CMakeLists.txt | 6 ++ .../0655-Print-Binary-Tree/cpp-0655/main.cpp | 63 +++++++++++++++++++ readme.md | 1 + 3 files changed, 70 insertions(+) create mode 100644 0501-1000/0655-Print-Binary-Tree/cpp-0655/CMakeLists.txt create mode 100644 0501-1000/0655-Print-Binary-Tree/cpp-0655/main.cpp diff --git a/0501-1000/0655-Print-Binary-Tree/cpp-0655/CMakeLists.txt b/0501-1000/0655-Print-Binary-Tree/cpp-0655/CMakeLists.txt new file mode 100644 index 00000000..91a702f6 --- /dev/null +++ b/0501-1000/0655-Print-Binary-Tree/cpp-0655/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0655) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0655 main.cpp) diff --git a/0501-1000/0655-Print-Binary-Tree/cpp-0655/main.cpp b/0501-1000/0655-Print-Binary-Tree/cpp-0655/main.cpp new file mode 100644 index 00000000..1af87ef6 --- /dev/null +++ b/0501-1000/0655-Print-Binary-Tree/cpp-0655/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/print-binary-tree/ +/// Author : liuyubobobo +/// Time : 2022-08-21 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(h) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector> printTree(TreeNode* root) { + + int d = dfs_depth(root); + + vector> res(d, vector((1 << d) - 1, "")); + dfs(root, 0, 0, (1 << d) - 1, res); + return res; + } + +private: + void dfs(TreeNode* node, int d, int l, int r, vector>& res){ + + if(!node) return; + + int mid = l + (r - l) / 2; + res[d][mid] = to_string(node->val); + + dfs(node->left, d + 1, l, mid - 1, res); + dfs(node->right, d + 1, mid + 1, r, res); + } + + int dfs_depth(TreeNode* node){ + + if(!node) return 0; + + int res = 0; + res = max(res, dfs_depth(node->left)); + res = max(res, dfs_depth(node->right)); + return res + 1; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 95b7c69b..9c62a9a1 100644 --- a/readme.md +++ b/readme.md @@ -668,6 +668,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 652 | [Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/description/) | [solution](https://leetcode.com/problems/find-duplicate-subtrees/solution/) | [C++](0501-1000/0652-Find-Duplicate-Subtrees/cpp-0652/) | | | | 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [无] | [C++](0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/) | | | | 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [solution](https://leetcode.com/problems/maximum-binary-tree/solution/) | [C++](0501-1000/0654-Maximum-Binary-Tree/cpp-0654/) | | | +| 655 | |[Print Binary Tree](https://leetcode.com/problems/print-binary-tree/) | [无] | [C++](0501-1000/0655-Print-Binary-Tree/cpp-0655/) | | | | | | | | | | 658 | [Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) | [solution](https://leetcode.com/problems/find-k-closest-elements/solution/)
[缺:二分] | [C++](0501-1000/0658-Find-K-Closest-Elements/cpp-0658/) | | | | 659 | [Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [solution](https://leetcode.com/problems/split-array-into-consecutive-subsequences/solution/) | [C++](0501-1000/0659-Split-Array-into-Consecutive-Subsequences/cpp-0659/) | | | From 783164d192c24af825816590b9323c2b67017932 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 22 Aug 2022 12:13:50 -0700 Subject: [PATCH 167/390] 2383-2385 solved. --- .../cpp-2383/CMakeLists.txt | 6 ++ .../cpp-2383/main.cpp | 47 +++++++++++ .../cpp-2384/CMakeLists.txt | 6 ++ .../cpp-2384/main.cpp | 65 +++++++++++++++ .../cpp-2385/CMakeLists.txt | 6 ++ .../cpp-2385/main.cpp | 82 +++++++++++++++++++ readme.md | 3 + 7 files changed, 215 insertions(+) create mode 100644 2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/CMakeLists.txt create mode 100644 2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/main.cpp create mode 100644 2001-2500/2384-Largest-Palindromic-Number/cpp-2384/CMakeLists.txt create mode 100644 2001-2500/2384-Largest-Palindromic-Number/cpp-2384/main.cpp create mode 100644 2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/CMakeLists.txt create mode 100644 2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/main.cpp diff --git a/2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/CMakeLists.txt b/2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/main.cpp b/2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/main.cpp new file mode 100644 index 00000000..95fd346f --- /dev/null +++ b/2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minNumberOfHours(int initialEnergy, int initialExperience, vector& energy, vector& experience) { + + int need_energy = 0, need_exp = 0, cur_energy = initialEnergy, cur_exp = initialExperience; + + int n = energy.size(); + for(int i = 0; i < n; i ++){ + + if(cur_energy <= energy[i]){ + need_energy += energy[i] + 1 - cur_energy; + cur_energy = energy[i] + 1; + } + if(cur_exp <= experience[i]){ + need_exp += experience[i] + 1 - cur_exp; + cur_exp = experience[i] + 1; + } + + cur_exp += experience[i]; + cur_energy -= energy[i]; + } + return need_exp + need_energy; + } +}; + + +int main() { + + vector energy1 = {1, 4, 3, 2}, experience1 = {2, 6, 3, 1}; + cout << Solution().minNumberOfHours(5, 3, energy1, experience1) << '\n'; + // 8 + + return 0; +} diff --git a/2001-2500/2384-Largest-Palindromic-Number/cpp-2384/CMakeLists.txt b/2001-2500/2384-Largest-Palindromic-Number/cpp-2384/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2384-Largest-Palindromic-Number/cpp-2384/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2384-Largest-Palindromic-Number/cpp-2384/main.cpp b/2001-2500/2384-Largest-Palindromic-Number/cpp-2384/main.cpp new file mode 100644 index 00000000..a0083dc0 --- /dev/null +++ b/2001-2500/2384-Largest-Palindromic-Number/cpp-2384/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.com/problems/largest-palindromic-number/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(|num|) +/// Space Complexity: O(1) +class Solution { +public: + string largestPalindromic(string num) { + + vector f(10, 0); + for(char c: num) f[c - '0'] ++; + + if(f[0] == num.size()) return "0"; + + map> even, odd; + for(int i = 0; i < 10; i ++) + if(f[i]){ + if(f[i] % 2 == 0) even[i] = f[i]; + else{ + odd[i] = 1; + if(f[i] - 1) even[i] = f[i]; + } + } + + string pre = ""; + for(const pair& p: even){ + if(pre == "" && p.first == 0) continue; + pre += string(p.second / 2, '0' + p.first); + } + + string suf = pre; + reverse(suf.begin(), suf.end()); + + if(!odd.empty()) pre += string(1, '0' + odd.begin()->first); + pre += suf; + return pre; + } +}; + + +int main() { + + cout << Solution().largestPalindromic("444947137") << '\n'; + // 7449447 + + cout << Solution().largestPalindromic("00009") << '\n'; + // 9 + + cout << Solution().largestPalindromic("0000") << '\n'; + // 0 + + cout << Solution().largestPalindromic("012303") << '\n'; + // 30203 + + return 0; +} diff --git a/2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/CMakeLists.txt b/2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/main.cpp b/2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/main.cpp new file mode 100644 index 00000000..5744407c --- /dev/null +++ b/2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/main.cpp @@ -0,0 +1,82 @@ +/// Source : https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/ +/// Author : liuyubobobo +/// Time : 2022-08-20 + +#include +#include +#include +#include + +using namespace std; + + +/// DFS + BFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + int amountOfTime(TreeNode* root, int start) { + + vector> edges; + dfs_edges(root, nullptr, edges); + + if(edges.empty()) return 0; + + int max_id = 0; + for(const pair& p: edges){ + max_id = max(max_id, p.first); + max_id = max(max_id, p.second); + } + + vector> tree(max_id + 1); + for(const pair& p: edges){ + int u = p.first, v = p.second; + tree[u].push_back(v), tree[v].push_back(u); + } + + vector dis(max_id + 1, -1); + queue q; + q.push(start); + dis[start] = 0; + int res = 0; + while(!q.empty()){ + int u = q.front(); q.pop(); + for(int v: tree[u]){ + if(dis[v] == -1){ + q.push(v); + dis[v] = dis[u] + 1; + res = max(res, dis[v]); + } + } + } + return res; + } + +private: + void dfs_edges(TreeNode* node, TreeNode* p, vector>& edges){ + + if(p){ + edges.push_back({p->val, node->val}); + } + + if(node->left) dfs_edges(node->left, node, edges); + if(node->right) dfs_edges(node->right, node, edges); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9c62a9a1..e47258ff 100644 --- a/readme.md +++ b/readme.md @@ -2243,6 +2243,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2380 | [Time Needed to Rearrange a Binary String](https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/) | [无] | [C++](2001-2500/2380-Time-Needed-to-Rearrange-a-Binary-String/cpp-2380/) | | | | 2381 | [Shifting Letters II](https://leetcode.com/problems/shifting-letters-ii/) | [无] | [C++](2001-2500/2381-Shifting-Letters-II/cpp-2381/) | | | | 2382 | [Maximum Segment Sum After Removals](https://leetcode.com/problems/maximum-segment-sum-after-removals/) | [无] | [C++](2001-2500/2382-Maximum-Segment-Sum-After-Removals/cpp-2382/) | | | +| 2383 | [Minimum Hours of Training to Win a Competition](https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/) | [无] | [C++](2001-2500/2383-Minimum-Hours-of-Training-to-Win-a-Competition/cpp-2383/) | | | +| 2384 | [Largest Palindromic Number](https://leetcode.com/problems/largest-palindromic-number/) | [无] | [C++](2001-2500/2384-Largest-Palindromic-Number/cpp-2384/) | | | +| 2385 | [Amount of Time for Binary Tree to Be Infected](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/) | [无] | [C++](2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/) | | | | | | | | | | ## 力扣中文站比赛 From 41912df163dc7ec94a652e3e89ea414a95295f6c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 26 Aug 2022 11:29:04 -0700 Subject: [PATCH 168/390] xdxykd contest first commit. --- Others/2022-xdxykd/A/CMakeLists.txt | 6 +++ Others/2022-xdxykd/A/main.cpp | 52 +++++++++++++++++++++++++ Others/2022-xdxykd/B/CMakeLists.txt | 6 +++ Others/2022-xdxykd/B/main.cpp | 60 +++++++++++++++++++++++++++++ Others/2022-xdxykd/C/CMakeLists.txt | 6 +++ Others/2022-xdxykd/C/main.cpp | 47 ++++++++++++++++++++++ readme.md | 5 +++ 7 files changed, 182 insertions(+) create mode 100644 Others/2022-xdxykd/A/CMakeLists.txt create mode 100644 Others/2022-xdxykd/A/main.cpp create mode 100644 Others/2022-xdxykd/B/CMakeLists.txt create mode 100644 Others/2022-xdxykd/B/main.cpp create mode 100644 Others/2022-xdxykd/C/CMakeLists.txt create mode 100644 Others/2022-xdxykd/C/main.cpp diff --git a/Others/2022-xdxykd/A/CMakeLists.txt b/Others/2022-xdxykd/A/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/Others/2022-xdxykd/A/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/Others/2022-xdxykd/A/main.cpp b/Others/2022-xdxykd/A/main.cpp new file mode 100644 index 00000000..c315fe15 --- /dev/null +++ b/Others/2022-xdxykd/A/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.cn/contest/ubiquant2022/problems/xdxykd/ +/// Author : liuyubobobo +/// Time : 2022-08-25 + +#include +#include +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int numberOfPairs(vector& nums) { + + int n = nums.size(); + vector nums2(n); + for(int i = 0; i < n; i ++){ + string s = to_string(nums[i]); + reverse(s.begin(), s.end()); + nums2[i] = atoi(s.c_str()); + } + + for(int i = 0; i < n; i ++) + nums[i] -= nums2[i]; + + map f; + for(int e: nums) f[e] ++; + + long long res = 0; + for(const pair& p: f){ + long long cnt = p.second; + res += cnt * (cnt - 1) / 2 % MOD; + res %= MOD; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-xdxykd/B/CMakeLists.txt b/Others/2022-xdxykd/B/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/Others/2022-xdxykd/B/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/Others/2022-xdxykd/B/main.cpp b/Others/2022-xdxykd/B/main.cpp new file mode 100644 index 00000000..293ea1f5 --- /dev/null +++ b/Others/2022-xdxykd/B/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.cn/contest/ubiquant2022/problems/3PHTGp/ +/// Author : liuyubobobo +/// Time : 2022-08-25 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { + +private: + const int dirs[8][2] = { + {1, 0}, {-1, 0}, {0, 1}, {0, -1}, + {1, 1}, {1, -1}, {-1, 1}, {-1, -1} + }; + int R, C; + +public: + int lakeCount(vector& field) { + + R = field.size(), C = field[0].size(); + vector> visited(R, vector(C, false)); + + int res = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(field[i][j] == '.' || visited[i][j]) continue; + + dfs(field, i, j, visited); + res ++; + } + return res; + } + +private: + void dfs(const vector& g, int x, int y, vector>& visited){ + + visited[x][y] = true; + for(int d = 0; d < 8; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny) && !visited[nx][ny] && g[nx][ny] != '.') + dfs(g, nx, ny, visited); + } + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-xdxykd/C/CMakeLists.txt b/Others/2022-xdxykd/C/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/Others/2022-xdxykd/C/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/Others/2022-xdxykd/C/main.cpp b/Others/2022-xdxykd/C/main.cpp new file mode 100644 index 00000000..1ef05a5e --- /dev/null +++ b/Others/2022-xdxykd/C/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.cn/contest/ubiquant2022/problems/uGuf0v/ +/// Author : liuyubobobo +/// Time : 2022-08-25 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Compelxity: O(nlogA) +/// Space Complexity: O(n) +class Solution { +public: + int minOperations(vector& numbers) { + + int n = numbers.size(); + + vector two(n, 0), three(n, 0), base(n, 0); + for(int i = 0; i < n; i ++){ + int x = numbers[i]; + while(x % 2 == 0) two[i] ++, x /= 2; + while(x % 3 == 0) three[i] ++, x /= 3; + base[i] = x; + } + + for(int i = 1; i < n; i ++) + if(base[i] != base[0]) return -1; + + int res = 0; + + int max_two = *max_element(two.begin(), two.end()); + for(int e: two) res += max_two - e; + + int max_three = *max_element(three.begin(), three.end()); + for(int e: three) res += max_three - e; + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index e47258ff..833281cb 100644 --- a/readme.md +++ b/readme.md @@ -2306,6 +2306,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | +| 22 九坤-04 | [筹码游戏](https://leetcode.cn/contest/ubiquant2022/problems/I3Gm2h/) | [无] | | | | +| 22 九坤-03 | [数字默契考验](https://leetcode.cn/contest/ubiquant2022/problems/uGuf0v/) | [无] | [C++](Others/2022-xdxykd/C/) | | | +| 22 九坤-02 | [池塘计数](https://leetcode.cn/contest/ubiquant2022/problems/3PHTGp/) | [无] | [C++](Others/2022-xdxykd/B/) | | | +| 22 九坤-01 | [可以读通讯稿的组数](https://leetcode.cn/contest/ubiquant2022/problems/xdxykd/) | [无] | [C++](Others/2022-xdxykd/A/) | | | +| | | | | | | | 22 zj-future01 | [信号接收](https://leetcode.cn/contest/zj-future2022/problems/WYKGLO/) | [无] | [C++](Others/2022-zj-future/A/) | | | | 22 zj-future02 | [黑白棋游戏](https://leetcode.cn/contest/zj-future2022/problems/GVbKaI/) | [无] | [C++](Others/2022-zj-future/B/) | | | | 22 zj-future03 | [快递中转站选址](https://leetcode.cn/contest/zj-future2022/problems/kflZMc/) | [无] | [C++](Others/2022-zj-future/C/) | | | From 2261708aa67d78b54d18c3883df6dff39321a37e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 26 Aug 2022 22:51:56 -0700 Subject: [PATCH 169/390] 0662 new algo added. --- .../cpp-0662/CMakeLists.txt | 2 +- .../cpp-0662/main2.cpp | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp diff --git a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/CMakeLists.txt b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/CMakeLists.txt index cf573080..8e5f9d1f 100644 --- a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/CMakeLists.txt +++ b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_0662) set(CMAKE_CXX_STANDARD 14) -add_executable(cpp_0662 main.cpp) +add_executable(cpp_0662 main2.cpp) diff --git a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp new file mode 100644 index 00000000..9b69fa67 --- /dev/null +++ b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.com/problems/maximum-width-of-binary-tree/ +/// Author : liuyubobobo +/// Time : 2022-08-26 +/// Time : 2022-08-26 + +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + int widthOfBinaryTree(TreeNode* root) { + + queue> q; + q.push({root, 0, 0}); + vector list; + + long long res = 0; + int now_depth = 0; + while(!q.empty()){ + TreeNode* node = get<0>(q.front()); + unsigned long long seq = get<1>(q.front()); + int d = get<2>(q.front()); + q.pop(); + + if( d != now_depth ){ + now_depth = d; + res = max(res, list.back() - list[0] + 1); + list.clear(); + } + list.push_back(seq); + + if(node->left){ + q.push({node->left, seq * 2 + 1, d + 1}); + } + + if(node->right){ + q.push({node->right, seq * 2 + 2, d + 1}); + } + } + + res = max(res, list.back() - list[0] + 1); + return res; + } +}; + + +int main() { + + return 0; +} From 86573ee07531e2ee72ef73a6bb19b0c2aed1ae3d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 27 Aug 2022 01:27:23 -0700 Subject: [PATCH 170/390] 2022 xdxykd D solved. --- .../cpp-0662/main2.cpp | 1 - Others/2022-xdxykd/D/CMakeLists.txt | 6 ++ Others/2022-xdxykd/D/main.cpp | 80 +++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 Others/2022-xdxykd/D/CMakeLists.txt create mode 100644 Others/2022-xdxykd/D/main.cpp diff --git a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp index 9b69fa67..1f58423f 100644 --- a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp +++ b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp @@ -1,7 +1,6 @@ /// Source : https://leetcode.com/problems/maximum-width-of-binary-tree/ /// Author : liuyubobobo /// Time : 2022-08-26 -/// Time : 2022-08-26 #include #include diff --git a/Others/2022-xdxykd/D/CMakeLists.txt b/Others/2022-xdxykd/D/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/Others/2022-xdxykd/D/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/Others/2022-xdxykd/D/main.cpp b/Others/2022-xdxykd/D/main.cpp new file mode 100644 index 00000000..2851ab78 --- /dev/null +++ b/Others/2022-xdxykd/D/main.cpp @@ -0,0 +1,80 @@ +/// Source : https://leetcode.cn/submissions/detail/355567783/ +/// Author : liuyubobobo +/// Time : 2022-08-27 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Memoization +/// The state transfer is super clever +/// Time Complexity: O(?) +/// Space Complexity: O(?) +class Solution { +public: + double chipGame(vector& nums, int kind) { + + while(nums.size() < kind) nums.push_back(0); + + sort(nums.begin(), nums.end()); + + map, double> dp; + dp[nums] = 0; + + vector cur(kind, 0); + double res = dfs(kind, cur, nums, dp); + return res; + } + +private: + double dfs(int n, vector& cur, const vector& target, + map, double>& dp){ + + if(dp.count(cur)) return dp[cur]; + + double res = 1; + int choice = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || cur[i] != cur[start]){ + + int index = i - 1; + if(cur[index] == target[index]){ + start = i; + continue; + } + + int len = i - start; + choice += len; + double p = (double)len / n; + + cur[index] ++; + res += p * dfs(n, cur, target, dp); + cur[index] --; + + start = i; + } + + assert(choice <= n); + res /= (1.0 - (double)(n - choice) / n); + return dp[cur] = res; + } +}; + + +int main() { + +// vector nums1 = {1, 1}; +// cout << Solution().chipGame(nums1, 2) << '\n'; +// // 3 + + vector nums2 = {1, 2}; + cout << Solution().chipGame(nums2, 4) << '\n'; + // 3.833333 + + return 0; +} From c884fa6d52b77a2eb4242264784cd37cf84e1d5a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 27 Aug 2022 22:36:27 -0700 Subject: [PATCH 171/390] 2389-2392 solved. --- .../cpp-2389/CMakeLists.txt | 6 ++ .../cpp-2389/main.cpp | 38 +++++++++ .../cpp-2389/main2.cpp | 45 +++++++++++ .../cpp-2390/CMakeLists.txt | 6 ++ .../cpp-2390/main.cpp | 31 ++++++++ .../cpp-2391/CMakeLists.txt | 6 ++ .../cpp-2391/main.cpp | 60 ++++++++++++++ .../cpp-2392/CMakeLists.txt | 6 ++ .../cpp-2392/main.cpp | 78 +++++++++++++++++++ readme.md | 5 ++ 10 files changed, 281 insertions(+) create mode 100644 2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/CMakeLists.txt create mode 100644 2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main.cpp create mode 100644 2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main2.cpp create mode 100644 2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/CMakeLists.txt create mode 100644 2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/main.cpp create mode 100644 2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/CMakeLists.txt create mode 100644 2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/main.cpp create mode 100644 2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/CMakeLists.txt create mode 100644 2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/main.cpp diff --git a/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/CMakeLists.txt b/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/CMakeLists.txt new file mode 100644 index 00000000..4741167f --- /dev/null +++ b/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main2.cpp) diff --git a/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main.cpp b/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main.cpp new file mode 100644 index 00000000..879ee2da --- /dev/null +++ b/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/longest-subsequence-with-limited-sum/ +/// Author : liuyubobobo +/// Time : 2022-08-27 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn + qn) +/// Space Complexity: O(1) +class Solution { +public: + vector answerQueries(vector& nums, vector& queries) { + + sort(nums.begin(), nums.end()); + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i ++){ + int q = queries[i]; + + int cur = 0, j; + for(j = 0; j < nums.size() && cur <= q; j ++) + cur += nums[j]; + res[i] = j - (cur > q); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main2.cpp b/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main2.cpp new file mode 100644 index 00000000..b335def2 --- /dev/null +++ b/2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/main2.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/longest-subsequence-with-limited-sum/ +/// Author : liuyubobobo +/// Time : 2022-08-27 + +#include +#include +#include + +using namespace std; + + +/// Greedy + Binary Search +/// Time Complexity: O(nlogn + qlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector answerQueries(vector& nums, vector& queries) { + + sort(nums.begin(), nums.end()); + + vector presum(nums.size() + 1, 0); + for(int i = 0; i < nums.size(); i ++) presum[i + 1] = presum[i] + nums[i]; + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i ++){ + int q = queries[i]; + + if(q >= presum.back()){ + res[i] = nums.size(); + continue; + } + + auto iter = upper_bound(presum.begin(), presum.end(), q); + iter --; + res[i] = iter - presum.begin(); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/CMakeLists.txt b/2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/main.cpp b/2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/main.cpp new file mode 100644 index 00000000..8a23213b --- /dev/null +++ b/2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/removing-stars-from-a-string/ +/// Author : liuyubobobo +/// Time : 2022-08-27 + +#include +#include + +using namespace std; + + +/// Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string removeStars(string s) { + + string res = ""; + for(char c: s){ + if(c != '*') res += c; + else if(!res.empty()) res.pop_back(); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/CMakeLists.txt b/2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/main.cpp b/2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/main.cpp new file mode 100644 index 00000000..71bbd7c4 --- /dev/null +++ b/2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/ +/// Author : liuyubobobo +/// Time : 2022-08-27 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int garbageCollection(vector& garbage, vector& travel) { + + int n = garbage.size(); + + vector> g(3, vector(n, 0)); + vector total(3, 0); + for(int i = 0; i < n; i ++){ + for(char c: garbage[i]){ + if(c == 'M') g[0][i] ++, total[0] ++; + if(c == 'P') g[1][i] ++, total[1] ++; + if(c == 'G') g[2][i] ++, total[2] ++; + } + } + + int res = 0; + for(int i = 0; i < 3; i ++) + res += solve(n, total[i], g[i], travel); + return res; + } + +private: + int solve(int n, int total, const vector& g, const vector& travel){ + + if(total == 0) return 0; + + int res = g[0]; + total -= g[0]; + for(int i = 1; i < n && total; i ++){ + res += travel[i - 1]; + res += g[i]; + total -= g[i]; + } + return res; + } +}; + + +int main() { + + vector garbage2 = {"MMM", "PGM", "GP"}; + vector travel2 = {3, 10}; + cout << Solution().garbageCollection(garbage2, travel2) << '\n'; + + return 0; +} diff --git a/2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/CMakeLists.txt b/2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/main.cpp b/2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/main.cpp new file mode 100644 index 00000000..bc8aa5ba --- /dev/null +++ b/2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/main.cpp @@ -0,0 +1,78 @@ +/// Source : https://leetcode.com/problems/build-a-matrix-with-conditions/ +/// Author : liuyubobobo +/// Time : 2022-08-27 + +#include +#include +#include +#include + +using namespace std; + + +/// Topological Sort +/// Time Complexity: O(k + |rowConstions| + |colConditions|) +/// Space Complexity: O(k + |rowConstions| + |colConditions|) +class Solution { +public: + vector> buildMatrix(int k, vector>& rowConditions, vector>& colConditions) { + + vector> rowG(k + 1), colG(k + 1); + for(const vector& rc: rowConditions){ + int u = rc[0], v = rc[1]; + rowG[u].insert(v); + } + for(const vector& cc: colConditions){ + int u = cc[0], v = cc[1]; + colG[u].insert(v); + } + + vector row_res = topo_sort(k, rowG); + if(row_res.size() < k) return {}; + vector col_res = topo_sort(k, colG); + if(col_res.size() < k) return {}; + + vector row(k + 1), col(k + 1); + for(int i = 0; i < k; i ++){ + row[row_res[i]] = i; + col[col_res[i]] = i; + } + + vector> res(k, vector(k, 0)); + for(int i = 1; i <= k; i ++) + res[row[i]][col[i]] = i; + + return res; + } + +private: + vector topo_sort(int k, vector>& g){ + + vector indegrees(k + 1, 0); + for(int u = 1; u <= k; u ++){ + for(int v: g[u]) indegrees[v] ++; + } + + queue q; + for(int u = 1; u <= k; u ++) + if(indegrees[u] == 0) q.push(u); + + vector res; + while(!q.empty()){ + int u = q.front(); q.pop(); + res.push_back(u); + + for(int v: g[u]){ + indegrees[v] --; + if(indegrees[v] == 0) q.push(v); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 833281cb..1999d88d 100644 --- a/readme.md +++ b/readme.md @@ -2247,6 +2247,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2384 | [Largest Palindromic Number](https://leetcode.com/problems/largest-palindromic-number/) | [无] | [C++](2001-2500/2384-Largest-Palindromic-Number/cpp-2384/) | | | | 2385 | [Amount of Time for Binary Tree to Be Infected](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/) | [无] | [C++](2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/) | | | | | | | | | | +| 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum/) | [无] | [C++](2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/) | | | +| 2390 | [Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/) | [无] | [C++](2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/) | | | +| 2391 | [Minimum Amount of Time to Collect Garbage](https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/) | [无] | [C++](2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/) | | | +| 2392 | [Build a Matrix With Conditions](https://leetcode.com/problems/build-a-matrix-with-conditions/) | [无] | [C++](2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/) | | | +| | | | | | | ## 力扣中文站比赛 From 9fea5c1d6751f0f6a580f26bf19c54c90db05830 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 28 Aug 2022 00:05:45 -0700 Subject: [PATCH 172/390] 0662 BFS codes updated. --- .../cpp-0662/main2.cpp | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp index 1f58423f..8e471089 100644 --- a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp +++ b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp @@ -28,35 +28,35 @@ class Solution { public: int widthOfBinaryTree(TreeNode* root) { - queue> q; - q.push({root, 0, 0}); + queue> q; + q.push({root, 0}); vector list; - long long res = 0; - int now_depth = 0; + unsigned long long res = 0; while(!q.empty()){ - TreeNode* node = get<0>(q.front()); - unsigned long long seq = get<1>(q.front()); - int d = get<2>(q.front()); - q.pop(); - - if( d != now_depth ){ - now_depth = d; - res = max(res, list.back() - list[0] + 1); - list.clear(); - } - list.push_back(seq); - if(node->left){ - q.push({node->left, seq * 2 + 1, d + 1}); + vector> v; + while(!q.empty()){ + TreeNode* node = q.front().first; + unsigned long long seq = q.front().second; + q.pop(); + v.push_back({node, seq}); } - if(node->right){ - q.push({node->right, seq * 2 + 2, d + 1}); + res = max(res, v.back().second - v.begin()->second + 1); + + for(const pair& p: v){ + TreeNode* node = p.first; + unsigned long long seq = p.second; + + if(node->left) + q.push({node->left, seq * 2}); + + if(node->right) + q.push({node->right, seq * 2 + 1}); } - } - res = max(res, list.back() - list[0] + 1); + } return res; } }; From 1ba1ac95c0971a0a6689b0c3459d7830ea4df037 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 28 Aug 2022 00:29:32 -0700 Subject: [PATCH 173/390] 0793 bug fixed. --- .../cpp-0793/main.cpp | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/0501-1000/0793-Preimage-Size-of-Factorial-Zeroes-Function/cpp-0793/main.cpp b/0501-1000/0793-Preimage-Size-of-Factorial-Zeroes-Function/cpp-0793/main.cpp index 4eab670c..799c3061 100644 --- a/0501-1000/0793-Preimage-Size-of-Factorial-Zeroes-Function/cpp-0793/main.cpp +++ b/0501-1000/0793-Preimage-Size-of-Factorial-Zeroes-Function/cpp-0793/main.cpp @@ -1,11 +1,13 @@ /// Source : https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/description/ /// Author : liuyubobobo /// Time : 2018-03-03 +/// Updated: 2022-08-28 #include using namespace std; + /// Binary Search /// Time Complexity: O(logK * logK) /// Space Complexity: O(1) @@ -16,25 +18,26 @@ class Solution { if(K == 0) return 5; - int l = 0, r = 1000000000; - while(l <= r){ - int mid = (l + r) / 2; - int f_res = f(mid); - if(f_res == K) - return 5; - else if(f_res > K) - r = mid - 1; + long long l = 0ll, r = 5ll * K; + while(l < r){ + long long mid = (l + r) / 2; + long long f_res = f(mid); + if(f_res >= K) + r = mid; else l = mid + 1; } + if(f(l) == K){ + return 5 - l % 5; + } return 0; } private: - int f(int x){ - int res = 0; - int factor = 5; + long long f(long long x){ + long long res = 0; + long long factor = 5; while(factor <= x){ res += x / factor; factor *= 5; @@ -43,10 +46,17 @@ class Solution { } }; + int main() { cout << Solution().preimageSizeFZF(0) << endl; + // 5 + cout << Solution().preimageSizeFZF(5) << endl; + // 0 + + cout << Solution().preimageSizeFZF(1000000000) << endl; + // 5 return 0; } \ No newline at end of file From 2be18bd1764f636ee24435271b9bbc06d99c32eb Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 29 Aug 2022 02:11:58 -0700 Subject: [PATCH 174/390] autox 2023 solved. --- Others/2022-autox2023/A/CMakeLists.txt | 6 + Others/2022-autox2023/A/main.cpp | 38 +++++ Others/2022-autox2023/B/CMakeLists.txt | 6 + Others/2022-autox2023/B/main.cpp | 65 ++++++++ Others/2022-autox2023/C/CMakeLists.txt | 6 + Others/2022-autox2023/C/main.cpp | 42 +++++ Others/2022-autox2023/D/CMakeLists.txt | 6 + Others/2022-autox2023/D/main.cpp | 220 +++++++++++++++++++++++++ Others/2022-autox2023/D/main2.cpp | 210 +++++++++++++++++++++++ readme.md | 5 + 10 files changed, 604 insertions(+) create mode 100644 Others/2022-autox2023/A/CMakeLists.txt create mode 100644 Others/2022-autox2023/A/main.cpp create mode 100644 Others/2022-autox2023/B/CMakeLists.txt create mode 100644 Others/2022-autox2023/B/main.cpp create mode 100644 Others/2022-autox2023/C/CMakeLists.txt create mode 100644 Others/2022-autox2023/C/main.cpp create mode 100644 Others/2022-autox2023/D/CMakeLists.txt create mode 100644 Others/2022-autox2023/D/main.cpp create mode 100644 Others/2022-autox2023/D/main2.cpp diff --git a/Others/2022-autox2023/A/CMakeLists.txt b/Others/2022-autox2023/A/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/Others/2022-autox2023/A/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/Others/2022-autox2023/A/main.cpp b/Others/2022-autox2023/A/main.cpp new file mode 100644 index 00000000..8aab483b --- /dev/null +++ b/Others/2022-autox2023/A/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.cn/contest/autox2023/problems/l9HbCJ/ +/// Author : liuyubobobo +/// Time : 2022-08-28 + +#include +#include +#include + +using namespace std; + + +/// Using Set +/// Time Complexity: O((num + |block|) * log(num)) +/// Space Complexity: O(num) +class Solution { +public: + int getLengthOfWaterfallFlow(int num, vector& block) { + + set> cols; // height -> col + for(int i = 0; i < num; i ++) + cols.insert({0, i}); + + for(int b: block){ + auto iter = cols.begin(); + int h = iter->first, c = iter->second; + cols.erase(iter); + cols.insert({h + b, c}); + } + + return cols.rbegin()->first; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-autox2023/B/CMakeLists.txt b/Others/2022-autox2023/B/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/Others/2022-autox2023/B/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/Others/2022-autox2023/B/main.cpp b/Others/2022-autox2023/B/main.cpp new file mode 100644 index 00000000..08177c7c --- /dev/null +++ b/Others/2022-autox2023/B/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.cn/contest/autox2023/problems/8p6t8R/ +/// Author : liuyubobobo +/// Time : 2022-08-28 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: type 1, 2, 3: O(1) +/// type 4: O(maxv) +/// Space Complexity: O(maxv) +class Solution { +public: + vector honeyQuotes(vector>& handle) { + + vector f(101, 0); + int sz = 0; + long long sum = 0; + + vector res; + for(const vector& h: handle){ + int type = h[0]; + if(type == 1){ + f[h[1]] ++; + sz ++; + sum += h[1]; + } + else if(type == 2){ + f[h[1]] --; + sz --; + sum -= h[1]; + } + else if(type == 3){ + if(sz == 0) res.push_back(-1); + else res.push_back((double)sum / sz); + } + else{ + + if(sz == 0){ + res.push_back(-1); + continue; + } + + double mean = (double)sum / sz; + + double upper = 0.0; + for(int v = 0; v <= 100; v ++){ + if(f[v] == 0) continue; + upper += (v - mean) * (v - mean) * f[v]; + } + res.push_back(upper / sz); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-autox2023/C/CMakeLists.txt b/Others/2022-autox2023/C/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/Others/2022-autox2023/C/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/Others/2022-autox2023/C/main.cpp b/Others/2022-autox2023/C/main.cpp new file mode 100644 index 00000000..1c2e4309 --- /dev/null +++ b/Others/2022-autox2023/C/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.cn/contest/autox2023/problems/BjAFy9/ +/// Author : liuyubobobo +/// Time : 2022-08-28 + +#include +#include +#include +#include + +using namespace std; + + +/// DP + Binary Search +/// Time Complexity: O(|days| * |tickets| * log|days|) +/// Space Complexity: O(|days|) +class Solution { +public: + long long minCostToTravelOnDays(vector& days, vector>& tickets) { + + int n = days.size(); + vector dp(n, LONG_LONG_MAX); + for(int i = n - 1; i >= 0; i --){ + long long cur = LONG_LONG_MAX; + for(const vector& ticket: tickets){ + long long price = ticket[1]; + int d = ticket[0]; + auto iter = lower_bound(days.begin() + i, days.end(), days[i] + d); + + if(iter == days.end()) cur = min(cur, price); + else cur = min(cur, price + dp[iter - days.begin()]); + } + dp[i] = cur; + } + return dp[0]; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-autox2023/D/CMakeLists.txt b/Others/2022-autox2023/D/CMakeLists.txt new file mode 100644 index 00000000..541b2eff --- /dev/null +++ b/Others/2022-autox2023/D/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) diff --git a/Others/2022-autox2023/D/main.cpp b/Others/2022-autox2023/D/main.cpp new file mode 100644 index 00000000..80ded1f5 --- /dev/null +++ b/Others/2022-autox2023/D/main.cpp @@ -0,0 +1,220 @@ +/// Source : https://leetcode.cn/contest/autox2023/problems/TcdlJS/ +/// Author : liuyubobobo +/// Time : 2022-08-29 + +#include +#include +#include +#include + +#define X real() +#define Y imag() + +using namespace std; + +typedef complex P; + + +/// 线段与圆相交,使用线段的参数方程 +/// x = t * x1 + (1 - t) * x2 +/// y = t * y1 + (1 - t) * y2 +/// t 在 [0, 1] 之间 +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) + +long long cross_product(P a, P b){ + return (conj(a) * b).Y; // a.X * b.Y - b.X * a.Y +} + +// 1 : left +// 0 : touch +// -1 : right +int test_point_loc(P p, P s1, P s2){ + double res = cross_product(p - s1, p - s2); + if(res == 0ll) return 0; + return res > 0 ? 1 : -1; +} + +bool seg_intersect(long long x1, long long y1, long long x2, long long y2, + long long x3, long long y3, long long x4, long long y4){ + + P p1 = {x1, y1}, p2 = {x2, y2}; + P p3 = {x3, y3}, p4 = {x4, y4}; + + int loc1 = test_point_loc(p1, p3, p4), loc2 = test_point_loc(p2, p3, p4); + int loc3 = test_point_loc(p3, p1, p2), loc4 = test_point_loc(p4, p1, p2); + if(loc1 == 0 && min(x3, x4) <= x1 && x1 <= max(x3, x4) + && min(y3, y4) <= y1 && y1 <= max(y3, y4)){ + return true; + } + if(loc2 == 0 && min(x3, x4) <= x2 && x2 <= max(x3, x4) + && min(y3, y4) <= y2 && y2 <= max(y3, y4)){ + return true; + } + if(loc3 == 0 && min(x1, x2) <= x3 && x3 <= max(x1, x2) + && min(y1, y2) <= y3 && y3 <= max(y1, y2)){ + return true; + } + if(loc4 == 0 && min(x1, x2) <= x4 && x4 <= max(x1, x2) + && min(y1, y2) <= y4 && y4 <= max(y1, y2)){ + return true; + } + + if(!loc1 || !loc2 || !loc3 || !loc4){ + return false; + } + + if(loc1 * loc2 > 0 || loc3 * loc4 > 0) + return false; + + return true; +} + +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + vector antPass(vector>& geometry, vector>& path) { + + int n = geometry.size(); + UF uf(n); + + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++){ + + if(uf.is_connected(i, j)) continue; + + vector v1 = geometry[i]; + vector v2 = geometry[j]; + if(v1.size() > v2.size()) swap(v1, v2); + + // v1.size() <= v2.size() + if(intersect(v1, v2)) + uf.union_elements(i, j); + } + + vector res(path.size()); + for(int i = 0; i < path.size(); i ++) + res[i] = uf.is_connected(path[i][0], path[i][1]); + return res; + } + +private: + bool intersect(const vector& v1, const vector& v2){ + + if(v1.size() == 3 && v2.size() == 3) + return circle_intersect(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]); + + if(v1.size() == 4 && v2.size() == 4) + return seg_intersect(v1[0], v1[1], v1[2], v1[3], v2[0], v2[1], v2[2], v2[3]); + + return circle_seg_intersect(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v2[3]); + } + + int sign(long long x){ + if(x > 0) return 1; + if(x == 0) return 0; + return -1; + } + + bool circle_seg_intersect(long long x0, long long y0, long long r0, + long long x1, long long y1, long long x2, long long y2){ + + long long a = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + long long b = 2ll * (x1 - x2) * (x2 - x0) + 2ll * (y1 - y2) * (y2 - y0); + long long c = (x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0) - r0 * r0; + + if(a == 0 && b == 0) return c == 0; + + if(a == 0){ + if(b < 0) b = -b, c = -c; + return 0 <= -c && -c <= b; + } + + if(a < 0){ + a = -a, b = -b, c = -c; + } + + long double delta = (long double)b * b - 4.0 * (long double)a * c; + if(delta < 0) return false; + + long double t1 = ((long double)(-b) - sqrt(delta)) / (2.0 * (long double)a); + if(0.0 <= t1 && t1 <= 1.0) return true; + + long double t2 = ((long double)(-b) + sqrt(delta)) / (2.0 * (long double)a); + if(0.0 <= t2 && t2 <= 1.0) return true; + + return false; + } + + bool circle_intersect(long long x1, long long y1, long long r1, + long long x2, long long y2, long long r2){ + + long long d2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + return (r1 - r2) * (r1 - r2) <= d2 && d2 <= (r1 + r2) * (r1 + r2); + } +}; + + +void print_vec(const vector& res){ + for(bool e: res) cout << e << ' '; cout << '\n'; +} + +int main(){ + + vector> g1 = {{2, 5, 7, 3}, {1, 1, 4, 2}, {4, 3, 2}}; + vector> p1 = {{0, 1}, {1, 2}, {0, 2}}; + print_vec(Solution().antPass(g1, p1)); + // 1 1 1 + + vector> g2 = {{4, 1, 1}, {3, 2, 1}, {1, 4, 5, 4}}; + vector> p2 = {{0, 1}, {2, 0}}; + print_vec(Solution().antPass(g2, p2)); + // 1 0 + + vector> g3 = {{5, 6, 5, 8}, {4, 3, 6, 4}, {2, 6, 5, 6}, {0, 5, 0, 6}, {4, 0, 6, 0}}; + vector> p3 = {{2, 0}, {2, 4}}; + print_vec(Solution().antPass(g3, p3)); + // 1 0 + + vector> g4 = {{3, 2, 6, 2}, {9, 0, 10, 0}, {3, 0, 6, 0}, {7, 2, 9, 3}, {4, 1, 5, 2}}; + vector> p4 = {{0, 1}, {3, 4}, {0, 3}}; + print_vec(Solution().antPass(g4, p4)); + // 0 0 0 + + vector> g5 = {{8, 8, 6}, {1, 1, 1, 2}, {1, 1, 3, 2}, {4, 3, 4, 4}, {2, 1, 5, 3}}; + vector> p5 = {{0, 4}, {3, 1}}; + print_vec(Solution().antPass(g5, p5)); + // 1 0 + + return 0; +} diff --git a/Others/2022-autox2023/D/main2.cpp b/Others/2022-autox2023/D/main2.cpp new file mode 100644 index 00000000..59ca3775 --- /dev/null +++ b/Others/2022-autox2023/D/main2.cpp @@ -0,0 +1,210 @@ +/// Source : https://leetcode.cn/contest/autox2023/problems/TcdlJS/ +/// Author : liuyubobobo +/// Time : 2022-08-29 + +#include +#include +#include +#include + +#define X real() +#define Y imag() + +using namespace std; + +typedef complex P; + + +/// 线段与圆相交,使用条件判断 +/// 参考这里:https://blog.csdn.net/SongBai1997/article/details/86599879 +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) + +long long cross_product(P a, P b){ + return (conj(a) * b).Y; // a.X * b.Y - b.X * a.Y +} + +// 1 : left +// 0 : touch +// -1 : right +int test_point_loc(P p, P s1, P s2){ + double res = cross_product(p - s1, p - s2); + if(res == 0ll) return 0; + return res > 0 ? 1 : -1; +} + +bool seg_intersect(long long x1, long long y1, long long x2, long long y2, + long long x3, long long y3, long long x4, long long y4){ + + P p1 = {x1, y1}, p2 = {x2, y2}; + P p3 = {x3, y3}, p4 = {x4, y4}; + + int loc1 = test_point_loc(p1, p3, p4), loc2 = test_point_loc(p2, p3, p4); + int loc3 = test_point_loc(p3, p1, p2), loc4 = test_point_loc(p4, p1, p2); + if(loc1 == 0 && min(x3, x4) <= x1 && x1 <= max(x3, x4) + && min(y3, y4) <= y1 && y1 <= max(y3, y4)){ + return true; + } + if(loc2 == 0 && min(x3, x4) <= x2 && x2 <= max(x3, x4) + && min(y3, y4) <= y2 && y2 <= max(y3, y4)){ + return true; + } + if(loc3 == 0 && min(x1, x2) <= x3 && x3 <= max(x1, x2) + && min(y1, y2) <= y3 && y3 <= max(y1, y2)){ + return true; + } + if(loc4 == 0 && min(x1, x2) <= x4 && x4 <= max(x1, x2) + && min(y1, y2) <= y4 && y4 <= max(y1, y2)){ + return true; + } + + if(!loc1 || !loc2 || !loc3 || !loc4){ + return false; + } + + if(loc1 * loc2 > 0 || loc3 * loc4 > 0) + return false; + + return true; +} + +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + vector antPass(vector>& geometry, vector>& path) { + + int n = geometry.size(); + UF uf(n); + + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++){ + + if(uf.is_connected(i, j)) continue; + + vector v1 = geometry[i]; + vector v2 = geometry[j]; + if(v1.size() > v2.size()) swap(v1, v2); + + // v1.size() <= v2.size() + if(intersect(v1, v2)) + uf.union_elements(i, j); + } + + vector res(path.size()); + for(int i = 0; i < path.size(); i ++) + res[i] = uf.is_connected(path[i][0], path[i][1]); + return res; + } + +private: + bool intersect(const vector& v1, const vector& v2){ + + if(v1.size() == 3 && v2.size() == 3) + return circle_intersect(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]); + + if(v1.size() == 4 && v2.size() == 4) + return seg_intersect(v1[0], v1[1], v1[2], v1[3], v2[0], v2[1], v2[2], v2[3]); + + return circle_seg_intersect(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v2[3]); + } + + int sign(long long x){ + if(x > 0) return 1; + if(x == 0) return 0; + return -1; + } + + bool circle_seg_intersect(long long x0, long long y0, long long r0, + long long x1, long long y1, long long x2, long long y2){ + + bool flag1 = (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) <= r0 * r0; + bool flag2 = (x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0) <= r0 * r0; + + if(flag1 && flag2) return false; + if((flag1 && !flag2) || (!flag1 && flag2)) return true; + + long long A = y2 - y1; + long long B = x1 - x2; + long long C = x2 * y1 - y2 * x1; + + if((long double)(A * x0 + B * y0 + C) * (A * x0 + B * y0 + C) > (long double)r0 * r0 * (A * A + B * B)) + return false; + + long long a1 = (x0 - x1) * (x2 - x1) + (y0 - y1) * (y2 - y1); + long long a2 = (x0 - x2) * (x1 - x2) + (y0 - y2) * (y1 - y2); + if(a1 > 0 && a2 > 0) return true; + return false; + } + + bool circle_intersect(long long x1, long long y1, long long r1, + long long x2, long long y2, long long r2){ + + long long d2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + return (r1 - r2) * (r1 - r2) <= d2 && d2 <= (r1 + r2) * (r1 + r2); + } +}; + + +void print_vec(const vector& res){ + for(bool e: res) cout << e << ' '; cout << '\n'; +} + +int main(){ + + vector> g1 = {{2, 5, 7, 3}, {1, 1, 4, 2}, {4, 3, 2}}; + vector> p1 = {{0, 1}, {1, 2}, {0, 2}}; + print_vec(Solution().antPass(g1, p1)); + // 1 1 1 + + vector> g2 = {{4, 1, 1}, {3, 2, 1}, {1, 4, 5, 4}}; + vector> p2 = {{0, 1}, {2, 0}}; + print_vec(Solution().antPass(g2, p2)); + // 1 0 + + vector> g3 = {{5, 6, 5, 8}, {4, 3, 6, 4}, {2, 6, 5, 6}, {0, 5, 0, 6}, {4, 0, 6, 0}}; + vector> p3 = {{2, 0}, {2, 4}}; + print_vec(Solution().antPass(g3, p3)); + // 1 0 + + vector> g4 = {{3, 2, 6, 2}, {9, 0, 10, 0}, {3, 0, 6, 0}, {7, 2, 9, 3}, {4, 1, 5, 2}}; + vector> p4 = {{0, 1}, {3, 4}, {0, 3}}; + print_vec(Solution().antPass(g4, p4)); + // 0 0 0 + + vector> g5 = {{8, 8, 6}, {1, 1, 1, 2}, {1, 1, 3, 2}, {4, 3, 4, 4}, {2, 1, 5, 3}}; + vector> p5 = {{0, 4}, {3, 1}}; + print_vec(Solution().antPass(g5, p5)); + // 1 0 + + return 0; +} diff --git a/readme.md b/readme.md index 1999d88d..3eb2ac2f 100644 --- a/readme.md +++ b/readme.md @@ -2311,6 +2311,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | +| 22 AutoX-4 | [蚂蚁爬行](https://leetcode.cn/contest/autox2023/problems/TcdlJS/) | [无] | [C++](Others/2022-autox2023/D/) | | | +| 22 AutoX-3 | [出行的最少购票费用](https://leetcode.cn/contest/autox2023/problems/BjAFy9/) | [无] | [C++](Others/2022-autox2023/C/) | | | +| 22 AutoX-2 | [蚂蚁王国的蜂蜜](https://leetcode.cn/contest/autox2023/problems/8p6t8R/) | [无] | [C++](Others/2022-autox2023/B/) | | | +| 22 AutoX-1 | [网页瀑布流](https://leetcode.cn/contest/autox2023/problems/l9HbCJ/) | [无] | [C++](Others/2022-autox2023/A/) | | | +| | | | | | | | 22 九坤-04 | [筹码游戏](https://leetcode.cn/contest/ubiquant2022/problems/I3Gm2h/) | [无] | | | | | 22 九坤-03 | [数字默契考验](https://leetcode.cn/contest/ubiquant2022/problems/uGuf0v/) | [无] | [C++](Others/2022-xdxykd/C/) | | | | 22 九坤-02 | [池塘计数](https://leetcode.cn/contest/ubiquant2022/problems/3PHTGp/) | [无] | [C++](Others/2022-xdxykd/B/) | | | From 0578fc9d09e6eb9777734c503504fcdf4bd0c8a8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 2 Sep 2022 11:00:42 -0700 Subject: [PATCH 175/390] 0646 solved. --- .../cpp-0646/CMakeLists.txt | 6 ++++ .../cpp-0646/main.cpp | 35 +++++++++++++++++++ readme.md | 4 +-- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/CMakeLists.txt create mode 100644 0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/main.cpp diff --git a/0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/CMakeLists.txt b/0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/CMakeLists.txt new file mode 100644 index 00000000..97af3683 --- /dev/null +++ b/0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0646) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0646 main.cpp) diff --git a/0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/main.cpp b/0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/main.cpp new file mode 100644 index 00000000..d7582acc --- /dev/null +++ b/0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/maximum-length-of-pair-chain/submissions/ +/// Author : liuyubobobo +/// Time : 2022-09-02 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(nlogn + n^2) +/// Space Complexity: O(n) +class Solution { +public: + int findLongestChain(vector>& pairs) { + + int n = pairs.size(); + sort(pairs.begin(), pairs.end()); + + vector dp(n, 1); + for(int i = n - 2; i >= 0; i --){ + for(int j = i + 1; j < n; j ++) + if(pairs[i][1] < pairs[j][0]) + dp[i] = max(dp[i], 1 + dp[j]); + } + return *max_element(dp.begin(), dp.end()); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3eb2ac2f..101cee4b 100644 --- a/readme.md +++ b/readme.md @@ -659,7 +659,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 643 | [Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/) | [solution](https://leetcode.com/problems/maximum-average-subarray-i/solution/) | [C++](0501-1000/0643-Maximum-Average-Subarray-I/cpp-0643/) | | | | 644 | [Maximum Average Subarray II](https://leetcode.com/problems/maximum-average-subarray-ii/) | [solution](https://leetcode.com/problems/maximum-average-subarray-ii/solution/) | [C++](0501-1000/0644-Maximum-Average-Subarray-II/cpp-0644/) | | | | 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch/) | [solution](https://leetcode.com/problems/set-mismatch/solution/)
[缺:空间 O(1) 解法] | [C++](0501-1000/0645-Set-Mismatch/cpp-0645/) | | | -| | | | | | | +| 646 | [Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [solution](https://leetcode.com/problems/maximum-length-of-pair-chain/solution/) | [C++](0501-1000/0646-Maximum-Length-of-Pair-Chain/cpp-0646/) | | | | 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [solution](https://leetcode.com/problems/palindromic-substrings/solution/) | [C++](0501-1000/0647-Palindromic-Substrings/cpp-0647/) | | | | 648 | [Replace Words](https://leetcode.com/problems/replace-words/description/) | [无] | [C++](0501-1000/0648-Replace-Words/cpp-0648/) | | | | 649 | [Dota2 Senate](https://leetcode.com/problems/dota2-senate/) | [solution](https://leetcode.com/problems/dota2-senate/solution/)
[缺:队列] | [C++](0501-1000/0649-Dota2-Senate/cpp-0649/) | | | @@ -738,7 +738,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 723 | [Candy Crush](https://leetcode.com/problems/candy-crush/description/) | [solution](https://leetcode.com/problems/candy-crush/solution/) | [C++](0501-1000/0723-Candy-Crush/cpp-0723/) | | | | 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/) | [solution](https://leetcode.com/problems/find-pivot-index/solution/) | [C++](0501-1000/0724-Find-Pivot-Index/cpp-0724/) | | | | 725 | [Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/description/) | [solution](https://leetcode.com/problems/split-linked-list-in-parts/solution/) | [C++](0501-1000/0725-Split-Linked-List-in-Parts/cpp-0725/) | | | -| 726 | [Number of Atoms](https://leetcode.com/problems/number-of-atoms/) | [C++](0501-1000/0726-Number-of-Atoms/cpp-0726/) | | | | +| 726 | [Number of Atoms](https://leetcode.com/problems/number-of-atoms/) | [solution](https://leetcode.com/problems/number-of-atoms/solution/) | [C++](0501-1000/0726-Number-of-Atoms/cpp-0726/) | | | | 727 | [Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/description/) | [solution](https://leetcode.com/problems/minimum-window-subsequence/solution/) | [C++](0501-1000/cpp-Minimum-Window-Subsequence/cpp-0727/) | | | | 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/description/) | [solution](https://leetcode.com/problems/self-dividing-numbers/solution/) | [C++](0501-1000/0728-Self-Dividing-Numbers/cpp-0728/) | | | | 729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i/description/) | [solution](https://leetcode.com/problems/my-calendar-i/solution/) | [C++](0501-1000/0729-My-Calendar-I/cpp-0729/) | | | From 3f440a3eabb26fec3b27d5acc47b85e48be46d30 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 3 Sep 2022 15:44:26 -0700 Subject: [PATCH 176/390] 2395-2398 solved. --- .../cpp-2395/CMakeLists.txt | 6 ++ .../cpp-2395/main.cpp | 33 +++++++ .../cpp-2395/main2.cpp | 38 ++++++++ .../cpp-2396/CMakeLists.txt | 6 ++ .../cpp-2396/main.cpp | 38 ++++++++ .../cpp-2397/CMakeLists.txt | 6 ++ .../cpp-2397/main.cpp | 62 +++++++++++++ .../cpp-2398/CMakeLists.txt | 6 ++ .../cpp-2398/main.cpp | 93 +++++++++++++++++++ readme.md | 6 ++ 10 files changed, 294 insertions(+) create mode 100644 2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/CMakeLists.txt create mode 100644 2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main.cpp create mode 100644 2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main2.cpp create mode 100644 2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/CMakeLists.txt create mode 100644 2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/main.cpp create mode 100644 2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/CMakeLists.txt create mode 100644 2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/main.cpp create mode 100644 2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/CMakeLists.txt create mode 100644 2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/main.cpp diff --git a/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/CMakeLists.txt b/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/CMakeLists.txt new file mode 100644 index 00000000..e6db4427 --- /dev/null +++ b/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2395) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2395 main2.cpp) diff --git a/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main.cpp b/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main.cpp new file mode 100644 index 00000000..86869fb2 --- /dev/null +++ b/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/find-subarrays-with-equal-sum/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + bool findSubarrays(vector& nums) { + + int n = nums.size(); + for(int start = 0; start + 1 < n; start ++){ + int t = nums[start] + nums[start + 1]; + for(int i = start + 1; i + 1 < n; i ++) + if(t == nums[i] + nums[i + 1]) return true; + } + return false; + } +}; + + +int main() { + + + return 0; +} diff --git a/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main2.cpp b/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main2.cpp new file mode 100644 index 00000000..2662789c --- /dev/null +++ b/2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/main2.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/find-subarrays-with-equal-sum/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include +#include + +using namespace std; + + +/// Using MultiSet +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + bool findSubarrays(vector& nums) { + + int n = nums.size(); + multiset s; + for(int start = 0; start + 1 < n; start ++) + s.insert(nums[start] + nums[start + 1]); + + for(int start = 0; start + 1 < n; start ++){ + int t = nums[start] + nums[start + 1]; + s.erase(s.find(t)); + if(s.count(t)) return true; + } + return false; + } +}; + + +int main() { + + + return 0; +} diff --git a/2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/CMakeLists.txt b/2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/CMakeLists.txt new file mode 100644 index 00000000..f1976760 --- /dev/null +++ b/2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2396) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2396 main.cpp) diff --git a/2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/main.cpp b/2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/main.cpp new file mode 100644 index 00000000..5147583b --- /dev/null +++ b/2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/main.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + bool isStrictlyPalindromic(int n) { + + bool ok = true; + for(int b = 2; b <= n - 2 && ok; b ++){ + vector res = get_base_number(n, b); + for(int i = 0, j = res.size() - 1; i < j && ok; i ++, j --) + if(res[i] != res[j]) ok = false; + } + return ok; + } + +private: + vector get_base_number(int n, int b){ + vector res; + while(n){ + res.push_back(n % b); + n /= b; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/CMakeLists.txt b/2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/CMakeLists.txt new file mode 100644 index 00000000..70195489 --- /dev/null +++ b/2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2397) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2397 main.cpp) diff --git a/2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/main.cpp b/2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/main.cpp new file mode 100644 index 00000000..2d163ce0 --- /dev/null +++ b/2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/maximum-rows-covered-by-columns/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include + +using namespace std; + + +/// Backtrack +/// Time Complexity: O(C(C, cols) * R * C) +/// Space Complexity: O(C) +class Solution { + +public: + int maximumRows(vector>& mat, int cols) { + + int R = mat.size(), C = mat[0].size(); + + int res = 0; + vector v(C, 0); + dfs(R, C, mat, cols, 0, v, res); + return res; + } + +private: + void dfs(int R, int C, const vector>& mat, + int k, int start, vector& v, int& res){ + + if(k == 0){ + res = max(res, get_rows(R, C, mat, v)); + return; + } + + for(int i = start; i <= C - k; i ++){ + v[i] = 1; + dfs(R, C, mat, k - 1, i + 1, v, res); + v[i] = 0; + } + } + + int get_rows(int R, int C, const vector>& mat, const vector& col){ + + int res = 0; + for(int i = 0; i < R; i ++) + res += ok(C, mat[i], col); + return res; + } + + bool ok(int n, const vector& v1, const vector& v2){ + for(int i = 0; i < n; i ++) + if(v1[i] == 1 && v2[i] == 0) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/CMakeLists.txt b/2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/CMakeLists.txt new file mode 100644 index 00000000..4a22ee63 --- /dev/null +++ b/2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2398) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2398 main.cpp) diff --git a/2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/main.cpp b/2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/main.cpp new file mode 100644 index 00000000..40840cbe --- /dev/null +++ b/2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/main.cpp @@ -0,0 +1,93 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-robots-within-budget/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include +#include + +using namespace std; + + +/// ST(query range max) + Binary Seach +/// Time Complexity: O(n * logn * logn) +/// Space Complexity: O(nlogn) +template +class SparseTable{ + +private: + int n; + vector> table; + vector log2; + T (*combine)(T a, T b); + +public: + SparseTable(const vector& data, T (*combine)(T a, T b)): n(data.size()), log2(n + 1, 1){ + + this->combine = combine; + + int len = 2, k = 1; + for(int i = 1; i <= n; i ++){ + if(i >= len) len <<= 1, k ++; + log2[i] = k; + } + + int K = log2[n]; + table = vector>(K, vector(n)); + + for(int i = 0; i < n; i ++) + table[0][i] = data[i]; + + for(int k = 1; k < K; k ++) + for(int i = 0; i + (1 << (k - 1)) < n; i ++) + table[k][i] = combine(table[k - 1][i], table[k - 1][i + (1 << (k - 1))]); + } + + T query(int l, int r){ + + int k = log2[r - l + 1]; + return combine(table[k - 1][l], table[k - 1][r + 1 - (1 << (k - 1))]); + } +}; + +class Solution { +public: + int maximumRobots(vector& chargeTimes, vector& runningCosts, long long budget) { + + int n = chargeTimes.size(); + assert(runningCosts.size() == n); + + SparseTable st(chargeTimes, [](int a, int b){return max(a, b);}); + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + runningCosts[i]; + + int res = 0; + for(int start = 0; start + res < n; start ++){ + + // check [start, start + res], len = res + 1 + // if not work, no need to search from start + if((presum[start + res + 1] - presum[start]) * (res + 1) + st.query(start, start + res) > budget) + continue; + + int end_l = start + res, end_r = n - 1; + while(end_l < end_r){ + int end_mid = (end_l + end_r + 1) / 2; + + // [start, end_mid] + long long t = (presum[end_mid + 1] - presum[start]) * (end_mid - start + 1) + st.query(start, end_mid); + if(t <= budget) + end_l = end_mid; + else + end_r = end_mid - 1; + } + res = max(res, end_l - start + 1); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 101cee4b..787fbecc 100644 --- a/readme.md +++ b/readme.md @@ -2252,6 +2252,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2391 | [Minimum Amount of Time to Collect Garbage](https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/) | [无] | [C++](2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/) | | | | 2392 | [Build a Matrix With Conditions](https://leetcode.com/problems/build-a-matrix-with-conditions/) | [无] | [C++](2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/) | | | | | | | | | | +| 2394 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [无] | [C++](2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/) | | | +| 2396 | [Strictly Palindromic Number](https://leetcode.com/problems/strictly-palindromic-number/) | [无] | [C++](2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/) | | | +| 2397 | [Maximum Rows Covered by Columns](https://leetcode.com/problems/maximum-rows-covered-by-columns/) | [无] | [C++](2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/) | | | +| 2398 | [Maximum Number of Robots Within Budget](https://leetcode.com/problems/maximum-number-of-robots-within-budget/) | [无][缺:滑动窗口] | [C++](2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/) | | | +| | | | | | | ## 力扣中文站比赛 From eac53d979e12ddb6066997467547e72f344d98f9 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 3 Sep 2022 15:53:15 -0700 Subject: [PATCH 177/390] 2393 solved. --- .../cpp-2393/CMakeLists.txt | 6 +++ .../cpp-2393/main.cpp | 45 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/CMakeLists.txt create mode 100644 2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/main.cpp diff --git a/2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/CMakeLists.txt b/2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/CMakeLists.txt new file mode 100644 index 00000000..c94958c9 --- /dev/null +++ b/2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2393) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2393 main.cpp) diff --git a/2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/main.cpp b/2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/main.cpp new file mode 100644 index 00000000..f5bbb1b1 --- /dev/null +++ b/2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/count-strictly-increasing-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Compelxity: O(1) +class Solution { +public: + long long countSubarrays(vector& nums) { + + int n = nums.size(); + long long res = 0; + int l = 0, r = -1; + while(l < n){ + if(r + 1 < n && (r + 1 == l || nums[r] < nums[r + 1])) + r ++; + else{ + res += r - l + 1; + l ++; + } + } + return res; + } +}; + + +int main() { + + vector nums1 = {1,3,5,4,4,6}; + cout << Solution().countSubarrays(nums1) << '\n'; + // 10 + + vector nums2 = {1,2, 3,4,5}; + cout << Solution().countSubarrays(nums2) << '\n'; + // 15 + + return 0; +} diff --git a/readme.md b/readme.md index 787fbecc..90604a72 100644 --- a/readme.md +++ b/readme.md @@ -2251,7 +2251,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2390 | [Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/) | [无] | [C++](2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/) | | | | 2391 | [Minimum Amount of Time to Collect Garbage](https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/) | [无] | [C++](2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/) | | | | 2392 | [Build a Matrix With Conditions](https://leetcode.com/problems/build-a-matrix-with-conditions/) | [无] | [C++](2001-2500/2392-Build-a-Matrix-With-Conditions/cpp-2392/) | | | -| | | | | | | +| 2393 | [Count Strictly Increasing Subarrays](https://leetcode.com/problems/count-strictly-increasing-subarrays/) | [无] | [C++](2001-2500/2393-Count-Strictly-Increasing-Subarrays/cpp-2393/) | | | | 2394 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [无] | [C++](2001-2500/2395-Find-Subarrays-With-Equal-Sum/cpp-2395/) | | | | 2396 | [Strictly Palindromic Number](https://leetcode.com/problems/strictly-palindromic-number/) | [无] | [C++](2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/) | | | From 74a36377b8201acbdb436d1999f889fad1b59320 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 3 Sep 2022 23:20:39 -0700 Subject: [PATCH 178/390] 2399-2402 solved. --- .../cpp-2399/CMakeLists.txt | 6 ++ .../cpp-2399/main.cpp | 34 +++++++ .../cpp-2400/CMakeLists.txt | 6 ++ .../cpp-2400/main.cpp | 53 +++++++++++ .../cpp-2401/CMakeLists.txt | 6 ++ .../cpp-2401/main.cpp | 66 ++++++++++++++ .../cpp-2401/main2.cpp | 63 +++++++++++++ .../cpp-2402/CMakeLists.txt | 6 ++ .../2402-Meeting-Rooms-III/cpp-2402/main.cpp | 91 +++++++++++++++++++ readme.md | 4 + 10 files changed, 335 insertions(+) create mode 100644 2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/CMakeLists.txt create mode 100644 2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/main.cpp create mode 100644 2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/CMakeLists.txt create mode 100644 2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/main.cpp create mode 100644 2001-2500/2401-Longest-Nice-Subarray/cpp-2401/CMakeLists.txt create mode 100644 2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main.cpp create mode 100644 2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main2.cpp create mode 100644 2001-2500/2402-Meeting-Rooms-III/cpp-2402/CMakeLists.txt create mode 100644 2001-2500/2402-Meeting-Rooms-III/cpp-2402/main.cpp diff --git a/2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/CMakeLists.txt b/2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/main.cpp b/2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/main.cpp new file mode 100644 index 00000000..c9f16927 --- /dev/null +++ b/2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/check-distances-between-same-letters/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool checkDistances(string s, vector& distance) { + + vector first(26, -1); + for(int i = 0; i < s.size(); i ++){ + if(first[s[i] - 'a'] == -1) first[s[i] - 'a'] = i; + else{ + int d = i - first[s[i] - 'a'] - 1; + if(distance[s[i] - 'a'] != d) return false; + } + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/CMakeLists.txt b/2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/main.cpp b/2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/main.cpp new file mode 100644 index 00000000..02ab7241 --- /dev/null +++ b/2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(k^2) +/// Space Complexity: O(k^2) +class Solution { + +private: + const long long MOD = 1e9 + 7; + int OFFSET = 1000; + +public: + int numberOfWays(int startPos, int endPos, int k) { + + OFFSET = -(startPos - k); + vector> dp(startPos + k + OFFSET, vector(k + 1, -1)); + return dfs(startPos, k, endPos, dp); + } + + long long dfs(int pos, int k, int target, vector>& dp){ + + if(k == 0) return pos == target; + if(dp[pos + OFFSET][k] != -1) return dp[pos + OFFSET][k]; + + long long res = 0; + res += dfs(pos + 1, k - 1, target, dp); + res += dfs(pos - 1, k - 1, target, dp); + return dp[pos + OFFSET][k] = res % MOD; + } +}; + + +int main() { + + cout << Solution().numberOfWays(1, 2, 3) << '\n'; + // 3 + + cout << Solution().numberOfWays(2, 5, 10) << '\n'; + // 0 + + cout << Solution().numberOfWays(921, 413, 716) << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/CMakeLists.txt b/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/CMakeLists.txt new file mode 100644 index 00000000..148bffbf --- /dev/null +++ b/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main.cpp b/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main.cpp new file mode 100644 index 00000000..0656f599 --- /dev/null +++ b/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/longest-nice-subarray/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int longestNiceSubarray(vector& nums) { + + int n = nums.size(); + vector> bits(30, vector(n, 0)); + for(int i = 0; i < n; i ++){ + for(int p = 0; p < 30; p ++) + bits[p][i] = ((nums[i] >> p) & 1); + } + + vector> presum(30, vector(n + 1, 0)); + for(int p = 0; p < 30; p ++) + for(int i = 0; i < n; i ++) + presum[p][i + 1] = presum[p][i] + bits[p][i]; + + int res = 0; + for(int start = 0; start < n; start ++){ + int end_l = start, end_r = n - 1; + while(end_l < end_r){ + int end_mid = (end_l + end_r + 1) / 2; + if(ok(presum, start, end_mid)) end_l = end_mid; + else end_r = end_mid - 1; + } + res = max(res, end_l - start + 1); + } + return res; + } + +private: + bool ok(const vector>& presum, int start, int end){ + + for(int p = 0; p < 30; p ++){ + int t = presum[p][end + 1] - presum[p][start]; + if(t > 1) return false; + } + return true; + } +}; + + +int main() { + + vector nums1 = {1, 3, 8, 48, 10}; + cout << Solution().longestNiceSubarray(nums1) << '\n'; + // 3 + + vector nums2 = {3, 1, 5, 11, 13}; + cout << Solution().longestNiceSubarray(nums2) << '\n'; + // 1 + + return 0; +} diff --git a/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main2.cpp b/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main2.cpp new file mode 100644 index 00000000..a36be6ea --- /dev/null +++ b/2001-2500/2401-Longest-Nice-Subarray/cpp-2401/main2.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/longest-nice-subarray/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int longestNiceSubarray(vector& nums) { + + int n = nums.size(); + + vector cur(30, 0); + int res = 0, l = 0, r = -1; + while(l < n){ + if(r + 1 < n && ok(cur, nums[r + 1])) + add(cur, nums[++r]); + else + remove(cur, nums[l ++]); + res = max(res, r - l + 1); + } + return res; + } + +private: + bool ok(const vector& bits, int x){ + + for(int p = 0; p < 30; p ++) + if(bits[p] + ((x >> p) & 1) > 1) return false; + return true; + } + + void add(vector& bits, int x){ + for(int p = 0; p < 30; p ++) + bits[p] += ((x >> p) & 1); + } + + void remove(vector& bits, int x){ + for(int p = 0; p < 30; p ++) + bits[p] -= ((x >> p) & 1); + } +}; + + +int main() { + + vector nums1 = {1, 3, 8, 48, 10}; + cout << Solution().longestNiceSubarray(nums1) << '\n'; + // 3 + + vector nums2 = {3, 1, 5, 11, 13}; + cout << Solution().longestNiceSubarray(nums2) << '\n'; + // 1 + + return 0; +} diff --git a/2001-2500/2402-Meeting-Rooms-III/cpp-2402/CMakeLists.txt b/2001-2500/2402-Meeting-Rooms-III/cpp-2402/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2402-Meeting-Rooms-III/cpp-2402/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2402-Meeting-Rooms-III/cpp-2402/main.cpp b/2001-2500/2402-Meeting-Rooms-III/cpp-2402/main.cpp new file mode 100644 index 00000000..9f686d26 --- /dev/null +++ b/2001-2500/2402-Meeting-Rooms-III/cpp-2402/main.cpp @@ -0,0 +1,91 @@ +/// Source : https://leetcode.com/problems/meeting-rooms-iii/ +/// Author : liuyubobobo +/// Time : 2022-09-03 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// PQ Events Simulation +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +private: + const int END_EVENT = 0, START_EVENT = 1; + +public: + int mostBooked(int n, vector>& meetings) { + + // event: + // start_time, + // 0 -end_meeting, 1 - start_meeting, + // end_meeting: room_number; start_meeting: end_time + priority_queue, long long>, vector, long long>>, greater, long long>>> events; + for(int i = 0; i < meetings.size(); i ++){ + long long start_time = meetings[i][0], end_time = meetings[i][1]; + events.push({{start_time, START_EVENT}, end_time}); + } + set availables; + set> waiting_list; // start, duration + for(int i = 0; i < n; i ++) availables.insert(i); + + vector cnt(n, 0); + while(!events.empty()){ + long long start_time = events.top().first.first; + int type = events.top().first.second; + long long x = events.top().second; + events.pop(); + + if(type == START_EVENT){ + long long end_time = x; + if(!availables.empty()){ + int room_id = *availables.begin(); + availables.erase(availables.begin()); + cnt[room_id] ++; + events.push({{end_time, END_EVENT}, room_id}); + } + else{ + waiting_list.insert({start_time, end_time - start_time}); + } + } + else{ + int room_id = x; + if(waiting_list.empty()){ + availables.insert(room_id); + continue; + } + + long long duration = waiting_list.begin()->second; + waiting_list.erase(waiting_list.begin()); + + cnt[room_id] ++; + events.push({{start_time + duration, END_EVENT}, room_id}); + } + } + + int best = -1, res = -1; + for(int i = 0; i < n; i ++) + if(cnt[i] > best) best = cnt[i], res = i; + return res; + } +}; + + +int main() { + + vector> meetings1 = {{0, 10}, {1, 5}, {2, 7}, {3, 4}}; + cout << Solution().mostBooked(2, meetings1) << '\n'; + // 0 + + vector> meetings2 = {{1, 20}, {2, 10}, {3, 5}, {4, 9}, {6, 8}}; + cout << Solution().mostBooked(3, meetings2) << '\n'; + // 1 + + return 0; +} diff --git a/readme.md b/readme.md index 90604a72..0bb4d6ad 100644 --- a/readme.md +++ b/readme.md @@ -2257,6 +2257,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2396 | [Strictly Palindromic Number](https://leetcode.com/problems/strictly-palindromic-number/) | [无] | [C++](2001-2500/2396-Strictly-Palindromic-Number/cpp-2396/) | | | | 2397 | [Maximum Rows Covered by Columns](https://leetcode.com/problems/maximum-rows-covered-by-columns/) | [无] | [C++](2001-2500/2397-Maximum-Rows-Covered-by-Columns/cpp-2397/) | | | | 2398 | [Maximum Number of Robots Within Budget](https://leetcode.com/problems/maximum-number-of-robots-within-budget/) | [无][缺:滑动窗口] | [C++](2001-2500/2398-Maximum-Number-of-Robots-Within-Budget/cpp-2398/) | | | +| 2399 | [Check Distances Between Same Letters](https://leetcode.com/problems/check-distances-between-same-letters/) | [无] | [C++](2001-2500/2399-Check-Distances-Between-Same-Letters/cpp-2399/) | | | +| 2400 | [Number of Ways to Reach a Position After Exactly k Steps](https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/) | [无][缺:数学解] | [C++](2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/) | | | +| 2401 | [Longest Nice Subarray](https://leetcode.com/problems/longest-nice-subarray/) | [无] | [C++](2001-2500/2401-Longest-Nice-Subarray/cpp-2401/) | | | +| 2402 | [Meeting Rooms III](https://leetcode.com/problems/meeting-rooms-iii/) | [无] | [C++](2001-2500/2402-Meeting-Rooms-III/cpp-2402/) | | | | | | | | | | ## 力扣中文站比赛 From 5df838795b6395b03e47c66f4fe93df3dc4107f5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 5 Sep 2022 22:52:44 -0700 Subject: [PATCH 179/390] 0828 solved. --- .../cpp-0828/CMakeLists.txt | 6 +++ .../cpp-0828/main.cpp | 54 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/CMakeLists.txt create mode 100644 0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/main.cpp diff --git a/0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/CMakeLists.txt b/0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/CMakeLists.txt new file mode 100644 index 00000000..946dcfd2 --- /dev/null +++ b/0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0828) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0828 main.cpp) diff --git a/0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/main.cpp b/0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/main.cpp new file mode 100644 index 00000000..81df2c5b --- /dev/null +++ b/0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/ +/// Author : liuyubobobo +/// Time : 2022-09-05 + +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Compelxity: O(n) +class Solution { +public: + int uniqueLetterString(string s) { + + vector> pos(26); + for(int i = 0; i < s.size(); i ++) + pos[s[i] - 'A'].push_back(i); + + vector indexes(26, 0); + + int res = 0; + for(int start = 0; start < s.size(); start ++){ + for(int c = 0; c < 26; c ++){ + int a = indexes[c]; + if(a >= pos[c].size()) continue; + + if(a + 1 >= pos[c].size()) res += (s.size() - pos[c][a]); + else res += pos[c][a + 1] - pos[c][a]; + + if(pos[c][a] == start) indexes[c] ++; + } + } + return res; + } +}; + +int main() { + + cout << Solution().uniqueLetterString("ABC") << '\n'; + // 10 + + cout << Solution().uniqueLetterString("ABA") << '\n'; + // 8 + + cout << Solution().uniqueLetterString("LEETCODE") << '\n'; + // 92 + + return 0; +} diff --git a/readme.md b/readme.md index 0bb4d6ad..527896cc 100644 --- a/readme.md +++ b/readme.md @@ -824,7 +824,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 825 | [Friends Of Appropriate Ages](https://leetcode.com/problems/friends-of-appropriate-ages/) | [无] | [C++](0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/) | | | | | | | | | | | 827 | [Making A Large Island](https://leetcode.com/problems/making-a-large-island/) | [无] | [C++](0501-1000/0827-Making-A-Large-Island/cpp-0827/) | | | -| | | | | | | +| 828 | [Count Unique Characters of All Substrings of a Given String](https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [无] | [C++](0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/) | | | | 829 | [Consecutive Numbers Sum](https://leetcode.com/problems/consecutive-numbers-sum/) | [无] | [C++](0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/) | | | | 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [solution](https://leetcode.com/problems/positions-of-large-groups/solution/) | [C++](0501-1000/0830-Positions-of-Large-Groups/cpp-0830/) | | | | | | | | | | From c3ba2e3bf186497746d86a628ab3062028d3727e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 7 Sep 2022 05:02:17 -0700 Subject: [PATCH 180/390] 2387 solved. --- .../cpp-2387/CMakeLists.txt | 6 +++ .../cpp-2387/main.cpp | 47 +++++++++++++++++++ readme.md | 2 + 3 files changed, 55 insertions(+) create mode 100644 2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/CMakeLists.txt create mode 100644 2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/main.cpp diff --git a/2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/CMakeLists.txt b/2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/CMakeLists.txt new file mode 100644 index 00000000..2c3ab7b3 --- /dev/null +++ b/2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2387) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2387 main.cpp) diff --git a/2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/main.cpp b/2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/main.cpp new file mode 100644 index 00000000..38ee52ea --- /dev/null +++ b/2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/median-of-a-row-wise-sorted-matrix/ +/// Author : liuyubobobo +/// Time : 2022-09-07 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(R * logC * log(maxv)) +/// Space Complexity: O(1) +class Solution { + +public: + int matrixMedian(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + int n = (R * C + 1) / 2; + + int l = 1, r = 1e6; + while(l < r){ + int mid = (l + r) / 2; + if(less_than_or_equal_to(R, grid, mid) < n) l = mid + 1; + else r = mid; + } + return l; + } + +private: + int less_than_or_equal_to(int R, const vector>& grid, int v){ + + int res = 0; + for(int i = 0; i < R; i ++){ + res += upper_bound(grid[i].begin(), grid[i].end(), v) - grid[i].begin(); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 527896cc..3fd25ef7 100644 --- a/readme.md +++ b/readme.md @@ -2247,6 +2247,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2384 | [Largest Palindromic Number](https://leetcode.com/problems/largest-palindromic-number/) | [无] | [C++](2001-2500/2384-Largest-Palindromic-Number/cpp-2384/) | | | | 2385 | [Amount of Time for Binary Tree to Be Infected](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/) | [无] | [C++](2001-2500/2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected/cpp-2385/) | | | | | | | | | | +| 2387 | [Median of a Row Wise Sorted Matrix](https://leetcode.com/problems/median-of-a-row-wise-sorted-matrix/) | [无] | [C++](2001-2500/2387-Median-of-a-Row-Wise-Sorted-Matrix/cpp-2387/) | | | +| 2388 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum/) | [无] | [C++](2001-2500/2389-Longest-Subsequence-With-Limited-Sum/cpp-2389/) | | | | 2390 | [Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/) | [无] | [C++](2001-2500/2390-Removing-Stars-From-a-String/cpp-2390/) | | | | 2391 | [Minimum Amount of Time to Collect Garbage](https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/) | [无] | [C++](2001-2500/2391-Minimum-Amount-of-Time-to-Collect-Garbage/cpp-2391/) | | | From a5ef7e65c6fd9ddf3ab6fc2106e2e599f94a8e9c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 7 Sep 2022 17:18:18 -0700 Subject: [PATCH 181/390] 0362 solved. --- .../cpp-0362/CMakeLists.txt | 6 +++ .../0362-Design-Hit-Counter/cpp-0362/main.cpp | 39 +++++++++++++++++++ readme.md | 1 + 3 files changed, 46 insertions(+) create mode 100644 0001-0500/0362-Design-Hit-Counter/cpp-0362/CMakeLists.txt create mode 100644 0001-0500/0362-Design-Hit-Counter/cpp-0362/main.cpp diff --git a/0001-0500/0362-Design-Hit-Counter/cpp-0362/CMakeLists.txt b/0001-0500/0362-Design-Hit-Counter/cpp-0362/CMakeLists.txt new file mode 100644 index 00000000..416f2838 --- /dev/null +++ b/0001-0500/0362-Design-Hit-Counter/cpp-0362/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0362) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0362 main.cpp) diff --git a/0001-0500/0362-Design-Hit-Counter/cpp-0362/main.cpp b/0001-0500/0362-Design-Hit-Counter/cpp-0362/main.cpp new file mode 100644 index 00000000..af67ce68 --- /dev/null +++ b/0001-0500/0362-Design-Hit-Counter/cpp-0362/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/design-hit-counter/ +/// Author : liuyubobobo +/// Time : 2022-09-07 + +#include +#include +#include + +using namespace std; + + +/// Using deque +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class HitCounter { + +private: + deque hits; + +public: + HitCounter() {} + + void hit(int timestamp) { + hits.push_back(timestamp); + while(timestamp - hits.front() + 1 > 300) hits.pop_front(); + } + + int getHits(int timestamp) { + + while(!hits.empty() && timestamp - hits.front() + 1 > 300) hits.pop_front(); + return hits.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3fd25ef7..9aaafac8 100644 --- a/readme.md +++ b/readme.md @@ -396,6 +396,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/description/) | [无] | [C++](0001-0500/0359-Logger-Rate-Limiter/cpp-0359/) | | | | 360 | [Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/description/) | [无] | [C++](0001-0500/0360-Sort-Transformed-Array/cpp-0360/) | | | | | | | | | | +| 362 | [Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [无] | [C++](0001-0500/0362-Design-Hit-Counter/cpp-0362/) | | | | 363 | [Max Sum of Rectangle No Larger Than K](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [无] | [C++](0001-0500/0363-Max-Sum-of-Rectangle-No-Larger-Than-K/cpp-0363/) | | | | 364 | [Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/) | [无] | [C++](0001-0500/0364-Nested-List-Weight-Sum-II/cpp-0364/) | | | | 365 | [Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem/) | [题解](https://leetcode-cn.com/problems/water-and-jug-problem/solution/shui-hu-wen-ti-by-leetcode-solution/)
[缺:数学解法] | [C++](0001-0500/0365-Water-and-Jug-Problem/cpp-0365/) | | | From 65379a116e7fbccc48f2ba5a1e5cb801377db957 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 7 Sep 2022 18:50:26 -0700 Subject: [PATCH 182/390] 2403 solved. --- .../cpp-2403/CMakeLists.txt | 6 +++ .../cpp-2403/main.cpp | 46 +++++++++++++++++++ readme.md | 1 + 3 files changed, 53 insertions(+) create mode 100644 2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/CMakeLists.txt create mode 100644 2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/main.cpp diff --git a/2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/CMakeLists.txt b/2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/CMakeLists.txt new file mode 100644 index 00000000..d94035d7 --- /dev/null +++ b/2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2403) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2403 main.cpp) diff --git a/2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/main.cpp b/2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/main.cpp new file mode 100644 index 00000000..a26248c3 --- /dev/null +++ b/2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-kill-all-monsters/ +/// Author : liuyubobobo +/// Time : 2022-09-07 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(2^n * n) +/// Space Complexity: O(2^n) +class Solution { +public: + long long minimumTime(vector& power) { + + int n = power.size(); + + vector dp(1 << n, -1); + return dfs(n, power, 0, dp); + } + +private: + long long dfs(int n, const vector& power, int state, + vector& dp){ + + if(state + 1 == (1 << n)) return 0; + if(dp[state] != -1) return dp[state]; + + long long gain = __builtin_popcount(state) + 1; + long long res = LONG_LONG_MAX; + for(int i = 0; i < n; i ++){ + if((state >> i) & 1) continue; + long long days = power[i] / gain + !!(power[i] % gain); + res = min(res, days + dfs(n, power, state | (1 << i), dp)); + } + return dp[state] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9aaafac8..1f89e10a 100644 --- a/readme.md +++ b/readme.md @@ -2264,6 +2264,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2400 | [Number of Ways to Reach a Position After Exactly k Steps](https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/) | [无][缺:数学解] | [C++](2001-2500/2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps/cpp-2400/) | | | | 2401 | [Longest Nice Subarray](https://leetcode.com/problems/longest-nice-subarray/) | [无] | [C++](2001-2500/2401-Longest-Nice-Subarray/cpp-2401/) | | | | 2402 | [Meeting Rooms III](https://leetcode.com/problems/meeting-rooms-iii/) | [无] | [C++](2001-2500/2402-Meeting-Rooms-III/cpp-2402/) | | | +| 2403 | [Minimum Time to Kill All Monsters](https://leetcode.com/problems/minimum-time-to-kill-all-monsters/) | [无] | [C++](2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/) | | | | | | | | | | ## 力扣中文站比赛 From e3ac95dd00539677ce8a59dffc43ef39f4c361b7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 12 Sep 2022 08:49:44 -0700 Subject: [PATCH 183/390] 2404-2407 solved. --- .../cpp-2404/CMakeLists.txt | 6 + .../cpp-2404/main.cpp | 34 +++++ .../cpp-2405/CMakeLists.txt | 6 + .../cpp-2405/main.cpp | 36 +++++ .../cpp-2406/CMakeLists.txt | 6 + .../cpp-2406/main.cpp | 45 ++++++ .../cpp-2406/main2.cpp | 45 ++++++ .../cpp-2407/CMakeLists.txt | 6 + .../cpp-2407/main.cpp | 128 ++++++++++++++++++ readme.md | 4 + 10 files changed, 316 insertions(+) create mode 100644 2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/CMakeLists.txt create mode 100644 2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/main.cpp create mode 100644 2001-2500/2405-Optimal-Partition-of-String/cpp-2405/CMakeLists.txt create mode 100644 2001-2500/2405-Optimal-Partition-of-String/cpp-2405/main.cpp create mode 100644 2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/CMakeLists.txt create mode 100644 2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main.cpp create mode 100644 2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main2.cpp create mode 100644 2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/CMakeLists.txt create mode 100644 2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/main.cpp diff --git a/2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/CMakeLists.txt b/2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/main.cpp b/2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/main.cpp new file mode 100644 index 00000000..bd309f64 --- /dev/null +++ b/2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/most-frequent-even-element/ +/// Author : liuyubobobo +/// Time : 2022-09-10 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int mostFrequentEven(vector& nums) { + + map f; + for(int e: nums) + if(e % 2 == 0) f[e] ++; + + int res = -1, best_f = 0; + for(const pair& p: f) + if(p.second > best_f) res = p.first, best_f = p.second; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2405-Optimal-Partition-of-String/cpp-2405/CMakeLists.txt b/2001-2500/2405-Optimal-Partition-of-String/cpp-2405/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2405-Optimal-Partition-of-String/cpp-2405/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2405-Optimal-Partition-of-String/cpp-2405/main.cpp b/2001-2500/2405-Optimal-Partition-of-String/cpp-2405/main.cpp new file mode 100644 index 00000000..862e30ce --- /dev/null +++ b/2001-2500/2405-Optimal-Partition-of-String/cpp-2405/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/optimal-partition-of-string/ +/// Author : liuyubobobo +/// Time : 2022-09-10 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int partitionString(string s) { + + vector used(26, false); + int res = 1; + for(char c: s){ + if(used[c - 'a']){ + res ++; + used.assign(26, false); + } + used[c - 'a'] = true; + } + return res; + } +}; + + +int main() { + + + return 0; +} diff --git a/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/CMakeLists.txt b/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/CMakeLists.txt new file mode 100644 index 00000000..148bffbf --- /dev/null +++ b/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main.cpp b/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main.cpp new file mode 100644 index 00000000..443a3834 --- /dev/null +++ b/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/ +/// Author : liuyubobobo +/// Time : 2022-09-10 + +#include +#include +#include +#include + +using namespace std; + + +/// Greedy and Using Set +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int minGroups(vector>& intervals) { + + sort(intervals.begin(), intervals.end()); + + multiset> ends; + for(const vector& interval: intervals){ + int a = interval[0], b = interval[1]; + auto iter = ends.upper_bound(a); + if(iter != ends.end()) ends.erase(iter); + ends.insert(b); + } + return ends.size(); + } +}; + + +int main() { + + vector> intervals1 = {{5, 10}, {6, 8}, {1, 5}, {2, 3}, {1, 10}}; + cout << Solution().minGroups(intervals1) << '\n'; + // 3 + + vector> intervals2 = {{1, 3}, {5, 6}, {8, 10}, {11, 13}}; + cout << Solution().minGroups(intervals2) << '\n'; + // 1 + + return 0; +} diff --git a/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main2.cpp b/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main2.cpp new file mode 100644 index 00000000..b613fcb5 --- /dev/null +++ b/2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/main2.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/ +/// Author : liuyubobobo +/// Time : 2022-09-12 + +#include +#include +#include +#include + +using namespace std; + + +/// Greedy and Using PQ +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int minGroups(vector>& intervals) { + + sort(intervals.begin(), intervals.end()); + + priority_queue, greater> ends; + for(const vector& interval: intervals){ + int a = interval[0], b = interval[1]; + if(!ends.empty() && a > ends.top()) + ends.pop(); + ends.push(b); + } + return ends.size(); + } +}; + + +int main() { + + vector> intervals1 = {{5, 10}, {6, 8}, {1, 5}, {2, 3}, {1, 10}}; + cout << Solution().minGroups(intervals1) << '\n'; + // 3 + + vector> intervals2 = {{1, 3}, {5, 6}, {8, 10}, {11, 13}}; + cout << Solution().minGroups(intervals2) << '\n'; + // 1 + + return 0; +} diff --git a/2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/CMakeLists.txt b/2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/main.cpp b/2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/main.cpp new file mode 100644 index 00000000..dcd44ca2 --- /dev/null +++ b/2001-2500/2407-Longest-Increasing-Subsequence-II/cpp-2407/main.cpp @@ -0,0 +1,128 @@ +/// Source : https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/ +/// Author : liuyubobobo +/// Time : 2022-09-10 + +#include +#include + +using namespace std; + + +/// DP + Segment Tree +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class SegmentTree{ + +private: + int n; + vector data, tree; + T (*combine)(T a, T b); + +public: + SegmentTree(const vector& data, T (*combine)(T a, T b)): n(data.size()), data(data), tree(4 * n, 0){ + this->combine = combine; + buildSegTree(0, 0, n - 1); + } + + SegmentTree(int n, T (*combine)(T a, T b)): n(n), data(n, 0), tree(4 * n, 0){ + this->combine = combine; + } + + void update(int index, T value){ + if(data[index] == value) return; + data[index] = value; + update(0, 0, n - 1, index, value); + } + + T query(int index){ + assert(0 <= index && index < n); + return data[index]; + } + + T query(int l, int r){ + if(l > r) return 0; + return query(0, 0, n - 1, l, r); + } + +private: + void buildSegTree(int treeID, int l, int r){ + + if(l == r){ + tree[treeID] = data[l]; + return; + } + + int mid = (l + r) / 2; + buildSegTree(treeID * 2 + 1, l, mid); + buildSegTree(treeID * 2 + 2, mid + 1, r); + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + void update(int treeID, int l, int r, int index, T value){ + + if(l == r){ + assert(l == index); + tree[treeID] = value; + return; + } + + int mid = (l + r) / 2; + if(index <= mid) update(treeID * 2 + 1, l, mid, index, value); + else update(treeID * 2 + 2, mid + 1, r, index, value); + + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + T query(int treeID, int l, int r, int ql, int qr){ + + if(ql == l && qr == r) + return tree[treeID]; + + int mid = (l + r) / 2; + if(qr <= mid) return query(treeID * 2 + 1, l, mid, ql, qr); + else if(ql > mid) return query(treeID * 2 + 2, mid + 1, r, ql, qr); + + T resl = query(treeID * 2 + 1, l, mid, ql, mid); + T resr = query(treeID * 2 + 2, mid + 1, r, mid + 1, qr); + return combine(resl, resr); + } +}; + +class Solution { +public: + int lengthOfLIS(vector& nums, int k) { + + int maxv = *max_element(nums.begin(), nums.end()); + SegmentTree seg_tree(maxv + 1, [](int a, int b){return max(a, b);}); + + seg_tree.update(nums[0], 1); + for(int i = 1; i < nums.size(); i ++){ + int e = nums[i]; + int l = max(e - k, 0), r = e - 1; + int t = seg_tree.query(l, r); + seg_tree.update(e, max(seg_tree.query(e), t + 1)); + } + return seg_tree.query(0, maxv); + } +}; + + +int main() { + + vector nums1 = {4,2,1,4,3,4,5,8,15}; + cout << Solution().lengthOfLIS(nums1, 3) << '\n'; + // 5 + + vector nums2 = {7,4,5,1,8,12,4,7}; + cout << Solution().lengthOfLIS(nums2, 5) << '\n'; + // 4 + + vector nums3 = {1, 5}; + cout << Solution().lengthOfLIS(nums3, 1) << '\n'; + // 1 + + return 0; +} diff --git a/readme.md b/readme.md index 1f89e10a..77355bff 100644 --- a/readme.md +++ b/readme.md @@ -2265,6 +2265,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2401 | [Longest Nice Subarray](https://leetcode.com/problems/longest-nice-subarray/) | [无] | [C++](2001-2500/2401-Longest-Nice-Subarray/cpp-2401/) | | | | 2402 | [Meeting Rooms III](https://leetcode.com/problems/meeting-rooms-iii/) | [无] | [C++](2001-2500/2402-Meeting-Rooms-III/cpp-2402/) | | | | 2403 | [Minimum Time to Kill All Monsters](https://leetcode.com/problems/minimum-time-to-kill-all-monsters/) | [无] | [C++](2001-2500/2403-Minimum-Time-to-Kill-All-Monsters/cpp-2403/) | | | +| 2404 | [Most Frequent Even Element](https://leetcode.com/problems/most-frequent-even-element/) | [无] | [C++](2001-2500/2404-Most-Frequent-Even-Element/cpp-2404/) | | | +| 2405 | [Optimal Partition of String](https://leetcode.com/problems/optimal-partition-of-string/) | [无] | [C++](2001-2500/2405-Optimal-Partition-of-String/cpp-2405/) | | | +| 2406 | [Divide Intervals Into Minimum Number of Groups](https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/) | [无] | [C++](2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/) | | | +| 2407 | [Longest Increasing Subsequence II](https://leetcode.com/problems/longest-increasing-subsequence-ii/) | [无] | [C++](2001-2500/2407-Longest-Increasing-Subsequence-II/cpp2407/) | | | | | | | | | | ## 力扣中文站比赛 From 876234554564ddd2f2cbcd48790fb4d759143a81 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 12 Sep 2022 10:14:33 -0700 Subject: [PATCH 184/390] 0670 solved. --- .../0670-Maximum-Swap/cpp-0670/CMakeLists.txt | 6 ++++ 0501-1000/0670-Maximum-Swap/cpp-0670/main.cpp | 36 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0670-Maximum-Swap/cpp-0670/CMakeLists.txt create mode 100644 0501-1000/0670-Maximum-Swap/cpp-0670/main.cpp diff --git a/0501-1000/0670-Maximum-Swap/cpp-0670/CMakeLists.txt b/0501-1000/0670-Maximum-Swap/cpp-0670/CMakeLists.txt new file mode 100644 index 00000000..c33ca0a9 --- /dev/null +++ b/0501-1000/0670-Maximum-Swap/cpp-0670/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0670) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0670 main.cpp) diff --git a/0501-1000/0670-Maximum-Swap/cpp-0670/main.cpp b/0501-1000/0670-Maximum-Swap/cpp-0670/main.cpp new file mode 100644 index 00000000..e32a7b13 --- /dev/null +++ b/0501-1000/0670-Maximum-Swap/cpp-0670/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/maximum-swap/ +/// Author : liuyubobobo +/// Time : 2022-09-12 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O((log(num))^2) +/// Space Complexity: O(log(num)) +class Solution { +public: + int maximumSwap(int num) { + + if(num < 10) return num; + + string num_s = to_string(num); + for(int i = 0; i < num_s.size(); i ++) + for(int j = i + 1; j < num_s.size(); j ++){ + swap(num_s[i], num_s[j]); + int x = atoi(num_s.c_str()); + num = max(num, x); + swap(num_s[i], num_s[j]); + } + return num; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 77355bff..fc81b4b3 100644 --- a/readme.md +++ b/readme.md @@ -683,7 +683,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 667 | [Beautiful Arrangement II](https://leetcode.com/problems/beautiful-arrangement-ii/) | [solution](https://leetcode.com/problems/beautiful-arrangement-ii/solution/) | [C++](0501-1000/0667-Beautiful-Arrangement-II/ccp-0667/) | | | | 668 | [Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/) | [solution](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/) | [C++](0501-1000/0668-Kth-Smallest-Number-in-Multiplication-Table/cpp-0668/) | | | | 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/) | [solution](https://leetcode.com/problems/trim-a-binary-search-tree/solution/) | [C++](0501-1000/0669-Trim-a-Binary-Search-Tree/cpp-0669/) | | | -| | | | | | | +| 670 | [Maximum Swap](https://leetcode.com/problems/maximum-swap/) | [无] | [C++](0501-1000/0670-Maximum-Swap/cpp-0670/) | | | | 671 | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [solution](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/solution/) | [C++](0501-1000/0671-Second-Minimum-Node-In-a-Binary-Tree/cpp-0671/) | | | | 672 | [Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/description/) | [solution](https://leetcode.com/problems/bulb-switcher-ii/solution/) | [C++](0501-1000/0672-Bulb-Switcher-II/cpp-0672/) | | | | 673 | [Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/) | [缺:BIT;线段树] | [C++](0501-1000/0673-Number-of-Longest-Increasing-Subsequence/cpp-0673/) | | | From 619c8ccecf40d5e10be90cdab627c0c3ec8fe7f0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 20 Sep 2022 10:46:04 -0700 Subject: [PATCH 185/390] 2409-2411 added. --- .../cpp-2409/CMakeLists.txt | 6 ++ .../cpp-2409/main.cpp | 55 ++++++++++++++++ .../cpp-2410/CMakeLists.txt | 6 ++ .../cpp-2410/main.cpp | 33 ++++++++++ .../cpp-2411/CMakeLists.txt | 6 ++ .../cpp-2411/main.cpp | 65 +++++++++++++++++++ readme.md | 4 ++ 7 files changed, 175 insertions(+) create mode 100644 2001-2500/2409-Count-Days-Spent-Together/cpp-2409/CMakeLists.txt create mode 100644 2001-2500/2409-Count-Days-Spent-Together/cpp-2409/main.cpp create mode 100644 2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/CMakeLists.txt create mode 100644 2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/main.cpp create mode 100644 2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/CMakeLists.txt create mode 100644 2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/main.cpp diff --git a/2001-2500/2409-Count-Days-Spent-Together/cpp-2409/CMakeLists.txt b/2001-2500/2409-Count-Days-Spent-Together/cpp-2409/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2409-Count-Days-Spent-Together/cpp-2409/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2409-Count-Days-Spent-Together/cpp-2409/main.cpp b/2001-2500/2409-Count-Days-Spent-Together/cpp-2409/main.cpp new file mode 100644 index 00000000..b099fe9b --- /dev/null +++ b/2001-2500/2409-Count-Days-Spent-Together/cpp-2409/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/count-days-spent-together/ +/// Author : liuyubobobo +/// Time : 2022-09-17 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { + +private: + const vector months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +public: + int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) { + + int a = get_day_index(arriveAlice); + int b = get_day_index(leaveAlice); + + int c = get_day_index(arriveBob); + int d = get_day_index(leaveBob); + + int x = max(a, c), y = min(b, d); + return max(0, y - x + 1); + } + +private: + int get_day_index(string date){ + + int month = atoi(date.substr(0, 2).c_str()); + int day = atoi(date.substr(3).c_str()); + + int res = 0; + for(int i = 1; i < month ; i ++) res += months[i]; + res += day; + return res; + } +}; + + +int main() { + + cout << Solution().countDaysTogether("08-15", "08-18", "08-16", "08-19") << '\n'; + // 3 + + cout << Solution().countDaysTogether("10-01", "10-31", "11-01", "12-31") << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/CMakeLists.txt b/2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/main.cpp b/2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/main.cpp new file mode 100644 index 00000000..1362009c --- /dev/null +++ b/2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp-2410/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/maximum-matching-of-players-with-trainers/ +/// Author : liuyubobobo +/// Time : 2022-09-17 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int matchPlayersAndTrainers(vector& players, vector& trainers) { + + sort(players.begin(), players.end(), greater()); + sort(trainers.begin(), trainers.end(), greater()); + + int res = 0, i = 0, j = 0; + for(i = 0; i < players.size() && j < trainers.size(); i ++) + if(players[i] <= trainers[j]) res ++, j ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/CMakeLists.txt b/2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/main.cpp b/2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/main.cpp new file mode 100644 index 00000000..9467affd --- /dev/null +++ b/2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp-2411/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/ +/// Author : liuyubobobo +/// Time : 2022-09-17 + +#include +#include + +using namespace std; + + +/// Bitwise + Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +private: + const int L = 30; + +public: + vector smallestSubarrays(vector& nums) { + + int n = nums.size(); + + vector> presum(L, vector(n + 1, 0)); + for(int i = 0; i < n; i ++){ + for(int p = 0; p < L; p ++) + presum[p][i + 1] = (nums[i] >> p) & 1; + } + + for(int p = 0; p < L; p ++) + for(int i = 0; i < n; i ++) presum[p][i + 1] += presum[p][i]; + + vector res(n, 1); + for(int i = 0; i < n; i ++){ + int t = 1; + for(int p = 0; p < L; p ++){ + if((nums[i] >> p) & 1) continue; + if(presum[p][i + 1] == presum[p].back()) continue; + auto iter = lower_bound(presum[p].begin() + (i + 1), presum[p].end(), presum[p][i + 1] + 1); + int index = iter - presum[p].begin(); + t = max(t, index - (i + 1) + 1); + } + res[i] = t; + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector nums1 = {1, 0, 2, 1, 3}; + print_vec(Solution().smallestSubarrays(nums1)); + // 3 3 2 2 1 + + vector nums2 = {1, 2}; + print_vec(Solution().smallestSubarrays(nums2)); + // 2 1 + + return 0; +} diff --git a/readme.md b/readme.md index fc81b4b3..a14390e7 100644 --- a/readme.md +++ b/readme.md @@ -2270,6 +2270,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2406 | [Divide Intervals Into Minimum Number of Groups](https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/) | [无] | [C++](2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/) | | | | 2407 | [Longest Increasing Subsequence II](https://leetcode.com/problems/longest-increasing-subsequence-ii/) | [无] | [C++](2001-2500/2407-Longest-Increasing-Subsequence-II/cpp2407/) | | | | | | | | | | +| 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [无] | [C++](2001-2500/2409-Count-Days-Spent-Together/cpp2409/) | | | +| 2410 | [Maximum Matching of Players With Trainers](https://leetcode.com/problems/maximum-matching-of-players-with-trainers/) | [无] | [C++](2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp2410/) | | | +| 2411 | [Smallest Subarrays With Maximum Bitwise OR](https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/) | [无][缺:O(n) 算法] | [C++](2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp2411/) | | | +| | | | | | | ## 力扣中文站比赛 From 0ff2fe4d8c6bbb5c514da7b5dd31b77c0c1db422 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 20 Sep 2022 10:46:39 -0700 Subject: [PATCH 186/390] 0336 hash algo added. --- .../0336-Palindrome-Pairs/cpp-0336/main.cpp | 87 +++++++++++-------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/0001-0500/0336-Palindrome-Pairs/cpp-0336/main.cpp b/0001-0500/0336-Palindrome-Pairs/cpp-0336/main.cpp index 65f18329..94dc7526 100644 --- a/0001-0500/0336-Palindrome-Pairs/cpp-0336/main.cpp +++ b/0001-0500/0336-Palindrome-Pairs/cpp-0336/main.cpp @@ -1,6 +1,6 @@ /// Source : https://leetcode.com/problems/palindrome-pairs/ /// Author : liuyubobobo -/// Time : 2021-06-14 +/// Time : 2022-09-17 #include #include @@ -10,58 +10,67 @@ using namespace std; -/// Using HashMap -/// Time Complexity: O(n*k^2) -/// Space Complexity: O(nk) +/// String Hash +/// Time Complexity: O(n^2 * k) where k is the average length pf each words +/// Space Complexity: O(n * k) class Solution { + +private: + const unsigned long long B = 13331; + public: vector> palindromePairs(vector& words) { - unordered_map table; - for(int i = 0; i < words.size(); i ++) - table[words[i]] = i; + int n = words.size(); + vector> hash(n), rhash(n); - set> res; - for(int i = 0; i < words.size(); i ++){ + int maxlen = 0; + for(int i = 0; i < n; i ++){ + int len = words[i].size(); + maxlen = max(maxlen, len); - string a = ""; - unordered_map::iterator iter; - for(int j = 0; j < words[i].size(); j ++){ - a = string(1, words[i][j]) + a; - iter = table.find(a); - if(iter != table.end() && i != iter->second && is_palindrome(words[i], j + 1, words[i].size() - 1)) - res.insert({i, iter->second}); - } + hash[i].resize(len + 1, 0); + for(int j = 0; j < len; j ++) + hash[i][j + 1] = hash[i][j] * B + words[i][j]; - a = ""; - for(int j = words[i].size() - 1; j >= 0; j --){ - a += words[i][j]; - iter = table.find(a); - if(iter != table.end() && i != iter->second && is_palindrome(words[i], 0, j - 1)) - res.insert({iter->second, i}); - } - - iter = table.find(""); - if(iter != table.end() && i != iter->second && is_palindrome(words[i], 0, words[i].size() - 1)) - res.insert({i, iter->second}), res.insert({iter->second, i}); + rhash[i].resize(len + 1, 0); + for(int j = len - 1; j >= 0; j --) + rhash[i][j] = rhash[i][j + 1] * B + words[i][j]; } - return vector>(res.begin(), res.end()); - } -private: - bool is_palindrome(const string& s, int start, int end){ + vector powB(maxlen + 1, 1); + for(int i = 1; i <= maxlen; i ++) powB[i] = powB[i - 1] * B; + + vector> res; + for(int i = 0; i < n; i ++){ + int len1 = words[i].size(); + for(int j = 0; j < n; j ++){ + + if(j == i) continue; - if(start >= end) return true; + int len2 = words[j].size(); - for(int i = start, j = end; i < j; i ++, j --) - if(s[i] != s[j]) return false; - return true; + if(len1 <= len2){ + if(hash[i].back() != rhash[j][len2 - len1]) continue; + if(hash[j][len2 - len1] != rhash[j][0] - rhash[j][len2 - len1] * powB[len2 - len1]) continue; + res.push_back({i, j}); + } + else{ // len1 > len2 + if(hash[i][len2] != rhash[j][0]) continue; + if(hash[i].back() - hash[i][len2] * powB[len1 - len2] != rhash[i][len2]) continue; + res.push_back({i, j}); + } + } + } + return res; } }; void print_res(const vector>& v){ - for(const vector& e: v) cout << e[0] << " " << e[1] << endl; + for(const vector& e: v) + cout << "(" << e[0] << "," << e[1] << ")"; + cout << '\n'; } int main() { @@ -69,5 +78,9 @@ int main() { vector words1 = {"a","ab"}; print_res(Solution().palindromePairs(words1)); + vector words2 = {"abcd","dcba","lls","s","sssll"}; + print_res(Solution().palindromePairs(words2)); + // [[0,1],[1,0],[3,2],[2,4]] + return 0; } From 3f6f323fe51965b742c8118c2043b547766f0f09 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 20 Sep 2022 13:34:34 -0700 Subject: [PATCH 187/390] 2413 solved. --- .../cpp-2413/CMakeLists.txt | 6 +++++ .../cpp-2413/main.cpp | 26 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 34 insertions(+) create mode 100644 2001-2500/2413-Smallest-Even-Multiple/cpp-2413/CMakeLists.txt create mode 100644 2001-2500/2413-Smallest-Even-Multiple/cpp-2413/main.cpp diff --git a/2001-2500/2413-Smallest-Even-Multiple/cpp-2413/CMakeLists.txt b/2001-2500/2413-Smallest-Even-Multiple/cpp-2413/CMakeLists.txt new file mode 100644 index 00000000..daf73152 --- /dev/null +++ b/2001-2500/2413-Smallest-Even-Multiple/cpp-2413/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2413) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2413 main.cpp) diff --git a/2001-2500/2413-Smallest-Even-Multiple/cpp-2413/main.cpp b/2001-2500/2413-Smallest-Even-Multiple/cpp-2413/main.cpp new file mode 100644 index 00000000..530da38d --- /dev/null +++ b/2001-2500/2413-Smallest-Even-Multiple/cpp-2413/main.cpp @@ -0,0 +1,26 @@ +/// Source : https://leetcode.com/problems/smallest-even-multiple/submissions/ +/// Author : liuyubobobo +/// Time : 2022-09-20 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int smallestEvenMultiple(int n) { + + if(n % 2 == 0) return n; + return n << 1; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a14390e7..c65d2b9c 100644 --- a/readme.md +++ b/readme.md @@ -2274,6 +2274,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2410 | [Maximum Matching of Players With Trainers](https://leetcode.com/problems/maximum-matching-of-players-with-trainers/) | [无] | [C++](2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp2410/) | | | | 2411 | [Smallest Subarrays With Maximum Bitwise OR](https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/) | [无][缺:O(n) 算法] | [C++](2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp2411/) | | | | | | | | | | +| 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [无] | [C++](2001-2500/2413-Smallest-Even-Multiple/cpp-2413/) | | | +| | | | | | | ## 力扣中文站比赛 From 9f8abd7da91698d48ba776b51b281bdcf32b27f6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 20 Sep 2022 13:51:22 -0700 Subject: [PATCH 188/390] 2414 solved. --- .../cpp-2414/CMakeLists.txt | 6 +++++ .../cpp-2414/main.cpp | 23 +++++++++++++++++++ readme.md | 1 + 3 files changed, 30 insertions(+) create mode 100644 2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/CMakeLists.txt create mode 100644 2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp diff --git a/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/CMakeLists.txt b/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/CMakeLists.txt new file mode 100644 index 00000000..8f903bf3 --- /dev/null +++ b/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2414) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2414 main.cpp) diff --git a/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp b/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp new file mode 100644 index 00000000..ebb44ce2 --- /dev/null +++ b/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + + +class Solution { +public: + int longestContinuousSubstring(string s) { + + int n = s.size(), res = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || s[i] - s[start] != i - start){ + res = max(res, i - start); + start = i; + } + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index c65d2b9c..f6beaf3c 100644 --- a/readme.md +++ b/readme.md @@ -2275,6 +2275,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2411 | [Smallest Subarrays With Maximum Bitwise OR](https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/) | [无][缺:O(n) 算法] | [C++](2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp2411/) | | | | | | | | | | | 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [无] | [C++](2001-2500/2413-Smallest-Even-Multiple/cpp-2413/) | | | +| 2414 | [Length of the Longest Alphabetical Continuous Substring](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) | [无] | [C++](2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/) | | | | | | | | | | ## 力扣中文站比赛 From 547c45463aa2fc9224a2b33f932b40be87ba7655 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 20 Sep 2022 14:04:26 -0700 Subject: [PATCH 189/390] 2415 solved. --- .../cpp-2414/main.cpp | 7 +++ .../cpp-2415/CMakeLists.txt | 6 ++ .../cpp-2415/main.cpp | 63 +++++++++++++++++++ readme.md | 1 + 4 files changed, 77 insertions(+) create mode 100644 2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/CMakeLists.txt create mode 100644 2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/main.cpp diff --git a/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp b/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp index ebb44ce2..ff7b5e6a 100644 --- a/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp +++ b/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp @@ -1,8 +1,15 @@ +/// Source : https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/ +/// Author : liuyubobobo +/// Time : 2022-09-20 + #include using namespace std; +/// String Split +/// Time Complexity: O(n) +/// Space Complexity: O(1) class Solution { public: int longestContinuousSubstring(string s) { diff --git a/2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/CMakeLists.txt b/2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/CMakeLists.txt new file mode 100644 index 00000000..ac6c4d31 --- /dev/null +++ b/2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2415) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2415 main.cpp) diff --git a/2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/main.cpp b/2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/main.cpp new file mode 100644 index 00000000..4c3135fa --- /dev/null +++ b/2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/ +/// Author : liuyubobobo +/// Time : 2022-09-20 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +///Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + TreeNode* reverseOddLevels(TreeNode* root) { + + vector> levels; + dfs(root, 0, levels); + + for(vector& level: levels){ + for(int i = 0, j = level.size() - 1; i < j; i ++, j --) + swap(level[i]->val, level[j]->val); + } + return root; + } + +private: + void dfs(TreeNode* node, int d, vector>& levels){ + + if(!node) return; + + if(d & 1){ + int index = (d - 1) / 2; + if(index >= levels.size()){ + assert(index == levels.size()); + levels.push_back({node}); + } + else levels[index].push_back(node); + } + + dfs(node->left, d + 1, levels); + dfs(node->right, d + 1, levels); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index f6beaf3c..871e1b36 100644 --- a/readme.md +++ b/readme.md @@ -2276,6 +2276,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [无] | [C++](2001-2500/2413-Smallest-Even-Multiple/cpp-2413/) | | | | 2414 | [Length of the Longest Alphabetical Continuous Substring](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) | [无] | [C++](2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/) | | | +| 2415 | [Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | [无] | [C++](2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/) | | | | | | | | | | ## 力扣中文站比赛 From 7ceb4094df4adf86072548ddf3df7e9d76556e99 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 20 Sep 2022 14:14:59 -0700 Subject: [PATCH 190/390] 2416 solved. --- .../cpp-2416/CMakeLists.txt | 6 ++ .../cpp-2416/main.cpp | 75 +++++++++++++++++++ readme.md | 1 + 3 files changed, 82 insertions(+) create mode 100644 2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/CMakeLists.txt create mode 100644 2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/main.cpp diff --git a/2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/CMakeLists.txt b/2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/CMakeLists.txt new file mode 100644 index 00000000..f85f655f --- /dev/null +++ b/2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2416) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2416 main.cpp) diff --git a/2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/main.cpp b/2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/main.cpp new file mode 100644 index 00000000..d052f2ef --- /dev/null +++ b/2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/main.cpp @@ -0,0 +1,75 @@ +/// Source : https://leetcode.com/problems/sum-of-prefix-scores-of-strings/ +/// Author : liuyubobobo +/// Time : 2022-09-20 + +#include +#include + +using namespace std; + + +/// Using Trie +/// Time Complexity: O(all_characters int words) +/// Space Complexity: O(all_characters int words) +class Trie { + +private: + class Node{ + + public: + vector next; + int sz; + + Node() : next(26, nullptr), sz(0) {}; + }; + + Node* root; + +public: + Trie() : root(new Node()) {} + + void insert(const string& word) { + + Node* cur = root; + root->sz ++; + for(char c: word){ + if(cur->next[c - 'a'] == nullptr) + cur->next[c - 'a'] = new Node(); + cur = cur->next[c - 'a']; + cur->sz ++; + } + } + + int query(const string& word) { + + Node* cur = root; + int res = 0; + for(char c: word){ + if(cur->next[c - 'a'] == nullptr) + return res; + cur = cur->next[c - 'a']; + res += cur->sz; + } + return res; + } +}; + +class Solution { +public: + vector sumPrefixScores(vector& words) { + + Trie trie; + for(const string& word: words) + trie.insert(word); + + vector res; + for(const string& word: words) + res.push_back(trie.query(word)); + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 871e1b36..f9b7d6a2 100644 --- a/readme.md +++ b/readme.md @@ -2277,6 +2277,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [无] | [C++](2001-2500/2413-Smallest-Even-Multiple/cpp-2413/) | | | | 2414 | [Length of the Longest Alphabetical Continuous Substring](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) | [无] | [C++](2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/) | | | | 2415 | [Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | [无] | [C++](2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/) | | | +| 2416 | [Sum of Prefix Scores of Strings](https://leetcode.com/problems/sum-of-prefix-scores-of-strings/) | [无] | [C++](2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/) | | | | | | | | | | ## 力扣中文站比赛 From c9b404a6426488b417785c410a0256e5742baa7c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 20 Sep 2022 14:38:19 -0700 Subject: [PATCH 191/390] 2408 solved. --- .../2408-Design-SQL/cpp-2408/CMakeLists.txt | 6 +++ 2001-2500/2408-Design-SQL/cpp-2408/main.cpp | 53 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2408-Design-SQL/cpp-2408/CMakeLists.txt create mode 100644 2001-2500/2408-Design-SQL/cpp-2408/main.cpp diff --git a/2001-2500/2408-Design-SQL/cpp-2408/CMakeLists.txt b/2001-2500/2408-Design-SQL/cpp-2408/CMakeLists.txt new file mode 100644 index 00000000..b299b759 --- /dev/null +++ b/2001-2500/2408-Design-SQL/cpp-2408/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2408) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2408 main.cpp) diff --git a/2001-2500/2408-Design-SQL/cpp-2408/main.cpp b/2001-2500/2408-Design-SQL/cpp-2408/main.cpp new file mode 100644 index 00000000..39717891 --- /dev/null +++ b/2001-2500/2408-Design-SQL/cpp-2408/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/design-sql/ +/// Author : liuyubobobo +/// Time : 2022-09-20 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: init: O(n) +/// query: O(logn) +/// Space Complexity: O(all rows) +class SQL { + +private: + map table_name_2_table_id; + const vector columns; + vector next_row_id; + vector>> tables; + +public: + SQL(vector& names, vector& columns) : + columns(columns), next_row_id(names.size(), 1), tables(names.size()) { + + for(int i = 0; i < names.size(); i ++) + table_name_2_table_id[names[i]] = i; + } + + void insertRow(string name, vector row) { + + int table_id = table_name_2_table_id[name]; + + tables[table_id].push_back(row); + next_row_id[table_id] ++; + } + + void deleteRow(string name, int rowId) { + + } + + string selectCell(string name, int rowId, int columnId) { + int table_id = table_name_2_table_id[name]; + return tables[table_id][rowId - 1][columnId - 1]; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index f9b7d6a2..0811b653 100644 --- a/readme.md +++ b/readme.md @@ -2269,7 +2269,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2405 | [Optimal Partition of String](https://leetcode.com/problems/optimal-partition-of-string/) | [无] | [C++](2001-2500/2405-Optimal-Partition-of-String/cpp-2405/) | | | | 2406 | [Divide Intervals Into Minimum Number of Groups](https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/) | [无] | [C++](2001-2500/2406-Divide-Intervals-Into-Minimum-Number-of-Groups/cpp-2406/) | | | | 2407 | [Longest Increasing Subsequence II](https://leetcode.com/problems/longest-increasing-subsequence-ii/) | [无] | [C++](2001-2500/2407-Longest-Increasing-Subsequence-II/cpp2407/) | | | -| | | | | | | +| 2408 | [Design SQL](https://leetcode.com/problems/design-sql/) | [无] | [C++](2001-2500/2408-Design-SQL/cpp2408/) | | | | 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [无] | [C++](2001-2500/2409-Count-Days-Spent-Together/cpp2409/) | | | | 2410 | [Maximum Matching of Players With Trainers](https://leetcode.com/problems/maximum-matching-of-players-with-trainers/) | [无] | [C++](2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp2410/) | | | | 2411 | [Smallest Subarrays With Maximum Bitwise OR](https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/) | [无][缺:O(n) 算法] | [C++](2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp2411/) | | | From ac57c72b058d943f3be2bfe2f12bace06d6b8f8d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 23 Sep 2022 08:53:02 -0700 Subject: [PATCH 192/390] 1272 solved. --- .../cpp-1272/CMakeLists.txt | 6 +++ .../1272-Remove-Interval/cpp-1272/main.cpp | 49 +++++++++++++++++++ readme.md | 2 + 3 files changed, 57 insertions(+) create mode 100644 1001-1500/1272-Remove-Interval/cpp-1272/CMakeLists.txt create mode 100644 1001-1500/1272-Remove-Interval/cpp-1272/main.cpp diff --git a/1001-1500/1272-Remove-Interval/cpp-1272/CMakeLists.txt b/1001-1500/1272-Remove-Interval/cpp-1272/CMakeLists.txt new file mode 100644 index 00000000..4d99d4d7 --- /dev/null +++ b/1001-1500/1272-Remove-Interval/cpp-1272/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_1272) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1272 main.cpp) diff --git a/1001-1500/1272-Remove-Interval/cpp-1272/main.cpp b/1001-1500/1272-Remove-Interval/cpp-1272/main.cpp new file mode 100644 index 00000000..2b93a482 --- /dev/null +++ b/1001-1500/1272-Remove-Interval/cpp-1272/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/remove-interval/ +/// Author : liuyubobobo +/// Time : 2022-09-23 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + vector> removeInterval(vector>& intervals, vector& toBeRemoved) { + + sort(intervals.begin(), intervals.end()); + + int x = toBeRemoved[0], y = toBeRemoved[1] - 1; + + vector> res; + for(const vector& v: intervals){ + int a = v[0], b = v[1] - 1; + if(x < a){ + if(y < a) res.push_back({a, b + 1}); + else if(y <= b - 1) res.push_back({y + 1, b + 1}); + else continue; + } + else if(x == a){ + if(y <= b - 1) res.push_back({y + 1, b + 1}); + else continue; + } + else if(x <= b){ + if(y <= b - 1) res.push_back({a, x}), res.push_back({y + 1, b + 1}); + else res.push_back({a, x}); + } + else res.push_back({a, b + 1}); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 0811b653..821b8cbe 100644 --- a/readme.md +++ b/readme.md @@ -1220,6 +1220,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1263 | [Minimum Moves to Move a Box to Their Target Location](https://leetcode.com/problems/minimum-moves-to-move-a-box-to-their-target-location/) | [无] | [C++](1001-1500/1263-Minimum-Moves-to-Move-a-Box-to-Their-Target-Location/cpp-1263/) | | | | 1264 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 1272 | [Remove Interval](https://leetcode.com/problems/remove-interval/) | [无] | [C++](1001-1500/1272-Remove-Interval/cpp-1272/) | | | +| | | | | | | | 1275 | [Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/) | [无] | [C++](1001-1500/1275-Find-Winner-on-a-Tic-Tac-Toe-Game/cpp-1275/) | | | | | | | | | | | 1277 | [Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/) | [题解](https://leetcode-cn.com/problems/count-square-submatrices-with-all-ones/solution/tong-ji-quan-wei-1-de-zheng-fang-xing-zi-ju-zhen-2/) | [C++](1001-1500/1277-Count-Square-Submatrices-with-All-Ones/cpp-1277/) | | | From 53b47a4fe0e4cac08983f4cc71557f54d2ba4403 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 23 Sep 2022 14:15:43 -0700 Subject: [PATCH 193/390] 2022 fall cnuniobpay solved. --- Others/2022fall-cnunionpay/A/CMakeLists.txt | 6 ++ Others/2022fall-cnunionpay/A/main.cpp | 41 +++++++++++ Others/2022fall-cnunionpay/B/CMakeLists.txt | 6 ++ Others/2022fall-cnunionpay/B/main.cpp | 45 ++++++++++++ Others/2022fall-cnunionpay/C/CMakeLists.txt | 6 ++ Others/2022fall-cnunionpay/C/main.cpp | 35 +++++++++ Others/2022fall-cnunionpay/D/CMakeLists.txt | 6 ++ Others/2022fall-cnunionpay/D/main.cpp | 80 +++++++++++++++++++++ readme.md | 5 ++ 9 files changed, 230 insertions(+) create mode 100644 Others/2022fall-cnunionpay/A/CMakeLists.txt create mode 100644 Others/2022fall-cnunionpay/A/main.cpp create mode 100644 Others/2022fall-cnunionpay/B/CMakeLists.txt create mode 100644 Others/2022fall-cnunionpay/B/main.cpp create mode 100644 Others/2022fall-cnunionpay/C/CMakeLists.txt create mode 100644 Others/2022fall-cnunionpay/C/main.cpp create mode 100644 Others/2022fall-cnunionpay/D/CMakeLists.txt create mode 100644 Others/2022fall-cnunionpay/D/main.cpp diff --git a/Others/2022fall-cnunionpay/A/CMakeLists.txt b/Others/2022fall-cnunionpay/A/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/Others/2022fall-cnunionpay/A/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/Others/2022fall-cnunionpay/A/main.cpp b/Others/2022fall-cnunionpay/A/main.cpp new file mode 100644 index 00000000..8c698c0b --- /dev/null +++ b/Others/2022fall-cnunionpay/A/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.cn/contest/cnunionpay2022/problems/VLNEbD/ +/// Author : liuyubobobo +/// Time : 2022-09-23 + +#include + +using namespace std; + + +/// Linked List +/// Time Complexity: O(n) +/// Space Complexity: O(1) + +/// Definition for singly-linked list. +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +class Solution { +public: + ListNode* reContruct(ListNode* head) { + + ListNode* dummyHead = new ListNode(-1); + dummyHead->next = head; + + ListNode* pre = dummyHead; + while(pre->next){ + if(pre->next->val % 2 == 0) pre->next = pre->next->next; + else pre = pre->next; + } + return dummyHead->next; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022fall-cnunionpay/B/CMakeLists.txt b/Others/2022fall-cnunionpay/B/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/Others/2022fall-cnunionpay/B/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/Others/2022fall-cnunionpay/B/main.cpp b/Others/2022fall-cnunionpay/B/main.cpp new file mode 100644 index 00000000..2b6688dc --- /dev/null +++ b/Others/2022fall-cnunionpay/B/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.cn/contest/cnunionpay2022/problems/6olJmJ/ +/// Author : liuyubobobo +/// Time : 2022-09-23 + +#include +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(|pos| * log(|station|)) +/// Space Complexity: O(1) +class Solution { +public: + vector explorationSupply(vector& station, vector& pos) { + + int q = pos.size(); + vector res(q, -1); + for(int i = 0; i < q; i ++){ + int p = pos[i]; + int best_d = INT_MAX; + + auto iter = lower_bound(station.begin(), station.end(), p); + if(iter != station.end()){ + if(*iter - p < best_d) + res[i] = iter - station.begin(), best_d = *iter - p; + } + if(iter != station.begin()){ + iter --; + if(p - *iter <= best_d) + res[i] = iter - station.begin(), best_d = p - *iter; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022fall-cnunionpay/C/CMakeLists.txt b/Others/2022fall-cnunionpay/C/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/Others/2022fall-cnunionpay/C/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/Others/2022fall-cnunionpay/C/main.cpp b/Others/2022fall-cnunionpay/C/main.cpp new file mode 100644 index 00000000..04259972 --- /dev/null +++ b/Others/2022fall-cnunionpay/C/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.cn/contest/cnunionpay2022/problems/wMGN0t/ +/// Author : liuyubobobo +/// Time : 2022-09-23 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int StoredEnergy(int storeLimit, const vector& power, const vector>& supply){ + + int cur_store = 0, min_supply, max_supply; + for(int i = 0, j = 0; i < power.size(); i ++){ + if(j < supply.size() && supply[j][0] == i) + min_supply = supply[j][1], max_supply = supply[j][2], j ++; + + if(min_supply <= power[i] && power[i] <= max_supply) continue; + if(power[i] > max_supply) cur_store = min(cur_store + power[i] - max_supply, storeLimit); + if(power[i] < min_supply) cur_store = max(cur_store - (min_supply - power[i]), 0); + } + return cur_store; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022fall-cnunionpay/D/CMakeLists.txt b/Others/2022fall-cnunionpay/D/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/Others/2022fall-cnunionpay/D/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/Others/2022fall-cnunionpay/D/main.cpp b/Others/2022fall-cnunionpay/D/main.cpp new file mode 100644 index 00000000..1ef17438 --- /dev/null +++ b/Others/2022fall-cnunionpay/D/main.cpp @@ -0,0 +1,80 @@ +/// Source : https://leetcode.cn/contest/cnunionpay2022/problems/NyZD2B/ +/// Author : liuyubobobo +/// Time : 2022-09-23 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Map and Set to simulate +/// Time Complexity: O(logn) +/// Space Complexity: O(n) +class VendingMachine { + +private: + map discounts; + map, int>> items; // name -> price, expire_time -> number + +public: + VendingMachine() { + + } + + void addItem(int time, int number, string item, int price, int duration) { + items[item][{price, time + duration}] += number; + } + + long long sell(int time, string customer, string item, int number) { + + if(!items.count(item)) return -1; + + map, int>& s = items[item]; + vector> v, dels; + int total = 0; + for(const pair, int>& p: s){ + int expiration = p.first.second, cnt = p.second; + if(expiration < time){ + dels.push_back(p.first); + continue; + } + + v.push_back(p.first); + total += cnt; + if(total >= number) break; + } + + for(const pair& del_key: dels) s.erase(del_key); + + if(total < number) return -1; + + int discount = 100; + if(discounts.count(customer)){ + discount = discounts[customer]; + discounts[customer] = discount - 1; + } + else discounts[customer] = 99; + + long long total_price = 0; + for(const pair& buy_key: v){ + int cnt = s[buy_key]; + int buy_cnt = min(number, cnt); + number -= buy_cnt; + cnt -= buy_cnt; + total_price += 1ll * buy_key.first * buy_cnt; + + if(cnt == 0) s.erase(buy_key); + else s[buy_key] = cnt; + } + + return total_price * discount / 100ll + !!(total_price * discount % 100ll); + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 821b8cbe..6e1516a1 100644 --- a/readme.md +++ b/readme.md @@ -2340,6 +2340,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | +| 22秋 银联-4 | [设计自动售货机](https://leetcode.cn/contest/cnunionpay2022/problems/NyZD2B/) | [无] | [C++](Others/2022fall-cnunionpay/D/) | | | +| 22秋 银联-3 | [风能发电](https://leetcode.cn/contest/cnunionpay2022/problems/wMGN0t/) | [无] | [C++](Others/2022fall-cnunionpay/C/) | | | +| 22秋 银联-2 | [勘探补给](https://leetcode.cn/contest/cnunionpay2022/problems/6olJmJ/) | [无] | [C++](Others/2022fall-cnunionpay/B/) | | | +| 22秋 银联-1 | [重构链表](https://leetcode.cn/contest/cnunionpay2022/problems/VLNEbD/) | [无] | [C++](Others/2022fall-cnunionpay/A/) | | | +| | | | | | | | 22 AutoX-4 | [蚂蚁爬行](https://leetcode.cn/contest/autox2023/problems/TcdlJS/) | [无] | [C++](Others/2022-autox2023/D/) | | | | 22 AutoX-3 | [出行的最少购票费用](https://leetcode.cn/contest/autox2023/problems/BjAFy9/) | [无] | [C++](Others/2022-autox2023/C/) | | | | 22 AutoX-2 | [蚂蚁王国的蜂蜜](https://leetcode.cn/contest/autox2023/problems/8p6t8R/) | [无] | [C++](Others/2022-autox2023/B/) | | | From c032ae88009d503cbd5594474728e51f096b05fb Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 24 Sep 2022 00:55:32 -0700 Subject: [PATCH 194/390] 0494 dp java solution added. --- .../java-0494/src/Solution.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0001-0500/0494-Target-Sum/java-0494/src/Solution.java diff --git a/0001-0500/0494-Target-Sum/java-0494/src/Solution.java b/0001-0500/0494-Target-Sum/java-0494/src/Solution.java new file mode 100644 index 00000000..729d77b6 --- /dev/null +++ b/0001-0500/0494-Target-Sum/java-0494/src/Solution.java @@ -0,0 +1,27 @@ +/// Source : https://leetcode.com/problems/target-sum/description/ +/// Author : liuyubobobo +/// Time : 2022-09-24 +class Solution { + public int findTargetSumWays(int[] nums, int target) { + + int sum = 0; + for(int num: nums) sum += num; + + if(target < -sum || target > sum) return 0; + + int n = nums.length; + int[][] dp = new int[n + 1][2 * sum + 1]; + + int offset = sum; + dp[0][0 + offset] = 1; + for(int i = 0; i < n; i ++){ + for(int s = -sum; s <= sum; s ++){ + if(-sum <= s + nums[i] && s + nums[i] <= sum) + dp[i + 1][s + offset] += dp[i][s + nums[i] + offset]; + if(-sum <= s - nums[i] && s - nums[i] <= sum) + dp[i + 1][s + offset] += dp[i][s - nums[i] + offset]; + } + } + return dp[n][target + offset]; + } +} \ No newline at end of file From e6ad25e678cb4c359620ef3b84538e1d25c12892 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 24 Sep 2022 01:40:57 -0700 Subject: [PATCH 195/390] 0322 java solution added. --- .../0322-Coin-Change/java-0322/src/Solution.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0001-0500/0322-Coin-Change/java-0322/src/Solution.java diff --git a/0001-0500/0322-Coin-Change/java-0322/src/Solution.java b/0001-0500/0322-Coin-Change/java-0322/src/Solution.java new file mode 100644 index 00000000..277ac283 --- /dev/null +++ b/0001-0500/0322-Coin-Change/java-0322/src/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int coinChange(int[] coins, int amount) { + + int n = coins.length; + + int[] dp = new int[amount + 1]; + for(int s = 0; s <= amount; s ++) dp[s] = Integer.MAX_VALUE / 2; + dp[0] = 0; + + for(int i = 0; i < n; i ++) + for(int s = coins[i]; s <= amount; s ++) + dp[s] = Math.min(dp[s], 1 + dp[s - coins[i]]); + return dp[amount] < Integer.MAX_VALUE / 2 ? dp[amount] : -1; + } +} \ No newline at end of file From 6a1524e719ed69249eb2284bc1c059af01e9f3e4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 26 Sep 2022 14:15:04 -0700 Subject: [PATCH 196/390] 2418 solved. --- .../cpp-2418/CMakeLists.txt | 6 +++++ .../2418-Sort-the-People/cpp-2418/main.cpp | 27 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 35 insertions(+) create mode 100644 2001-2500/2418-Sort-the-People/cpp-2418/CMakeLists.txt create mode 100644 2001-2500/2418-Sort-the-People/cpp-2418/main.cpp diff --git a/2001-2500/2418-Sort-the-People/cpp-2418/CMakeLists.txt b/2001-2500/2418-Sort-the-People/cpp-2418/CMakeLists.txt new file mode 100644 index 00000000..ff24cc37 --- /dev/null +++ b/2001-2500/2418-Sort-the-People/cpp-2418/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2418) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2418 main.cpp) diff --git a/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp b/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp new file mode 100644 index 00000000..d4144ec3 --- /dev/null +++ b/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using namespace std; + + +class Solution { +public: + vector sortPeople(vector& names, vector& heights) { + + int n = names.size(); + + vector> data(n); + for(int i = 0; i < n; i ++) data[i] = {heights[i], names[i]}; + sort(data.begin(), data.end(), greater>()); + + vector res(n); + for(int i = 0; i < n; i ++) res[i] = data[i].second; + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 6e1516a1..7e2435e5 100644 --- a/readme.md +++ b/readme.md @@ -2281,6 +2281,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2415 | [Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | [无] | [C++](2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/) | | | | 2416 | [Sum of Prefix Scores of Strings](https://leetcode.com/problems/sum-of-prefix-scores-of-strings/) | [无] | [C++](2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/) | | | | | | | | | | +| 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [无] | [C++](2001-2500/2418-Sort-the-People/cpp-2418/) | | | +| | | | | | | ## 力扣中文站比赛 From 77b74e7b19a7ed11aa4bcebc754a82abc31383ee Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 26 Sep 2022 14:28:47 -0700 Subject: [PATCH 197/390] 2419 solved. --- .../2418-Sort-the-People/cpp-2418/main.cpp | 7 +++++ .../cpp-2419/CMakeLists.txt | 6 ++++ .../cpp-2419/main.cpp | 28 +++++++++++++++++++ readme.md | 1 + 4 files changed, 42 insertions(+) create mode 100644 2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/CMakeLists.txt create mode 100644 2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp diff --git a/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp b/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp index d4144ec3..0de539e0 100644 --- a/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp +++ b/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp @@ -1,3 +1,7 @@ +/// Source : https://leetcode.com/problems/sort-the-people/ +/// Author : liuyubobobo +/// Time : 2022-09-26 + #include #include #include @@ -5,6 +9,9 @@ using namespace std; +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) class Solution { public: vector sortPeople(vector& names, vector& heights) { diff --git a/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/CMakeLists.txt b/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/CMakeLists.txt new file mode 100644 index 00000000..2571f75a --- /dev/null +++ b/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2419) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2419 main.cpp) diff --git a/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp b/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp new file mode 100644 index 00000000..86139fad --- /dev/null +++ b/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +using namespace std; + + +class Solution { +public: + int longestSubarray(vector& nums) { + + int target = *max_element(nums.begin(), nums.end()); + + int res = 1; + for(int start = 0, i = 1; i <= nums.size(); i ++){ + if(i == nums.size() || nums[i] != nums[start]){ + if(nums[start] == target) res = max(res, i - start); + start = i; + } + } + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 7e2435e5..d1bfbb7d 100644 --- a/readme.md +++ b/readme.md @@ -2282,6 +2282,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2416 | [Sum of Prefix Scores of Strings](https://leetcode.com/problems/sum-of-prefix-scores-of-strings/) | [无] | [C++](2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/) | | | | | | | | | | | 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [无] | [C++](2001-2500/2418-Sort-the-People/cpp-2418/) | | | +| 2419 | [Longest Subarray With Maximum Bitwise AND](https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/) | [无] | [C++](2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/) | | | | | | | | | | ## 力扣中文站比赛 From dac87639409467b3322fea0df80f3da2097b74ab Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 26 Sep 2022 14:41:04 -0700 Subject: [PATCH 198/390] 2420 solved. --- .../cpp-2419/main.cpp | 8 +++++ .../cpp-2420/CMakeLists.txt | 6 ++++ .../cpp-2420/main.cpp | 36 +++++++++++++++++++ readme.md | 1 + 4 files changed, 51 insertions(+) create mode 100644 2001-2500/2420-Find-All-Good-Indices/cpp-2420/CMakeLists.txt create mode 100644 2001-2500/2420-Find-All-Good-Indices/cpp-2420/main.cpp diff --git a/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp b/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp index 86139fad..fdf64a77 100644 --- a/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp +++ b/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp @@ -1,3 +1,7 @@ +/// Source : https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/ +/// Author : liuyubobobo +/// Time : 2022-09-26 + #include #include #include @@ -5,6 +9,9 @@ using namespace std; +/// Split +/// Time Complexity: O(n) +/// Space Complexity: O(1) class Solution { public: int longestSubarray(vector& nums) { @@ -22,6 +29,7 @@ class Solution { } }; + int main() { return 0; diff --git a/2001-2500/2420-Find-All-Good-Indices/cpp-2420/CMakeLists.txt b/2001-2500/2420-Find-All-Good-Indices/cpp-2420/CMakeLists.txt new file mode 100644 index 00000000..5310359f --- /dev/null +++ b/2001-2500/2420-Find-All-Good-Indices/cpp-2420/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2420) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2420 main.cpp) diff --git a/2001-2500/2420-Find-All-Good-Indices/cpp-2420/main.cpp b/2001-2500/2420-Find-All-Good-Indices/cpp-2420/main.cpp new file mode 100644 index 00000000..17ba665e --- /dev/null +++ b/2001-2500/2420-Find-All-Good-Indices/cpp-2420/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/find-all-good-indices/ +/// Author : liuyubobobo +/// Time : 2022-09-26 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector goodIndices(vector& nums, int k) { + + int n = nums.size(); + vector dp1(n, 1), dp2(n, 1); + for(int i = 1; i < n; i ++) + if(nums[i] <= nums[i - 1]) dp1[i] = 1 + dp1[i - 1]; + for(int i = n - 2; i >= 0; i --) + if(nums[i] <= nums[i + 1]) dp2[i] = 1 + dp2[i + 1]; + + vector res; + for(int i = 1; i < n - 1; i ++) + if(dp1[i - 1] >= k && dp2[i + 1] >= k) res.push_back(i); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d1bfbb7d..f05a7be6 100644 --- a/readme.md +++ b/readme.md @@ -2283,6 +2283,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [无] | [C++](2001-2500/2418-Sort-the-People/cpp-2418/) | | | | 2419 | [Longest Subarray With Maximum Bitwise AND](https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/) | [无] | [C++](2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/) | | | +| 2420 | [Find All Good Indices](https://leetcode.com/problems/find-all-good-indices/) | [无] | [C++](2001-2500/2420-Find-All-Good-Indices/cpp-2420/) | | | | | | | | | | ## 力扣中文站比赛 From 759ebc92f4dcd37844c3dee1cd64e8b8af76ff6d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 27 Sep 2022 11:01:58 -0700 Subject: [PATCH 199/390] Cracking The Coding Interview 17-09 solved. --- .../17-09/cpp-17-09/CMakeLists.txt | 6 +++ .../17-09/cpp-17-09/main.cpp | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Cracking-The-Coding-Interview/17-09/cpp-17-09/CMakeLists.txt create mode 100644 Cracking-The-Coding-Interview/17-09/cpp-17-09/main.cpp diff --git a/Cracking-The-Coding-Interview/17-09/cpp-17-09/CMakeLists.txt b/Cracking-The-Coding-Interview/17-09/cpp-17-09/CMakeLists.txt new file mode 100644 index 00000000..bc8175f7 --- /dev/null +++ b/Cracking-The-Coding-Interview/17-09/cpp-17-09/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_17_09) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_17_09 main.cpp) diff --git a/Cracking-The-Coding-Interview/17-09/cpp-17-09/main.cpp b/Cracking-The-Coding-Interview/17-09/cpp-17-09/main.cpp new file mode 100644 index 00000000..4c03f80d --- /dev/null +++ b/Cracking-The-Coding-Interview/17-09/cpp-17-09/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.cn/problems/get-kth-magic-number-lcci/ +/// Author : liuyubobobo +/// Time : 2022-09-27 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(klogk) +/// Space Complexity: O(k) +class Solution { +public: + int getKthMagicNumber(int k) { + + priority_queue, greater> pq; + pq.push(1); + + set visited; + + long long res = -1; + while(k){ + res = pq.top(); pq.pop(); + if(visited.count(res)) continue; + + visited.insert(res); + + k --; + pq.push(res * 3); + pq.push(res * 5); + pq.push(res * 7); + } + return res; + } +}; + + +int main() { + + cout << Solution().getKthMagicNumber(2) << '\n'; + // 3 + + return 0; +} From e39b7e784668934b0a3a6b94a517c895f7252bb8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 30 Sep 2022 22:55:20 -0700 Subject: [PATCH 200/390] 2422 solved. --- .../cpp-2422/CMakeLists.txt | 6 +++ .../cpp-2422/main.cpp | 47 +++++++++++++++++++ readme.md | 2 + 3 files changed, 55 insertions(+) create mode 100644 2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/CMakeLists.txt create mode 100644 2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/main.cpp diff --git a/2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/CMakeLists.txt b/2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/CMakeLists.txt new file mode 100644 index 00000000..a25cd34d --- /dev/null +++ b/2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2422) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2422 main.cpp) diff --git a/2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/main.cpp b/2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/main.cpp new file mode 100644 index 00000000..590e6a7a --- /dev/null +++ b/2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/merge-operations-to-turn-array-into-a-palindrome/ +/// Author : liuyubobobo +/// Time : 2022-09-30 + +#include +#include +#include + +using namespace std; + + +/// Greedy and using deque +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumOperations(vector& nums) { + + deque q; + for(int e: nums) q.push_back(e); + + int res = 0; + while(q.size() > 1){ + long long front = q.front(), tail = q.back(); + if(front == tail) q.pop_front(), q.pop_back(); + else if(front < tail){ + q.pop_front(); + long long x = q.front(); q.pop_front(); + q.push_front(x + front); + res ++; + } + else{ + q.pop_back(); + long long x = q.back(); q.pop_back(); + q.push_back(x + tail); + res ++; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index f05a7be6..511463bd 100644 --- a/readme.md +++ b/readme.md @@ -2285,6 +2285,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2419 | [Longest Subarray With Maximum Bitwise AND](https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/) | [无] | [C++](2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/) | | | | 2420 | [Find All Good Indices](https://leetcode.com/problems/find-all-good-indices/) | [无] | [C++](2001-2500/2420-Find-All-Good-Indices/cpp-2420/) | | | | | | | | | | +| 2422 | [Merge Operations to Turn Array Into a Palindrome](https://leetcode.com/problems/merge-operations-to-turn-array-into-a-palindrome/) | [无] | [C++](2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/) | | | +| | | | | | | ## 力扣中文站比赛 From 8756a7c4e147aee287233b9077a4ba8f1a294a34 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 30 Sep 2022 23:11:45 -0700 Subject: [PATCH 201/390] LC folder names updated. --- .../cpp-LCP001}/CMakeLists.txt | 0 .../cpp-LCP01 => LCP001/cpp-LCP001}/main.cpp | 0 .../cpp-LCP002}/CMakeLists.txt | 0 .../cpp-LCP02 => LCP002/cpp-LCP002}/main.cpp | 0 .../cpp-LCP02 => LCP002/cpp-LCP002}/main2.cpp | 0 .../cpp-LCP003}/CMakeLists.txt | 0 .../cpp-LCP03 => LCP003/cpp-LCP003}/main.cpp | 0 .../cpp-LCP004}/CMakeLists.txt | 0 .../cpp-LCP04 => LCP004/cpp-LCP004}/main.cpp | 0 .../cpp-LCP04 => LCP004/cpp-LCP004}/main2.cpp | 0 .../cpp-LCP005}/CMakeLists.txt | 0 .../cpp-LCP05 => LCP005/cpp-LCP005}/main.cpp | 0 .../cpp-LCP006}/CMakeLists.txt | 0 .../cpp-LCP06 => LCP006/cpp-LCP006}/main.cpp | 0 .../cpp-LCP007}/CMakeLists.txt | 0 .../cpp-LCP07 => LCP007/cpp-LCP007}/main.cpp | 0 .../cpp-LCP07 => LCP007/cpp-LCP007}/main2.cpp | 0 .../cpp-LCP07 => LCP007/cpp-LCP007}/main3.cpp | 0 .../cpp-LCP07 => LCP007/cpp-LCP007}/main4.cpp | 0 .../cpp-LCP008}/CMakeLists.txt | 0 .../cpp-LCP08 => LCP008/cpp-LCP008}/main.cpp | 0 .../cpp-LCP08 => LCP008/cpp-LCP008}/main2.cpp | 0 .../cpp-LCP009}/CMakeLists.txt | 0 .../cpp-LCP09 => LCP009/cpp-LCP009}/main.cpp | 0 .../cpp-LCP09 => LCP009/cpp-LCP009}/main2.cpp | 0 .../cpp-LCP010}/CMakeLists.txt | 0 .../cpp-LCP10 => LCP010/cpp-LCP010}/main.cpp | 0 .../cpp-LCP011}/CMakeLists.txt | 0 .../cpp-LCP11 => LCP011/cpp-LCP011}/main.cpp | 0 .../cpp-LCP11 => LCP011/cpp-LCP011}/main2.cpp | 0 .../cpp-LCP012}/CMakeLists.txt | 0 .../cpp-LCP12 => LCP012/cpp-LCP012}/main.cpp | 0 .../cpp-LCP013}/CMakeLists.txt | 0 .../cpp-LCP13 => LCP013/cpp-LCP013}/main.cpp | 0 .../cpp-LCP13 => LCP013/cpp-LCP013}/main2.cpp | 0 .../cpp-LCP014}/CMakeLists.txt | 0 .../cpp-LCP14 => LCP014/cpp-LCP014}/main.cpp | 0 .../cpp-LCP14 => LCP014/cpp-LCP014}/main2.cpp | 0 .../cpp-LCP015}/CMakeLists.txt | 0 .../cpp-LCP15 => LCP015/cpp-LCP015}/main.cpp | 0 .../cpp-LCP15 => LCP015/cpp-LCP015}/main2.cpp | 0 .../cpp-LCP15 => LCP015/cpp-LCP015}/main3.cpp | 0 .../cpp-LCP016}/CMakeLists.txt | 0 .../cpp-LCP16 => LCP016/cpp-LCP016}/main.cpp | 0 .../cpp-LCP017}/CMakeLists.txt | 0 .../cpp-LCP17 => LCP017/cpp-LCP017}/main.cpp | 0 .../cpp-LCP018}/CMakeLists.txt | 0 .../cpp-LCP18 => LCP018/cpp-LCP018}/main.cpp | 0 .../cpp-LCP019}/CMakeLists.txt | 0 .../cpp-LCP19 => LCP019/cpp-LCP019}/main.cpp | 0 .../cpp-LCP19 => LCP019/cpp-LCP019}/main2.cpp | 0 .../D => LCP020/cpp-LCP020}/CMakeLists.txt | 0 LC/{LCP20/D => LCP020/cpp-LCP020}/main.cpp | 0 .../cpp-LCP021}/CMakeLists.txt | 0 .../cpp-LCP21 => LCP021/cpp-LCP021}/main.cpp | 0 .../cpp-LCP022}/CMakeLists.txt | 0 .../cpp-LCP22 => LCP022/cpp-LCP022}/main.cpp | 0 .../cpp-LCP22 => LCP022/cpp-LCP022}/main2.cpp | 0 .../cpp-LCP023}/CMakeLists.txt | 0 .../cpp-LCP23 => LCP023/cpp-LCP023}/main.cpp | 0 .../cpp-LCP024}/CMakeLists.txt | 0 .../cpp-LCP24 => LCP024/cpp-LCP024}/main.cpp | 0 .../cpp-LCP025}/CMakeLists.txt | 0 .../cpp-LCP25 => LCP025/cpp-LCP025}/main.cpp | 0 .../cpp-LCP25 => LCP025/cpp-LCP025}/main2.cpp | 0 .../cpp-LCP028}/CMakeLists.txt | 0 .../cpp-LCP28 => LCP028/cpp-LCP028}/main.cpp | 0 .../cpp-LCP029}/CMakeLists.txt | 0 .../cpp-LCP29 => LCP029/cpp-LCP029}/main.cpp | 0 .../cpp-LCP039}/CMakeLists.txt | 0 .../cpp-LCP39 => LCP039/cpp-LCP039}/main.cpp | 0 .../cpp-LCP040}/CMakeLists.txt | 0 .../cpp-LCP40 => LCP040/cpp-LCP040}/main.cpp | 0 .../cpp-LCP041}/CMakeLists.txt | 0 .../cpp-LCP41 => LCP041/cpp-LCP041}/main.cpp | 0 .../cpp-LCP042}/CMakeLists.txt | 0 .../cpp-LCP42 => LCP042/cpp-LCP042}/main.cpp | 0 .../cpp-LCP043}/CMakeLists.txt | 0 .../cpp-LCP43 => LCP043/cpp-LCP043}/main.cpp | 0 .../cpp-LCP044}/CMakeLists.txt | 0 .../cpp-LCP44 => LCP044/cpp-LCP044}/main.cpp | 0 .../cpp-LCP045}/CMakeLists.txt | 0 .../cpp-LCP45 => LCP045/cpp-LCP045}/main.cpp | 0 .../cpp-LCP046}/CMakeLists.txt | 0 .../cpp-LCP46 => LCP046/cpp-LCP046}/main.cpp | 0 .../cpp-LCP047}/CMakeLists.txt | 0 .../cpp-LCP47 => LCP047/cpp-LCP047}/main.cpp | 0 .../cpp-LCP47 => LCP047/cpp-LCP047}/main2.cpp | 0 .../cpp-LCP048}/CMakeLists.txt | 0 .../cpp-LCP48 => LCP048/cpp-LCP048}/main.cpp | 0 .../cpp-LCP050}/CMakeLists.txt | 0 .../cpp-LCP50 => LCP050/cpp-LCP050}/main.cpp | 0 .../cpp-LCP051}/CMakeLists.txt | 0 .../cpp-LCP51 => LCP051/cpp-LCP051}/main.cpp | 0 .../cpp-LCP052}/CMakeLists.txt | 0 .../cpp-LCP52 => LCP052/cpp-LCP052}/main.cpp | 0 .../cpp-LCP055}/CMakeLists.txt | 0 .../cpp-LCP55 => LCP055/cpp-LCP055}/main.cpp | 0 .../cpp-LCP056}/CMakeLists.txt | 0 .../cpp-LCP56 => LCP056/cpp-LCP056}/main.cpp | 0 .../cpp-LCP057}/CMakeLists.txt | 0 .../cpp-LCP57 => LCP057/cpp-LCP057}/main.cpp | 0 readme.md | 94 +++++++++---------- 103 files changed, 47 insertions(+), 47 deletions(-) rename LC/{LCP01/cpp-LCP01 => LCP001/cpp-LCP001}/CMakeLists.txt (100%) rename LC/{LCP01/cpp-LCP01 => LCP001/cpp-LCP001}/main.cpp (100%) rename LC/{LCP02/cpp-LCP02 => LCP002/cpp-LCP002}/CMakeLists.txt (100%) rename LC/{LCP02/cpp-LCP02 => LCP002/cpp-LCP002}/main.cpp (100%) rename LC/{LCP02/cpp-LCP02 => LCP002/cpp-LCP002}/main2.cpp (100%) rename LC/{LCP03/cpp-LCP03 => LCP003/cpp-LCP003}/CMakeLists.txt (100%) rename LC/{LCP03/cpp-LCP03 => LCP003/cpp-LCP003}/main.cpp (100%) rename LC/{LCP04/cpp-LCP04 => LCP004/cpp-LCP004}/CMakeLists.txt (100%) rename LC/{LCP04/cpp-LCP04 => LCP004/cpp-LCP004}/main.cpp (100%) rename LC/{LCP04/cpp-LCP04 => LCP004/cpp-LCP004}/main2.cpp (100%) rename LC/{LCP05/cpp-LCP05 => LCP005/cpp-LCP005}/CMakeLists.txt (100%) rename LC/{LCP05/cpp-LCP05 => LCP005/cpp-LCP005}/main.cpp (100%) rename LC/{LCP06/cpp-LCP06 => LCP006/cpp-LCP006}/CMakeLists.txt (100%) rename LC/{LCP06/cpp-LCP06 => LCP006/cpp-LCP006}/main.cpp (100%) rename LC/{LCP07/cpp-LCP07 => LCP007/cpp-LCP007}/CMakeLists.txt (100%) rename LC/{LCP07/cpp-LCP07 => LCP007/cpp-LCP007}/main.cpp (100%) rename LC/{LCP07/cpp-LCP07 => LCP007/cpp-LCP007}/main2.cpp (100%) rename LC/{LCP07/cpp-LCP07 => LCP007/cpp-LCP007}/main3.cpp (100%) rename LC/{LCP07/cpp-LCP07 => LCP007/cpp-LCP007}/main4.cpp (100%) rename LC/{LCP08/cpp-LCP08 => LCP008/cpp-LCP008}/CMakeLists.txt (100%) rename LC/{LCP08/cpp-LCP08 => LCP008/cpp-LCP008}/main.cpp (100%) rename LC/{LCP08/cpp-LCP08 => LCP008/cpp-LCP008}/main2.cpp (100%) rename LC/{LCP09/cpp-LCP09 => LCP009/cpp-LCP009}/CMakeLists.txt (100%) rename LC/{LCP09/cpp-LCP09 => LCP009/cpp-LCP009}/main.cpp (100%) rename LC/{LCP09/cpp-LCP09 => LCP009/cpp-LCP009}/main2.cpp (100%) rename LC/{LCP10/cpp-LCP10 => LCP010/cpp-LCP010}/CMakeLists.txt (100%) rename LC/{LCP10/cpp-LCP10 => LCP010/cpp-LCP010}/main.cpp (100%) rename LC/{LCP11/cpp-LCP11 => LCP011/cpp-LCP011}/CMakeLists.txt (100%) rename LC/{LCP11/cpp-LCP11 => LCP011/cpp-LCP011}/main.cpp (100%) rename LC/{LCP11/cpp-LCP11 => LCP011/cpp-LCP011}/main2.cpp (100%) rename LC/{LCP12/cpp-LCP12 => LCP012/cpp-LCP012}/CMakeLists.txt (100%) rename LC/{LCP12/cpp-LCP12 => LCP012/cpp-LCP012}/main.cpp (100%) rename LC/{LCP13/cpp-LCP13 => LCP013/cpp-LCP013}/CMakeLists.txt (100%) rename LC/{LCP13/cpp-LCP13 => LCP013/cpp-LCP013}/main.cpp (100%) rename LC/{LCP13/cpp-LCP13 => LCP013/cpp-LCP013}/main2.cpp (100%) rename LC/{LCP14/cpp-LCP14 => LCP014/cpp-LCP014}/CMakeLists.txt (100%) rename LC/{LCP14/cpp-LCP14 => LCP014/cpp-LCP014}/main.cpp (100%) rename LC/{LCP14/cpp-LCP14 => LCP014/cpp-LCP014}/main2.cpp (100%) rename LC/{LCP15/cpp-LCP15 => LCP015/cpp-LCP015}/CMakeLists.txt (100%) rename LC/{LCP15/cpp-LCP15 => LCP015/cpp-LCP015}/main.cpp (100%) rename LC/{LCP15/cpp-LCP15 => LCP015/cpp-LCP015}/main2.cpp (100%) rename LC/{LCP15/cpp-LCP15 => LCP015/cpp-LCP015}/main3.cpp (100%) rename LC/{LCP16/cpp-LCP16 => LCP016/cpp-LCP016}/CMakeLists.txt (100%) rename LC/{LCP16/cpp-LCP16 => LCP016/cpp-LCP016}/main.cpp (100%) rename LC/{LCP17/cpp-LCP17 => LCP017/cpp-LCP017}/CMakeLists.txt (100%) rename LC/{LCP17/cpp-LCP17 => LCP017/cpp-LCP017}/main.cpp (100%) rename LC/{LCP18/cpp-LCP18 => LCP018/cpp-LCP018}/CMakeLists.txt (100%) rename LC/{LCP18/cpp-LCP18 => LCP018/cpp-LCP018}/main.cpp (100%) rename LC/{LCP19/cpp-LCP19 => LCP019/cpp-LCP019}/CMakeLists.txt (100%) rename LC/{LCP19/cpp-LCP19 => LCP019/cpp-LCP019}/main.cpp (100%) rename LC/{LCP19/cpp-LCP19 => LCP019/cpp-LCP019}/main2.cpp (100%) rename LC/{LCP20/D => LCP020/cpp-LCP020}/CMakeLists.txt (100%) rename LC/{LCP20/D => LCP020/cpp-LCP020}/main.cpp (100%) rename LC/{LCP21/cpp-LCP21 => LCP021/cpp-LCP021}/CMakeLists.txt (100%) rename LC/{LCP21/cpp-LCP21 => LCP021/cpp-LCP021}/main.cpp (100%) rename LC/{LCP22/cpp-LCP22 => LCP022/cpp-LCP022}/CMakeLists.txt (100%) rename LC/{LCP22/cpp-LCP22 => LCP022/cpp-LCP022}/main.cpp (100%) rename LC/{LCP22/cpp-LCP22 => LCP022/cpp-LCP022}/main2.cpp (100%) rename LC/{LCP23/cpp-LCP23 => LCP023/cpp-LCP023}/CMakeLists.txt (100%) rename LC/{LCP23/cpp-LCP23 => LCP023/cpp-LCP023}/main.cpp (100%) rename LC/{LCP24/cpp-LCP24 => LCP024/cpp-LCP024}/CMakeLists.txt (100%) rename LC/{LCP24/cpp-LCP24 => LCP024/cpp-LCP024}/main.cpp (100%) rename LC/{LCP25/cpp-LCP25 => LCP025/cpp-LCP025}/CMakeLists.txt (100%) rename LC/{LCP25/cpp-LCP25 => LCP025/cpp-LCP025}/main.cpp (100%) rename LC/{LCP25/cpp-LCP25 => LCP025/cpp-LCP025}/main2.cpp (100%) rename LC/{LCP28/cpp-LCP28 => LCP028/cpp-LCP028}/CMakeLists.txt (100%) rename LC/{LCP28/cpp-LCP28 => LCP028/cpp-LCP028}/main.cpp (100%) rename LC/{LCP29/cpp-LCP29 => LCP029/cpp-LCP029}/CMakeLists.txt (100%) rename LC/{LCP29/cpp-LCP29 => LCP029/cpp-LCP029}/main.cpp (100%) rename LC/{LCP39/cpp-LCP39 => LCP039/cpp-LCP039}/CMakeLists.txt (100%) rename LC/{LCP39/cpp-LCP39 => LCP039/cpp-LCP039}/main.cpp (100%) rename LC/{LCP40/cpp-LCP40 => LCP040/cpp-LCP040}/CMakeLists.txt (100%) rename LC/{LCP40/cpp-LCP40 => LCP040/cpp-LCP040}/main.cpp (100%) rename LC/{LCP41/cpp-LCP41 => LCP041/cpp-LCP041}/CMakeLists.txt (100%) rename LC/{LCP41/cpp-LCP41 => LCP041/cpp-LCP041}/main.cpp (100%) rename LC/{LCP42/cpp-LCP42 => LCP042/cpp-LCP042}/CMakeLists.txt (100%) rename LC/{LCP42/cpp-LCP42 => LCP042/cpp-LCP042}/main.cpp (100%) rename LC/{LCP43/cpp-LCP43 => LCP043/cpp-LCP043}/CMakeLists.txt (100%) rename LC/{LCP43/cpp-LCP43 => LCP043/cpp-LCP043}/main.cpp (100%) rename LC/{LCP44/cpp-LCP44 => LCP044/cpp-LCP044}/CMakeLists.txt (100%) rename LC/{LCP44/cpp-LCP44 => LCP044/cpp-LCP044}/main.cpp (100%) rename LC/{LCP45/cpp-LCP45 => LCP045/cpp-LCP045}/CMakeLists.txt (100%) rename LC/{LCP45/cpp-LCP45 => LCP045/cpp-LCP045}/main.cpp (100%) rename LC/{LCP46/cpp-LCP46 => LCP046/cpp-LCP046}/CMakeLists.txt (100%) rename LC/{LCP46/cpp-LCP46 => LCP046/cpp-LCP046}/main.cpp (100%) rename LC/{LCP47/cpp-LCP47 => LCP047/cpp-LCP047}/CMakeLists.txt (100%) rename LC/{LCP47/cpp-LCP47 => LCP047/cpp-LCP047}/main.cpp (100%) rename LC/{LCP47/cpp-LCP47 => LCP047/cpp-LCP047}/main2.cpp (100%) rename LC/{LCP48/cpp-LCP48 => LCP048/cpp-LCP048}/CMakeLists.txt (100%) rename LC/{LCP48/cpp-LCP48 => LCP048/cpp-LCP048}/main.cpp (100%) rename LC/{LCP50/cpp-LCP50 => LCP050/cpp-LCP050}/CMakeLists.txt (100%) rename LC/{LCP50/cpp-LCP50 => LCP050/cpp-LCP050}/main.cpp (100%) rename LC/{LCP51/cpp-LCP51 => LCP051/cpp-LCP051}/CMakeLists.txt (100%) rename LC/{LCP51/cpp-LCP51 => LCP051/cpp-LCP051}/main.cpp (100%) rename LC/{LCP52/cpp-LCP52 => LCP052/cpp-LCP052}/CMakeLists.txt (100%) rename LC/{LCP52/cpp-LCP52 => LCP052/cpp-LCP052}/main.cpp (100%) rename LC/{LCP55/cpp-LCP55 => LCP055/cpp-LCP055}/CMakeLists.txt (100%) rename LC/{LCP55/cpp-LCP55 => LCP055/cpp-LCP055}/main.cpp (100%) rename LC/{LCP56/cpp-LCP56 => LCP056/cpp-LCP056}/CMakeLists.txt (100%) rename LC/{LCP56/cpp-LCP56 => LCP056/cpp-LCP056}/main.cpp (100%) rename LC/{LCP57/cpp-LCP57 => LCP057/cpp-LCP057}/CMakeLists.txt (100%) rename LC/{LCP57/cpp-LCP57 => LCP057/cpp-LCP057}/main.cpp (100%) diff --git a/LC/LCP01/cpp-LCP01/CMakeLists.txt b/LC/LCP001/cpp-LCP001/CMakeLists.txt similarity index 100% rename from LC/LCP01/cpp-LCP01/CMakeLists.txt rename to LC/LCP001/cpp-LCP001/CMakeLists.txt diff --git a/LC/LCP01/cpp-LCP01/main.cpp b/LC/LCP001/cpp-LCP001/main.cpp similarity index 100% rename from LC/LCP01/cpp-LCP01/main.cpp rename to LC/LCP001/cpp-LCP001/main.cpp diff --git a/LC/LCP02/cpp-LCP02/CMakeLists.txt b/LC/LCP002/cpp-LCP002/CMakeLists.txt similarity index 100% rename from LC/LCP02/cpp-LCP02/CMakeLists.txt rename to LC/LCP002/cpp-LCP002/CMakeLists.txt diff --git a/LC/LCP02/cpp-LCP02/main.cpp b/LC/LCP002/cpp-LCP002/main.cpp similarity index 100% rename from LC/LCP02/cpp-LCP02/main.cpp rename to LC/LCP002/cpp-LCP002/main.cpp diff --git a/LC/LCP02/cpp-LCP02/main2.cpp b/LC/LCP002/cpp-LCP002/main2.cpp similarity index 100% rename from LC/LCP02/cpp-LCP02/main2.cpp rename to LC/LCP002/cpp-LCP002/main2.cpp diff --git a/LC/LCP03/cpp-LCP03/CMakeLists.txt b/LC/LCP003/cpp-LCP003/CMakeLists.txt similarity index 100% rename from LC/LCP03/cpp-LCP03/CMakeLists.txt rename to LC/LCP003/cpp-LCP003/CMakeLists.txt diff --git a/LC/LCP03/cpp-LCP03/main.cpp b/LC/LCP003/cpp-LCP003/main.cpp similarity index 100% rename from LC/LCP03/cpp-LCP03/main.cpp rename to LC/LCP003/cpp-LCP003/main.cpp diff --git a/LC/LCP04/cpp-LCP04/CMakeLists.txt b/LC/LCP004/cpp-LCP004/CMakeLists.txt similarity index 100% rename from LC/LCP04/cpp-LCP04/CMakeLists.txt rename to LC/LCP004/cpp-LCP004/CMakeLists.txt diff --git a/LC/LCP04/cpp-LCP04/main.cpp b/LC/LCP004/cpp-LCP004/main.cpp similarity index 100% rename from LC/LCP04/cpp-LCP04/main.cpp rename to LC/LCP004/cpp-LCP004/main.cpp diff --git a/LC/LCP04/cpp-LCP04/main2.cpp b/LC/LCP004/cpp-LCP004/main2.cpp similarity index 100% rename from LC/LCP04/cpp-LCP04/main2.cpp rename to LC/LCP004/cpp-LCP004/main2.cpp diff --git a/LC/LCP05/cpp-LCP05/CMakeLists.txt b/LC/LCP005/cpp-LCP005/CMakeLists.txt similarity index 100% rename from LC/LCP05/cpp-LCP05/CMakeLists.txt rename to LC/LCP005/cpp-LCP005/CMakeLists.txt diff --git a/LC/LCP05/cpp-LCP05/main.cpp b/LC/LCP005/cpp-LCP005/main.cpp similarity index 100% rename from LC/LCP05/cpp-LCP05/main.cpp rename to LC/LCP005/cpp-LCP005/main.cpp diff --git a/LC/LCP06/cpp-LCP06/CMakeLists.txt b/LC/LCP006/cpp-LCP006/CMakeLists.txt similarity index 100% rename from LC/LCP06/cpp-LCP06/CMakeLists.txt rename to LC/LCP006/cpp-LCP006/CMakeLists.txt diff --git a/LC/LCP06/cpp-LCP06/main.cpp b/LC/LCP006/cpp-LCP006/main.cpp similarity index 100% rename from LC/LCP06/cpp-LCP06/main.cpp rename to LC/LCP006/cpp-LCP006/main.cpp diff --git a/LC/LCP07/cpp-LCP07/CMakeLists.txt b/LC/LCP007/cpp-LCP007/CMakeLists.txt similarity index 100% rename from LC/LCP07/cpp-LCP07/CMakeLists.txt rename to LC/LCP007/cpp-LCP007/CMakeLists.txt diff --git a/LC/LCP07/cpp-LCP07/main.cpp b/LC/LCP007/cpp-LCP007/main.cpp similarity index 100% rename from LC/LCP07/cpp-LCP07/main.cpp rename to LC/LCP007/cpp-LCP007/main.cpp diff --git a/LC/LCP07/cpp-LCP07/main2.cpp b/LC/LCP007/cpp-LCP007/main2.cpp similarity index 100% rename from LC/LCP07/cpp-LCP07/main2.cpp rename to LC/LCP007/cpp-LCP007/main2.cpp diff --git a/LC/LCP07/cpp-LCP07/main3.cpp b/LC/LCP007/cpp-LCP007/main3.cpp similarity index 100% rename from LC/LCP07/cpp-LCP07/main3.cpp rename to LC/LCP007/cpp-LCP007/main3.cpp diff --git a/LC/LCP07/cpp-LCP07/main4.cpp b/LC/LCP007/cpp-LCP007/main4.cpp similarity index 100% rename from LC/LCP07/cpp-LCP07/main4.cpp rename to LC/LCP007/cpp-LCP007/main4.cpp diff --git a/LC/LCP08/cpp-LCP08/CMakeLists.txt b/LC/LCP008/cpp-LCP008/CMakeLists.txt similarity index 100% rename from LC/LCP08/cpp-LCP08/CMakeLists.txt rename to LC/LCP008/cpp-LCP008/CMakeLists.txt diff --git a/LC/LCP08/cpp-LCP08/main.cpp b/LC/LCP008/cpp-LCP008/main.cpp similarity index 100% rename from LC/LCP08/cpp-LCP08/main.cpp rename to LC/LCP008/cpp-LCP008/main.cpp diff --git a/LC/LCP08/cpp-LCP08/main2.cpp b/LC/LCP008/cpp-LCP008/main2.cpp similarity index 100% rename from LC/LCP08/cpp-LCP08/main2.cpp rename to LC/LCP008/cpp-LCP008/main2.cpp diff --git a/LC/LCP09/cpp-LCP09/CMakeLists.txt b/LC/LCP009/cpp-LCP009/CMakeLists.txt similarity index 100% rename from LC/LCP09/cpp-LCP09/CMakeLists.txt rename to LC/LCP009/cpp-LCP009/CMakeLists.txt diff --git a/LC/LCP09/cpp-LCP09/main.cpp b/LC/LCP009/cpp-LCP009/main.cpp similarity index 100% rename from LC/LCP09/cpp-LCP09/main.cpp rename to LC/LCP009/cpp-LCP009/main.cpp diff --git a/LC/LCP09/cpp-LCP09/main2.cpp b/LC/LCP009/cpp-LCP009/main2.cpp similarity index 100% rename from LC/LCP09/cpp-LCP09/main2.cpp rename to LC/LCP009/cpp-LCP009/main2.cpp diff --git a/LC/LCP10/cpp-LCP10/CMakeLists.txt b/LC/LCP010/cpp-LCP010/CMakeLists.txt similarity index 100% rename from LC/LCP10/cpp-LCP10/CMakeLists.txt rename to LC/LCP010/cpp-LCP010/CMakeLists.txt diff --git a/LC/LCP10/cpp-LCP10/main.cpp b/LC/LCP010/cpp-LCP010/main.cpp similarity index 100% rename from LC/LCP10/cpp-LCP10/main.cpp rename to LC/LCP010/cpp-LCP010/main.cpp diff --git a/LC/LCP11/cpp-LCP11/CMakeLists.txt b/LC/LCP011/cpp-LCP011/CMakeLists.txt similarity index 100% rename from LC/LCP11/cpp-LCP11/CMakeLists.txt rename to LC/LCP011/cpp-LCP011/CMakeLists.txt diff --git a/LC/LCP11/cpp-LCP11/main.cpp b/LC/LCP011/cpp-LCP011/main.cpp similarity index 100% rename from LC/LCP11/cpp-LCP11/main.cpp rename to LC/LCP011/cpp-LCP011/main.cpp diff --git a/LC/LCP11/cpp-LCP11/main2.cpp b/LC/LCP011/cpp-LCP011/main2.cpp similarity index 100% rename from LC/LCP11/cpp-LCP11/main2.cpp rename to LC/LCP011/cpp-LCP011/main2.cpp diff --git a/LC/LCP12/cpp-LCP12/CMakeLists.txt b/LC/LCP012/cpp-LCP012/CMakeLists.txt similarity index 100% rename from LC/LCP12/cpp-LCP12/CMakeLists.txt rename to LC/LCP012/cpp-LCP012/CMakeLists.txt diff --git a/LC/LCP12/cpp-LCP12/main.cpp b/LC/LCP012/cpp-LCP012/main.cpp similarity index 100% rename from LC/LCP12/cpp-LCP12/main.cpp rename to LC/LCP012/cpp-LCP012/main.cpp diff --git a/LC/LCP13/cpp-LCP13/CMakeLists.txt b/LC/LCP013/cpp-LCP013/CMakeLists.txt similarity index 100% rename from LC/LCP13/cpp-LCP13/CMakeLists.txt rename to LC/LCP013/cpp-LCP013/CMakeLists.txt diff --git a/LC/LCP13/cpp-LCP13/main.cpp b/LC/LCP013/cpp-LCP013/main.cpp similarity index 100% rename from LC/LCP13/cpp-LCP13/main.cpp rename to LC/LCP013/cpp-LCP013/main.cpp diff --git a/LC/LCP13/cpp-LCP13/main2.cpp b/LC/LCP013/cpp-LCP013/main2.cpp similarity index 100% rename from LC/LCP13/cpp-LCP13/main2.cpp rename to LC/LCP013/cpp-LCP013/main2.cpp diff --git a/LC/LCP14/cpp-LCP14/CMakeLists.txt b/LC/LCP014/cpp-LCP014/CMakeLists.txt similarity index 100% rename from LC/LCP14/cpp-LCP14/CMakeLists.txt rename to LC/LCP014/cpp-LCP014/CMakeLists.txt diff --git a/LC/LCP14/cpp-LCP14/main.cpp b/LC/LCP014/cpp-LCP014/main.cpp similarity index 100% rename from LC/LCP14/cpp-LCP14/main.cpp rename to LC/LCP014/cpp-LCP014/main.cpp diff --git a/LC/LCP14/cpp-LCP14/main2.cpp b/LC/LCP014/cpp-LCP014/main2.cpp similarity index 100% rename from LC/LCP14/cpp-LCP14/main2.cpp rename to LC/LCP014/cpp-LCP014/main2.cpp diff --git a/LC/LCP15/cpp-LCP15/CMakeLists.txt b/LC/LCP015/cpp-LCP015/CMakeLists.txt similarity index 100% rename from LC/LCP15/cpp-LCP15/CMakeLists.txt rename to LC/LCP015/cpp-LCP015/CMakeLists.txt diff --git a/LC/LCP15/cpp-LCP15/main.cpp b/LC/LCP015/cpp-LCP015/main.cpp similarity index 100% rename from LC/LCP15/cpp-LCP15/main.cpp rename to LC/LCP015/cpp-LCP015/main.cpp diff --git a/LC/LCP15/cpp-LCP15/main2.cpp b/LC/LCP015/cpp-LCP015/main2.cpp similarity index 100% rename from LC/LCP15/cpp-LCP15/main2.cpp rename to LC/LCP015/cpp-LCP015/main2.cpp diff --git a/LC/LCP15/cpp-LCP15/main3.cpp b/LC/LCP015/cpp-LCP015/main3.cpp similarity index 100% rename from LC/LCP15/cpp-LCP15/main3.cpp rename to LC/LCP015/cpp-LCP015/main3.cpp diff --git a/LC/LCP16/cpp-LCP16/CMakeLists.txt b/LC/LCP016/cpp-LCP016/CMakeLists.txt similarity index 100% rename from LC/LCP16/cpp-LCP16/CMakeLists.txt rename to LC/LCP016/cpp-LCP016/CMakeLists.txt diff --git a/LC/LCP16/cpp-LCP16/main.cpp b/LC/LCP016/cpp-LCP016/main.cpp similarity index 100% rename from LC/LCP16/cpp-LCP16/main.cpp rename to LC/LCP016/cpp-LCP016/main.cpp diff --git a/LC/LCP17/cpp-LCP17/CMakeLists.txt b/LC/LCP017/cpp-LCP017/CMakeLists.txt similarity index 100% rename from LC/LCP17/cpp-LCP17/CMakeLists.txt rename to LC/LCP017/cpp-LCP017/CMakeLists.txt diff --git a/LC/LCP17/cpp-LCP17/main.cpp b/LC/LCP017/cpp-LCP017/main.cpp similarity index 100% rename from LC/LCP17/cpp-LCP17/main.cpp rename to LC/LCP017/cpp-LCP017/main.cpp diff --git a/LC/LCP18/cpp-LCP18/CMakeLists.txt b/LC/LCP018/cpp-LCP018/CMakeLists.txt similarity index 100% rename from LC/LCP18/cpp-LCP18/CMakeLists.txt rename to LC/LCP018/cpp-LCP018/CMakeLists.txt diff --git a/LC/LCP18/cpp-LCP18/main.cpp b/LC/LCP018/cpp-LCP018/main.cpp similarity index 100% rename from LC/LCP18/cpp-LCP18/main.cpp rename to LC/LCP018/cpp-LCP018/main.cpp diff --git a/LC/LCP19/cpp-LCP19/CMakeLists.txt b/LC/LCP019/cpp-LCP019/CMakeLists.txt similarity index 100% rename from LC/LCP19/cpp-LCP19/CMakeLists.txt rename to LC/LCP019/cpp-LCP019/CMakeLists.txt diff --git a/LC/LCP19/cpp-LCP19/main.cpp b/LC/LCP019/cpp-LCP019/main.cpp similarity index 100% rename from LC/LCP19/cpp-LCP19/main.cpp rename to LC/LCP019/cpp-LCP019/main.cpp diff --git a/LC/LCP19/cpp-LCP19/main2.cpp b/LC/LCP019/cpp-LCP019/main2.cpp similarity index 100% rename from LC/LCP19/cpp-LCP19/main2.cpp rename to LC/LCP019/cpp-LCP019/main2.cpp diff --git a/LC/LCP20/D/CMakeLists.txt b/LC/LCP020/cpp-LCP020/CMakeLists.txt similarity index 100% rename from LC/LCP20/D/CMakeLists.txt rename to LC/LCP020/cpp-LCP020/CMakeLists.txt diff --git a/LC/LCP20/D/main.cpp b/LC/LCP020/cpp-LCP020/main.cpp similarity index 100% rename from LC/LCP20/D/main.cpp rename to LC/LCP020/cpp-LCP020/main.cpp diff --git a/LC/LCP21/cpp-LCP21/CMakeLists.txt b/LC/LCP021/cpp-LCP021/CMakeLists.txt similarity index 100% rename from LC/LCP21/cpp-LCP21/CMakeLists.txt rename to LC/LCP021/cpp-LCP021/CMakeLists.txt diff --git a/LC/LCP21/cpp-LCP21/main.cpp b/LC/LCP021/cpp-LCP021/main.cpp similarity index 100% rename from LC/LCP21/cpp-LCP21/main.cpp rename to LC/LCP021/cpp-LCP021/main.cpp diff --git a/LC/LCP22/cpp-LCP22/CMakeLists.txt b/LC/LCP022/cpp-LCP022/CMakeLists.txt similarity index 100% rename from LC/LCP22/cpp-LCP22/CMakeLists.txt rename to LC/LCP022/cpp-LCP022/CMakeLists.txt diff --git a/LC/LCP22/cpp-LCP22/main.cpp b/LC/LCP022/cpp-LCP022/main.cpp similarity index 100% rename from LC/LCP22/cpp-LCP22/main.cpp rename to LC/LCP022/cpp-LCP022/main.cpp diff --git a/LC/LCP22/cpp-LCP22/main2.cpp b/LC/LCP022/cpp-LCP022/main2.cpp similarity index 100% rename from LC/LCP22/cpp-LCP22/main2.cpp rename to LC/LCP022/cpp-LCP022/main2.cpp diff --git a/LC/LCP23/cpp-LCP23/CMakeLists.txt b/LC/LCP023/cpp-LCP023/CMakeLists.txt similarity index 100% rename from LC/LCP23/cpp-LCP23/CMakeLists.txt rename to LC/LCP023/cpp-LCP023/CMakeLists.txt diff --git a/LC/LCP23/cpp-LCP23/main.cpp b/LC/LCP023/cpp-LCP023/main.cpp similarity index 100% rename from LC/LCP23/cpp-LCP23/main.cpp rename to LC/LCP023/cpp-LCP023/main.cpp diff --git a/LC/LCP24/cpp-LCP24/CMakeLists.txt b/LC/LCP024/cpp-LCP024/CMakeLists.txt similarity index 100% rename from LC/LCP24/cpp-LCP24/CMakeLists.txt rename to LC/LCP024/cpp-LCP024/CMakeLists.txt diff --git a/LC/LCP24/cpp-LCP24/main.cpp b/LC/LCP024/cpp-LCP024/main.cpp similarity index 100% rename from LC/LCP24/cpp-LCP24/main.cpp rename to LC/LCP024/cpp-LCP024/main.cpp diff --git a/LC/LCP25/cpp-LCP25/CMakeLists.txt b/LC/LCP025/cpp-LCP025/CMakeLists.txt similarity index 100% rename from LC/LCP25/cpp-LCP25/CMakeLists.txt rename to LC/LCP025/cpp-LCP025/CMakeLists.txt diff --git a/LC/LCP25/cpp-LCP25/main.cpp b/LC/LCP025/cpp-LCP025/main.cpp similarity index 100% rename from LC/LCP25/cpp-LCP25/main.cpp rename to LC/LCP025/cpp-LCP025/main.cpp diff --git a/LC/LCP25/cpp-LCP25/main2.cpp b/LC/LCP025/cpp-LCP025/main2.cpp similarity index 100% rename from LC/LCP25/cpp-LCP25/main2.cpp rename to LC/LCP025/cpp-LCP025/main2.cpp diff --git a/LC/LCP28/cpp-LCP28/CMakeLists.txt b/LC/LCP028/cpp-LCP028/CMakeLists.txt similarity index 100% rename from LC/LCP28/cpp-LCP28/CMakeLists.txt rename to LC/LCP028/cpp-LCP028/CMakeLists.txt diff --git a/LC/LCP28/cpp-LCP28/main.cpp b/LC/LCP028/cpp-LCP028/main.cpp similarity index 100% rename from LC/LCP28/cpp-LCP28/main.cpp rename to LC/LCP028/cpp-LCP028/main.cpp diff --git a/LC/LCP29/cpp-LCP29/CMakeLists.txt b/LC/LCP029/cpp-LCP029/CMakeLists.txt similarity index 100% rename from LC/LCP29/cpp-LCP29/CMakeLists.txt rename to LC/LCP029/cpp-LCP029/CMakeLists.txt diff --git a/LC/LCP29/cpp-LCP29/main.cpp b/LC/LCP029/cpp-LCP029/main.cpp similarity index 100% rename from LC/LCP29/cpp-LCP29/main.cpp rename to LC/LCP029/cpp-LCP029/main.cpp diff --git a/LC/LCP39/cpp-LCP39/CMakeLists.txt b/LC/LCP039/cpp-LCP039/CMakeLists.txt similarity index 100% rename from LC/LCP39/cpp-LCP39/CMakeLists.txt rename to LC/LCP039/cpp-LCP039/CMakeLists.txt diff --git a/LC/LCP39/cpp-LCP39/main.cpp b/LC/LCP039/cpp-LCP039/main.cpp similarity index 100% rename from LC/LCP39/cpp-LCP39/main.cpp rename to LC/LCP039/cpp-LCP039/main.cpp diff --git a/LC/LCP40/cpp-LCP40/CMakeLists.txt b/LC/LCP040/cpp-LCP040/CMakeLists.txt similarity index 100% rename from LC/LCP40/cpp-LCP40/CMakeLists.txt rename to LC/LCP040/cpp-LCP040/CMakeLists.txt diff --git a/LC/LCP40/cpp-LCP40/main.cpp b/LC/LCP040/cpp-LCP040/main.cpp similarity index 100% rename from LC/LCP40/cpp-LCP40/main.cpp rename to LC/LCP040/cpp-LCP040/main.cpp diff --git a/LC/LCP41/cpp-LCP41/CMakeLists.txt b/LC/LCP041/cpp-LCP041/CMakeLists.txt similarity index 100% rename from LC/LCP41/cpp-LCP41/CMakeLists.txt rename to LC/LCP041/cpp-LCP041/CMakeLists.txt diff --git a/LC/LCP41/cpp-LCP41/main.cpp b/LC/LCP041/cpp-LCP041/main.cpp similarity index 100% rename from LC/LCP41/cpp-LCP41/main.cpp rename to LC/LCP041/cpp-LCP041/main.cpp diff --git a/LC/LCP42/cpp-LCP42/CMakeLists.txt b/LC/LCP042/cpp-LCP042/CMakeLists.txt similarity index 100% rename from LC/LCP42/cpp-LCP42/CMakeLists.txt rename to LC/LCP042/cpp-LCP042/CMakeLists.txt diff --git a/LC/LCP42/cpp-LCP42/main.cpp b/LC/LCP042/cpp-LCP042/main.cpp similarity index 100% rename from LC/LCP42/cpp-LCP42/main.cpp rename to LC/LCP042/cpp-LCP042/main.cpp diff --git a/LC/LCP43/cpp-LCP43/CMakeLists.txt b/LC/LCP043/cpp-LCP043/CMakeLists.txt similarity index 100% rename from LC/LCP43/cpp-LCP43/CMakeLists.txt rename to LC/LCP043/cpp-LCP043/CMakeLists.txt diff --git a/LC/LCP43/cpp-LCP43/main.cpp b/LC/LCP043/cpp-LCP043/main.cpp similarity index 100% rename from LC/LCP43/cpp-LCP43/main.cpp rename to LC/LCP043/cpp-LCP043/main.cpp diff --git a/LC/LCP44/cpp-LCP44/CMakeLists.txt b/LC/LCP044/cpp-LCP044/CMakeLists.txt similarity index 100% rename from LC/LCP44/cpp-LCP44/CMakeLists.txt rename to LC/LCP044/cpp-LCP044/CMakeLists.txt diff --git a/LC/LCP44/cpp-LCP44/main.cpp b/LC/LCP044/cpp-LCP044/main.cpp similarity index 100% rename from LC/LCP44/cpp-LCP44/main.cpp rename to LC/LCP044/cpp-LCP044/main.cpp diff --git a/LC/LCP45/cpp-LCP45/CMakeLists.txt b/LC/LCP045/cpp-LCP045/CMakeLists.txt similarity index 100% rename from LC/LCP45/cpp-LCP45/CMakeLists.txt rename to LC/LCP045/cpp-LCP045/CMakeLists.txt diff --git a/LC/LCP45/cpp-LCP45/main.cpp b/LC/LCP045/cpp-LCP045/main.cpp similarity index 100% rename from LC/LCP45/cpp-LCP45/main.cpp rename to LC/LCP045/cpp-LCP045/main.cpp diff --git a/LC/LCP46/cpp-LCP46/CMakeLists.txt b/LC/LCP046/cpp-LCP046/CMakeLists.txt similarity index 100% rename from LC/LCP46/cpp-LCP46/CMakeLists.txt rename to LC/LCP046/cpp-LCP046/CMakeLists.txt diff --git a/LC/LCP46/cpp-LCP46/main.cpp b/LC/LCP046/cpp-LCP046/main.cpp similarity index 100% rename from LC/LCP46/cpp-LCP46/main.cpp rename to LC/LCP046/cpp-LCP046/main.cpp diff --git a/LC/LCP47/cpp-LCP47/CMakeLists.txt b/LC/LCP047/cpp-LCP047/CMakeLists.txt similarity index 100% rename from LC/LCP47/cpp-LCP47/CMakeLists.txt rename to LC/LCP047/cpp-LCP047/CMakeLists.txt diff --git a/LC/LCP47/cpp-LCP47/main.cpp b/LC/LCP047/cpp-LCP047/main.cpp similarity index 100% rename from LC/LCP47/cpp-LCP47/main.cpp rename to LC/LCP047/cpp-LCP047/main.cpp diff --git a/LC/LCP47/cpp-LCP47/main2.cpp b/LC/LCP047/cpp-LCP047/main2.cpp similarity index 100% rename from LC/LCP47/cpp-LCP47/main2.cpp rename to LC/LCP047/cpp-LCP047/main2.cpp diff --git a/LC/LCP48/cpp-LCP48/CMakeLists.txt b/LC/LCP048/cpp-LCP048/CMakeLists.txt similarity index 100% rename from LC/LCP48/cpp-LCP48/CMakeLists.txt rename to LC/LCP048/cpp-LCP048/CMakeLists.txt diff --git a/LC/LCP48/cpp-LCP48/main.cpp b/LC/LCP048/cpp-LCP048/main.cpp similarity index 100% rename from LC/LCP48/cpp-LCP48/main.cpp rename to LC/LCP048/cpp-LCP048/main.cpp diff --git a/LC/LCP50/cpp-LCP50/CMakeLists.txt b/LC/LCP050/cpp-LCP050/CMakeLists.txt similarity index 100% rename from LC/LCP50/cpp-LCP50/CMakeLists.txt rename to LC/LCP050/cpp-LCP050/CMakeLists.txt diff --git a/LC/LCP50/cpp-LCP50/main.cpp b/LC/LCP050/cpp-LCP050/main.cpp similarity index 100% rename from LC/LCP50/cpp-LCP50/main.cpp rename to LC/LCP050/cpp-LCP050/main.cpp diff --git a/LC/LCP51/cpp-LCP51/CMakeLists.txt b/LC/LCP051/cpp-LCP051/CMakeLists.txt similarity index 100% rename from LC/LCP51/cpp-LCP51/CMakeLists.txt rename to LC/LCP051/cpp-LCP051/CMakeLists.txt diff --git a/LC/LCP51/cpp-LCP51/main.cpp b/LC/LCP051/cpp-LCP051/main.cpp similarity index 100% rename from LC/LCP51/cpp-LCP51/main.cpp rename to LC/LCP051/cpp-LCP051/main.cpp diff --git a/LC/LCP52/cpp-LCP52/CMakeLists.txt b/LC/LCP052/cpp-LCP052/CMakeLists.txt similarity index 100% rename from LC/LCP52/cpp-LCP52/CMakeLists.txt rename to LC/LCP052/cpp-LCP052/CMakeLists.txt diff --git a/LC/LCP52/cpp-LCP52/main.cpp b/LC/LCP052/cpp-LCP052/main.cpp similarity index 100% rename from LC/LCP52/cpp-LCP52/main.cpp rename to LC/LCP052/cpp-LCP052/main.cpp diff --git a/LC/LCP55/cpp-LCP55/CMakeLists.txt b/LC/LCP055/cpp-LCP055/CMakeLists.txt similarity index 100% rename from LC/LCP55/cpp-LCP55/CMakeLists.txt rename to LC/LCP055/cpp-LCP055/CMakeLists.txt diff --git a/LC/LCP55/cpp-LCP55/main.cpp b/LC/LCP055/cpp-LCP055/main.cpp similarity index 100% rename from LC/LCP55/cpp-LCP55/main.cpp rename to LC/LCP055/cpp-LCP055/main.cpp diff --git a/LC/LCP56/cpp-LCP56/CMakeLists.txt b/LC/LCP056/cpp-LCP056/CMakeLists.txt similarity index 100% rename from LC/LCP56/cpp-LCP56/CMakeLists.txt rename to LC/LCP056/cpp-LCP056/CMakeLists.txt diff --git a/LC/LCP56/cpp-LCP56/main.cpp b/LC/LCP056/cpp-LCP056/main.cpp similarity index 100% rename from LC/LCP56/cpp-LCP56/main.cpp rename to LC/LCP056/cpp-LCP056/main.cpp diff --git a/LC/LCP57/cpp-LCP57/CMakeLists.txt b/LC/LCP057/cpp-LCP057/CMakeLists.txt similarity index 100% rename from LC/LCP57/cpp-LCP57/CMakeLists.txt rename to LC/LCP057/cpp-LCP057/CMakeLists.txt diff --git a/LC/LCP57/cpp-LCP57/main.cpp b/LC/LCP057/cpp-LCP057/main.cpp similarity index 100% rename from LC/LCP57/cpp-LCP57/main.cpp rename to LC/LCP057/cpp-LCP057/main.cpp diff --git a/readme.md b/readme.md index 511463bd..df628713 100644 --- a/readme.md +++ b/readme.md @@ -2292,53 +2292,53 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | -| LCP01 | [猜数字](https://leetcode-cn.com/problems/guess-numbers/) | [无] | [C++](LC/LCP01/cpp-LCP01/) | | | -| LCP02 | [分式化简](https://leetcode-cn.com/problems/deep-dark-fraction/) | [无] | [C++](LC/LCP02/cpp-LCP02/) | | | -| LCP03 | [机器人大冒险](https://leetcode-cn.com/problems/programmable-robot/) | [无] | [C++](LC/LCP03/cpp-LCP03/) | | | -| LCP04 | [覆盖](https://leetcode-cn.com/problems/broken-board-dominoes/) | [无] | [C++](LC/LCP04/cpp-LCP04/) | | | -| LCP05 | [发 LeetCoin](https://leetcode-cn.com/problems/coin-bonus/) | [无] | [C++](LC/LCP05/cpp-LCP05/) | | | -| LCP06 | [拿硬币](https://leetcode-cn.com/problems/na-ying-bi/) | [题解](https://leetcode-cn.com/problems/na-ying-bi/solution/na-ying-bi-by-leetcode-solution/) | [C++](LC/LCP06/cpp-LCP06/) | -| LCP07 | [传递信息](https://leetcode-cn.com/problems/chuan-di-xin-xi/) | [题解](https://leetcode-cn.com/problems/chuan-di-xin-xi/solution/chuan-di-xin-xi-by-leetcode-solution/) | [C++](LC/LCP07/cpp-LCP07/) | | | -| LCP08 | [剧情触发时间](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/) | [题解](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/solution/ju-qing-hong-fa-shi-jian-by-leetcode-solution/) | [C++](LC/LCP08/cpp-LCP08/) | | | -| LCP09 | [最小跳跃次数](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/) | [题解](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/solution/zui-xiao-tiao-yue-ci-shu-by-leetcode-solution/) | [C++](LC/LCP09/cpp-LCP09/) | | | -| LCP10 | [二叉树任务调度](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/) | [题解](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/solution/dfs-si-lu-dai-ma-he-zheng-ming-by-leetcode-solutio/) | [C++](LC/LCP10/cpp-LCP10/) | | | -| LCP11 | [期望个数统计](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/) | [题解](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/solution/qi-wang-ge-shu-tong-ji-qi-wang-ji-suan-yu-zheng-mi/) | [C++](LC/LCP11/cpp-LCP11/) | | | -| LCP12 | [小张刷题计划](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/) | [题解](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/solution/xiao-zhang-shua-ti-ji-hua-er-fen-cha-zhao-by-leetc/) | [C++](LC/LCP12/cpp-LCP12/) | | | -| LCP13 | [寻宝](https://leetcode-cn.com/problems/xun-bao/) | [题解](https://leetcode-cn.com/problems/xun-bao/solution/xun-bao-bfs-dp-by-leetcode-solution/) | [C++](LC/LCP13/cpp-LCP13/) | | | -| LCP14 | [切分数组](https://leetcode-cn.com/problems/qie-fen-shu-zu/) | [题解](https://leetcode-cn.com/problems/qie-fen-shu-zu/solution/qie-fen-shu-zu-zhi-shu-shai-dp-by-leetcode-solutio/) | [C++](LC/LCP14/cpp-LCP14/) | | | -| LCP15 | [游乐园的迷宫](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/solution/you-le-yuan-de-mi-gong-tan-xin-si-lu-by-leetcode-s/) | [C++](LC/LCP15/cpp-LCP15/) | | | -| LCP16 | [游乐园的游览计划](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/solution/you-le-yuan-de-you-lan-ji-hua-tu-lun-jie-xi-by-l-2/) | [C++](LC/LCP16/cpp-LCP16/) | | | -| LCP17 | [速算机器人](https://leetcode-cn.com/problems/nGK0Fy/) | [无] | [C++](LC/LCP17/cpp-LCP17/) | | | -| LCP18 | [早餐组合](https://leetcode-cn.com/problems/2vYnGI/) | [无] | [C++](LC/LCP18/cpp-LCP18/) | | | -| LCP19 | [秋叶收藏集](https://leetcode-cn.com/problems/UlBDOe/) | [题解](https://leetcode-cn.com/problems/UlBDOe/solution/qiu-xie-shou-cang-ji-by-leetcode-solution/) | [C++](LC/LCP19/cpp-LCP19/) | | | -| LCP20 | [快速公交](https://leetcode-cn.com/problems/meChtZ/) | [无] | [C++](LC/LCP20/cpp-LCP20/) | | | -| LCP21 | [追逐游戏](https://leetcode-cn.com/problems/Za25hA/) | [无] | [C++](LC/LCP21/cpp-LCP21/) | | | -| LCP22 | [黑白方格画](https://leetcode-cn.com/problems/ccw6C7/) | [无] | [C++](LC/LCP22/cpp-LCP22/) | | | -| LCP23 | [魔术排列](https://leetcode-cn.com/problems/er94lq/) | [无] | [C++](LC/LCP23/cpp-LCP23/) | | | -| LCP24 | [数字游戏](https://leetcode-cn.com/problems/5TxKeK/) | [无] | [C++](LC/LCP24/cpp-LCP24/) | | | -| LCP25 | [古董键盘](https://leetcode-cn.com/problems/Uh984O/) | [无] | [C++](LC/LCP25/cpp-LCP25/) | | | -| | | | | | | -| LCP28 | [采购方案](https://leetcode-cn.com/problems/4xy4Wx/) | [无] | [C++](LC/LCP28/cpp-LCP28/) | | | -| LCP29 | [乐团站位](https://leetcode-cn.com/problems/SNJvJP/) | [无] | [C++](LC/LCP29/cpp-LCP29/) | | | -| | | | | | | -| LCP39 | [无人机方阵](https://leetcode-cn.com/problems/0jQkd0/) | [无] | [C++](LC/LCP39/cpp-LCP39/) | | | -| LCP40 | [心算挑战](https://leetcode-cn.com/problems/uOAnQW/) | [无] | [C++](LC/LCP40/cpp-LCP40/) | | | -| LCP41 | [黑白翻转棋](https://leetcode-cn.com/problems/fHi6rV/) | [无] | [C++](LC/LCP41/cpp-LCP41/)| | | -| LCP42 | [玩具套圈](https://leetcode-cn.com/problems/vFjcfV/) | [无] | [C++](LC/LCP42/cpp-LCP42/) | | | -| LCP43 | [十字路口的交通](https://leetcode-cn.com/problems/Y1VbOX/) | [无] | [C++](LC/LCP43/cpp-LCP43/) | | | -| LCP44 | [开幕式焰火](https://leetcode-cn.com/problems/sZ59z6/) | [无] | [C++](LC/LCP44/cpp-LCP44/) | | | -| LCP45 | [自行车炫技赛场](https://leetcode-cn.com/problems/kplEvH/) | [无] | [C++](LC/LCP45/cpp-LCP45/) | | | -| LCP46 | [志愿者调配](https://leetcode-cn.com/problems/05ZEDJ/) | [无] | [C++](LC/LCP46/cpp-LCP46/) | | | -| LCP47 | [入场安检](https://leetcode-cn.com/problems/oPs9Bm/) | [无] | [C++](LC/LCP47/cpp-LCP47/) | | | -| LCP48 | [无限棋局](https://leetcode-cn.com/problems/fsa7oZ/) | [无] | [C++](LC/LCP48/cpp-LCP48/) | | | -| | | | | | | -| LCP50 | [宝石补给](https://leetcode-cn.com/problems/WHnhjV/) | [无] | [C++](LC/LCP50/cpp-LCP50/) | | | -| LCP51 | [烹饪料理](https://leetcode-cn.com/problems/UEcfPD/) | [无] | [C++](LC/LCP51/cpp-LCP51/) | | | -| LCP52 | [二叉搜索树染色](https://leetcode-cn.com/problems/QO5KpG/) | [无] [缺:非线段树做法] | [C++](LC/LCP52/cpp-LCP52/) | | | -| | | | | | | -| LCP55 | [采集果实](https://leetcode-cn.com/problems/PTXy4P/) | [无] | [C++](LC/LCP55/cpp-LCP55/) | | | -| LCP56 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LC/LCP56/cpp-LCP56/) | | | -| LCP57 | [打地鼠](https://leetcode-cn.com/problems/ZbAuEH/) | [无] | [C++](LC/LCP57/cpp-LCP57/) | | | +| LCP001 | [猜数字](https://leetcode-cn.com/problems/guess-numbers/) | [无] | [C++](LC/LCP001/cpp-LCP001/) | | | +| LCP002 | [分式化简](https://leetcode-cn.com/problems/deep-dark-fraction/) | [无] | [C++](LC/LCP002/cpp-LCP002/) | | | +| LCP003 | [机器人大冒险](https://leetcode-cn.com/problems/programmable-robot/) | [无] | [C++](LC/LCP003/cpp-LCP003/) | | | +| LCP004 | [覆盖](https://leetcode-cn.com/problems/broken-board-dominoes/) | [无] | [C++](LC/LCP004/cpp-LCP004/) | | | +| LCP005 | [发 LeetCoin](https://leetcode-cn.com/problems/coin-bonus/) | [无] | [C++](LC/LCP005/cpp-LCP005/) | | | +| LCP006 | [拿硬币](https://leetcode-cn.com/problems/na-ying-bi/) | [题解](https://leetcode-cn.com/problems/na-ying-bi/solution/na-ying-bi-by-leetcode-solution/) | [C++](LC/LCP006/cpp-LCP006/) | +| LCP007 | [传递信息](https://leetcode-cn.com/problems/chuan-di-xin-xi/) | [题解](https://leetcode-cn.com/problems/chuan-di-xin-xi/solution/chuan-di-xin-xi-by-leetcode-solution/) | [C++](LC/LCP007/cpp-LCP007/) | | | +| LCP008 | [剧情触发时间](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/) | [题解](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/solution/ju-qing-hong-fa-shi-jian-by-leetcode-solution/) | [C++](LC/LCP008/cpp-LCP008/) | | | +| LCP009 | [最小跳跃次数](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/) | [题解](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/solution/zui-xiao-tiao-yue-ci-shu-by-leetcode-solution/) | [C++](LC/LCP009/cpp-LCP009/) | | | +| LCP010 | [二叉树任务调度](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/) | [题解](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/solution/dfs-si-lu-dai-ma-he-zheng-ming-by-leetcode-solutio/) | [C++](LC/LCP010/cpp-LCP010/) | | | +| LCP011 | [期望个数统计](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/) | [题解](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/solution/qi-wang-ge-shu-tong-ji-qi-wang-ji-suan-yu-zheng-mi/) | [C++](LC/LCP011/cpp-LCP011/) | | | +| LCP012 | [小张刷题计划](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/) | [题解](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/solution/xiao-zhang-shua-ti-ji-hua-er-fen-cha-zhao-by-leetc/) | [C++](LC/LCP012/cpp-LCP012/) | | | +| LCP013 | [寻宝](https://leetcode-cn.com/problems/xun-bao/) | [题解](https://leetcode-cn.com/problems/xun-bao/solution/xun-bao-bfs-dp-by-leetcode-solution/) | [C++](LC/LCP013/cpp-LCP013/) | | | +| LCP014 | [切分数组](https://leetcode-cn.com/problems/qie-fen-shu-zu/) | [题解](https://leetcode-cn.com/problems/qie-fen-shu-zu/solution/qie-fen-shu-zu-zhi-shu-shai-dp-by-leetcode-solutio/) | [C++](LC/LCP014/cpp-LCP014/) | | | +| LCP015 | [游乐园的迷宫](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/solution/you-le-yuan-de-mi-gong-tan-xin-si-lu-by-leetcode-s/) | [C++](LC/LCP015/cpp-LCP015/) | | | +| LCP016 | [游乐园的游览计划](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/solution/you-le-yuan-de-you-lan-ji-hua-tu-lun-jie-xi-by-l-2/) | [C++](LC/LCP016/cpp-LCP016/) | | | +| LCP017 | [速算机器人](https://leetcode-cn.com/problems/nGK0Fy/) | [无] | [C++](LC/LCP017/cpp-LCP017/) | | | +| LCP018 | [早餐组合](https://leetcode-cn.com/problems/2vYnGI/) | [无] | [C++](LC/LCP018/cpp-LCP018/) | | | +| LCP019 | [秋叶收藏集](https://leetcode-cn.com/problems/UlBDOe/) | [题解](https://leetcode-cn.com/problems/UlBDOe/solution/qiu-xie-shou-cang-ji-by-leetcode-solution/) | [C++](LC/LCP019/cpp-LCP019/) | | | +| LCP020 | [快速公交](https://leetcode-cn.com/problems/meChtZ/) | [无] | [C++](LC/LCP020/cpp-LCP020/) | | | +| LCP021 | [追逐游戏](https://leetcode-cn.com/problems/Za25hA/) | [无] | [C++](LC/LCP021/cpp-LCP021/) | | | +| LCP022 | [黑白方格画](https://leetcode-cn.com/problems/ccw6C7/) | [无] | [C++](LC/LCP022/cpp-LCP022/) | | | +| LCP023 | [魔术排列](https://leetcode-cn.com/problems/er94lq/) | [无] | [C++](LC/LCP023/cpp-LCP023/) | | | +| LCP024 | [数字游戏](https://leetcode-cn.com/problems/5TxKeK/) | [无] | [C++](LC/LCP024/cpp-LCP024/) | | | +| LCP025 | [古董键盘](https://leetcode-cn.com/problems/Uh984O/) | [无] | [C++](LC/LCP025/cpp-LCP025/) | | | +| | | | | | | +| LCP028 | [采购方案](https://leetcode-cn.com/problems/4xy4Wx/) | [无] | [C++](LC/LCP028/cpp-LCP028/) | | | +| LCP029 | [乐团站位](https://leetcode-cn.com/problems/SNJvJP/) | [无] | [C++](LC/LCP029/cpp-LCP029/) | | | +| | | | | | | +| LCP039 | [无人机方阵](https://leetcode-cn.com/problems/0jQkd0/) | [无] | [C++](LC/LCP039/cpp-LCP039/) | | | +| LCP040 | [心算挑战](https://leetcode-cn.com/problems/uOAnQW/) | [无] | [C++](LC/LCP040/cpp-LCP040/) | | | +| LCP041 | [黑白翻转棋](https://leetcode-cn.com/problems/fHi6rV/) | [无] | [C++](LC/LCP041/cpp-LCP041/)| | | +| LCP042 | [玩具套圈](https://leetcode-cn.com/problems/vFjcfV/) | [无] | [C++](LC/LCP042/cpp-LCP042/) | | | +| LCP043 | [十字路口的交通](https://leetcode-cn.com/problems/Y1VbOX/) | [无] | [C++](LC/LCP043/cpp-LCP043/) | | | +| LCP044 | [开幕式焰火](https://leetcode-cn.com/problems/sZ59z6/) | [无] | [C++](LC/LCP044/cpp-LCP044/) | | | +| LCP045 | [自行车炫技赛场](https://leetcode-cn.com/problems/kplEvH/) | [无] | [C++](LC/LCP045/cpp-LCP045/) | | | +| LCP046 | [志愿者调配](https://leetcode-cn.com/problems/05ZEDJ/) | [无] | [C++](LC/LCP046/cpp-LCP046/) | | | +| LCP047 | [入场安检](https://leetcode-cn.com/problems/oPs9Bm/) | [无] | [C++](LC/LCP047/cpp-LCP047/) | | | +| LCP048 | [无限棋局](https://leetcode-cn.com/problems/fsa7oZ/) | [无] | [C++](LC/LCP048/cpp-LCP048/) | | | +| | | | | | | +| LCP050 | [宝石补给](https://leetcode-cn.com/problems/WHnhjV/) | [无] | [C++](LC/LCP050/cpp-LCP050/) | | | +| LCP051 | [烹饪料理](https://leetcode-cn.com/problems/UEcfPD/) | [无] | [C++](LC/LCP051/cpp-LCP051/) | | | +| LCP052 | [二叉搜索树染色](https://leetcode-cn.com/problems/QO5KpG/) | [无] [缺:非线段树做法] | [C++](LC/LCP052/cpp-LCP052/) | | | +| | | | | | | +| LCP055 | [采集果实](https://leetcode-cn.com/problems/PTXy4P/) | [无] | [C++](LC/LCP055/cpp-LCP055/) | | | +| LCP056 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LC/LCP056/cpp-LCP056/) | | | +| LCP057 | [打地鼠](https://leetcode-cn.com/problems/ZbAuEH/) | [无] | [C++](LC/LCP057/cpp-LCP057/) | | | | | | | | | | ## 其他 From 6dc14737a43eb2d9dad8e1c577347ec8a215fda5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Oct 2022 14:53:21 -0700 Subject: [PATCH 202/390] 2427-2430 solved. --- .../cpp-2427/CMakeLists.txt | 6 ++ .../cpp-2427/main.cpp | 30 +++++++++ .../cpp-2428/CMakeLists.txt | 6 ++ .../cpp-2428/main.cpp | 32 +++++++++ .../2429-Minimize-XOR/cpp-2429/CMakeLists.txt | 6 ++ 2001-2500/2429-Minimize-XOR/cpp-2429/main.cpp | 53 +++++++++++++++ .../cpp-2430/CMakeLists.txt | 6 ++ .../cpp-2430/main.cpp | 66 +++++++++++++++++++ .../cpp-2430/main2.cpp | 47 +++++++++++++ readme.md | 5 ++ 10 files changed, 257 insertions(+) create mode 100644 2001-2500/2427-Number-of-Common-Factors/cpp-2427/CMakeLists.txt create mode 100644 2001-2500/2427-Number-of-Common-Factors/cpp-2427/main.cpp create mode 100644 2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/CMakeLists.txt create mode 100644 2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/main.cpp create mode 100644 2001-2500/2429-Minimize-XOR/cpp-2429/CMakeLists.txt create mode 100644 2001-2500/2429-Minimize-XOR/cpp-2429/main.cpp create mode 100644 2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/CMakeLists.txt create mode 100644 2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main.cpp create mode 100644 2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main2.cpp diff --git a/2001-2500/2427-Number-of-Common-Factors/cpp-2427/CMakeLists.txt b/2001-2500/2427-Number-of-Common-Factors/cpp-2427/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2427-Number-of-Common-Factors/cpp-2427/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2427-Number-of-Common-Factors/cpp-2427/main.cpp b/2001-2500/2427-Number-of-Common-Factors/cpp-2427/main.cpp new file mode 100644 index 00000000..52ee93e1 --- /dev/null +++ b/2001-2500/2427-Number-of-Common-Factors/cpp-2427/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/number-of-common-factors/ +/// Author : liuyubobobo +/// Time : 2022-10-01 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(min(a, b)) +/// Space Complexity: O(1) +class Solution { +public: + int commonFactors(int a, int b) { + + int res = 0; + int n = min(a, b); + for(int i = 1; i <= n; i ++) + if(a % i == 0 && b % i == 0) res ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/CMakeLists.txt b/2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/main.cpp b/2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/main.cpp new file mode 100644 index 00000000..33dec21b --- /dev/null +++ b/2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/maximum-sum-of-an-hourglass/ +/// Author : liuyubobobo +/// Time : 2022-10-01 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(R * C) +/// Space Complexity: O(1) +class Solution { +public: + int maxSum(vector>& g) { + + int R = g.size(), C = g[0].size(), res = 0; + for(int i = 0; i + 2 < R; i ++) + for(int j = 0; j + 2 < C; j ++){ + int t = g[i][j] + g[i][j + 1] + g[i][j + 2] + g[i + 1][j + 1] + g[i + 2][j] + g[i + 2][j + 1] + g[i + 2][j + 2]; + res = max(res, t); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2429-Minimize-XOR/cpp-2429/CMakeLists.txt b/2001-2500/2429-Minimize-XOR/cpp-2429/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2429-Minimize-XOR/cpp-2429/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2429-Minimize-XOR/cpp-2429/main.cpp b/2001-2500/2429-Minimize-XOR/cpp-2429/main.cpp new file mode 100644 index 00000000..b2d16528 --- /dev/null +++ b/2001-2500/2429-Minimize-XOR/cpp-2429/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/minimize-xor/ +/// Author : liuyubobobo +/// Time : 2022-10-01 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(log(num1) + log(num2)) +/// Space Complexity: O(1) +class Solution { +public: + int minimizeXor(int num1, int num2) { + + int one_cnt = __builtin_popcount(num2); + + int D = get_len(num1), res = 0; + for(int p = D - 1; p >= 0 && one_cnt; p --) + if((num1 >> p) & 1) one_cnt --, res += (1 << p); + + for(int p = 0; p <= 30 && one_cnt; p ++) + if(((num1 >> p) & 1) == 0) one_cnt --, res += (1 << p); + + return res; + } + +private: + int get_len(int x){ + int res = 0; + while(x){ + res ++, x >>= 1; + } + return res; + } +}; + + +int main() { + + cout << Solution().minimizeXor(3, 5) << '\n'; + // 3 + + cout << Solution().minimizeXor(1, 12) << '\n'; + // 3 + + cout << Solution().minimizeXor(91, 18) << '\n'; + // 80 + + return 0; +} diff --git a/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/CMakeLists.txt b/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/CMakeLists.txt new file mode 100644 index 00000000..541b2eff --- /dev/null +++ b/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) diff --git a/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main.cpp b/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main.cpp new file mode 100644 index 00000000..32b579a6 --- /dev/null +++ b/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/maximum-deletions-on-a-string/ +/// Author : liuyubobobo +/// Time : 2022-10-01 + +#include +#include + +using namespace std; + + +/// RK + DP +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class StringHashU{ + +private: + int n; + unsigned long long B; + vector h, p; + +public: + StringHashU(const string& s, unsigned long long B = 13331) : + n(s.size()), h(n + 1, 0), p(n + 1, 1), B(B){ + + for(int i = 0; i < n; i ++){ + h[i + 1] = h[i] * B + s[i]; + p[i + 1] = p[i] * B; + } + } + + unsigned long long get_hash(int l, int r){ + assert(l >= 0 && l < n); + assert(r >= 0 && r < n); + return h[r + 1] - h[l] * p[r - l + 1]; + } +}; + +class Solution { +public: + int deleteString(string s) { + + StringHashU hash(s); + + int n = s.size(); + vector dp(n + 1, 0); + dp[n - 1] = 1; + for(int start = n - 2; start >= 0; start --){ + int len = n - start, tres = 1; + for(int i = 1; i <= len / 2; i ++) + if(hash.get_hash(start, start + i - 1) == hash.get_hash(start + i, start + 2 * i - 1)) + tres = max(tres, 1 + dp[start + i]); + dp[start] = tres; + } + return dp[0]; + } +}; + + +int main() { + + string s2 = "aaabaab"; + cout << Solution().deleteString(s2) << '\n'; + // 4 + + return 0; +} diff --git a/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main2.cpp b/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main2.cpp new file mode 100644 index 00000000..01d026e7 --- /dev/null +++ b/2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/main2.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/maximum-deletions-on-a-string/ +/// Author : liuyubobobo +/// Time : 2022-10-02 + +#include +#include + +using namespace std; + + +/// Double DP +/// It's slower than using RK but this method is more standard. +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { +public: + int deleteString(string s) { + + int n = s.size(); + + vector> lcs(n + 1, vector(n + 1, 0)); + for(int i = n - 1; i >= 0; i --) + for(int j = n - 1; j >= 0; j --) + if(s[i] == s[j]) lcs[i][j] = 1 + lcs[i + 1][j + 1]; + + vector dp(n + 1, 0); + dp[n - 1] = 1; + for(int start = n - 2; start >= 0; start --){ + int len = n - start, tres = 1; + for(int i = 1; i <= len / 2; i ++) + if(lcs[start][start + i] >= i) + tres = max(tres, 1 + dp[start + i]); + dp[start] = tres; + } + return dp[0]; + } +}; + + +int main() { + + string s2 = "aaabaab"; + cout << Solution().deleteString(s2) << '\n'; + // 4 + + return 0; +} diff --git a/readme.md b/readme.md index df628713..d6ae271b 100644 --- a/readme.md +++ b/readme.md @@ -2287,6 +2287,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2422 | [Merge Operations to Turn Array Into a Palindrome](https://leetcode.com/problems/merge-operations-to-turn-array-into-a-palindrome/) | [无] | [C++](2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/) | | | | | | | | | | +| 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [无] | [C++](2001-2500/2427-Number-of-Common-Factors/cpp-2427/) | | | +| 2428 | [Maximum Sum of an Hourglass](https://leetcode.com/problems/maximum-sum-of-an-hourglass/) | [无] | [C++](2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/) | | | +| 2429 | [Minimize XOR](https://leetcode.com/problems/minimize-xor/) | [无] | [C++](2001-2500/2429-Minimize-XOR/cpp-2429/) | | | +| 2430 | [Maximum Deletions on a String](https://leetcode.com/problems/maximum-deletions-on-a-string/) | [无] | [C++](2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/) | | | +| | | | | | | ## 力扣中文站比赛 From 42a2b021b12def59c0b5420e4d367a783cb8f2c1 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Oct 2022 15:11:07 -0700 Subject: [PATCH 203/390] 2423 solved. --- .../cpp-2423/CMakeLists.txt | 6 +++ .../cpp-2423/main.cpp | 48 +++++++++++++++++++ readme.md | 1 + 3 files changed, 55 insertions(+) create mode 100644 2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/CMakeLists.txt create mode 100644 2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/main.cpp diff --git a/2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/CMakeLists.txt b/2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/CMakeLists.txt new file mode 100644 index 00000000..28513559 --- /dev/null +++ b/2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2423) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2423 main.cpp) diff --git a/2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/main.cpp b/2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/main.cpp new file mode 100644 index 00000000..37c69071 --- /dev/null +++ b/2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/remove-letter-to-equalize-frequency/ +/// Author : liuyubobobo +/// Time : 2022-10-02 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Compelxity: O(|word| + 26^2) +/// Space Complexity: O(26) +class Solution { +public: + bool equalFrequency(string word) { + + vector f(26, 0); + for(char c: word) f[c - 'a'] ++; + + for(int i = 0; i < 26; i ++) + if(f[i]){ + f[i] --; + if(ok(f)) return true; + f[i] ++; + } + return false; + } + +private: + bool ok(const vector& f){ + + int t = -1; + for(int i = 0; i < 26 && t == -1; i ++) + if(f[i]) t = f[i]; + + for(int i = 0; i < 26; i ++) + if(f[i] && f[i] != t) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d6ae271b..75f9ed39 100644 --- a/readme.md +++ b/readme.md @@ -2286,6 +2286,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2420 | [Find All Good Indices](https://leetcode.com/problems/find-all-good-indices/) | [无] | [C++](2001-2500/2420-Find-All-Good-Indices/cpp-2420/) | | | | | | | | | | | 2422 | [Merge Operations to Turn Array Into a Palindrome](https://leetcode.com/problems/merge-operations-to-turn-array-into-a-palindrome/) | [无] | [C++](2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/) | | | +| 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [无] | [C++](2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/) | | | | | | | | | | | 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [无] | [C++](2001-2500/2427-Number-of-Common-Factors/cpp-2427/) | | | | 2428 | [Maximum Sum of an Hourglass](https://leetcode.com/problems/maximum-sum-of-an-hourglass/) | [无] | [C++](2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/) | | | From 72d395f4948126e6b9a7ed33795ee57f0b48a0e5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Oct 2022 22:29:15 -0700 Subject: [PATCH 204/390] 2424 solved, --- .../cpp-2424/CMakeLists.txt | 6 +++ .../cpp-2424/main.cpp | 39 +++++++++++++++++++ readme.md | 1 + 3 files changed, 46 insertions(+) create mode 100644 2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/CMakeLists.txt create mode 100644 2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/main.cpp diff --git a/2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/CMakeLists.txt b/2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/CMakeLists.txt new file mode 100644 index 00000000..5fd22870 --- /dev/null +++ b/2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2424) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2424 main.cpp) diff --git a/2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/main.cpp b/2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/main.cpp new file mode 100644 index 00000000..5bb656d9 --- /dev/null +++ b/2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/longest-uploaded-prefix/ +/// Author : liuyubobobo +/// Time : 2022-10-02 + +#include +#include + +using namespace std; + + +/// Using TreeSet +/// Time Complexity: init: O(nlogn) +/// upload and longest: O(logn) +/// Space Complexity: O(n) +class LUPrefix { + +private: + set available; + +public: + LUPrefix(int n) { + for(int i = 1; i <= n + 1; i ++) available.insert(i); + } + + void upload(int video) { + available.erase(video); + } + + int longest() { + auto iter = available.lower_bound(1); + return *iter - 1; + } +}; + + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/readme.md b/readme.md index 75f9ed39..3075273b 100644 --- a/readme.md +++ b/readme.md @@ -2287,6 +2287,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2422 | [Merge Operations to Turn Array Into a Palindrome](https://leetcode.com/problems/merge-operations-to-turn-array-into-a-palindrome/) | [无] | [C++](2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/) | | | | 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [无] | [C++](2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/) | | | +| 2424 | [Longest Uploaded Prefix](https://leetcode.com/problems/longest-uploaded-prefix/) | [无] | [C++](2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/) | | | | | | | | | | | 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [无] | [C++](2001-2500/2427-Number-of-Common-Factors/cpp-2427/) | | | | 2428 | [Maximum Sum of an Hourglass](https://leetcode.com/problems/maximum-sum-of-an-hourglass/) | [无] | [C++](2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/) | | | From 79244a5d62754eb6dc8231d575a23db4f2870aec Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 3 Oct 2022 00:04:31 -0700 Subject: [PATCH 205/390] 2425 solved. --- .../cpp-2425/CMakeLists.txt | 6 ++++ .../cpp-2425/main.cpp | 33 +++++++++++++++++++ readme.md | 1 + 3 files changed, 40 insertions(+) create mode 100644 2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/CMakeLists.txt create mode 100644 2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/main.cpp diff --git a/2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/CMakeLists.txt b/2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/CMakeLists.txt new file mode 100644 index 00000000..58130ea2 --- /dev/null +++ b/2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2425) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2425 main.cpp) diff --git a/2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/main.cpp b/2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/main.cpp new file mode 100644 index 00000000..2b109210 --- /dev/null +++ b/2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/bitwise-xor-of-all-pairings/ +/// Author : liuyubobobo +/// Time : 2022-10-03 + +#include +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(n + m) +/// Space Complexity: O(1) +class Solution { +public: + int xorAllNums(vector& nums1, vector& nums2) { + + int n = nums1.size(), m = nums2.size(); + + int res = 0; + for(int e: nums1) + if(m & 1) res ^= e; + for(int e: nums2) + if(n & 1) res ^= e; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3075273b..0b2ed4d8 100644 --- a/readme.md +++ b/readme.md @@ -2288,6 +2288,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2422 | [Merge Operations to Turn Array Into a Palindrome](https://leetcode.com/problems/merge-operations-to-turn-array-into-a-palindrome/) | [无] | [C++](2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/) | | | | 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [无] | [C++](2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/) | | | | 2424 | [Longest Uploaded Prefix](https://leetcode.com/problems/longest-uploaded-prefix/) | [无] | [C++](2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/) | | | +| 2425 | [Bitwise XOR of All Pairings](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [无] | [C++](2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/) | | | | | | | | | | | 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [无] | [C++](2001-2500/2427-Number-of-Common-Factors/cpp-2427/) | | | | 2428 | [Maximum Sum of an Hourglass](https://leetcode.com/problems/maximum-sum-of-an-hourglass/) | [无] | [C++](2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/) | | | From f6098c359f15f1e2306a123f39191e6dc9849939 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 3 Oct 2022 00:21:41 -0700 Subject: [PATCH 206/390] 2426 solved. --- .../cpp-2426/CMakeLists.txt | 6 + .../cpp-2426/main.cpp | 327 ++++++++++++++++++ readme.md | 2 +- 3 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/CMakeLists.txt create mode 100644 2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/main.cpp diff --git a/2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/CMakeLists.txt b/2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/CMakeLists.txt new file mode 100644 index 00000000..93932f83 --- /dev/null +++ b/2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2426) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2426 main.cpp) diff --git a/2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/main.cpp b/2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/main.cpp new file mode 100644 index 00000000..739b277f --- /dev/null +++ b/2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/main.cpp @@ -0,0 +1,327 @@ +/// Source : https://leetcode.com/problems/number-of-pairs-satisfying-inequality/ +/// Author : liuyubobobo +/// Time : 2022-10-03 + +#include +#include +#include + +using namespace std; + + +/// Using AVL Tree +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class AVLTreeMultiSet{ + +private: + class Node{ + public: + Key key; + Node *left = nullptr, *right = nullptr; + int height = 1, size = 1; + + Node(const Key& key): key(key){} + }; + + Node* root = nullptr; + +public: + AVLTreeMultiSet(){} + + int size(){ + return size(root); + } + + void add(const Key& key){ + root = add(root, key); + } + + void remove(const Key& key){ + root = remove(root, key); + } + + bool contains(const Key& key){ + return get_node(root, key) != nullptr; + } + + // 0-based rank + int get_index(const Key& key){ + + Node* node = get_node(root, key); + assert(node); + + return size_less_than(key); + } + + // 0-based select + Key select(int index){ + + assert(index < size(root)); + return select(root, index); + } + + // < key 的元素有几个? + int size_less_than(const Key& key){ + return size_less_than(root, key); + } + + // <= key 的元素有几个? + int size_less_than_or_equal_to(const Key& key){ + return size_less_than_or_equal_to(root, key); + } + + // > key 的元素有几个? + int size_larger_than(const Key& key){ + return size_larger_than(root, key); + } + + // >= key 的元素有几个? + int size_larger_than_or_equal_to(const Key& key){ + return size_larger_than_or_equal_to(root, key); + } + +private: + int size(Node* node){ + return node ? node->size : 0; + } + + int height(Node* node){ + return node ? node->height : 0; + } + + int get_balance_factor(Node* node){ + return height(node->left) - height(node->right); + } + + Node* right_rotate(Node* y) { + Node* x = y->left; + Node* T3 = x->right; + + // 向右旋转过程 + x->right = y; + y->left = T3; + + // 更新 height 和 size + y->height = max(height(y->left), height(y->right)) + 1; + x->height = max(height(x->left), height(x->right)) + 1; + + y->size = size(y->left) + size(y->right) + 1; + x->size = size(x->left) + size(x->right) + 1; + + return x; + } + + Node* left_rotate(Node* y) { + Node* x = y->right; + Node* T2 = x->left; + + // 向左旋转过程 + x->left = y; + y->right = T2; + + // 更新 height 和 size + y->height = max(height(y->left), height(y->right)) + 1; + x->height = max(height(x->left), height(x->right)) + 1; + + y->size = size(y->left) + size(y->right) + 1; + x->size = size(x->left) + size(x->right) + 1; + + return x; + } + + // 向以node为根的二分搜索树中插入元素(key, value),递归算法 + // 返回插入新节点后二分搜索树的根 + Node* add(Node* node, const Key& key){ + + if(node == nullptr) + return new Node(key); + + if(key <= node->key) + node->left = add(node->left, key); + else if(key > node->key) + node->right = add(node->right, key); + + return keep_balance(node); + } + + Node* remove(Node* node, const Key& key){ + + if(node == nullptr) + return nullptr; + + Node* ret_node; + if(key < node->key){ + node->left = remove(node->left, key); + ret_node = node; + } + else if(key > node->key){ + node->right = remove(node->right, key); + ret_node = node; + } + else{ + + // 待删除节点左子树为空的情况 + if(node->left == nullptr){ + Node* right_node = node->right; + node->right = nullptr; + ret_node = right_node; + } + // 待删除节点右子树为空的情况 + else if(node->right == nullptr){ + Node* left_node = node->left; + node->left = nullptr; + ret_node = left_node; + } + // 待删除节点左右子树均不为空的情况 + else{ + // 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点 + // 用这个节点顶替待删除节点的位置 + Node* min_node = get_min_node(node->right); + assert(min_node); + node->key = min_node->key; + + node->right = remove_min(node->right); + ret_node = node; + } + } + + return keep_balance(ret_node); + } + + Node* remove_min(Node* node){ + + if(node == nullptr) + return nullptr; + + Node* ret_node; + if(node->left) { + node->left = remove_min(node->left); + ret_node = node; + } + else + ret_node = node->right; + + return keep_balance(ret_node); + } + + Node* get_min_node(Node* node){ + + if(!node) return nullptr; + + if(node->left) return get_min_node(node->left); + return node; + } + + Node* keep_balance(Node* node){ + + if(!node) return nullptr; + + // 更新 height 和 size + node->height = 1 + max(height(node->left), height(node->right)); + node->size = 1 + size(node->left) + size(node->right); + + // 计算平衡因子 + int balance_factor = get_balance_factor(node); + + // 平衡维护 + // LL + if (balance_factor > 1 && get_balance_factor(node->left) >= 0) + return right_rotate(node); + + // RR + if (balance_factor < -1 && get_balance_factor(node->right) <= 0) + return left_rotate(node); + + // LR + if (balance_factor > 1 && get_balance_factor(node->left) < 0) { + node->left = left_rotate(node->left); + return right_rotate(node); + } + + // RL + if (balance_factor < -1 && get_balance_factor(node->right) > 0) { + node->right = right_rotate(node->right); + return left_rotate(node); + } + + return node; + } + + Node* get_node(Node* node, const Key& key){ + + if(node == nullptr) + return nullptr; + + if(key == node->key) + return node; + else if(key < node->key) + return get_node(node->left, key); + else + return get_node(node->right, key); + } + + Key select(Node* node, int index){ + + if(index < size(node->left)) return select(node->left, index); + + if(index == size(node->left)) return node->key; + + return select(node->right, index - size(node->left) - 1); + } + + // < key 的元素有几个 + int size_less_than(Node* node, const Key& key){ + if(!node) return 0; + if(key <= node->key) return size_less_than(node->left, key); + return size(node->left) + 1 + size_less_than(node->right, key); + } + + // <= key 的元素有几个 + int size_less_than_or_equal_to(Node* node, const Key& key){ + if(!node) return 0; + if(key < node->key) return size_less_than_or_equal_to(node->left, key); + return size(node->left) + 1 + size_less_than_or_equal_to(node->right, key); + } + + // > key 的元素有几个 + int size_larger_than(Node* node, const Key& key){ + if(!node) return 0; + if(key >= node->key) return size_larger_than(node->right, key); + return size_larger_than(node->left, key) + 1 + size(node->right); + } + + // >= key 的元素有几个 + int size_larger_than_or_equal_to(Node* node, const Key& key){ + if(!node) return 0; + if(key > node->key) return size_larger_than_or_equal_to(node->right, key); + return size_larger_than_or_equal_to(node->left, key) + 1 + size(node->right); + } +}; + +class Solution { +public: + long long numberOfPairs(vector& nums1, vector& nums2, int diff) { + + int n = nums1.size(); + vector data(n); + for(int i = 0; i < n; i ++) data[i] = nums1[i] - nums2[i]; + + AVLTreeMultiSet tree; + for(int e: data) tree.add(e); + + long long res = 0; + for(int e: data){ + tree.remove(e); + res += tree.size_larger_than_or_equal_to(e - diff); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 0b2ed4d8..4811e020 100644 --- a/readme.md +++ b/readme.md @@ -2289,7 +2289,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [无] | [C++](2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/) | | | | 2424 | [Longest Uploaded Prefix](https://leetcode.com/problems/longest-uploaded-prefix/) | [无] | [C++](2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/) | | | | 2425 | [Bitwise XOR of All Pairings](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [无] | [C++](2001-2500/2425-Bitwise-XOR-of-All-Pairings/cpp-2425/) | | | -| | | | | | | +| 2426 | [Number of Pairs Satisfying Inequality](https://leetcode.com/problems/number-of-pairs-satisfying-inequality/) | [无]
[缺:493 问题整理] | [C++](2001-2500/2426-Number-of-Pairs-Satisfying-Inequality/cpp-2426/) | | | | 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [无] | [C++](2001-2500/2427-Number-of-Common-Factors/cpp-2427/) | | | | 2428 | [Maximum Sum of an Hourglass](https://leetcode.com/problems/maximum-sum-of-an-hourglass/) | [无] | [C++](2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/) | | | | 2429 | [Minimize XOR](https://leetcode.com/problems/minimize-xor/) | [无] | [C++](2001-2500/2429-Minimize-XOR/cpp-2429/) | | | From 4e9076f73736866815647094adb2a1523555ae61 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 4 Oct 2022 19:19:29 -0700 Subject: [PATCH 207/390] 2022 hhrc solved. --- Others/2022-hhrc/A/CMakeLists.txt | 6 ++ Others/2022-hhrc/A/main.cpp | 34 +++++++ Others/2022-hhrc/B/CMakeLists.txt | 6 ++ Others/2022-hhrc/B/main.cpp | 142 ++++++++++++++++++++++++++++++ Others/2022-hhrc/C/CMakeLists.txt | 6 ++ Others/2022-hhrc/C/main.cpp | 58 ++++++++++++ Others/2022-hhrc/D/CMakeLists.txt | 6 ++ Others/2022-hhrc/D/main.cpp | 72 +++++++++++++++ readme.md | 5 ++ 9 files changed, 335 insertions(+) create mode 100644 Others/2022-hhrc/A/CMakeLists.txt create mode 100644 Others/2022-hhrc/A/main.cpp create mode 100644 Others/2022-hhrc/B/CMakeLists.txt create mode 100644 Others/2022-hhrc/B/main.cpp create mode 100644 Others/2022-hhrc/C/CMakeLists.txt create mode 100644 Others/2022-hhrc/C/main.cpp create mode 100644 Others/2022-hhrc/D/CMakeLists.txt create mode 100644 Others/2022-hhrc/D/main.cpp diff --git a/Others/2022-hhrc/A/CMakeLists.txt b/Others/2022-hhrc/A/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/Others/2022-hhrc/A/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/Others/2022-hhrc/A/main.cpp b/Others/2022-hhrc/A/main.cpp new file mode 100644 index 00000000..f186aa2c --- /dev/null +++ b/Others/2022-hhrc/A/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.cn/contest/hhrc2022/problems/o0Ma2v/ +/// Author : liuyubobobo +/// Time : 2022-10-03 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int lastMaterial(vector& material) { + + priority_queue pq; + for(int e: material) pq.push(e); + + while(pq.size() >= 2){ + int a = pq.top(); pq.pop(); + int b = pq.top(); pq.pop(); + if(a - b) pq.push(a - b); + } + return pq.empty() ? 0 : pq.top(); + } +}; + +int main() { + + return 0; +} diff --git a/Others/2022-hhrc/B/CMakeLists.txt b/Others/2022-hhrc/B/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/Others/2022-hhrc/B/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/Others/2022-hhrc/B/main.cpp b/Others/2022-hhrc/B/main.cpp new file mode 100644 index 00000000..97cc6a6a --- /dev/null +++ b/Others/2022-hhrc/B/main.cpp @@ -0,0 +1,142 @@ +/// Source : https://leetcode.cn/contest/hhrc2022/problems/0Wx4Pc/ +/// Author : liuyubobobo +/// Time : 2022-10-03 + +#include +#include + +using namespace std; + + +/// Using Segment Tree +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class SegmentTree{ + +private: + int n; + vector data, tree; + T (*combine)(T a, T b); + +public: + SegmentTree(const vector& data, T (*combine)(T a, T b)): n(data.size()), data(data), tree(4 * n, 0){ + this->combine = combine; + buildSegTree(0, 0, n - 1); + } + + SegmentTree(int n, T (*combine)(T a, T b)): n(n), data(n, 0), tree(4 * n, 0){ + this->combine = combine; + } + + void update(int index, T value){ + if(data[index] == value) return; + data[index] = value; + update(0, 0, n - 1, index, value); + } + + T query(int index){ + assert(0 <= index && index < n); + return data[index]; + } + + T query(int l, int r){ + assert(l <= r); + assert(0 <= l && l < n); + assert(0 <= r && r < n); + return query(0, 0, n - 1, l, r); + } + +private: + void buildSegTree(int treeID, int l, int r){ + + if(l == r){ + tree[treeID] = data[l]; + return; + } + + int mid = (l + r) / 2; + buildSegTree(treeID * 2 + 1, l, mid); + buildSegTree(treeID * 2 + 2, mid + 1, r); + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + void update(int treeID, int l, int r, int index, T value){ + + if(l == r){ + assert(l == index); + tree[treeID] = value; + return; + } + + int mid = (l + r) / 2; + if(index <= mid) update(treeID * 2 + 1, l, mid, index, value); + else update(treeID * 2 + 2, mid + 1, r, index, value); + + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + T query(int treeID, int l, int r, int ql, int qr){ + + if(ql == l && qr == r) + return tree[treeID]; + + int mid = (l + r) / 2; + if(qr <= mid) return query(treeID * 2 + 1, l, mid, ql, qr); + else if(ql > mid) return query(treeID * 2 + 2, mid + 1, r, ql, qr); + + T resl = query(treeID * 2 + 1, l, mid, ql, mid); + T resr = query(treeID * 2 + 2, mid + 1, r, mid + 1, qr); + return combine(resl, resr); + } +}; + +class Solution { +public: + int longestESR(vector& sales) { + + int n = sales.size(); + vector data(n); + for(int i = 0; i < n; i ++) data[i] = sales[i] > 8 ? 1 : -1; + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + data[i]; + + int OFFSET = n + 5; + SegmentTree seg_tree(vector(2 * n + 11, -1), [](int a, int b){return max(a, b);}); + seg_tree.update(presum.back() + OFFSET, n); + int res = 0; + for(int l = n - 1; l >= 0; l --){ + int cur = presum[l]; + int max_r = seg_tree.query(cur + 1 + OFFSET, 2 * n + 10); + res = max(res, max_r - l); + + seg_tree.update(cur + OFFSET, max(seg_tree.query(cur + OFFSET), l)); + } + return res; + } +}; + + +int main() { + + vector sales1 = {10,2,1,4,3,9,6,9,9}; + cout << Solution().longestESR(sales1) << '\n'; + // 5 + + vector sales2 = {5,6,7}; + cout << Solution().longestESR(sales2) << '\n'; + // 0 + + vector sales3 = {6,9,6}; + cout << Solution().longestESR(sales3) << '\n'; + // 1 + + vector sales4 = {9,9,6}; + cout << Solution().longestESR(sales4) << '\n'; + // 3 + + return 0; +} diff --git a/Others/2022-hhrc/C/CMakeLists.txt b/Others/2022-hhrc/C/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/Others/2022-hhrc/C/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/Others/2022-hhrc/C/main.cpp b/Others/2022-hhrc/C/main.cpp new file mode 100644 index 00000000..bb0077c8 --- /dev/null +++ b/Others/2022-hhrc/C/main.cpp @@ -0,0 +1,58 @@ +/// Source : https://leetcode.cn/contest/hhrc2022/problems/VAc7h3/ +/// Author : liuyubobobo +/// Time : 2022-10-03 + +#include +#include +#include + +using namespace std; + + +/// Hash +/// Time Complexity: O(n) +/// Space Complexity: O(n^2) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector lightDistribution(TreeNode* root) { + + map> m; + dfs(root, m); + + vector res; + for(const pair>& p: m) + if(p.second.size() > 1) res.push_back(p.second[0]); + return res; + } + +private: + string dfs(TreeNode* node, map>& m){ + + if(node == nullptr) return "(null)"; + + string hash = "(" + to_string(node->val); + hash += dfs(node->left, m); + hash += dfs(node->right, m); + hash += ")"; + + m[hash].push_back(node); + return hash; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022-hhrc/D/CMakeLists.txt b/Others/2022-hhrc/D/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/Others/2022-hhrc/D/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/Others/2022-hhrc/D/main.cpp b/Others/2022-hhrc/D/main.cpp new file mode 100644 index 00000000..6a8ab801 --- /dev/null +++ b/Others/2022-hhrc/D/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode.cn/contest/hhrc2022/problems/wFtovi/ +/// Author : liuyubobobo +/// Time : 2022-10-04 + +#include +#include +#include + +using namespace std; + + +/// Tree DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +class Solution { + +private: + const int INF = INT_MAX / 2; + +public: + int minSupplyStationNumber(TreeNode* root) { + + map, int> dp; + return dfs(root, 0, 0, dp); + } + +private: + int dfs(TreeNode* node, int covered, int must_set, map, int>& dp){ + + if(!node) return must_set ? INF : 0; + + tuple state = {node, covered, must_set}; + auto iter = dp.find(state); + if(iter != dp.end()) return iter->second; + + int res = INF; + if(must_set){ + res = min(res, 1 + dfs(node->left, 1, 0, dp) + dfs(node->right, 1, 0, dp)); + } + else if(covered){ + res = min(res, 1 + dfs(node->left, 1, 0, dp) + dfs(node->right, 1, 0, dp)); + res = min(res, dfs(node->left, 0, 0, dp) + dfs(node->right, 0, 0, dp)); + } + else{ + res = min(res, 1 + dfs(node->left, 1, 0, dp) + dfs(node->right, 1, 0, dp)); + if(node->left){ + res = min(res, dfs(node->left, 1, 1, dp) + dfs(node->right, 0, 0, dp)); + res = min(res, dfs(node->left, 1, 1, dp) + dfs(node->right, 1, 1, dp)); + } + if(node->right){ + res = min(res, dfs(node->left, 0, 0, dp) + dfs(node->right, 1, 1, dp)); + res = min(res, dfs(node->left, 1, 1, dp) + dfs(node->right, 1, 1, dp)); + } + } + return dp[state] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 4811e020..4deee654 100644 --- a/readme.md +++ b/readme.md @@ -2354,6 +2354,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | +| 22 天堂硅谷-4 | [补给覆盖](https://leetcode.cn/contest/hhrc2022/problems/wFtovi/) | [无] | [C++](Others/2022-hhrc/D/) | | | +| 22 天堂硅谷-3 | [重复的彩灯树](https://leetcode.cn/contest/hhrc2022/problems/VAc7h3/) | [无] | [C++](Others/2022-hhrc/C/) | | | +| 22 天堂硅谷-2 | [销售出色区间](https://leetcode.cn/contest/hhrc2022/problems/0Wx4Pc/) | [无] | [C++](Others/2022-hhrc/B/) | | | +| 22 天堂硅谷-1 | [化学反应](https://leetcode.cn/contest/hhrc2022/problems/o0Ma2v/) | [无] | [C++](Others/2022-hhrc/A/) | | | +| | | | | | | | 22秋 银联-4 | [设计自动售货机](https://leetcode.cn/contest/cnunionpay2022/problems/NyZD2B/) | [无] | [C++](Others/2022fall-cnunionpay/D/) | | | | 22秋 银联-3 | [风能发电](https://leetcode.cn/contest/cnunionpay2022/problems/wMGN0t/) | [无] | [C++](Others/2022fall-cnunionpay/C/) | | | | 22秋 银联-2 | [勘探补给](https://leetcode.cn/contest/cnunionpay2022/problems/6olJmJ/) | [无] | [C++](Others/2022fall-cnunionpay/B/) | | | From e974436cbb58c258713a72d3bff4090f65681f3a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 6 Oct 2022 11:29:27 -0700 Subject: [PATCH 208/390] 2431 solved. --- .../cpp-2431/CMakeLists.txt | 6 +++ .../cpp-2431/main.cpp | 44 +++++++++++++++++++ readme.md | 1 + 3 files changed, 51 insertions(+) create mode 100644 2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/CMakeLists.txt create mode 100644 2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/main.cpp diff --git a/2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/CMakeLists.txt b/2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/CMakeLists.txt new file mode 100644 index 00000000..6268af43 --- /dev/null +++ b/2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2431) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2431 main.cpp) diff --git a/2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/main.cpp b/2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/main.cpp new file mode 100644 index 00000000..971e79ec --- /dev/null +++ b/2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/maximize-total-tastiness-of-purchased-fruits/ +/// Author : liuyubobobo +/// Time : 2022-10-06 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n * maxAmount * maxCoupons) +/// Space Complexity: O(n * maxAmount * maxCoupons) +class Solution { +public: + int maxTastiness(vector& price, vector& tastiness, int maxAmount, int maxCoupons) { + + int n = price.size(); + + vector>> dp(n, vector>(maxAmount + 1, vector(maxCoupons + 1, -1))); + return dfs(price, tastiness, n - 1, maxAmount, maxCoupons, dp); + } + +private: + int dfs(const vector& price, const vector& tastiness, + int index, int amount, int coupons, vector>>& dp){ + + if(index == -1) return 0; + if(dp[index][amount][coupons] != -1) return dp[index][amount][coupons]; + + int res = dfs(price, tastiness, index - 1, amount, coupons, dp); + if(price[index] <= amount) + res = max(res, tastiness[index] + dfs(price, tastiness, index - 1, amount - price[index], coupons, dp)); + if(coupons && price[index] / 2 <= amount) + res = max(res, tastiness[index] + dfs(price, tastiness, index - 1, amount - price[index] / 2, coupons - 1, dp)); + return dp[index][amount][coupons] = res; + } +}; + + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/readme.md b/readme.md index 4deee654..a03516cf 100644 --- a/readme.md +++ b/readme.md @@ -2294,6 +2294,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2428 | [Maximum Sum of an Hourglass](https://leetcode.com/problems/maximum-sum-of-an-hourglass/) | [无] | [C++](2001-2500/2428-Maximum-Sum-of-an-Hourglass/cpp-2428/) | | | | 2429 | [Minimize XOR](https://leetcode.com/problems/minimize-xor/) | [无] | [C++](2001-2500/2429-Minimize-XOR/cpp-2429/) | | | | 2430 | [Maximum Deletions on a String](https://leetcode.com/problems/maximum-deletions-on-a-string/) | [无] | [C++](2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/) | | | +| 2431 | [Maximize Total Tastiness of Purchased Fruits](https://leetcode.com/problems/maximize-total-tastiness-of-purchased-fruits/) | [无] | [C++](2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/) | | | | | | | | | | ## 力扣中文站比赛 From dada7c041ae8eef295be075fc6bcef3a80aa80a8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 7 Oct 2022 17:00:28 -0700 Subject: [PATCH 209/390] LCP 066-070 solved. --- LC/LCP066/cpp-LCP066/CMakeLists.txt | 6 ++ LC/LCP066/cpp-LCP066/main.cpp | 33 +++++++++++ LC/LCP067/cpp-LCP067/CMakeLists.txt | 6 ++ LC/LCP067/cpp-LCP067/main.cpp | 52 +++++++++++++++++ LC/LCP068/cpp-LCP068/CMakeLists.txt | 6 ++ LC/LCP068/cpp-LCP068/main.cpp | 43 ++++++++++++++ LC/LCP069/cpp-LCP069/CMakeLists.txt | 6 ++ LC/LCP069/cpp-LCP069/main.cpp | 87 +++++++++++++++++++++++++++++ LC/LCP070/cpp-LCP070/CMakeLists.txt | 6 ++ LC/LCP070/cpp-LCP070/main.cpp | 70 +++++++++++++++++++++++ readme.md | 6 ++ 11 files changed, 321 insertions(+) create mode 100644 LC/LCP066/cpp-LCP066/CMakeLists.txt create mode 100644 LC/LCP066/cpp-LCP066/main.cpp create mode 100644 LC/LCP067/cpp-LCP067/CMakeLists.txt create mode 100644 LC/LCP067/cpp-LCP067/main.cpp create mode 100644 LC/LCP068/cpp-LCP068/CMakeLists.txt create mode 100644 LC/LCP068/cpp-LCP068/main.cpp create mode 100644 LC/LCP069/cpp-LCP069/CMakeLists.txt create mode 100644 LC/LCP069/cpp-LCP069/main.cpp create mode 100644 LC/LCP070/cpp-LCP070/CMakeLists.txt create mode 100644 LC/LCP070/cpp-LCP070/main.cpp diff --git a/LC/LCP066/cpp-LCP066/CMakeLists.txt b/LC/LCP066/cpp-LCP066/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/LC/LCP066/cpp-LCP066/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/LC/LCP066/cpp-LCP066/main.cpp b/LC/LCP066/cpp-LCP066/main.cpp new file mode 100644 index 00000000..31c931f9 --- /dev/null +++ b/LC/LCP066/cpp-LCP066/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.cn/problems/600YaG/ +/// Author : liuyubobobo +/// Time : 2022-10-07 + +#include +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minNumBooths(vector& demand) { + + vector res(26, 0); + for(const string& s: demand){ + vector t(26, 0); + for(char c: s) t[c - 'a'] ++; + for(int i = 0; i < 26; i ++) + res[i] = max(res[i], t[i]); + } + return accumulate(res.begin(), res.end(), 0); + } +}; + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/LC/LCP067/cpp-LCP067/CMakeLists.txt b/LC/LCP067/cpp-LCP067/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/LC/LCP067/cpp-LCP067/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/LC/LCP067/cpp-LCP067/main.cpp b/LC/LCP067/cpp-LCP067/main.cpp new file mode 100644 index 00000000..cbb5e881 --- /dev/null +++ b/LC/LCP067/cpp-LCP067/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.cn/problems/KnLfVT/ +/// Author : liuyubobobo +/// Time : 2022-10-07 + +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(h) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + TreeNode* expandBinaryTree(TreeNode* root) { + + return dfs(root); + } + +private: + TreeNode* dfs(TreeNode* node){ + + if(node->left){ + TreeNode* lnode = new TreeNode(-1, dfs(node->left), nullptr); + node->left = lnode; + } + + if(node->right){ + TreeNode* rnode = new TreeNode(-1, nullptr, dfs(node->right)); + node->right = rnode; + } + + return node; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP068/cpp-LCP068/CMakeLists.txt b/LC/LCP068/cpp-LCP068/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/LC/LCP068/cpp-LCP068/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/LC/LCP068/cpp-LCP068/main.cpp b/LC/LCP068/cpp-LCP068/main.cpp new file mode 100644 index 00000000..7959391b --- /dev/null +++ b/LC/LCP068/cpp-LCP068/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.cn/problems/1GxJYY/ +/// Author : liuyubobobo +/// Time : 2022-10-07 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(max(flowers)) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int beautifulBouquet(vector& flowers, int cnt) { + + vector f(100001, 0); + int n = flowers.size(), l = 0, r = -1; + long long res = 0; + while(l < n){ + if(r + 1 < n && f[flowers[r + 1]] + 1 <= cnt){ + f[flowers[++ r]] ++; + res = (res + 1) % MOD; + } + else{ + f[flowers[l ++]] --; + res = (res + (r - l + 1)) % MOD; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP069/cpp-LCP069/CMakeLists.txt b/LC/LCP069/cpp-LCP069/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/LC/LCP069/cpp-LCP069/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/LC/LCP069/cpp-LCP069/main.cpp b/LC/LCP069/cpp-LCP069/main.cpp new file mode 100644 index 00000000..af4ee8fa --- /dev/null +++ b/LC/LCP069/cpp-LCP069/main.cpp @@ -0,0 +1,87 @@ +/// Source : https://leetcode.cn/problems/rMeRt2/ +/// Author : liuyubobobo +/// Time : 2022-10-07 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(1000 * n * m * 2^m) +/// Space Complexity: O(1000 * n * 2^m) +int dp[2][2][5][2][4][3][2][25][1 << 8]; + +class Solution { + +private: + const string target = "helloleetcode"; + vector c2index; + const int INF = INT_MAX / 2; + +public: + int Leetcode(vector& words) { + + c2index.resize(26, -1); + c2index['c' - 'a'] = 0; + c2index['d' - 'a'] = 1; + c2index['e' - 'a'] = 2; + c2index['h' - 'a'] = 3; + c2index['l' - 'a'] = 4; + c2index['o' - 'a'] = 5; + c2index['t' - 'a'] = 6; + + vector leftv = {1, 1, 4, 1, 3, 2, 1}; + memset(dp, -1, sizeof(dp)); + + int res = dfs(words.size(), words, leftv, 0, 0, 13); + return res == INF ? -1 : res; + } + +private: + int dfs(int n, const vector& words, + vector& l, int index, int word_state, int left){ + + if(index == n) return left == 0 ? 0 : INF; + + if(dp[l[0]][l[1]][l[2]][l[3]][l[4]][l[5]][l[6]][index][word_state] != -1) + return dp[l[0]][l[1]][l[2]][l[3]][l[4]][l[5]][l[6]][index][word_state]; + + int res = dfs(n, words, l, index + 1, 0, left); + for(int i = 0; i < words[index].size(); i ++) + if(((word_state >> i) & 1) == 0){ + int cindex = c2index[words[index][i] - 'a']; + if(cindex != -1 && l[cindex] > 0){ + int a = i - __builtin_popcount(word_state & ((1 << i) - 1)); + int b = words[index].size() - 1 - i - __builtin_popcount(word_state >> (i + 1)); +// assert(a >= 0 && b >= 0); + l[cindex] --; + res = min(res, a * b + dfs(n, words, l, index, word_state + (1 << i), left - 1)); + l[cindex] ++; + } + } + return dp[l[0]][l[1]][l[2]][l[3]][l[4]][l[5]][l[6]][index][word_state] = res; + } +}; + + +int main() { + + vector words1 = {"engineer","hold","cost","level"}; + cout << Solution().Leetcode(words1) << '\n'; + // 5 + + vector words2 = {"hello","leetcode"}; + cout << Solution().Leetcode(words2) << '\n'; + // 0 + + vector words3 = {"ecleob","rho","tw","lpl","ebolddec"}; + cout << Solution().Leetcode(words3) << '\n'; + // 6 + + return 0; +} diff --git a/LC/LCP070/cpp-LCP070/CMakeLists.txt b/LC/LCP070/cpp-LCP070/CMakeLists.txt new file mode 100644 index 00000000..399824bd --- /dev/null +++ b/LC/LCP070/cpp-LCP070/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(E) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(E main.cpp) diff --git a/LC/LCP070/cpp-LCP070/main.cpp b/LC/LCP070/cpp-LCP070/main.cpp new file mode 100644 index 00000000..984381e7 --- /dev/null +++ b/LC/LCP070/cpp-LCP070/main.cpp @@ -0,0 +1,70 @@ +/// Source : https://leetcode.cn/problems/XxZZjK/ +/// Author : liuyubobobo +/// Time : 2022-10-07 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(size^2) +/// Space Complexity: O(1) +class Solution { +public: + vector> sandyLandManagement(int size) { + + if(size == 1) + return {{1, 1}}; + + vector> res = {{1, 1}}; + if(size % 2 == 1){ + for(int i = 2; i <= size; i += 2){ + if(i % 4 == 2){ + res.push_back({i, 1}); + for(int j = 3; j <= 2 * i + 1; j += 2) + res.push_back({i + 1, j}); + } + else{ + res.push_back({i, 2}); + for(int j = 1; j <= 2 * i + 1; j += 2) + res.push_back({i + 1, j}); + } + } + if((size - 1) % 4 == 2) res.push_back({size, 1}); + } + else if(size % 4 == 2){ + for(int i = 2; i < size; i += 2){ + if(i % 4 == 2){ + for(int j = 1; j <= 2 * i - 1; j += 2) res.push_back({i, j}); + res.push_back({i + 1, 1}); + } + else{ + for(int j = 3; j <= 2 * i - 1; j += 2) res.push_back({i, j}); + res.push_back({i + 1, 2}); + } + } + for(int j = 1; j <= 2 * size - 1; j += 2) res.push_back({size, j}); + } + else{ + res.push_back({2, 3}); + for(int i = 3; i <= size; i += 2){ + if(i % 4 == 3){ + res.push_back({i, 2}); + for(int j = 1; j <= 2 * (i + 1) - 1; j += 2) res.push_back({i + 1, j}); + } + else{ + res.push_back({i, 1}); + for(int j = 3; j <= 2 * (i + 1) - 1; j += 2) res.push_back({i + 1, j}); + } + } + } + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a03516cf..7ba11979 100644 --- a/readme.md +++ b/readme.md @@ -2349,6 +2349,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | LCP056 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LC/LCP056/cpp-LCP056/) | | | | LCP057 | [打地鼠](https://leetcode-cn.com/problems/ZbAuEH/) | [无] | [C++](LC/LCP057/cpp-LCP057/) | | | | | | | | | | +| LCP066 | [最小展台数量](https://leetcode.cn/problems/600YaG/) | [无] | [C++](LC/LCP0066/cpp-LCP066/) | | | +| LCP067 | [装饰树](https://leetcode.cn/problems/KnLfVT/) | [无] | [C++](LC/LCP0067/cpp-LCP067/) | | | +| LCP068 | [美观的花束](https://leetcode.cn/problems/1GxJYY/) | [无] | [C++](LC/LCP0068/cpp-LCP068/) | | | +| LCP069 | [Hello LeetCode!](https://leetcode.cn/problems/rMeRt2/) | [无] | [C++](LC/LCP0069/cpp-LCP069/) | | | +| LCP070 | [沙地治理](https://leetcode.cn/problems/XxZZjK/) | [无] | [C++](LC/LCP0070/cpp-LCP070/) | | | +| | | | | | | ## 其他 From 72bbdd405d49ceaa3eae665cf9395d19359a499b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 7 Oct 2022 22:10:34 -0700 Subject: [PATCH 210/390] LC readme updated. --- LC/readme.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 59 +------------------------------------------------- 2 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 LC/readme.md diff --git a/LC/readme.md b/LC/readme.md new file mode 100644 index 00000000..3766ed8e --- /dev/null +++ b/LC/readme.md @@ -0,0 +1,61 @@ +## 力扣中文站比赛 + +| ID | Problem | Official
Solution | C++ | Java | Python | +| --- | --- | :---: | :---: | :---: | :---: | +| | | | | | | +| LCP070 | [沙地治理](https://leetcode.cn/problems/XxZZjK/) | [无] | [C++](LCP0070/cpp-LCP070/) | | | +| LCP069 | [Hello LeetCode!](https://leetcode.cn/problems/rMeRt2/) | [无] | [C++](LCP0069/cpp-LCP069/) | | | +| LCP068 | [美观的花束](https://leetcode.cn/problems/1GxJYY/) | [无] | [C++](LCP0068/cpp-LCP068/) | | | +| LCP067 | [装饰树](https://leetcode.cn/problems/KnLfVT/) | [无] | [C++](LCP0067/cpp-LCP067/) | | | +| LCP066 | [最小展台数量](https://leetcode.cn/problems/600YaG/) | [无] | [C++](LCP0066/cpp-LCP066/) | | | +| | | | | | | +| LCP057 | [打地鼠](https://leetcode-cn.com/problems/ZbAuEH/) | [无] | [C++](LCP057/cpp-LCP057/) | | | +| LCP056 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LCP056/cpp-LCP056/) | | | +| LCP055 | [采集果实](https://leetcode-cn.com/problems/PTXy4P/) | [无] | [C++](LCP055/cpp-LCP055/) | | | +| | | | | | | +| LCP052 | [二叉搜索树染色](https://leetcode-cn.com/problems/QO5KpG/) | [无] [缺:非线段树做法] | [C++](LCP052/cpp-LCP052/) | | | +| LCP051 | [烹饪料理](https://leetcode-cn.com/problems/UEcfPD/) | [无] | [C++](LCP051/cpp-LCP051/) | | | +| LCP050 | [宝石补给](https://leetcode-cn.com/problems/WHnhjV/) | [无] | [C++](LCP050/cpp-LCP050/) | | | +| | | | | | | +| LCP048 | [无限棋局](https://leetcode-cn.com/problems/fsa7oZ/) | [无] | [C++](LCP048/cpp-LCP048/) | | | +| LCP047 | [入场安检](https://leetcode-cn.com/problems/oPs9Bm/) | [无] | [C++](LCP047/cpp-LCP047/) | | | +| LCP046 | [志愿者调配](https://leetcode-cn.com/problems/05ZEDJ/) | [无] | [C++](LCP046/cpp-LCP046/) | | | +| LCP045 | [自行车炫技赛场](https://leetcode-cn.com/problems/kplEvH/) | [无] | [C++](LCP045/cpp-LCP045/) | | | +| LCP044 | [开幕式焰火](https://leetcode-cn.com/problems/sZ59z6/) | [无] | [C++](LCP044/cpp-LCP044/) | | | +| LCP043 | [十字路口的交通](https://leetcode-cn.com/problems/Y1VbOX/) | [无] | [C++](LCP043/cpp-LCP043/) | | | +| LCP042 | [玩具套圈](https://leetcode-cn.com/problems/vFjcfV/) | [无] | [C++](LCP042/cpp-LCP042/) | | | +| LCP041 | [黑白翻转棋](https://leetcode-cn.com/problems/fHi6rV/) | [无] | [C++](LCP041/cpp-LCP041/)| | | +| LCP040 | [心算挑战](https://leetcode-cn.com/problems/uOAnQW/) | [无] | [C++](LCP040/cpp-LCP040/) | | | +| LCP039 | [无人机方阵](https://leetcode-cn.com/problems/0jQkd0/) | [无] | [C++](LCP039/cpp-LCP039/) | | | +| | | | | | | +| LCP029 | [乐团站位](https://leetcode-cn.com/problems/SNJvJP/) | [无] | [C++](LCP029/cpp-LCP029/) | | | +| LCP028 | [采购方案](https://leetcode-cn.com/problems/4xy4Wx/) | [无] | [C++](LCP028/cpp-LCP028/) | | | +| | | | | | | +| LCP025 | [古董键盘](https://leetcode-cn.com/problems/Uh984O/) | [无] | [C++](LCP025/cpp-LCP025/) | | | +| LCP024 | [数字游戏](https://leetcode-cn.com/problems/5TxKeK/) | [无] | [C++](LCP024/cpp-LCP024/) | | | +| LCP023 | [魔术排列](https://leetcode-cn.com/problems/er94lq/) | [无] | [C++](LCP023/cpp-LCP023/) | | | +| LCP022 | [黑白方格画](https://leetcode-cn.com/problems/ccw6C7/) | [无] | [C++](LCP022/cpp-LCP022/) | | | +| LCP021 | [追逐游戏](https://leetcode-cn.com/problems/Za25hA/) | [无] | [C++](LCP021/cpp-LCP021/) | | | +| LCP020 | [快速公交](https://leetcode-cn.com/problems/meChtZ/) | [无] | [C++](LCP020/cpp-LCP020/) | | | +| LCP019 | [秋叶收藏集](https://leetcode-cn.com/problems/UlBDOe/) | [题解](https://leetcode-cn.com/problems/UlBDOe/solution/qiu-xie-shou-cang-ji-by-leetcode-solution/) | [C++](LCP019/cpp-LCP019/) | | | +| LCP018 | [早餐组合](https://leetcode-cn.com/problems/2vYnGI/) | [无] | [C++](LCP018/cpp-LCP018/) | | | +| LCP017 | [速算机器人](https://leetcode-cn.com/problems/nGK0Fy/) | [无] | [C++](LCP017/cpp-LCP017/) | | | +| LCP016 | [游乐园的游览计划](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/solution/you-le-yuan-de-you-lan-ji-hua-tu-lun-jie-xi-by-l-2/) | [C++](LCP016/cpp-LCP016/) | | | +| LCP015 | [游乐园的迷宫](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/solution/you-le-yuan-de-mi-gong-tan-xin-si-lu-by-leetcode-s/) | [C++](LCP015/cpp-LCP015/) | | | +| LCP014 | [切分数组](https://leetcode-cn.com/problems/qie-fen-shu-zu/) | [题解](https://leetcode-cn.com/problems/qie-fen-shu-zu/solution/qie-fen-shu-zu-zhi-shu-shai-dp-by-leetcode-solutio/) | [C++](LCP014/cpp-LCP014/) | | | +| LCP013 | [寻宝](https://leetcode-cn.com/problems/xun-bao/) | [题解](https://leetcode-cn.com/problems/xun-bao/solution/xun-bao-bfs-dp-by-leetcode-solution/) | [C++](LCP013/cpp-LCP013/) | | | +| LCP012 | [小张刷题计划](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/) | [题解](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/solution/xiao-zhang-shua-ti-ji-hua-er-fen-cha-zhao-by-leetc/) | [C++](LCP012/cpp-LCP012/) | | | +| LCP011 | [期望个数统计](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/) | [题解](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/solution/qi-wang-ge-shu-tong-ji-qi-wang-ji-suan-yu-zheng-mi/) | [C++](LCP011/cpp-LCP011/) | | | +| LCP010 | [二叉树任务调度](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/) | [题解](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/solution/dfs-si-lu-dai-ma-he-zheng-ming-by-leetcode-solutio/) | [C++](LCP010/cpp-LCP010/) | | | +| LCP009 | [最小跳跃次数](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/) | [题解](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/solution/zui-xiao-tiao-yue-ci-shu-by-leetcode-solution/) | [C++](LCP009/cpp-LCP009/) | | | +| LCP008 | [剧情触发时间](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/) | [题解](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/solution/ju-qing-hong-fa-shi-jian-by-leetcode-solution/) | [C++](LCP008/cpp-LCP008/) | | | +| LCP007 | [传递信息](https://leetcode-cn.com/problems/chuan-di-xin-xi/) | [题解](https://leetcode-cn.com/problems/chuan-di-xin-xi/solution/chuan-di-xin-xi-by-leetcode-solution/) | [C++](LCP007/cpp-LCP007/) | | | +| LCP006 | [拿硬币](https://leetcode-cn.com/problems/na-ying-bi/) | [题解](https://leetcode-cn.com/problems/na-ying-bi/solution/na-ying-bi-by-leetcode-solution/) | [C++](LCP006/cpp-LCP006/) | +| LCP005 | [发 LeetCoin](https://leetcode-cn.com/problems/coin-bonus/) | [无] | [C++](LCP005/cpp-LCP005/) | | | +| LCP004 | [覆盖](https://leetcode-cn.com/problems/broken-board-dominoes/) | [无] | [C++](LCP004/cpp-LCP004/) | | | +| LCP003 | [机器人大冒险](https://leetcode-cn.com/problems/programmable-robot/) | [无] | [C++](LCP003/cpp-LCP003/) | | | +| LCP002 | [分式化简](https://leetcode-cn.com/problems/deep-dark-fraction/) | [无] | [C++](LCP002/cpp-LCP002/) | | | +| LCP001 | [猜数字](https://leetcode-cn.com/problems/guess-numbers/) | [无] | [C++](LCP001/cpp-LCP001/) | | | + + + diff --git a/readme.md b/readme.md index 7ba11979..957c1db6 100644 --- a/readme.md +++ b/readme.md @@ -2297,64 +2297,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2431 | [Maximize Total Tastiness of Purchased Fruits](https://leetcode.com/problems/maximize-total-tastiness-of-purchased-fruits/) | [无] | [C++](2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/) | | | | | | | | | | -## 力扣中文站比赛 - -| ID | Problem | Official
Solution | C++ | Java | Python | -| --- | --- | :---: | :---: | :---: | :---: | -| LCP001 | [猜数字](https://leetcode-cn.com/problems/guess-numbers/) | [无] | [C++](LC/LCP001/cpp-LCP001/) | | | -| LCP002 | [分式化简](https://leetcode-cn.com/problems/deep-dark-fraction/) | [无] | [C++](LC/LCP002/cpp-LCP002/) | | | -| LCP003 | [机器人大冒险](https://leetcode-cn.com/problems/programmable-robot/) | [无] | [C++](LC/LCP003/cpp-LCP003/) | | | -| LCP004 | [覆盖](https://leetcode-cn.com/problems/broken-board-dominoes/) | [无] | [C++](LC/LCP004/cpp-LCP004/) | | | -| LCP005 | [发 LeetCoin](https://leetcode-cn.com/problems/coin-bonus/) | [无] | [C++](LC/LCP005/cpp-LCP005/) | | | -| LCP006 | [拿硬币](https://leetcode-cn.com/problems/na-ying-bi/) | [题解](https://leetcode-cn.com/problems/na-ying-bi/solution/na-ying-bi-by-leetcode-solution/) | [C++](LC/LCP006/cpp-LCP006/) | -| LCP007 | [传递信息](https://leetcode-cn.com/problems/chuan-di-xin-xi/) | [题解](https://leetcode-cn.com/problems/chuan-di-xin-xi/solution/chuan-di-xin-xi-by-leetcode-solution/) | [C++](LC/LCP007/cpp-LCP007/) | | | -| LCP008 | [剧情触发时间](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/) | [题解](https://leetcode-cn.com/problems/ju-qing-hong-fa-shi-jian/solution/ju-qing-hong-fa-shi-jian-by-leetcode-solution/) | [C++](LC/LCP008/cpp-LCP008/) | | | -| LCP009 | [最小跳跃次数](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/) | [题解](https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/solution/zui-xiao-tiao-yue-ci-shu-by-leetcode-solution/) | [C++](LC/LCP009/cpp-LCP009/) | | | -| LCP010 | [二叉树任务调度](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/) | [题解](https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du/solution/dfs-si-lu-dai-ma-he-zheng-ming-by-leetcode-solutio/) | [C++](LC/LCP010/cpp-LCP010/) | | | -| LCP011 | [期望个数统计](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/) | [题解](https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/solution/qi-wang-ge-shu-tong-ji-qi-wang-ji-suan-yu-zheng-mi/) | [C++](LC/LCP011/cpp-LCP011/) | | | -| LCP012 | [小张刷题计划](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/) | [题解](https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua/solution/xiao-zhang-shua-ti-ji-hua-er-fen-cha-zhao-by-leetc/) | [C++](LC/LCP012/cpp-LCP012/) | | | -| LCP013 | [寻宝](https://leetcode-cn.com/problems/xun-bao/) | [题解](https://leetcode-cn.com/problems/xun-bao/solution/xun-bao-bfs-dp-by-leetcode-solution/) | [C++](LC/LCP013/cpp-LCP013/) | | | -| LCP014 | [切分数组](https://leetcode-cn.com/problems/qie-fen-shu-zu/) | [题解](https://leetcode-cn.com/problems/qie-fen-shu-zu/solution/qie-fen-shu-zu-zhi-shu-shai-dp-by-leetcode-solutio/) | [C++](LC/LCP014/cpp-LCP014/) | | | -| LCP015 | [游乐园的迷宫](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-mi-gong/solution/you-le-yuan-de-mi-gong-tan-xin-si-lu-by-leetcode-s/) | [C++](LC/LCP015/cpp-LCP015/) | | | -| LCP016 | [游乐园的游览计划](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/) | [题解](https://leetcode-cn.com/problems/you-le-yuan-de-you-lan-ji-hua/solution/you-le-yuan-de-you-lan-ji-hua-tu-lun-jie-xi-by-l-2/) | [C++](LC/LCP016/cpp-LCP016/) | | | -| LCP017 | [速算机器人](https://leetcode-cn.com/problems/nGK0Fy/) | [无] | [C++](LC/LCP017/cpp-LCP017/) | | | -| LCP018 | [早餐组合](https://leetcode-cn.com/problems/2vYnGI/) | [无] | [C++](LC/LCP018/cpp-LCP018/) | | | -| LCP019 | [秋叶收藏集](https://leetcode-cn.com/problems/UlBDOe/) | [题解](https://leetcode-cn.com/problems/UlBDOe/solution/qiu-xie-shou-cang-ji-by-leetcode-solution/) | [C++](LC/LCP019/cpp-LCP019/) | | | -| LCP020 | [快速公交](https://leetcode-cn.com/problems/meChtZ/) | [无] | [C++](LC/LCP020/cpp-LCP020/) | | | -| LCP021 | [追逐游戏](https://leetcode-cn.com/problems/Za25hA/) | [无] | [C++](LC/LCP021/cpp-LCP021/) | | | -| LCP022 | [黑白方格画](https://leetcode-cn.com/problems/ccw6C7/) | [无] | [C++](LC/LCP022/cpp-LCP022/) | | | -| LCP023 | [魔术排列](https://leetcode-cn.com/problems/er94lq/) | [无] | [C++](LC/LCP023/cpp-LCP023/) | | | -| LCP024 | [数字游戏](https://leetcode-cn.com/problems/5TxKeK/) | [无] | [C++](LC/LCP024/cpp-LCP024/) | | | -| LCP025 | [古董键盘](https://leetcode-cn.com/problems/Uh984O/) | [无] | [C++](LC/LCP025/cpp-LCP025/) | | | -| | | | | | | -| LCP028 | [采购方案](https://leetcode-cn.com/problems/4xy4Wx/) | [无] | [C++](LC/LCP028/cpp-LCP028/) | | | -| LCP029 | [乐团站位](https://leetcode-cn.com/problems/SNJvJP/) | [无] | [C++](LC/LCP029/cpp-LCP029/) | | | -| | | | | | | -| LCP039 | [无人机方阵](https://leetcode-cn.com/problems/0jQkd0/) | [无] | [C++](LC/LCP039/cpp-LCP039/) | | | -| LCP040 | [心算挑战](https://leetcode-cn.com/problems/uOAnQW/) | [无] | [C++](LC/LCP040/cpp-LCP040/) | | | -| LCP041 | [黑白翻转棋](https://leetcode-cn.com/problems/fHi6rV/) | [无] | [C++](LC/LCP041/cpp-LCP041/)| | | -| LCP042 | [玩具套圈](https://leetcode-cn.com/problems/vFjcfV/) | [无] | [C++](LC/LCP042/cpp-LCP042/) | | | -| LCP043 | [十字路口的交通](https://leetcode-cn.com/problems/Y1VbOX/) | [无] | [C++](LC/LCP043/cpp-LCP043/) | | | -| LCP044 | [开幕式焰火](https://leetcode-cn.com/problems/sZ59z6/) | [无] | [C++](LC/LCP044/cpp-LCP044/) | | | -| LCP045 | [自行车炫技赛场](https://leetcode-cn.com/problems/kplEvH/) | [无] | [C++](LC/LCP045/cpp-LCP045/) | | | -| LCP046 | [志愿者调配](https://leetcode-cn.com/problems/05ZEDJ/) | [无] | [C++](LC/LCP046/cpp-LCP046/) | | | -| LCP047 | [入场安检](https://leetcode-cn.com/problems/oPs9Bm/) | [无] | [C++](LC/LCP047/cpp-LCP047/) | | | -| LCP048 | [无限棋局](https://leetcode-cn.com/problems/fsa7oZ/) | [无] | [C++](LC/LCP048/cpp-LCP048/) | | | -| | | | | | | -| LCP050 | [宝石补给](https://leetcode-cn.com/problems/WHnhjV/) | [无] | [C++](LC/LCP050/cpp-LCP050/) | | | -| LCP051 | [烹饪料理](https://leetcode-cn.com/problems/UEcfPD/) | [无] | [C++](LC/LCP051/cpp-LCP051/) | | | -| LCP052 | [二叉搜索树染色](https://leetcode-cn.com/problems/QO5KpG/) | [无] [缺:非线段树做法] | [C++](LC/LCP052/cpp-LCP052/) | | | -| | | | | | | -| LCP055 | [采集果实](https://leetcode-cn.com/problems/PTXy4P/) | [无] | [C++](LC/LCP055/cpp-LCP055/) | | | -| LCP056 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LC/LCP056/cpp-LCP056/) | | | -| LCP057 | [打地鼠](https://leetcode-cn.com/problems/ZbAuEH/) | [无] | [C++](LC/LCP057/cpp-LCP057/) | | | -| | | | | | | -| LCP066 | [最小展台数量](https://leetcode.cn/problems/600YaG/) | [无] | [C++](LC/LCP0066/cpp-LCP066/) | | | -| LCP067 | [装饰树](https://leetcode.cn/problems/KnLfVT/) | [无] | [C++](LC/LCP0067/cpp-LCP067/) | | | -| LCP068 | [美观的花束](https://leetcode.cn/problems/1GxJYY/) | [无] | [C++](LC/LCP0068/cpp-LCP068/) | | | -| LCP069 | [Hello LeetCode!](https://leetcode.cn/problems/rMeRt2/) | [无] | [C++](LC/LCP0069/cpp-LCP069/) | | | -| LCP070 | [沙地治理](https://leetcode.cn/problems/XxZZjK/) | [无] | [C++](LC/LCP0070/cpp-LCP070/) | | | -| | | | | | | +### 力扣中文站比赛 [传送门](LC/) ## 其他 From efe19fcd8840d8d4c600f36438f1d481411da1cf Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 7 Oct 2022 22:18:25 -0700 Subject: [PATCH 211/390] Others readme updated. --- {LC => Others}/LCS01/cpp-LCS01/CMakeLists.txt | 0 {LC => Others}/LCS01/cpp-LCS01/main.cpp | 0 {LC => Others}/LCS02/cpp-LCS02/CMakeLists.txt | 0 {LC => Others}/LCS02/cpp-LCS02/main.cpp | 0 {LC => Others}/LCS03/cpp-LCS03/CMakeLists.txt | 0 {LC => Others}/LCS03/cpp-LCS03/main.cpp | 0 Others/readme.md | 51 ++++++++++++++++++ readme.md | 52 +------------------ 8 files changed, 52 insertions(+), 51 deletions(-) rename {LC => Others}/LCS01/cpp-LCS01/CMakeLists.txt (100%) rename {LC => Others}/LCS01/cpp-LCS01/main.cpp (100%) rename {LC => Others}/LCS02/cpp-LCS02/CMakeLists.txt (100%) rename {LC => Others}/LCS02/cpp-LCS02/main.cpp (100%) rename {LC => Others}/LCS03/cpp-LCS03/CMakeLists.txt (100%) rename {LC => Others}/LCS03/cpp-LCS03/main.cpp (100%) create mode 100644 Others/readme.md diff --git a/LC/LCS01/cpp-LCS01/CMakeLists.txt b/Others/LCS01/cpp-LCS01/CMakeLists.txt similarity index 100% rename from LC/LCS01/cpp-LCS01/CMakeLists.txt rename to Others/LCS01/cpp-LCS01/CMakeLists.txt diff --git a/LC/LCS01/cpp-LCS01/main.cpp b/Others/LCS01/cpp-LCS01/main.cpp similarity index 100% rename from LC/LCS01/cpp-LCS01/main.cpp rename to Others/LCS01/cpp-LCS01/main.cpp diff --git a/LC/LCS02/cpp-LCS02/CMakeLists.txt b/Others/LCS02/cpp-LCS02/CMakeLists.txt similarity index 100% rename from LC/LCS02/cpp-LCS02/CMakeLists.txt rename to Others/LCS02/cpp-LCS02/CMakeLists.txt diff --git a/LC/LCS02/cpp-LCS02/main.cpp b/Others/LCS02/cpp-LCS02/main.cpp similarity index 100% rename from LC/LCS02/cpp-LCS02/main.cpp rename to Others/LCS02/cpp-LCS02/main.cpp diff --git a/LC/LCS03/cpp-LCS03/CMakeLists.txt b/Others/LCS03/cpp-LCS03/CMakeLists.txt similarity index 100% rename from LC/LCS03/cpp-LCS03/CMakeLists.txt rename to Others/LCS03/cpp-LCS03/CMakeLists.txt diff --git a/LC/LCS03/cpp-LCS03/main.cpp b/Others/LCS03/cpp-LCS03/main.cpp similarity index 100% rename from LC/LCS03/cpp-LCS03/main.cpp rename to Others/LCS03/cpp-LCS03/main.cpp diff --git a/Others/readme.md b/Others/readme.md new file mode 100644 index 00000000..bdc21322 --- /dev/null +++ b/Others/readme.md @@ -0,0 +1,51 @@ +## 力扣其他比赛 + +| ID | Problem | Official
Solution | C++ | Java | Python | +| --- | --- | :---: | :---: | :---: | :---: | +| | | | | | | +| 22 天堂硅谷-4 | [补给覆盖](https://leetcode.cn/contest/hhrc2022/problems/wFtovi/) | [无] | [C++](2022-hhrc/D/) | | | +| 22 天堂硅谷-3 | [重复的彩灯树](https://leetcode.cn/contest/hhrc2022/problems/VAc7h3/) | [无] | [C++](2022-hhrc/C/) | | | +| 22 天堂硅谷-2 | [销售出色区间](https://leetcode.cn/contest/hhrc2022/problems/0Wx4Pc/) | [无] | [C++](2022-hhrc/B/) | | | +| 22 天堂硅谷-1 | [化学反应](https://leetcode.cn/contest/hhrc2022/problems/o0Ma2v/) | [无] | [C++](2022-hhrc/A/) | | | +| | | | | | | +| 22秋 银联-4 | [设计自动售货机](https://leetcode.cn/contest/cnunionpay2022/problems/NyZD2B/) | [无] | [C++](2022fall-cnunionpay/D/) | | | +| 22秋 银联-3 | [风能发电](https://leetcode.cn/contest/cnunionpay2022/problems/wMGN0t/) | [无] | [C++](2022fall-cnunionpay/C/) | | | +| 22秋 银联-2 | [勘探补给](https://leetcode.cn/contest/cnunionpay2022/problems/6olJmJ/) | [无] | [C++](2022fall-cnunionpay/B/) | | | +| 22秋 银联-1 | [重构链表](https://leetcode.cn/contest/cnunionpay2022/problems/VLNEbD/) | [无] | [C++](2022fall-cnunionpay/A/) | | | +| | | | | | | +| 22 AutoX-4 | [蚂蚁爬行](https://leetcode.cn/contest/autox2023/problems/TcdlJS/) | [无] | [C++](2022-autox2023/D/) | | | +| 22 AutoX-3 | [出行的最少购票费用](https://leetcode.cn/contest/autox2023/problems/BjAFy9/) | [无] | [C++](2022-autox2023/C/) | | | +| 22 AutoX-2 | [蚂蚁王国的蜂蜜](https://leetcode.cn/contest/autox2023/problems/8p6t8R/) | [无] | [C++](2022-autox2023/B/) | | | +| 22 AutoX-1 | [网页瀑布流](https://leetcode.cn/contest/autox2023/problems/l9HbCJ/) | [无] | [C++](2022-autox2023/A/) | | | +| | | | | | | +| 22 九坤-04 | [筹码游戏](https://leetcode.cn/contest/ubiquant2022/problems/I3Gm2h/) | [无] | [C++](2022-xdxykd/D/) | | | +| 22 九坤-03 | [数字默契考验](https://leetcode.cn/contest/ubiquant2022/problems/uGuf0v/) | [无] | [C++](2022-xdxykd/C/) | | | +| 22 九坤-02 | [池塘计数](https://leetcode.cn/contest/ubiquant2022/problems/3PHTGp/) | [无] | [C++](2022-xdxykd/B/) | | | +| 22 九坤-01 | [可以读通讯稿的组数](https://leetcode.cn/contest/ubiquant2022/problems/xdxykd/) | [无] | [C++](2022-xdxykd/A/) | | | +| | | | | | | +| 22 zj-future01 | [信号接收](https://leetcode.cn/contest/zj-future2022/problems/WYKGLO/) | [无] | [C++](2022-zj-future/A/) | | | +| 22 zj-future02 | [黑白棋游戏](https://leetcode.cn/contest/zj-future2022/problems/GVbKaI/) | [无] | [C++](2022-zj-future/B/) | | | +| 22 zj-future03 | [快递中转站选址](https://leetcode.cn/contest/zj-future2022/problems/kflZMc/) | [无] | [C++](2022-zj-future/C/) | | | +| 22 zj-future04 | [门店商品调配](https://leetcode.cn/contest/zj-future2022/problems/NBCXIp/) | [无] | [C++](2022-zj-future/D/) | | | +| | | | | | | +| 22 顺丰01 | [顺丰鄂州枢纽运转中心环线检测](https://leetcode.cn/contest/sf-tech/problems/EUpcmh/) | [无] | [C++](2022-sf-tech/A/) | | | +| 22 顺丰02 | [小哥派件装载问题](https://leetcode.cn/contest/sf-tech/problems/cINqyA/) | [无] | [C++](2022-sf-tech/B/) | | | +| 22 顺丰03 | [收件节节高](https://leetcode.cn/contest/sf-tech/problems/8oimK4/) | [无] | [C++](2022-sf-tech/C/) | | | +| 22 顺丰04 | [顺丰中转场车辆入场识别-电子围栏](https://leetcode.cn/contest/sf-tech/problems/uWWzsv/) | [无] | [C++](2022-sf-tech/D/) | | | +| 22 顺丰05 | [慧眼神瞳](https://leetcode.cn/contest/sf-tech/problems/BN8jAm/) | [无] | [C++](2022-sf-tech/E/) | | | +| | | | | | | +| 22春 招商银行-01 | [文本编辑程序设计](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/fWcPGC/) | [无] | [C++](2022spring-cmbchina/A/) | | | +| 22春 招商银行-02 | [公园规划](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/ReWLAw/) | [无] | [C++](2022spring-cmbchina/B/) | | | +| 22春 招商银行-03 | [点燃木棒](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/Dk2Ytp/) | [无] | [C++](2022spring-cmbchina/C/) | | | +| 22春 招商银行-04 | [商店促销活动](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/OK3hsO/) | [无] | [C++](2022spring-cmbchina/D/) | | | +| | | | | | | +| 22春 银联-01 | [回文链表](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/D7rekZ/) | [无] | [C++](2022spring-cnunionpay/A/) | | | +| 22春 银联-02 | [优惠活动系统](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/kDPV0f/) | [无] | [C++](2022spring-cnunionpay/B/) | | | +| 22春 银联-03 | [理财产品](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/I4mOGz/) | [无] | [C++](2022spring-cnunionpay/C/) | | | +| 22春 银联-04 | [合作开发](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/lCh58I/) | [无] | [C++](2022spring-cnunionpay/D/) | | | +| | | | | | | +| LCS01 | [下载插件](https://leetcode-cn.com/problems/Ju9Xwi/) | [无] | [C++](LCS01/cpp-LCS01/) | | | +| LCS02 | [完成一半题目](https://leetcode-cn.com/problems/WqXACV/) | [无] | [C++](LCS02/cpp-LCS02/) | | | +| LCS03 | [主题空间](https://leetcode-cn.com/problems/YesdPw/) | [无] | [C++](LCS03/cpp-LCS03/) | | | +| | | | | | | + diff --git a/readme.md b/readme.md index 957c1db6..7e3e14e6 100644 --- a/readme.md +++ b/readme.md @@ -2299,54 +2299,4 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) ### 力扣中文站比赛 [传送门](LC/) -## 其他 - -| ID | Problem | Official
Solution | C++ | Java | Python | -| --- | --- | :---: | :---: | :---: | :---: | -| | | | | | | -| 22 天堂硅谷-4 | [补给覆盖](https://leetcode.cn/contest/hhrc2022/problems/wFtovi/) | [无] | [C++](Others/2022-hhrc/D/) | | | -| 22 天堂硅谷-3 | [重复的彩灯树](https://leetcode.cn/contest/hhrc2022/problems/VAc7h3/) | [无] | [C++](Others/2022-hhrc/C/) | | | -| 22 天堂硅谷-2 | [销售出色区间](https://leetcode.cn/contest/hhrc2022/problems/0Wx4Pc/) | [无] | [C++](Others/2022-hhrc/B/) | | | -| 22 天堂硅谷-1 | [化学反应](https://leetcode.cn/contest/hhrc2022/problems/o0Ma2v/) | [无] | [C++](Others/2022-hhrc/A/) | | | -| | | | | | | -| 22秋 银联-4 | [设计自动售货机](https://leetcode.cn/contest/cnunionpay2022/problems/NyZD2B/) | [无] | [C++](Others/2022fall-cnunionpay/D/) | | | -| 22秋 银联-3 | [风能发电](https://leetcode.cn/contest/cnunionpay2022/problems/wMGN0t/) | [无] | [C++](Others/2022fall-cnunionpay/C/) | | | -| 22秋 银联-2 | [勘探补给](https://leetcode.cn/contest/cnunionpay2022/problems/6olJmJ/) | [无] | [C++](Others/2022fall-cnunionpay/B/) | | | -| 22秋 银联-1 | [重构链表](https://leetcode.cn/contest/cnunionpay2022/problems/VLNEbD/) | [无] | [C++](Others/2022fall-cnunionpay/A/) | | | -| | | | | | | -| 22 AutoX-4 | [蚂蚁爬行](https://leetcode.cn/contest/autox2023/problems/TcdlJS/) | [无] | [C++](Others/2022-autox2023/D/) | | | -| 22 AutoX-3 | [出行的最少购票费用](https://leetcode.cn/contest/autox2023/problems/BjAFy9/) | [无] | [C++](Others/2022-autox2023/C/) | | | -| 22 AutoX-2 | [蚂蚁王国的蜂蜜](https://leetcode.cn/contest/autox2023/problems/8p6t8R/) | [无] | [C++](Others/2022-autox2023/B/) | | | -| 22 AutoX-1 | [网页瀑布流](https://leetcode.cn/contest/autox2023/problems/l9HbCJ/) | [无] | [C++](Others/2022-autox2023/A/) | | | -| | | | | | | -| 22 九坤-04 | [筹码游戏](https://leetcode.cn/contest/ubiquant2022/problems/I3Gm2h/) | [无] | | | | -| 22 九坤-03 | [数字默契考验](https://leetcode.cn/contest/ubiquant2022/problems/uGuf0v/) | [无] | [C++](Others/2022-xdxykd/C/) | | | -| 22 九坤-02 | [池塘计数](https://leetcode.cn/contest/ubiquant2022/problems/3PHTGp/) | [无] | [C++](Others/2022-xdxykd/B/) | | | -| 22 九坤-01 | [可以读通讯稿的组数](https://leetcode.cn/contest/ubiquant2022/problems/xdxykd/) | [无] | [C++](Others/2022-xdxykd/A/) | | | -| | | | | | | -| 22 zj-future01 | [信号接收](https://leetcode.cn/contest/zj-future2022/problems/WYKGLO/) | [无] | [C++](Others/2022-zj-future/A/) | | | -| 22 zj-future02 | [黑白棋游戏](https://leetcode.cn/contest/zj-future2022/problems/GVbKaI/) | [无] | [C++](Others/2022-zj-future/B/) | | | -| 22 zj-future03 | [快递中转站选址](https://leetcode.cn/contest/zj-future2022/problems/kflZMc/) | [无] | [C++](Others/2022-zj-future/C/) | | | -| 22 zj-future04 | [门店商品调配](https://leetcode.cn/contest/zj-future2022/problems/NBCXIp/) | [无] | [C++](Others/2022-zj-future/D/) | | | -| | | | | | | -| 22 顺丰01 | [顺丰鄂州枢纽运转中心环线检测](https://leetcode.cn/contest/sf-tech/problems/EUpcmh/) | [无] | [C++](Others/2022-sf-tech/A/) | | | -| 22 顺丰02 | [小哥派件装载问题](https://leetcode.cn/contest/sf-tech/problems/cINqyA/) | [无] | [C++](Others/2022-sf-tech/B/) | | | -| 22 顺丰03 | [收件节节高](https://leetcode.cn/contest/sf-tech/problems/8oimK4/) | [无] | [C++](Others/2022-sf-tech/C/) | | | -| 22 顺丰04 | [顺丰中转场车辆入场识别-电子围栏](https://leetcode.cn/contest/sf-tech/problems/uWWzsv/) | [无] | [C++](Others/2022-sf-tech/D/) | | | -| 22 顺丰05 | [慧眼神瞳](https://leetcode.cn/contest/sf-tech/problems/BN8jAm/) | [无] | [C++](Others/2022-sf-tech/E/) | | | -| | | | | | | -| 22春 招商银行-01 | [文本编辑程序设计](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/fWcPGC/) | [无] | [C++](Others/2022spring-cmbchina/A/) | | | -| 22春 招商银行-02 | [公园规划](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/ReWLAw/) | [无] | [C++](Others/2022spring-cmbchina/B/) | | | -| 22春 招商银行-03 | [点燃木棒](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/Dk2Ytp/) | [无] | [C++](Others/2022spring-cmbchina/C/) | | | -| 22春 招商银行-04 | [商店促销活动](https://leetcode-cn.com/contest/cmbchina-2022spring/problems/OK3hsO/) | [无] | [C++](Others/2022spring-cmbchina/D/) | | | -| | | | | | | -| 22春 银联-01 | [回文链表](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/D7rekZ/) | [无] | [C++](Others/2022spring-cnunionpay/A/) | | | -| 22春 银联-02 | [优惠活动系统](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/kDPV0f/) | [无] | [C++](Others/2022spring-cnunionpay/B/) | | | -| 22春 银联-03 | [理财产品](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/I4mOGz/) | [无] | [C++](Others/2022spring-cnunionpay/C/) | | | -| 22春 银联-04 | [合作开发](https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/lCh58I/) | [无] | [C++](Others/2022spring-cnunionpay/D/) | | | -| | | | | | | -| LCS01 | [下载插件](https://leetcode-cn.com/problems/Ju9Xwi/) | [无] | [C++](LC/LCS01/cpp-LCS01/) | | | -| LCS02 | [完成一半题目](https://leetcode-cn.com/problems/WqXACV/) | [无] | [C++](LC/LCS02/cpp-LCS02/) | | | -| LCS03 | [主题空间](https://leetcode-cn.com/problems/YesdPw/) | [无] | [C++](LC/LCS03/cpp-LCS03/) | | | -| | | | | | | - +### 力扣中文站其他比赛 [传送门](Others/) From 82829ef4eb6c6489d8369e63044527c786d586a2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 7 Oct 2022 22:28:45 -0700 Subject: [PATCH 212/390] 0716 codes updated. --- .../0716-Max-Stack/cpp-0716/CMakeLists.txt | 2 +- 0501-1000/0716-Max-Stack/cpp-0716/main.cpp | 1 + 0501-1000/0716-Max-Stack/cpp-0716/main2.cpp | 107 -------------- 0501-1000/0716-Max-Stack/cpp-0716/main3.cpp | 137 ------------------ 4 files changed, 2 insertions(+), 245 deletions(-) delete mode 100644 0501-1000/0716-Max-Stack/cpp-0716/main2.cpp delete mode 100644 0501-1000/0716-Max-Stack/cpp-0716/main3.cpp diff --git a/0501-1000/0716-Max-Stack/cpp-0716/CMakeLists.txt b/0501-1000/0716-Max-Stack/cpp-0716/CMakeLists.txt index 60bc5041..ecac7975 100644 --- a/0501-1000/0716-Max-Stack/cpp-0716/CMakeLists.txt +++ b/0501-1000/0716-Max-Stack/cpp-0716/CMakeLists.txt @@ -3,5 +3,5 @@ project(cpp_0716) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main3.cpp) +set(SOURCE_FILES main.cpp) add_executable(cpp_0716 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0501-1000/0716-Max-Stack/cpp-0716/main.cpp b/0501-1000/0716-Max-Stack/cpp-0716/main.cpp index 4f9d95bd..fe295a10 100644 --- a/0501-1000/0716-Max-Stack/cpp-0716/main.cpp +++ b/0501-1000/0716-Max-Stack/cpp-0716/main.cpp @@ -8,6 +8,7 @@ using namespace std; + /// Using two sets /// Time Complexity: push: O(logn) /// pop: O(logn) diff --git a/0501-1000/0716-Max-Stack/cpp-0716/main2.cpp b/0501-1000/0716-Max-Stack/cpp-0716/main2.cpp deleted file mode 100644 index cb931f9e..00000000 --- a/0501-1000/0716-Max-Stack/cpp-0716/main2.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/// Source : https://leetcode.com/problems/max-stack/description/ -/// Author : liuyubobobo -/// Time : 2017-11-27 - -#include -#include -#include - -using namespace std; - -/// Using two stacks -/// We have one regular stack for push, pop and top operations -/// We have another stack for peekMax and popMax operations, which named maxStack -/// maxStack will store the current max value in the stack -/// for popMax, we will find the max value in the stack in O(n) time -/// -/// Time Complexity: push: O(1) -/// pop: O(1) -/// top: O(1) -/// peekMax: O(1) -/// popMax: O(n) -/// Space Complexity: O(n) -class MaxStack { - -private: - stack normalStack; - stack maxStack; - -public: - /** initialize your data structure here. */ - MaxStack() { - while(!normalStack.empty()) - normalStack.pop(); - - while(!maxStack.empty()) - maxStack.pop(); - } - - void push(int x) { - normalStack.push(x); - if(maxStack.empty()) - maxStack.push(x); - else - maxStack.push(max(maxStack.top(), x)); - } - - int pop() { - - assert(normalStack.size() > 0); - - int v = normalStack.top(); - normalStack.pop(); - maxStack.pop(); - - return v; - } - - int top() { - assert(normalStack.size() > 0); - return normalStack.top(); - } - - int peekMax() { - assert(normalStack.size() > 0); - return maxStack.top(); - } - - int popMax() { - - assert(normalStack.size() > 0); - - int maxValue = peekMax(); - stack tstack; - - while(true){ - int value = pop(); - - if(value == maxValue) - break; - - tstack.push(value); - } - - while(!tstack.empty()){ - push(tstack.top()); - tstack.pop(); - } - - return maxValue; - } -}; - -int main() { - - MaxStack stack; - stack.push(5); - stack.push(1); - stack.push(5); - cout << stack.top() << endl; // -> 5 - cout << stack.popMax() << endl; // -> 5 - cout << stack.top() << endl; // -> 1 - cout << stack.peekMax() << endl; // -> 5 - cout << stack.pop() << endl; // -> 1 - cout << stack.top() << endl; // -> 5 - - return 0; -} \ No newline at end of file diff --git a/0501-1000/0716-Max-Stack/cpp-0716/main3.cpp b/0501-1000/0716-Max-Stack/cpp-0716/main3.cpp deleted file mode 100644 index 8b62d5d8..00000000 --- a/0501-1000/0716-Max-Stack/cpp-0716/main3.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/// Source : https://leetcode.com/problems/max-stack/description/ -/// Author : liuyubobobo -/// Time : 2017-11-27 - -#include -#include -#include -#include -#include - -using namespace std; - -/// Using a list to simulate th stack -/// Using a TreeMap to record every node in the list -/// So we can get the max value in the list quickly and erase it easily -/// -/// Time Complexity: push: O(1) -/// pop: O(1) -/// top: O(1) -/// peekMax: O(1) -/// popMax: O(logn) -/// Space Complexity: O(n) -class MaxStack { - -private: - list stack; - map::iterator>> record; - -public: - /** initialize your data structure here. */ - MaxStack() { - stack.clear(); - record.clear(); - } - - void push(int x) { - stack.push_back(x); - - list::iterator iter = stack.begin(); - advance(iter, stack.size() - 1); - - record[x].push_back(iter); - } - - int pop() { - - int ret = stack.back(); - stack.pop_back(); - - record[ret].pop_back(); - if(record[ret].size() == 0) - record.erase(ret); - - return ret; - } - - int top() { - assert(stack.size() > 0); - return stack.back(); - } - - int peekMax() { - assert(stack.size() > 0); - return record.rbegin()->first; - } - - int popMax() { - assert(stack.size() > 0); - int ret = record.rbegin()->first; - - stack.erase((record.rbegin()->second).back()); - (record.rbegin()->second).pop_back(); - if((record.rbegin()->second).size() == 0) - record.erase(ret); - return ret; - } -}; - - -int main() { - - MaxStack stack1; - stack1.push(5); - stack1.push(1); - stack1.push(5); - cout << stack1.top() << endl; // -> 5 - cout << stack1.popMax() << endl; // -> 5 - cout << stack1.top() << endl; // -> 1 - cout << stack1.peekMax() << endl; // -> 5 - cout << stack1.pop() << endl; // -> 1 - cout << stack1.top() << endl; // -> 5 - cout << endl; - - // --- - - MaxStack stack2; - stack2.push(-83); - stack2.push(-1); - stack2.push(98); - stack2.push(38); - stack2.push(-99); - cout << stack2.top() << endl; // -> -99 - cout << stack2.popMax() << endl; // -> 98 - cout << stack2.popMax() << endl; // -> 38 - stack2.push(-92); - stack2.push(-17); - stack2.push(-1); - stack2.push(-74); - cout << stack2.popMax() << endl; // -> -1 - cout << stack2.pop() << endl; // -> -74 - cout << stack2.popMax() << endl; // -> -1 - stack2.push(-80); - stack2.push(-13); - cout << stack2.top() << endl; // -13 - stack2.push(-25); - cout << endl; - - // --- - - MaxStack stack3; - stack3.push(74); - cout << stack3.popMax() << endl; // -> 74 - stack3.push(89); - stack3.push(67); - cout << stack3.popMax() << endl; // -> 89 - cout << stack3.pop() << endl; // -> 67 - stack3.push(61); - stack3.push(-77); - cout << stack3.peekMax() << endl; // 61 - cout << stack3.popMax() << endl; // 61 - stack3.push(81); - cout << stack3.pop() << endl; // 81 - stack3.push(-71); - stack3.push(32); - - return 0; -} \ No newline at end of file From 129acc1373e4f0dec19780716874b64ce2a8c96b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 9 Oct 2022 15:23:35 -0700 Subject: [PATCH 213/390] 2432-2435 solved. --- .../cpp-2432/CMakeLists.txt | 6 ++ .../cpp-2432/main.cpp | 40 +++++++++++ .../cpp-2433/CMakeLists.txt | 6 ++ .../cpp-2433/main.cpp | 29 ++++++++ .../cpp-2434/CMakeLists.txt | 6 ++ .../cpp-2434/main.cpp | 69 +++++++++++++++++++ .../cpp-2435/CMakeLists.txt | 6 ++ .../cpp-2435/main.cpp | 54 +++++++++++++++ .../cpp-2435/main2.cpp | 52 ++++++++++++++ readme.md | 4 ++ 10 files changed, 272 insertions(+) create mode 100644 2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/CMakeLists.txt create mode 100644 2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/main.cpp create mode 100644 2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/CMakeLists.txt create mode 100644 2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/main.cpp create mode 100644 2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/CMakeLists.txt create mode 100644 2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/main.cpp create mode 100644 2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/CMakeLists.txt create mode 100644 2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main.cpp create mode 100644 2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main2.cpp diff --git a/2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/CMakeLists.txt b/2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/main.cpp b/2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/main.cpp new file mode 100644 index 00000000..f48392c8 --- /dev/null +++ b/2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/ +/// Author : liuyubobobo +/// Time : 2022-10-08 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int hardestWorker(int n, vector>& logs) { + + int max_t = -1, res = INT_MAX; + int cur = 0; + for(const vector& log: logs){ + int id = log[0], t = log[1]; + if(t - cur > max_t) + res = id, max_t = t - cur; + else if(t - cur == max_t) + res = min(res, id); + cur = t; + } + return res; + } +}; + + +int main() { + + vector> logs1 = {{0, 3}, {2, 5}, {0, 9}, {1, 15}}; + cout << Solution().hardestWorker(10, logs1) << '\n'; + // 1 + + return 0; +} diff --git a/2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/CMakeLists.txt b/2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/main.cpp b/2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/main.cpp new file mode 100644 index 00000000..b57ebb8f --- /dev/null +++ b/2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/find-the-original-array-of-prefix-xor/ +/// Author : liuyubobobo +/// Time : 2022-10-08 + +#include +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector findArray(vector& pref) { + + int n = pref.size(); + vector res(n, pref[0]); + for(int i = 1; i < n; i ++) res[i] = pref[i] ^ pref[i - 1]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/CMakeLists.txt b/2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/main.cpp b/2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/main.cpp new file mode 100644 index 00000000..f274eb54 --- /dev/null +++ b/2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/main.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/ +/// Author : liuyubobobo +/// Time : 2022-10-09 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string robotWithString(string s) { + + vector f(26, 0); + for(char c: s) f[c - 'a'] ++; + + vector stack; + string res = ""; + for(char c: s){ + stack.push_back(c); + f[c - 'a'] --; + while(!stack.empty() && is_min(f, stack.back())){ + res += stack.back(); + stack.pop_back(); + } + + } + while(!stack.empty()) res += stack.back(), stack.pop_back(); + return res; + } + +private: + bool is_min(const vector& f, char c){ + + char min_c = 'z' + 1; + for(int i = 0; i < 26; i ++) + if(f[i]){ + min_c = (char)('a' + i); + break; + } + return c <= min_c; + } +}; + + +int main() { + + string s1 = "zza"; + cout << Solution().robotWithString(s1) << '\n'; + // azz + + string s2 = "bac"; + cout << Solution().robotWithString(s2) << '\n'; + // abc + + string s3 = "bdda"; + cout << Solution().robotWithString(s3) << '\n'; + // addb + + string s4 = "vzhofnpo"; + cout << Solution().robotWithString(s4) << '\n'; + // fnohopzv + + return 0; +} diff --git a/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/CMakeLists.txt b/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/CMakeLists.txt new file mode 100644 index 00000000..541b2eff --- /dev/null +++ b/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) diff --git a/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main.cpp b/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main.cpp new file mode 100644 index 00000000..dd8589bc --- /dev/null +++ b/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/ +/// Author : liuyubobobo +/// Time : 2022-10-09 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(R * C * k) +/// Space Complexity: O(R * C * k) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int numberOfPaths(vector>& grid, int k) { + + int R = grid.size(), C = grid[0].size(); + vector>> dp(R, vector>(C, vector(k, 0))); + dp[0][0][grid[0][0] % k] = 1; + + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) { + if(i == 0 && j == 0) continue; + for (int m = 0; m < k; m++) { + long long t = 0; + + int a = m - grid[i][j]; + if (a < 0) + a = (a % k + k) % k; + + if(i - 1 >= 0) t += dp[i - 1][j][a]; + if(j - 1 >= 0) t += dp[i][j - 1][a]; + + dp[i][j][m] = t % MOD; + } + } + return dp[R - 1][C - 1][0]; + } +}; + + +int main() { + + vector> grid1 = {{5,2,4},{3,0,5},{0,7,2}}; + cout << Solution().numberOfPaths(grid1, 3) << '\n'; + // 2 + + return 0; +} diff --git a/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main2.cpp b/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main2.cpp new file mode 100644 index 00000000..7ad28ca1 --- /dev/null +++ b/2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/main2.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/ +/// Author : liuyubobobo +/// Time : 2022-10-09 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(R * C * k) +/// Space Complexity: O(R * C * k) +class Solution { + +private: + const long long MOD = 1e9 + 7; + int R, C, k; + +public: + int numberOfPaths(vector>& grid, int k) { + + R = grid.size(), C = grid[0].size(); + this->k = k; + vector>> dp(R, vector>(C, vector(k, -1))); + + return dfs(grid, 0, 0, grid[0][0] % k, dp); + } + +private: + long long dfs(const vector>& grid, int x, int y, int m, + vector>>& dp){ + + if(x == R - 1 && y == C - 1) return m == 0; + if(dp[x][y][m] != -1) return dp[x][y][m]; + + long long res = 0; + if(x + 1 < R) res += dfs(grid, x + 1, y, (m + grid[x + 1][y]) % k, dp); + if(y + 1 < C) res += dfs(grid, x, y + 1, (m + grid[x][y + 1]) % k, dp); + return dp[x][y][m] = res % MOD; + } +}; + + +int main() { + + vector> grid1 = {{5,2,4},{3,0,5},{0,7,2}}; + cout << Solution().numberOfPaths(grid1, 3) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 7e3e14e6..a614170b 100644 --- a/readme.md +++ b/readme.md @@ -2295,6 +2295,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2429 | [Minimize XOR](https://leetcode.com/problems/minimize-xor/) | [无] | [C++](2001-2500/2429-Minimize-XOR/cpp-2429/) | | | | 2430 | [Maximum Deletions on a String](https://leetcode.com/problems/maximum-deletions-on-a-string/) | [无] | [C++](2001-2500/2430-Maximum-Deletions-on-a-String/cpp-2430/) | | | | 2431 | [Maximize Total Tastiness of Purchased Fruits](https://leetcode.com/problems/maximize-total-tastiness-of-purchased-fruits/) | [无] | [C++](2001-2500/2431-Maximize-Total-Tastiness-of-Purchased-Fruits/cpp-2431/) | | | +| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/) | [无] | [C++](2001-2500/2432-The-Employee-That-Worked-on-the-Longest-Task/cpp-2432/) | | | +| 2433 | [Find The Original Array of Prefix Xor](https://leetcode.com/problems/find-the-original-array-of-prefix-xor/) | [无] | [C++](2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/) | | | +| 2434 | [Using a Robot to Print the Lexicographically Smallest String](https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/) | [无] | [C++](2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/) | | | +| 2435 | [Paths in Matrix Whose Sum Is Divisible by K](https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/) | [无] | [C++](2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 05a204950a56a2dfe48b3e20cd223543b497d496 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 12 Oct 2022 11:26:19 -0700 Subject: [PATCH 214/390] 2436 solved. --- .../cpp-2436/CMakeLists.txt | 6 +++ .../cpp-2436/main.cpp | 46 +++++++++++++++++++ readme.md | 1 + 3 files changed, 53 insertions(+) create mode 100644 2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/CMakeLists.txt create mode 100644 2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/main.cpp diff --git a/2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/CMakeLists.txt b/2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/CMakeLists.txt new file mode 100644 index 00000000..a784c2cf --- /dev/null +++ b/2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2436) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2436 main.cpp) diff --git a/2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/main.cpp b/2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/main.cpp new file mode 100644 index 00000000..286962f5 --- /dev/null +++ b/2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/minimum-split-into-subarrays-with-gcd-greater-than-one/ +/// Author : liuyubobobo +/// Time : 2022-10-12 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2 * log(max(nums))) +/// Space Complexity: O(n) +class Solution { +public: + int minimumSplits(vector& nums) { + + int n = nums.size(); + + vector dp(n + 1, INT_MAX); + dp[n] = 0; + for(int i = n - 1; i >= 0; i --){ + dp[i] = 1 + dp[i + 1]; + int cur = nums[i]; + for(int j = i + 1; j < n && cur > 1; j ++){ + cur = gcd(cur,nums[j]); + if(cur > 1) dp[i] = min(dp[i], 1 + dp[j + 1]); + } + } + return dp[0]; + } + +private: + int gcd(int a, int b){ + if(a > b) swap(a, b); + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a614170b..611ba53f 100644 --- a/readme.md +++ b/readme.md @@ -2299,6 +2299,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2433 | [Find The Original Array of Prefix Xor](https://leetcode.com/problems/find-the-original-array-of-prefix-xor/) | [无] | [C++](2001-2500/2433-Find-The-Original-Array-of-Prefix-Xor/cpp-2433/) | | | | 2434 | [Using a Robot to Print the Lexicographically Smallest String](https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/) | [无] | [C++](2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/) | | | | 2435 | [Paths in Matrix Whose Sum Is Divisible by K](https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/) | [无] | [C++](2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/) | | | +| 2436 | [Minimum Split Into Subarrays With GCD Greater Than One](https://leetcode.com/problems/minimum-split-into-subarrays-with-gcd-greater-than-one/) | [无] | [C++](2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 7fc0dd9e821119badfbdbb903a8f2fa240e83d31 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 12 Oct 2022 16:08:34 -0700 Subject: [PATCH 215/390] 2421 solved. --- .../cpp-2421/CMakeLists.txt | 6 ++ .../cpp-2421/main.cpp | 92 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2421-Number-of-Good-Paths/cpp-2421/CMakeLists.txt create mode 100644 2001-2500/2421-Number-of-Good-Paths/cpp-2421/main.cpp diff --git a/2001-2500/2421-Number-of-Good-Paths/cpp-2421/CMakeLists.txt b/2001-2500/2421-Number-of-Good-Paths/cpp-2421/CMakeLists.txt new file mode 100644 index 00000000..51dae3bd --- /dev/null +++ b/2001-2500/2421-Number-of-Good-Paths/cpp-2421/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2421) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2421 main.cpp) diff --git a/2001-2500/2421-Number-of-Good-Paths/cpp-2421/main.cpp b/2001-2500/2421-Number-of-Good-Paths/cpp-2421/main.cpp new file mode 100644 index 00000000..e4a923ec --- /dev/null +++ b/2001-2500/2421-Number-of-Good-Paths/cpp-2421/main.cpp @@ -0,0 +1,92 @@ +/// Source : https://leetcode.com/problems/number-of-good-paths/ +/// Author : liuyubobobo +/// Time : 2022-10-12 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Using UF +/// Time Complexity: O(max(vals) + |edges|) +/// Space Complexity: O(n) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + int numberOfGoodPaths(vector& vals, vector>& edges) { + + int n = vals.size(); + int max_vals = *max_element(vals.begin(), vals.end()); + + vector>> data(max_vals + 1); + for(const vector& edge: edges){ + int a = edge[0], b = edge[1]; + data[max(vals[a], vals[b])].push_back({a, b}); + } + + UF uf(n); + int res = n; + for(int v = 0; v <= max_vals; v ++){ + list>& l = data[v]; + set vset; + for(const pair& p: l) { + uf.union_elements(p.first, p.second); + if(vals[p.first] == v) vset.insert(p.first); + if(vals[p.second] == v) vset.insert(p.second); + } + + map cc; + for(int u: vset) cc[uf.find(u)] ++; + + for(const pair& p: cc){ + int k = p.second; + res += k * (k - 1) / 2; + } + } + return res; + } +}; + + +int main() { + + vector vals1 = {1, 3, 2, 1, 3}; + vector> edges1 = {{0, 1}, {0, 2}, {2, 3}, {2, 4}}; + cout << Solution().numberOfGoodPaths(vals1, edges1) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index 611ba53f..abd5819d 100644 --- a/readme.md +++ b/readme.md @@ -2284,7 +2284,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [无] | [C++](2001-2500/2418-Sort-the-People/cpp-2418/) | | | | 2419 | [Longest Subarray With Maximum Bitwise AND](https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/) | [无] | [C++](2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/) | | | | 2420 | [Find All Good Indices](https://leetcode.com/problems/find-all-good-indices/) | [无] | [C++](2001-2500/2420-Find-All-Good-Indices/cpp-2420/) | | | -| | | | | | | +| 2421 | [Number of Good Paths](https://leetcode.com/problems/number-of-good-paths/) | [无] | [C++](2001-2500/2421-Number-of-Good-Paths/cpp-2421/) | | | | 2422 | [Merge Operations to Turn Array Into a Palindrome](https://leetcode.com/problems/merge-operations-to-turn-array-into-a-palindrome/) | [无] | [C++](2001-2500/2422-Merge-Operations-to-Turn-Array-Into-a-Palindrome/cpp-2422/) | | | | 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [无] | [C++](2001-2500/2423-Remove-Letter-To-Equalize-Frequency/cpp-2423/) | | | | 2424 | [Longest Uploaded Prefix](https://leetcode.com/problems/longest-uploaded-prefix/) | [无] | [C++](2001-2500/2424-Longest-Uploaded-Prefix/cpp-2424/) | | | From f876d8fe33f1f81479b82c672d13b55944ae5705 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 12 Oct 2022 16:21:31 -0700 Subject: [PATCH 216/390] 2417 solved. --- .../cpp-2417/CMakeLists.txt | 6 +++ .../cpp-2417/main.cpp | 50 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2417-Closest-Fair-Integer/cpp-2417/CMakeLists.txt create mode 100644 2001-2500/2417-Closest-Fair-Integer/cpp-2417/main.cpp diff --git a/2001-2500/2417-Closest-Fair-Integer/cpp-2417/CMakeLists.txt b/2001-2500/2417-Closest-Fair-Integer/cpp-2417/CMakeLists.txt new file mode 100644 index 00000000..66e0b340 --- /dev/null +++ b/2001-2500/2417-Closest-Fair-Integer/cpp-2417/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2417) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2417 main.cpp) diff --git a/2001-2500/2417-Closest-Fair-Integer/cpp-2417/main.cpp b/2001-2500/2417-Closest-Fair-Integer/cpp-2417/main.cpp new file mode 100644 index 00000000..cb11dc1d --- /dev/null +++ b/2001-2500/2417-Closest-Fair-Integer/cpp-2417/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/closest-fair-integer/ +/// Author : liuyubobobo +/// Time : 2022-10-12 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(logn) +/// Space Complexity: O(logn) +class Solution { +public: + int closestFair(int n) { + + string s = to_string(n); + int len = s.size(); + + int start = n; + if(len % 2) start = (int)pow(10, len); + + int i; + for(i = start; to_string(i).size() % 2 == 0; i ++) + if(good(i)) return i; + return closestFair(i); + } + +private: + bool good(int x){ + + vector f(2, 0); + while(x){ + int d = x % 10; + f[d & 1] ++; + x /= 10; + } + return f[0] == f[1]; + } +}; + + +int main() { + + cout << Solution().closestFair(2) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index abd5819d..84510e65 100644 --- a/readme.md +++ b/readme.md @@ -2280,7 +2280,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2414 | [Length of the Longest Alphabetical Continuous Substring](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) | [无] | [C++](2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/) | | | | 2415 | [Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | [无] | [C++](2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/) | | | | 2416 | [Sum of Prefix Scores of Strings](https://leetcode.com/problems/sum-of-prefix-scores-of-strings/) | [无] | [C++](2001-2500/2416-Sum-of-Prefix-Scores-of-Strings/cpp-2416/) | | | -| | | | | | | +| 2417 | [Closest Fair Integer](https://leetcode.com/problems/closest-fair-integer/) | [无] | [C++](2001-2500/2417-Closest-Fair-Integer/cpp-2417/) | | | | 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [无] | [C++](2001-2500/2418-Sort-the-People/cpp-2418/) | | | | 2419 | [Longest Subarray With Maximum Bitwise AND](https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/) | [无] | [C++](2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/) | | | | 2420 | [Find All Good Indices](https://leetcode.com/problems/find-all-good-indices/) | [无] | [C++](2001-2500/2420-Find-All-Good-Indices/cpp-2420/) | | | From f92bb24312763a3c1672448b95f8ee729626b706 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 15 Oct 2022 23:47:11 -0700 Subject: [PATCH 217/390] 2437-2439 solved. --- .../cpp-2437/CMakeLists.txt | 6 +++ .../cpp-2437/main.cpp | 53 +++++++++++++++++++ .../cpp-2438/CMakeLists.txt | 6 +++ .../cpp-2438/main.cpp | 42 +++++++++++++++ .../cpp-2439/CMakeLists.txt | 6 +++ .../cpp-2439/main.cpp | 48 +++++++++++++++++ readme.md | 4 ++ 7 files changed, 165 insertions(+) create mode 100644 2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/CMakeLists.txt create mode 100644 2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/main.cpp create mode 100644 2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/CMakeLists.txt create mode 100644 2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/main.cpp create mode 100644 2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/CMakeLists.txt create mode 100644 2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/main.cpp diff --git a/2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/CMakeLists.txt b/2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/CMakeLists.txt new file mode 100644 index 00000000..36154dc8 --- /dev/null +++ b/2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2437) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2437 main.cpp) diff --git a/2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/main.cpp b/2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/main.cpp new file mode 100644 index 00000000..864a6f0d --- /dev/null +++ b/2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/number-of-valid-clock-times/ +/// Author : liuyubobobo +/// Time : 2022-10-15 + +#include + +using namespace std; + + +/// Backtrack +/// Time Complexity: O(10^4) +/// Space Complexity: O(1) +class Solution { +public: + int countTime(string time) { + + return dfs(time, 0); + } + +private: + int dfs(string& time, int index){ + + if(index == 5){ + return is_valid_time(time); + } + + if(time[index] != '?') return dfs(time, index + 1); + + int res = 0; + for(char c = '0'; c <= '9'; c ++){ + time[index] = c; + res += dfs(time, index + 1); + time[index] = '?'; + } + return res; + } + + bool is_valid_time(const string& time){ + + int h = atoi(time.substr(0, 2).c_str()); + int m = atoi(time.substr(3).c_str()); + return 0 <= h && h <= 23 && 0 <= m && m <= 59; + } +}; + + +int main() { + + cout << Solution().countTime("0?:0?") << '\n'; + // 100 + + return 0; +} diff --git a/2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/CMakeLists.txt b/2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/CMakeLists.txt new file mode 100644 index 00000000..c7c49622 --- /dev/null +++ b/2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2438) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2438 main.cpp) diff --git a/2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/main.cpp b/2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/main.cpp new file mode 100644 index 00000000..001f7993 --- /dev/null +++ b/2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/range-product-queries-of-powers/ +/// Author : liuyubobobo +/// Time : 2022-10-15 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(logn + qlogn) +/// Space Complexity: O(logn) +class Solution { + +private: + long long MOD = 1e9 + 7; + +public: + vector productQueries(int n, vector>& queries) { + + vector powers; + for(int p = 0; p <= 30; p ++) + if((n >> p) & 1) powers.push_back(1 << p); + + int q = queries.size(); + vector res(q); + for(int i = 0; i < q; i ++){ + long long tres = 1; + for(int j = queries[i][0]; j <= queries[i][1]; j ++) + tres = tres * powers[j] % MOD; + res[i] = tres; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/CMakeLists.txt b/2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/CMakeLists.txt new file mode 100644 index 00000000..a8791ee1 --- /dev/null +++ b/2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2439) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2439 main.cpp) diff --git a/2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/main.cpp b/2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/main.cpp new file mode 100644 index 00000000..1e12b95f --- /dev/null +++ b/2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/minimize-maximum-of-array/ +/// Author : liuyubobobo +/// Time : 2022-10-15 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int minimizeArrayValue(vector& nums) { + + int n = nums.size(); + vector data(n); + for(int i = 0; i < n; i ++) data[i] = nums[i]; + + int l = 0, r = 2e9; + while(l < r){ + int mid = l + (r - l) / 2; + if(ok(n, data, mid)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(int n, vector nums, long long x){ + + for(int i = n - 1; i > 0; i --) + if(nums[i] > x){ + long long t = nums[i] - x; + nums[i] -= t; + nums[i - 1] += t; + } + return nums[0] <= x; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 84510e65..c7e365db 100644 --- a/readme.md +++ b/readme.md @@ -2300,6 +2300,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2434 | [Using a Robot to Print the Lexicographically Smallest String](https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/) | [无] | [C++](2001-2500/2434-Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/cpp-2434/) | | | | 2435 | [Paths in Matrix Whose Sum Is Divisible by K](https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/) | [无] | [C++](2001-2500/2435-Paths-in-Matrix-Whose-Sum-Is-Divisible-by-K/cpp-2435/) | | | | 2436 | [Minimum Split Into Subarrays With GCD Greater Than One](https://leetcode.com/problems/minimum-split-into-subarrays-with-gcd-greater-than-one/) | [无] | [C++](2001-2500/2436-Minimum-Split-Into-Subarrays-With-GCD-Greater-Than-One/cpp-2436/) | | | +| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times/) | [无] | [C++](2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/) | | | +| 2438 | [Range Product Queries of Powers](https://leetcode.com/problems/range-product-queries-of-powers/) | [无] | [C++](2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/) | | | +| 2439 | [Minimize Maximum of Array](https://leetcode.com/problems/minimize-maximum-of-array/) | [无] | [C++](2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/) | | | +| | | | | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 60e0687002f7cfbc5feb7822b859e1e8d7813a3f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 16 Oct 2022 00:13:25 -0700 Subject: [PATCH 218/390] 2441-2444 solved. --- .../cpp-2441/CMakeLists.txt | 6 ++ .../cpp-2441/main.cpp | 31 ++++++++++ .../cpp-2442/CMakeLists.txt | 6 ++ .../cpp-2442/main.cpp | 34 ++++++++++ .../cpp-2443/CMakeLists.txt | 6 ++ .../cpp-2443/main.cpp | 36 +++++++++++ .../cpp-2444/CMakeLists.txt | 6 ++ .../cpp-2444/main.cpp | 62 +++++++++++++++++++ readme.md | 4 ++ 9 files changed, 191 insertions(+) create mode 100644 2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/CMakeLists.txt create mode 100644 2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/main.cpp create mode 100644 2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/CMakeLists.txt create mode 100644 2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/main.cpp create mode 100644 2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/CMakeLists.txt create mode 100644 2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/main.cpp create mode 100644 2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/CMakeLists.txt create mode 100644 2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/main.cpp diff --git a/2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/CMakeLists.txt b/2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/CMakeLists.txt new file mode 100644 index 00000000..11d7a27d --- /dev/null +++ b/2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2441) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2441 main.cpp) diff --git a/2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/main.cpp b/2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/main.cpp new file mode 100644 index 00000000..bded374d --- /dev/null +++ b/2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/ +/// Author : liuyubobobo +/// Time : 2022-10-15 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int findMaxK(vector& nums) { + + set s(nums.begin(), nums.end()); + int res = -1; + for(int e: nums) + if(e > 0 && s.count(-e)) res = max(res, e); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/CMakeLists.txt b/2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/CMakeLists.txt new file mode 100644 index 00000000..ad4bbf22 --- /dev/null +++ b/2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2442) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2442 main.cpp) diff --git a/2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/main.cpp b/2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/main.cpp new file mode 100644 index 00000000..49c6e765 --- /dev/null +++ b/2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/count-number-of-distinct-integers-after-reverse-operations/ +/// Author : liuyubobobo +/// Time : 2022-10-15 + +#include +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(nlog(max_nums)) +/// Space Complexity: O(n) +class Solution { +public: + int countDistinctIntegers(vector& nums) { + + set s(nums.begin(), nums.end()); + for(int e: nums){ + string e_str = to_string(e); + reverse(e_str.begin(), e_str.end()); + s.insert(atoi(e_str.c_str())); + } + return s.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/CMakeLists.txt b/2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/CMakeLists.txt new file mode 100644 index 00000000..40b91463 --- /dev/null +++ b/2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2443) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2443 main.cpp) diff --git a/2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/main.cpp b/2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/main.cpp new file mode 100644 index 00000000..2581059a --- /dev/null +++ b/2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/sum-of-number-and-its-reverse/ +/// Author : liuyubobobo +/// Time : 2022-10-16 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(num * log(num)) +/// Space Complexity: O(log(num)) +class Solution { +public: + bool sumOfNumberAndReverse(int num) { + + for(int a = num; a >= num - a; a --){ + string a_str = to_string(a); + reverse(a_str.begin(), a_str.end()); + int ra = atoi(a_str.c_str()); + if(ra == num - a) return true; + } + return false; + } +}; + + +int main() { + + cout << Solution().sumOfNumberAndReverse(181) << '\n'; + // 1 + + return 0; +} diff --git a/2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/CMakeLists.txt b/2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/CMakeLists.txt new file mode 100644 index 00000000..688c8248 --- /dev/null +++ b/2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2444) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2444 main.cpp) diff --git a/2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/main.cpp b/2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/main.cpp new file mode 100644 index 00000000..30db351b --- /dev/null +++ b/2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/count-subarrays-with-fixed-bounds/ +/// Author : liuyubobobo +/// Time : 2022-10-16 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long countSubarrays(vector& nums, int minK, int maxK) { + + int n = nums.size(); + + vector ok(n); + for(int i = 0; i < n; i ++) + ok[i] = (minK <= nums[i] && nums[i] <= maxK); + + long long res = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || ok[i] != ok[start]){ + if(ok[start]) + res += solve(nums, start, i, minK, maxK); + start = i; + } + return res; + } + +private: + long long solve(const vector& nums, int l, int r, int minK, int maxK){ + + vector min_indices, max_indices; + for(int i = l; i < r; i ++){ + if(nums[i] == minK) min_indices.push_back(i); + if(nums[i] == maxK) max_indices.push_back(i); + } + + long long res = 0; + int i = l, min_i = 0, max_i = 0; + while(i < r && min_i < min_indices.size() && max_i < max_indices.size()){ + + int j = max(min_indices[min_i], max_indices[max_i]); + res += r - j; + + i ++; + if(min_indices[min_i] < i) min_i ++; + if(max_indices[max_i] < i) max_i ++; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index c7e365db..2906c116 100644 --- a/readme.md +++ b/readme.md @@ -2304,6 +2304,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2438 | [Range Product Queries of Powers](https://leetcode.com/problems/range-product-queries-of-powers/) | [无] | [C++](2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/) | | | | 2439 | [Minimize Maximum of Array](https://leetcode.com/problems/minimize-maximum-of-array/) | [无] | [C++](2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/) | | | | | | | | | | +| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | [无] | [C++](2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/) | | | +| 2442 | [Count Number of Distinct Integers After Reverse Operations](https://leetcode.com/problems/count-number-of-distinct-integers-after-reverse-operations/) | [无] | [C++](2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/) | | | +| 2443 | [Sum of Number and Its Reverse](https://leetcode.com/problems/sum-of-number-and-its-reverse/) | [无] | [C++](2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/) | | | +| 2444 | [Count Subarrays With Fixed Bounds](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/) | [无] | [C++](2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 29e6ee09ae1f5de7aa8f55eb1ae9ec8e539bb3d4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 16 Oct 2022 16:21:54 -0700 Subject: [PATCH 219/390] 2440 solved. --- .../cpp-2440/CMakeLists.txt | 6 ++ .../cpp-2440/main.cpp | 99 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/CMakeLists.txt create mode 100644 2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/main.cpp diff --git a/2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/CMakeLists.txt b/2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/CMakeLists.txt new file mode 100644 index 00000000..10329b23 --- /dev/null +++ b/2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2440) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2440 main.cpp) diff --git a/2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/main.cpp b/2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/main.cpp new file mode 100644 index 00000000..274342e4 --- /dev/null +++ b/2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/main.cpp @@ -0,0 +1,99 @@ +/// Source : https://leetcode.cn/problems/create-components-with-same-value/ +/// Author : liuyubobobo +/// Time : 2022-10-16 + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// Topological Sort +/// Time Complexity: O(sqrt(sum) * n) +/// Space Complexity: O(n) +class Solution { +public: + int componentValue(vector& nums, vector>& edges) { + + int sum = accumulate(nums.begin(), nums.end(), 0); + + vector D; + for(int i = 1; i * i <= sum; i ++) + if(sum % i == 0){ + D.push_back(i); + if(i * i < sum) D.push_back(sum / i); + } + sort(D.begin(), D.end()); + + int n = nums.size(); + vector> g(n); + vector degrees(n, 0); + for(const vector& edge: edges){ + int a = edge[0], b = edge[1]; + g[a].push_back(b), g[b].push_back(a); + degrees[a] ++, degrees[b] ++; + } + + for(int d: D){ + if(ok(n, g, degrees, nums, d)){ + return sum / d - 1; + } + } + assert(false); + return -1; + } + +private: + bool ok(int n, const vector>& g, + vector degrees, vector nums, int d){ + + if(n == 1) return nums[0] == d; + + queue q; + vector inqueue(n, false); + vector over(n, false); + for(int i = 0; i < n; i ++) + if(degrees[i] == 1) q.push(i), inqueue[i] = true; + + while(q.size() > 1){ + int cur = q.front(); q.pop(); + + if(nums[cur] > d) return false; + + for(int next: g[cur]){ + if(over[next]) continue; + degrees[next] --; + degrees[cur] --; + + if(nums[cur] < d) nums[next] += nums[cur]; + if(degrees[next] == 1 && !inqueue[next]){ + inqueue[next] = true; + q.push(next); + } + } + over[cur] = true; + } + return nums[q.front()] == d; + } +}; + + +int main() { + +// vector nums1 = {6,2,2,2,6}; +// vector> edges1 = {{0,1},{1,2},{1,3},{3,4}}; +// cout << Solution().componentValue(nums1, edges1) << '\n'; +// // 2 + + vector nums2 = {1}; + vector> edges2 = {}; + cout << Solution().componentValue(nums2, edges2) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 2906c116..7bcc7578 100644 --- a/readme.md +++ b/readme.md @@ -2303,7 +2303,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times/) | [无] | [C++](2001-2500/2437-Number-of-Valid-Clock-Times/cpp-2437/) | | | | 2438 | [Range Product Queries of Powers](https://leetcode.com/problems/range-product-queries-of-powers/) | [无] | [C++](2001-2500/2438-Range-Product-Queries-of-Powers/cpp-2438/) | | | | 2439 | [Minimize Maximum of Array](https://leetcode.com/problems/minimize-maximum-of-array/) | [无] | [C++](2001-2500/2439-Minimize-Maximum-of-Array/cpp-2439/) | | | -| | | | | | | +| 2440 | [Create Components With Same Value](https://leetcode.com/problems/create-components-with-same-value/) | [无] | [C++](2001-2500/2440-Create-Components-With-Same-Value/cpp-2440/) | | | | 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | [无] | [C++](2001-2500/2441-Largest-Positive-Integer-That-Exists-With-Its-Negative/cpp-2441/) | | | | 2442 | [Count Number of Distinct Integers After Reverse Operations](https://leetcode.com/problems/count-number-of-distinct-integers-after-reverse-operations/) | [无] | [C++](2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/) | | | | 2443 | [Sum of Number and Its Reverse](https://leetcode.com/problems/sum-of-number-and-its-reverse/) | [无] | [C++](2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/) | | | From 3c38005a674b3b66a911c89a91bceabe0bc9dfa3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 16 Oct 2022 20:43:32 -0700 Subject: [PATCH 220/390] 1531 solved. --- .../cpp-1531/CMakeLists.txt | 6 ++ .../cpp-1531/main.cpp | 80 +++++++++++++++++++ readme.md | 2 + 3 files changed, 88 insertions(+) create mode 100644 1501-2000/1531-String-Compression-II/cpp-1531/CMakeLists.txt create mode 100644 1501-2000/1531-String-Compression-II/cpp-1531/main.cpp diff --git a/1501-2000/1531-String-Compression-II/cpp-1531/CMakeLists.txt b/1501-2000/1531-String-Compression-II/cpp-1531/CMakeLists.txt new file mode 100644 index 00000000..3b24f0ff --- /dev/null +++ b/1501-2000/1531-String-Compression-II/cpp-1531/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_1531) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1531 main.cpp) diff --git a/1501-2000/1531-String-Compression-II/cpp-1531/main.cpp b/1501-2000/1531-String-Compression-II/cpp-1531/main.cpp new file mode 100644 index 00000000..2616d50e --- /dev/null +++ b/1501-2000/1531-String-Compression-II/cpp-1531/main.cpp @@ -0,0 +1,80 @@ +/// Source : https://leetcode.com/problems/string-compression-ii/ +/// Author : liuyubobobo +/// Time : 2022-10-16 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(27 * 11 * |s| * k) +/// Space Complexity: O(27 * 11 * |s| * k) +class Solution { + +public: + int getLengthOfOptimalCompression(string s, int k) { + + if(all_one_char(s)){ + s = s.substr(0, (int)s.size() - k); + return len(s.size()); + } + + // dp[pre_char][pre_cnt][index][k] + vector>>> dp(27, vector>>(11, vector>(s.size(), vector(k + 1, -1)))); + return dfs(s, 26, 0, 0, k, dp); + } + +private: + int dfs(const string& s, int pre_char, int pre_cnt, int index, int k, + vector>>>& dp){ + + int pre_len = len(pre_cnt); + + if(index == s.size()){ + return pre_len; + } + + if(dp[pre_char][pre_cnt][index][k] != -1) + return dp[pre_char][pre_cnt][index][k]; + + int res = INT_MAX; + if(s[index] - 'a' != pre_char) + res = min(res, pre_len + dfs(s, s[index] - 'a', 1, index + 1, k, dp)); + else + res = min(res, dfs(s, pre_char, min(pre_cnt + 1, 10), index + 1, k, dp)); + + if(k) + res = min(res, dfs(s, pre_char, pre_cnt, index + 1, k - 1, dp)); + return dp[pre_char][pre_cnt][index][k] = res; + } + + int len(int cnt){ + if(cnt == 0) return 0; + else if(cnt == 1) return 1; + else return 1 + to_string(cnt).size(); + } + + bool all_one_char(const string& s){ + for(int i = 1; i < s.size(); i ++) + if(s[i] != s[0]) return false; + return true; + } +}; + + +int main() { + + cout << Solution().getLengthOfOptimalCompression("aaabcccd", 2) << '\n'; + // 4 + + cout << Solution().getLengthOfOptimalCompression("aabbaa", 2) << '\n'; + // 2 + + cout << Solution().getLengthOfOptimalCompression("aaaaaaaaaaa", 0) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index 7bcc7578..78f13b8f 100644 --- a/readme.md +++ b/readme.md @@ -1430,6 +1430,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1527 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 1531 | [String Compression II](https://leetcode.com/problems/string-compression-ii/) | [无] | [C++](1501-2000/1531-String-Compression-II/cpp-1531/) | | | +| | | | | | | | 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [solution](https://leetcode.com/problems/kth-missing-positive-number/solution/) | [C++](1501-2000/1539-Kth-Missing-Positive-Number/cpp-1539/) | | | | | | | | | | | 1559 | [Detect Cycles in 2D Grid](https://leetcode.com/problems/detect-cycles-in-2d-grid/) | [无] | [C++](1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/) | | | From e528aba9d750f3199ae7d7596db506b1c09abd24 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 20 Oct 2022 11:26:28 -0700 Subject: [PATCH 221/390] 0779 added. --- .../cpp-0779/CMakeLists.txt | 6 ++++ .../cpp-0779/main.cpp | 34 +++++++++++++++++++ readme.md | 3 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/CMakeLists.txt create mode 100644 0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/main.cpp diff --git a/0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/CMakeLists.txt b/0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/CMakeLists.txt new file mode 100644 index 00000000..5f29590d --- /dev/null +++ b/0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0779) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0779 main.cpp) diff --git a/0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/main.cpp b/0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/main.cpp new file mode 100644 index 00000000..286e4dae --- /dev/null +++ b/0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/k-th-symbol-in-grammar/ +/// Author : liuyubobobo +/// Time : 2022-10-20 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int kthGrammar(int n, int k) { + return solve(0, n - 1, k - 1); + } + +private: + int solve(int parent, int n, int k){ + if(n == 0) return parent; + + int mid = 1 << (n - 1); + if(k < mid) return solve(parent == 0 ? 0 : 1, n - 1, k); + return solve(parent == 0 ? 1 : 0, n - 1, k - mid); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 78f13b8f..45e6893e 100644 --- a/readme.md +++ b/readme.md @@ -780,6 +780,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 774 | [Minimize Max Distance to Gas Station](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | [solution](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | [C++](0501-1000/0774-Minimize-Max-Distance-to-Gas-Station/cpp-0774/) | | | | | | | | | | +| 779 | [K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/) | [无] | [C++](0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/) | | | | 780 | [Reaching Points](https://leetcode.com/problems/reaching-points/description/) | [solution](https://leetcode.com/problems/reaching-points/solution/) | [C++](0501-1000/0780-Reaching-Points/cpp-0780/) | | | | 781 | [Rabbits in Forest](https://leetcode.com/problems/rabbits-in-forest/description/) | [solution](https://leetcode.com/problems/rabbits-in-forest/solution/) | [C++](0501-1000/0781-Rabbits-in-Forest/cpp-0781/) | | | | 782 | [Transform to Chessboard](https://leetcode.com/problems/transform-to-chessboard/) | [solution](https://leetcode.com/problems/transform-to-chessboard/solution/) | [C++](0501-1000/0782-Transform-to-Chessboard/cpp-0782/) | | | @@ -1430,7 +1431,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1527 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | -| 1531 | [String Compression II](https://leetcode.com/problems/string-compression-ii/) | [无] | [C++](1501-2000/1531-String-Compression-II/cpp-1531/) | | | +| 1531 | [String Compression II](https://leetcode.com/problems/string-compression-ii/) | [无][缺:其他解法] | [C++](1501-2000/1531-String-Compression-II/cpp-1531/) | | | | | | | | | | | 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [solution](https://leetcode.com/problems/kth-missing-positive-number/solution/) | [C++](1501-2000/1539-Kth-Missing-Positive-Number/cpp-1539/) | | | | | | | | | | From 421dd0b92957300e74972322dfdaa83df2e033c7 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 20 Oct 2022 11:52:04 -0700 Subject: [PATCH 222/390] 2445 solved. --- .../cpp-2445/CMakeLists.txt | 6 +++ .../cpp-2445/main.cpp | 38 +++++++++++++++++++ readme.md | 1 + 3 files changed, 45 insertions(+) create mode 100644 2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/CMakeLists.txt create mode 100644 2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/main.cpp diff --git a/2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/CMakeLists.txt b/2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/CMakeLists.txt new file mode 100644 index 00000000..d96ee76a --- /dev/null +++ b/2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2445) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2445 main.cpp) diff --git a/2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/main.cpp b/2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/main.cpp new file mode 100644 index 00000000..cf1fd3ce --- /dev/null +++ b/2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/number-of-nodes-with-value-one/ +/// Author : liuyubobobo +/// Time : 2022-10-20 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int numberOfNodes(int n, vector& queries) { + + vector qtable(n + 1, 0); + for(int q: queries) qtable[q] ^= 1; + + int res = 0; + for(int i = 1; i <= n; i ++){ + int x = i, t = 0; + while(x){ + t ^= qtable[x]; + x >>= 1; + } + res += t; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 45e6893e..6b546826 100644 --- a/readme.md +++ b/readme.md @@ -2311,6 +2311,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2442 | [Count Number of Distinct Integers After Reverse Operations](https://leetcode.com/problems/count-number-of-distinct-integers-after-reverse-operations/) | [无] | [C++](2001-2500/2442-Count-Number-of-Distinct-Integers-After-Reverse-Operations/cpp-2442/) | | | | 2443 | [Sum of Number and Its Reverse](https://leetcode.com/problems/sum-of-number-and-its-reverse/) | [无] | [C++](2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/) | | | | 2444 | [Count Subarrays With Fixed Bounds](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/) | [无] | [C++](2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/) | | | +| 2445 | [Number of Nodes With Value One](https://leetcode.com/problems/number-of-nodes-with-value-one/) | [无] | [C++](2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 2825aeb48e53be91d73e2390bc29fca6b21f106c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 20 Oct 2022 15:23:40 -0700 Subject: [PATCH 223/390] 0257 new algo added. --- .../cpp-0257/CMakeLists.txt | 2 +- .../0257-Binary-Tree-Paths/cpp-0257/main2.cpp | 46 ++++----- .../0257-Binary-Tree-Paths/cpp-0257/main3.cpp | 58 ++++++++++++ .../0257-Binary-Tree-Paths/cpp-0257/main4.cpp | 94 +++++++++++++++++++ 4 files changed, 176 insertions(+), 24 deletions(-) create mode 100644 0001-0500/0257-Binary-Tree-Paths/cpp-0257/main3.cpp create mode 100644 0001-0500/0257-Binary-Tree-Paths/cpp-0257/main4.cpp diff --git a/0001-0500/0257-Binary-Tree-Paths/cpp-0257/CMakeLists.txt b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/CMakeLists.txt index 36e37f34..d1c1203a 100644 --- a/0001-0500/0257-Binary-Tree-Paths/cpp-0257/CMakeLists.txt +++ b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/CMakeLists.txt @@ -3,5 +3,5 @@ project(cpp_0257) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main2.cpp) +set(SOURCE_FILES main4.cpp) add_executable(cpp_0257 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main2.cpp b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main2.cpp index a5db76d0..289ffa84 100644 --- a/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main2.cpp +++ b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main2.cpp @@ -1,11 +1,10 @@ /// Source : https://leetcode.com/problems/binary-tree-paths/description/ /// Author : liuyubobobo -/// Time : 2018-11-17 +/// Time : 2020-10-20 #include #include #include -#include using namespace std; @@ -19,36 +18,37 @@ struct TreeNode { }; -/// Non-Recursive -/// Using Stack -/// +/// Recursive /// Time Complexity: O(n), where n is the node's number in the tree /// Space Complexity: O(h), where h is the height of the tree class Solution { public: vector binaryTreePaths(TreeNode* root) { + if(root == nullptr) return {}; + + vector path; vector res; - if(root == NULL) - return res; - - stack> stack; - stack.push(make_pair(root, to_string(root->val))); - while(!stack.empty()){ - TreeNode* cur = stack.top().first; - string s = stack.top().second; - stack.pop(); - - if(!cur->left && !cur->right) - res.push_back(s); - - if(cur->left) - stack.push(make_pair(cur->left, s + "->" + to_string(cur->left->val))); - if(cur->right) - stack.push(make_pair(cur->right, s + "->" + to_string(cur->right->val))); - } + dfs(root, path, res); return res; } + +private: + void dfs(TreeNode* node, vector& path, vector& res){ + + path.push_back(node); + if(node->left == nullptr && node->right == nullptr){ + string str = to_string(path[0]->val); + for(int i = 1; i < path.size(); i ++) + str += "->" + to_string(path[i]->val); + res.push_back(str); + } + + if(node->left) dfs(node->left, path, res); + if(node->right) dfs(node->right, path, res); + + path.pop_back(); + } }; diff --git a/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main3.cpp b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main3.cpp new file mode 100644 index 00000000..a5db76d0 --- /dev/null +++ b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main3.cpp @@ -0,0 +1,58 @@ +/// Source : https://leetcode.com/problems/binary-tree-paths/description/ +/// Author : liuyubobobo +/// Time : 2018-11-17 + +#include +#include +#include +#include + +using namespace std; + + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + + +/// Non-Recursive +/// Using Stack +/// +/// Time Complexity: O(n), where n is the node's number in the tree +/// Space Complexity: O(h), where h is the height of the tree +class Solution { +public: + vector binaryTreePaths(TreeNode* root) { + + vector res; + if(root == NULL) + return res; + + stack> stack; + stack.push(make_pair(root, to_string(root->val))); + while(!stack.empty()){ + TreeNode* cur = stack.top().first; + string s = stack.top().second; + stack.pop(); + + if(!cur->left && !cur->right) + res.push_back(s); + + if(cur->left) + stack.push(make_pair(cur->left, s + "->" + to_string(cur->left->val))); + if(cur->right) + stack.push(make_pair(cur->right, s + "->" + to_string(cur->right->val))); + } + return res; + } +}; + + +int main() { + + return 0; +} \ No newline at end of file diff --git a/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main4.cpp b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main4.cpp new file mode 100644 index 00000000..381ecc82 --- /dev/null +++ b/0001-0500/0257-Binary-Tree-Paths/cpp-0257/main4.cpp @@ -0,0 +1,94 @@ +/// Source : https://leetcode.com/problems/binary-tree-paths/description/ +/// Author : liuyubobobo +/// Time : 2020-10-20 + +#include +#include +#include +#include + +using namespace std; + + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x, TreeNode* lt, TreeNode* rt) : val(x), left(lt), right(rt) {} + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + + +/// Non-Recursive +/// Using Command Stack to simulate system stack +/// +/// Time Complexity: O(n), where n is the node's number in the tree +/// Space Complexity: O(h), where h is the height of the tree +class Solution { + +private: + struct Command{ + string s; // go, print, pop + TreeNode* node; + Command(string s, TreeNode* node): s(s), node(node){} + }; + +public: + vector binaryTreePaths(TreeNode* root) { + + if(root == nullptr) return {}; + + vector res; + if(root == NULL) + return res; + + stack command_stack; + vector path; + + command_stack.push(Command("go", root)); + + while(!command_stack.empty()){ + Command command = command_stack.top(); + command_stack.pop(); + + if(command.s == "do"){ + path.push_back(command.node); + if(command.node->left == nullptr && command.node->right == nullptr){ + string str = to_string(path[0]->val); + for(int i = 1; i < path.size(); i ++) + str += "->" + to_string(path[i]->val); + res.push_back(str); + } + } + else if(command.s == "pop"){ + path.pop_back(); + } + else{ + command_stack.push(Command("pop", command.node)); + + if(command.node->right) + command_stack.push(Command("go",command.node->right)); + if(command.node->left) + command_stack.push(Command("go",command.node->left)); + + command_stack.push(Command("do", command.node)); + + } + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(const string& s: v) cout << s << '\n'; +} + +int main() { + + TreeNode* root1 = new TreeNode(1, new TreeNode(2, NULL, new TreeNode(5)), new TreeNode(3)); + print_vec(Solution().binaryTreePaths(root1)); + + return 0; +} \ No newline at end of file From 0271a6a946476066d8280d6944016cda30737508 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 22 Oct 2022 23:02:28 -0700 Subject: [PATCH 224/390] 2446-2449 added. --- .../cpp-2446/CMakeLists.txt | 6 ++ .../cpp-2446/main.cpp | 37 ++++++++++++ .../cpp-2447/CMakeLists.txt | 6 ++ .../cpp-2447/main.cpp | 55 +++++++++++++++++ .../cpp-2448/CMakeLists.txt | 6 ++ .../cpp-2448/main.cpp | 59 +++++++++++++++++++ .../cpp-2449/CMakeLists.txt | 6 ++ .../cpp-2449/main.cpp | 59 +++++++++++++++++++ readme.md | 4 ++ 9 files changed, 238 insertions(+) create mode 100644 2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/CMakeLists.txt create mode 100644 2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/main.cpp create mode 100644 2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/CMakeLists.txt create mode 100644 2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/main.cpp create mode 100644 2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/CMakeLists.txt create mode 100644 2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/main.cpp create mode 100644 2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/CMakeLists.txt create mode 100644 2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/main.cpp diff --git a/2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/CMakeLists.txt b/2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/main.cpp b/2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/main.cpp new file mode 100644 index 00000000..b1f75d90 --- /dev/null +++ b/2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/determine-if-two-events-have-conflict/ +/// Author : liuyubobobo +/// Time : 2022-10-22 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + bool haveConflict(vector& event1, vector& event2) { + + int s1 = get_time(event1[0]), e1 = get_time(event1[1]); + int s2 = get_time(event2[0]), e2 = get_time(event2[1]); + if(s1 > s2) swap(s1, s2), swap(e1, e2); + + return s2 <= e1; + } + +private: + int get_time(const string& s){ + int h = atoi(s.substr(0, 2).c_str()); + int m = atoi(s.substr(3).c_str()); + return h * 60 + m; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/CMakeLists.txt b/2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/main.cpp b/2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/main.cpp new file mode 100644 index 00000000..cbab9ecf --- /dev/null +++ b/2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/ +/// Author : liuyubobobo +/// Time : 2022-10-22 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2 * log(max_nums)) +/// Space Complexity: O(log(max_nums)) +class Solution { +public: + int subarrayGCD(vector& nums, int k) { + + int n = nums.size(); + int res = 0; + for(int l = 0; l < n; l ++){ + if(nums[l] == k) res ++; + int cur_g = nums[l]; + for(int r = l + 1; r < n; r ++){ + cur_g = gcd(cur_g, nums[r]); + if(cur_g == k) res ++; + } + } + return res; + } + +private: + int gcd(int a, int b){ + if(a > b) swap(a, b); + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + vector nums1 = {9, 3, 1, 2, 6, 3}; + cout << Solution().subarrayGCD(nums1, 3) << '\n'; + // 4 + + vector nums2 = {4}; + cout << Solution().subarrayGCD(nums2, 7) << '\n'; + // 0 + + vector nums3 = {3, 3, 4, 1, 2}; + cout << Solution().subarrayGCD(nums3, 1) << '\n'; + // 10 + + return 0; +} diff --git a/2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/CMakeLists.txt b/2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/main.cpp b/2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/main.cpp new file mode 100644 index 00000000..d71fe512 --- /dev/null +++ b/2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/minimum-cost-to-make-array-equal/ +/// Author : liuyubobobo +/// Time : 2022-10-22 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn + max_nums) +/// Space Complexity: O(n) +class Solution { +public: + long long minCost(vector& nums, vector& cost) { + + int n = nums.size(); + + long long cur_cost = 0; + for(int i = 0; i < n; i ++) cur_cost += 1ll * nums[i] * cost[i]; + long long res_cost = cur_cost; + + vector> data(n); + for(int i = 0; i < n; i ++) data[i] = {nums[i], cost[i]}; + sort(data.begin(), data.end()); + int index = 0; + + long long above_cost_d = 0; + for(int i = 0; i < n; i ++) above_cost_d += cost[i]; + long long below_cost_d = 0; + + for(int cur_target = 1; cur_target <= data.back().first; cur_target ++){ + cur_cost = cur_cost - above_cost_d + below_cost_d; + res_cost = min(res_cost, cur_cost); + + while(index < n && data[index].first == cur_target){ + above_cost_d -= data[index].second; + below_cost_d += data[index].second; + index ++; + } + } + return res_cost; + } +}; + + +int main() { + + vector nums1 = {1, 3, 5, 2}, cost1 = {2, 3, 1, 14}; + cout << Solution().minCost(nums1, cost1) << '\n'; + // 8 + + vector nums2 = {2, 2, 2, 2, 2}, cost2 = {4, 2, 8, 1, 3}; + cout << Solution().minCost(nums2, cost2) << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/CMakeLists.txt b/2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/main.cpp b/2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/main.cpp new file mode 100644 index 00000000..3202299a --- /dev/null +++ b/2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/ +/// Author : liuyubobobo +/// Time : 2022-10-22 + +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long makeSimilar(vector& nums, vector& target) { + + vector even_s, odd_s; + for(int e: nums) if(e & 1) odd_s.push_back(e); else even_s.push_back(e); + + vector even_t, odd_t; + for(int e: target) if(e & 1) odd_t.push_back(e); else even_t.push_back(e); + + sort(even_s.begin(), even_s.end()); + sort(odd_s.begin(), odd_s.end()); + sort(even_t.begin(), even_t.end()); + sort(odd_t.begin(), odd_t.end()); + + assert(even_s.size() == even_t.size() && odd_s.size() == odd_t.size()); + + long long add = 0, sub = 0; + for(int i = 0; i < even_s.size(); i ++){ + if(even_s[i] < even_t[i]) add += (even_t[i] - even_s[i]) / 2; + else sub += (even_s[i] - even_t[i]) / 2; + } + for(int i = 0; i < odd_s.size(); i ++){ + if(odd_s[i] < odd_t[i]) add += (odd_t[i] - odd_s[i]) / 2; + else sub += (odd_s[i] - odd_t[i]) / 2; + } + assert(add == sub); + return add; + } +}; + + +int main() { + + vector nums1 = {8, 12, 6}, target1 = {2, 14, 10}; + cout << Solution().makeSimilar(nums1, target1) << '\n'; + // 2 + + vector nums2 = {1, 2, 5}, target2 = {4, 1, 3}; + cout << Solution().makeSimilar(nums2, target2) << '\n'; + // 1 + + return 0; +} diff --git a/readme.md b/readme.md index 6b546826..b44fed28 100644 --- a/readme.md +++ b/readme.md @@ -2312,6 +2312,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2443 | [Sum of Number and Its Reverse](https://leetcode.com/problems/sum-of-number-and-its-reverse/) | [无] | [C++](2001-2500/2443-Sum-of-Number-and-Its-Reverse/cpp-2443/) | | | | 2444 | [Count Subarrays With Fixed Bounds](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/) | [无] | [C++](2001-2500/2444-Count-Subarrays-With-Fixed-Bounds/cpp-2444/) | | | | 2445 | [Number of Nodes With Value One](https://leetcode.com/problems/number-of-nodes-with-value-one/) | [无] | [C++](2001-2500/2445-Number-of-Nodes-With-Value-One/cpp-2445/) | | | +| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict/) | [无] | [C++](2001-2500/2446-Determine-if-Two-Events-Have-Conflict/cpp-2446/) | | | +| 2447 | [Number of Subarrays With GCD Equal to K](https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/) | [无] | [C++](2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/) | | | +| 2448 | [Minimum Cost to Make Array Equal](https://leetcode.com/problems/minimum-cost-to-make-array-equal/) | [无] | [C++](2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/) | | | +| 2449 | [Minimum Number of Operations to Make Arrays Similar](https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/) | [无] | [C++](2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 16941cf1774de748c90e414110b3b5bcc53d05cb Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 26 Oct 2022 23:05:00 -0700 Subject: [PATCH 225/390] 0835 solved. --- .../cpp-0835/CMakeLists.txt | 6 +++ .../0835-Image-Overlap/cpp-0835/main.cpp | 48 +++++++++++++++++++ readme.md | 1 + 3 files changed, 55 insertions(+) create mode 100644 0501-1000/0835-Image-Overlap/cpp-0835/CMakeLists.txt create mode 100644 0501-1000/0835-Image-Overlap/cpp-0835/main.cpp diff --git a/0501-1000/0835-Image-Overlap/cpp-0835/CMakeLists.txt b/0501-1000/0835-Image-Overlap/cpp-0835/CMakeLists.txt new file mode 100644 index 00000000..93467fbd --- /dev/null +++ b/0501-1000/0835-Image-Overlap/cpp-0835/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0835) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0835 main.cpp) diff --git a/0501-1000/0835-Image-Overlap/cpp-0835/main.cpp b/0501-1000/0835-Image-Overlap/cpp-0835/main.cpp new file mode 100644 index 00000000..c35d61d0 --- /dev/null +++ b/0501-1000/0835-Image-Overlap/cpp-0835/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/image-overlap/ +/// Author : liuyubobobo +/// Time : 2022-10-26 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^4) +/// Space Complexity: O(n^2) +class Solution { +public: + int largestOverlap(vector>& img1, vector>& img2) { + + int n = img1.size(); + int m = n * 3; + + vector> back(m, vector(m, 0)); + for(int i = 0; i < n; i ++) + for(int j = 0; j < n; j ++) back[i + n][j + n] = img1[i][j]; + + int res = 0; + for(int i = 0; i + n <= m; i ++) + for(int j = 0; j + n <= m; j ++) + res = max(res, match(back, i, j, img2, n)); + return res; + } + +private: + int match(const vector>& back, int sx, int sy, + const vector>& img, int n){ + + int res = 0; + for(int i = 0; i < n; i ++) + for(int j = 0; j < n; j ++) + res += (img[i][j] && back[i + sx][j + sy]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index b44fed28..c83c59ff 100644 --- a/readme.md +++ b/readme.md @@ -833,6 +833,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [solution](https://leetcode.com/problems/flipping-an-image/solution/) | [C++](0501-1000/0832-Flipping-an-Image/cpp-0832/) | | | | | | | | | | | 834 | [Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/) | [solution](https://leetcode.com/problems/sum-of-distances-in-tree/solution/) | [C++](0501-1000/0834-Sum-of-Distances-in-Tree/cpp-0834/) | | | +| 835 | [Image Overlap](https://leetcode.com/problems/image-overlap/) | [无] | [C++](0501-1000/0835-Image-Overlap/cpp-0835/) | | | | | | | | | | | 838 | [Push Dominoes](https://leetcode.com/problems/push-dominoes/) | [solution](https://leetcode.com/problems/push-dominoes/solution/) | [C++](0501-1000/0838-Push-Dominoes/cpp-0838/) | | | | 839 | [Similar String Groups](https://leetcode.com/problems/similar-string-groups/) | [solution](https://leetcode.com/problems/similar-string-groups/solution/) | From 219b69c45cbbb4e9234bd51214919d57e03ad383 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 29 Oct 2022 13:56:22 -0700 Subject: [PATCH 226/390] 0487 solved. --- .../cpp-0487/CMakeLists.txt | 6 +++ .../cpp-0487/main.cpp | 49 +++++++++++++++++++ readme.md | 1 + 3 files changed, 56 insertions(+) create mode 100644 0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/CMakeLists.txt create mode 100644 0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/main.cpp diff --git a/0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/CMakeLists.txt b/0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/CMakeLists.txt new file mode 100644 index 00000000..d77c8dc8 --- /dev/null +++ b/0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0487) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0487 main.cpp) diff --git a/0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/main.cpp b/0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/main.cpp new file mode 100644 index 00000000..0a1299a1 --- /dev/null +++ b/0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/max-consecutive-ones-ii/ +/// Author : liuyubobobo +/// Time : 2022-10-29 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int findMaxConsecutiveOnes(vector& nums) { + + int n = nums.size(); + + int res = 0; + vector> v; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || nums[i] != nums[start]){ + v.push_back({nums[start], i - start}); + if(nums[start]) res = max(res, i - start); + else res = max(res, 1); + start = i; + } + + for(int i = 0; i < v.size(); i ++) + if(v[i].first == 1 && (i - 1 >= 0 || i + 1 < v.size())) + res = max(res, v[i].second + 1); + + for(int i = 0; i < v.size(); i ++) + if(v[i].first == 0 && v[i].second == 1){ + int t = 1; + if(i - 1 >= 0) t += v[i - 1].second; + if(i + 1 < v.size()) t += v[i + 1].second; + res = max(res, t); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index c83c59ff..64cbb273 100644 --- a/readme.md +++ b/readme.md @@ -518,6 +518,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 484 | [Find Permutation](https://leetcode.com/problems/find-permutation/) | [solution](https://leetcode.com/problems/find-permutation/solution/) | [C++](0001-0500/0484-Find-Permutation/cpp-0484/) | | | | 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/description/) | [无] | [C++](0001-0500/0485-Max-Consecutive-Ones/cpp-0485/) | | | | | | | | | | +| 487 | [Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/) | [无] | [C++](0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/) | | | | 488 | [Zuma Game](https://leetcode.com/problems/zuma-game/) | [无] | [C++](0001-0500/0488-Zuma-Game/cpp-0488/) | | | | 489 | [Robot Room Cleaner](https://leetcode.com/problems/robot-room-cleaner/) | [solution](https://leetcode.com/problems/robot-room-cleaner/solution/) | [C++](0001-0500/0489-Robot-Room-Cleaner/cpp-0489/) | | | | 490 | [The Maze](https://leetcode.com/problems/the-maze/description/) | [solution](https://leetcode.com/problems/the-maze/solution/) | [C++](0001-0500/0490-The-Maze/cpp-0490/) | | | From 297a39c7fcbc560a8dd12b8aeda68e51ef04ed7c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Oct 2022 00:46:05 -0700 Subject: [PATCH 227/390] 2455-2458 solved. --- .../cpp-2455/CMakeLists.txt | 6 ++ .../cpp-2455/main.cpp | 29 ++++++ .../cpp-2456/CMakeLists.txt | 6 ++ .../cpp-2456/main.cpp | 55 +++++++++++ .../cpp-2457/CMakeLists.txt | 6 ++ .../cpp-2457/main.cpp | 62 ++++++++++++ .../cpp-2458/CMakeLists.txt | 6 ++ .../cpp-2458/main.cpp | 95 +++++++++++++++++++ readme.md | 5 + 9 files changed, 270 insertions(+) create mode 100644 2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/CMakeLists.txt create mode 100644 2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/main.cpp create mode 100644 2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/CMakeLists.txt create mode 100644 2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/main.cpp create mode 100644 2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/CMakeLists.txt create mode 100644 2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/main.cpp create mode 100644 2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/CMakeLists.txt create mode 100644 2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/main.cpp diff --git a/2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/CMakeLists.txt b/2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/main.cpp b/2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/main.cpp new file mode 100644 index 00000000..143421ad --- /dev/null +++ b/2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/ +/// Author : liuyubobobo +/// Time : 2022-10-29 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int averageValue(vector& nums) { + + int total = 0, cnt = 0; + for(int e: nums) + if(e % 6 == 0) total += e, cnt ++; + return cnt == 0 ? 0 : total / cnt; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/CMakeLists.txt b/2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/main.cpp b/2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/main.cpp new file mode 100644 index 00000000..31dcac60 --- /dev/null +++ b/2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/most-popular-video-creator/ +/// Author : liuyubobobo +/// Time : 2022-10-29 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector> mostPopularCreator(vector& creators, vector& ids, vector& views) { + + int n = creators.size(); + + map> best_video; + map total_views; + long long most_view = 0ll; + for(int i = 0; i < n; i ++){ + string creator = creators[i], id = ids[i]; + long long view = views[i]; + + total_views[creator] += view; + most_view = max(most_view, total_views[creator]); + + auto iter = best_video.find(creator); + if(iter == best_video.end()){ + best_video[creator] = {view, id}; + continue; + } + + if(view > iter->second.first || (view == iter->second.first && id < iter->second.second)) + best_video[creator] = {view, id}; + } + + vector> res; + for(const pair& p: total_views) + if(p.second == most_view){ + string creater = p.first; + res.push_back({creater, best_video[creater].second}); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/CMakeLists.txt b/2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/main.cpp b/2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/main.cpp new file mode 100644 index 00000000..7f125912 --- /dev/null +++ b/2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/ +/// Author : liuyubobobo +/// Time : 2022-10-29 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O((logn)^2) +/// Space Complexity: O(logn) +class Solution { +public: + long long makeIntegerBeautiful(long long n, int target) { + + vector pow10(18, 1ll); + for(int i = 1; i < 18; i ++) + pow10[i] = pow10[i - 1] * 10ll; + + long long res = 0; + while(true){ + string s = to_string(n); + int len = s.size(); + + if(digit_sum(len, s) <= target) break; + + int first_non_zero = len - 1; + for(first_non_zero = len - 1; first_non_zero >= 0; first_non_zero --) + if(s[first_non_zero] != '0') break; + + long long d = (10 - (s[first_non_zero] - '0')) * pow10[len - first_non_zero - 1]; + res += d; + n += d; + } + return res; + } + +private: + int digit_sum(int n, const string& s){ + int res = 0; + for(char d: s) + res += (d - '0'); + return res; + } +}; + + +int main() { + + cout << Solution().makeIntegerBeautiful(16, 6) << '\n'; + // 4 + + cout << Solution().makeIntegerBeautiful(467, 6) << '\n'; + // 33 + + cout << Solution().makeIntegerBeautiful(1, 1) << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/CMakeLists.txt b/2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/main.cpp b/2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/main.cpp new file mode 100644 index 00000000..88237b0e --- /dev/null +++ b/2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/main.cpp @@ -0,0 +1,95 @@ +/// Source : https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/ +/// Author : liuyubobobo +/// Time : 2022-10-29 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n + q) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector treeQueries(TreeNode* root, vector& queries) { + + int n = dfs_sz(root); + + vector depth(n + 1, -1), subtree_depth(n + 1, -1); + dfs_depth(root, 0, depth, subtree_depth); + + vector res_table(n + 1, 0); + dfs_res_table(root, depth, subtree_depth, 0, res_table); + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i ++){ + int q = queries[i]; + res[i] = res_table[q]; + } + return res; + } + +private: + void dfs_res_table(TreeNode* node, const vector& depth, const vector& subtree_depth, + int pre_max, vector& res_table){ + + if(!node) return; + res_table[node->val] = pre_max; + + int right_max_depth = depth[node->val] + (node->right ? subtree_depth[node->right->val] : 0); + dfs_res_table(node->left, depth, subtree_depth, max(pre_max, right_max_depth), res_table); + + int left_max_depth = depth[node->val] + (node->left ? subtree_depth[node->left->val] : 0); + dfs_res_table(node->right, depth, subtree_depth, max(pre_max, left_max_depth), res_table); + } + + void dfs_depth(TreeNode* node, int d, vector& depth, vector& subtree_depth){ + + if(!node) return; + + depth[node->val] = d; + + dfs_depth(node->left, d + 1, depth, subtree_depth); + dfs_depth(node->right, d + 1, depth, subtree_depth); + + int t = 0; + if(node->left) t = max(t, subtree_depth[node->left->val]); + if(node->right) t = max(t, subtree_depth[node->right->val]); + subtree_depth[node->val] = 1 + t; + } + + int dfs_sz(TreeNode* node){ + + if(!node) return 0; + return 1 + dfs_sz(node->left) + dfs_sz(node->right); + } +}; + + +int main() { + + TreeNode* root1 = new TreeNode(1); + root1->left = new TreeNode(3, new TreeNode(2), nullptr); + root1->right = new TreeNode(4, new TreeNode(6), new TreeNode(5, nullptr, new TreeNode(7))); + + vector queries1 = {4}; + vector res1 = Solution().treeQueries(root1, queries1); + for(int e: res1) cout << e << ' '; cout << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 64cbb273..e0c4bc62 100644 --- a/readme.md +++ b/readme.md @@ -2319,6 +2319,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2448 | [Minimum Cost to Make Array Equal](https://leetcode.com/problems/minimum-cost-to-make-array-equal/) | [无] | [C++](2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/) | | | | 2449 | [Minimum Number of Operations to Make Arrays Similar](https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/) | [无] | [C++](2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/) | | | | | | | | | | +| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [无] | [C++](2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/) | | | +| 2456 | [Most Popular Video Creator](https://leetcode.com/problems/most-popular-video-creator/) | [无] | [C++](2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/) | | | +| 2457 | [Minimum Addition to Make Integer Beautiful](https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/) | [无] | [C++](2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/) | | | +| 2458 | [Height of Binary Tree After Subtree Removal Queries](https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/) | [无] | [C++](2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 2a057722002b47b48febec3fd01448647aeb0153 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Oct 2022 14:53:27 -0700 Subject: [PATCH 228/390] 0481 solved. --- .../cpp-0481/CMakeLists.txt | 6 ++++ .../0481-Magical-String/cpp-0481/main.cpp | 30 +++++++++++++++++++ readme.md | 6 +++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0481-Magical-String/cpp-0481/CMakeLists.txt create mode 100644 0001-0500/0481-Magical-String/cpp-0481/main.cpp diff --git a/0001-0500/0481-Magical-String/cpp-0481/CMakeLists.txt b/0001-0500/0481-Magical-String/cpp-0481/CMakeLists.txt new file mode 100644 index 00000000..e3158934 --- /dev/null +++ b/0001-0500/0481-Magical-String/cpp-0481/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0481) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0481 main.cpp) diff --git a/0001-0500/0481-Magical-String/cpp-0481/main.cpp b/0001-0500/0481-Magical-String/cpp-0481/main.cpp new file mode 100644 index 00000000..cb016d45 --- /dev/null +++ b/0001-0500/0481-Magical-String/cpp-0481/main.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + + +/// Simulation +/// Time Complex +class Solution { +public: + int magicalString(int n) { + + string s = "122"; + int index = 2; + while(s.size() < n){ + s += string(s[index] - '0', s.back() == '1' ? '2' : '1'); + index ++; + } + + int res = 0; + for(int i = 0; i < n; i ++) + res += s[i] == '1'; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index e0c4bc62..0fae3946 100644 --- a/readme.md +++ b/readme.md @@ -512,7 +512,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 478 | [Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/description/) | [solution](https://leetcode.com/problems/generate-random-point-in-a-circle/solution/) | [C++](0001-0500/0478-Generate-Random-Point-in-a-Circle/cpp-0478/) | | | | 479 | [Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/) | [无] | [C++](0001-0500/0479-Largest-Palindrome-Product/cpp-0479/) | | | | 480 | [Sliding Window Median](https://leetcode.com/problems/sliding-window-median/) | [无]
[缺:双数据结构] | [C++](0001-0500/0480-Sliding-Window-Median/cpp-0480/) | | | -| | | | | | | +| 481 | [Magical String](https://leetcode.com/problems/magical-string/) | [无] | [C++](0001-0500/0481-Magical-String/cpp-0481/) | | | | 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [无] | [C++](0001-0500/0482-License-Key-Formatting/cpp-0482/) | | | | 483 | [Smallest Good Base](https://leetcode.com/problems/smallest-good-base/) | [无] | [C++](0001-0500/0483-Smallest-Good-Base/cpp-0483/) | | | | 484 | [Find Permutation](https://leetcode.com/problems/find-permutation/) | [solution](https://leetcode.com/problems/find-permutation/solution/) | [C++](0001-0500/0484-Find-Permutation/cpp-0484/) | | | @@ -2319,6 +2319,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2448 | [Minimum Cost to Make Array Equal](https://leetcode.com/problems/minimum-cost-to-make-array-equal/) | [无] | [C++](2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/) | | | | 2449 | [Minimum Number of Operations to Make Arrays Similar](https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/) | [无] | [C++](2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/) | | | | | | | | | | +| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference/) | [无] | [C++](2001-2500/2451-Odd-String-Difference/cpp-2451/) | | | +| 2452 | [Words Within Two Edits of Dictionary](https://leetcode.com/problems/words-within-two-edits-of-dictionary/) | [无] | [C++](2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/) | | | +| | | | | | | +| | | | | | | | 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [无] | [C++](2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/) | | | | 2456 | [Most Popular Video Creator](https://leetcode.com/problems/most-popular-video-creator/) | [无] | [C++](2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/) | | | | 2457 | [Minimum Addition to Make Integer Beautiful](https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/) | [无] | [C++](2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/) | | | From b9ad376fd01c831b34d984b6f075a6bd0a4bd7f9 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Oct 2022 15:07:20 -0700 Subject: [PATCH 229/390] 2451-2453 solved. --- .../0481-Magical-String/cpp-0481/main.cpp | 7 ++- .../cpp-2451/CMakeLists.txt | 6 +++ .../cpp-2451/main.cpp | 38 +++++++++++++++ .../cpp-2452/CMakeLists.txt | 6 +++ .../cpp-2452/main.cpp | 46 ++++++++++++++++++ .../cpp-2453/CMakeLists.txt | 6 +++ .../cpp-2453/main.cpp | 48 +++++++++++++++++++ 7 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2451-Odd-String-Difference/cpp-2451/CMakeLists.txt create mode 100644 2001-2500/2451-Odd-String-Difference/cpp-2451/main.cpp create mode 100644 2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/CMakeLists.txt create mode 100644 2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/main.cpp create mode 100644 2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/CMakeLists.txt create mode 100644 2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/main.cpp diff --git a/0001-0500/0481-Magical-String/cpp-0481/main.cpp b/0001-0500/0481-Magical-String/cpp-0481/main.cpp index cb016d45..a173e7aa 100644 --- a/0001-0500/0481-Magical-String/cpp-0481/main.cpp +++ b/0001-0500/0481-Magical-String/cpp-0481/main.cpp @@ -1,10 +1,15 @@ +/// Source : https://leetcode.com/problems/magical-string/ +/// Author : liuyubobobo +/// Time : 2022-10-30 + #include using namespace std; /// Simulation -/// Time Complex +/// Time Complexity: O(n) +/// Space Complexity: O(n) class Solution { public: int magicalString(int n) { diff --git a/2001-2500/2451-Odd-String-Difference/cpp-2451/CMakeLists.txt b/2001-2500/2451-Odd-String-Difference/cpp-2451/CMakeLists.txt new file mode 100644 index 00000000..fd13fd77 --- /dev/null +++ b/2001-2500/2451-Odd-String-Difference/cpp-2451/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2451) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2451 main.cpp) diff --git a/2001-2500/2451-Odd-String-Difference/cpp-2451/main.cpp b/2001-2500/2451-Odd-String-Difference/cpp-2451/main.cpp new file mode 100644 index 00000000..c3a8ac8e --- /dev/null +++ b/2001-2500/2451-Odd-String-Difference/cpp-2451/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/odd-string-difference/ +/// Author : liuyubobobo +/// Time : 2022-10-30 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(|words| * len) +/// Space Compelxity: O(|words| * len) +class Solution { +public: + string oddString(vector& words) { + + map, vector> diff2str; + for(const string& word: words){ + int n = word.size(); + vector d(n - 1, 0); + for(int i = 1; i < n; i ++) + d[i - 1] = word[i] - word[i - 1]; + diff2str[d].push_back(word); + } + + for(const auto& p: diff2str) + if(p.second.size() == 1) return p.second[0]; + return ""; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/CMakeLists.txt b/2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/CMakeLists.txt new file mode 100644 index 00000000..cbb0a308 --- /dev/null +++ b/2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2452) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2452 main.cpp) diff --git a/2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/main.cpp b/2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/main.cpp new file mode 100644 index 00000000..a4f14a86 --- /dev/null +++ b/2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/words-within-two-edits-of-dictionary/ +/// Author : liuyubobobo +/// Time : 2022-10-30 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(|queries| * |dict| * word_len) +/// Space Complexity: O(1) +class Solution { +public: + vector twoEditWords(vector& queries, vector& dictionary) { + + vector res; + for(const string& word: queries){ + if(ok(word, dictionary)) res.push_back(word); + } + return res; + } + +private: + bool ok(const string& word, const vector& dict){ + + for(const string& s: dict) + if(diff(word, s) <= 2) return true; + return false; + } + + int diff(const string& s1, const string& s2){ + + int n = s1.size(), res = 0; + for(int i = 0; i < n; i ++) + res += s1[i] != s2[i]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/CMakeLists.txt b/2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/CMakeLists.txt new file mode 100644 index 00000000..b18366d3 --- /dev/null +++ b/2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2453) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2453 main.cpp) diff --git a/2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/main.cpp b/2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/main.cpp new file mode 100644 index 00000000..c60dedd2 --- /dev/null +++ b/2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/destroy-sequential-targets/ +/// Author : liuyubobobo +/// Time : 2022-10-30 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int destroyTargets(vector& nums, int space) { + + map f; + map min_res; + for(int e: nums){ + f[e % space] ++; + + if(min_res.count(e % space)) min_res[e % space] = min(min_res[e % space], e); + else min_res[e % space] = e; + } + + int maxf = 0; + for(const pair& p: f) + maxf = max(maxf, p.second); + + int res = INT_MAX; + for(const pair& p: f) + if(p.second == maxf) + res = min(res, min_res[p.first]); + + return res; + } +}; + + +int main() { + + vector nums1 = {3,7,8,1,1,5}; + cout << Solution().destroyTargets(nums1, 2) << '\n'; + + return 0; +} From 6bb0e3da35d482cefe71171a837dfc2a0185769a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Oct 2022 15:31:32 -0700 Subject: [PATCH 230/390] 2454 solved. --- .../cpp-2454/CMakeLists.txt | 6 ++ .../cpp-2454/main.cpp | 86 +++++++++++++++++++ readme.md | 4 +- 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 2001-2500/2454-Next-Greater-Element-IV/cpp-2454/CMakeLists.txt create mode 100644 2001-2500/2454-Next-Greater-Element-IV/cpp-2454/main.cpp diff --git a/2001-2500/2454-Next-Greater-Element-IV/cpp-2454/CMakeLists.txt b/2001-2500/2454-Next-Greater-Element-IV/cpp-2454/CMakeLists.txt new file mode 100644 index 00000000..f2f8c98e --- /dev/null +++ b/2001-2500/2454-Next-Greater-Element-IV/cpp-2454/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2454) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2454 main.cpp) diff --git a/2001-2500/2454-Next-Greater-Element-IV/cpp-2454/main.cpp b/2001-2500/2454-Next-Greater-Element-IV/cpp-2454/main.cpp new file mode 100644 index 00000000..3ec32836 --- /dev/null +++ b/2001-2500/2454-Next-Greater-Element-IV/cpp-2454/main.cpp @@ -0,0 +1,86 @@ +/// Source : https://leetcode.com/problems/next-greater-element-iv/ +/// Author : liuyubobobo +/// Time : 2022-10-30 + +#include +#include + +using namespace std; + + +/// Mono Stack + ST + Binary Search +/// Time Complexity: O(n(logn)^2) +/// Space Complexity: O(nlogn) +template +class SparseTable{ + +private: + int n; + vector> table; + vector log2; + T (*combine)(T a, T b); + +public: + SparseTable(const vector& data, T (*combine)(T a, T b)): n(data.size()), log2(n + 1, 1){ + + this->combine = combine; + + int len = 2, k = 1; + for(int i = 1; i <= n; i ++){ + if(i >= len) len <<= 1, k ++; + log2[i] = k; + } + + int K = log2[n]; + table = vector>(K, vector(n)); + + for(int i = 0; i < n; i ++) + table[0][i] = data[i]; + + for(int k = 1; k < K; k ++) + for(int i = 0; i + (1 << (k - 1)) < n; i ++) + table[k][i] = combine(table[k - 1][i], table[k - 1][i + (1 << (k - 1))]); + } + + T query(int l, int r){ + + int k = log2[r - l + 1]; + return combine(table[k - 1][l], table[k - 1][r + 1 - (1 << (k - 1))]); + } +}; + +class Solution { +public: + vector secondGreaterElement(vector& nums) { + + int n = nums.size(); + vector next(n, -1), s; + for(int i = 0; i < n; i ++){ + while(!s.empty() && nums[i] > nums[s.back()]) + next[s.back()] = i, s.pop_back(); + s.push_back(i); + } + + SparseTable st(nums, [](int a, int b){return max(a, b);}); + vector res(n, -1); + for(int i = 0; i < n; i ++){ + int a = next[i]; + if(a == -1) continue; + + int l = a + 1, r = n; + while(l < r){ + int mid = (l + r) / 2; + if(st.query(a + 1, mid) > nums[i]) r = mid; + else l = mid + 1; + } + res[i] = l == n ? -1 : nums[l]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 0fae3946..10da990f 100644 --- a/readme.md +++ b/readme.md @@ -2321,8 +2321,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference/) | [无] | [C++](2001-2500/2451-Odd-String-Difference/cpp-2451/) | | | | 2452 | [Words Within Two Edits of Dictionary](https://leetcode.com/problems/words-within-two-edits-of-dictionary/) | [无] | [C++](2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/) | | | -| | | | | | | -| | | | | | | +| 2453 | [Destroy Sequential Targets](https://leetcode.com/problems/destroy-sequential-targets/) | [无] | [C++](2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/) | | | +| 2454 | [Next Greater Element IV](https://leetcode.com/problems/next-greater-element-iv/) | [无]
[缺:O(n) 解法] | [C++](2001-2500/2454-Next-Greater-Element-IV/cpp-2454/) | | | | 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [无] | [C++](2001-2500/2455-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three/cpp-2455/) | | | | 2456 | [Most Popular Video Creator](https://leetcode.com/problems/most-popular-video-creator/) | [无] | [C++](2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/) | | | | 2457 | [Minimum Addition to Make Integer Beautiful](https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/) | [无] | [C++](2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/) | | | From 1ea6c081ebf6f8c54ffd8e7da0ff3c55db85f4a0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Oct 2022 15:46:50 -0700 Subject: [PATCH 231/390] 2450 solved. --- .../cpp-2450/CMakeLists.txt | 6 +++ .../cpp-2450/main.cpp | 37 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/CMakeLists.txt create mode 100644 2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/main.cpp diff --git a/2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/CMakeLists.txt b/2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/CMakeLists.txt new file mode 100644 index 00000000..9c060e2f --- /dev/null +++ b/2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2450) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2450 main.cpp) diff --git a/2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/main.cpp b/2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/main.cpp new file mode 100644 index 00000000..2bd8fcc1 --- /dev/null +++ b/2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/number-of-distinct-binary-strings-after-applying-operations/ +/// Author : liuyubobobo +/// Time : 2022-10-30 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(log(n - k + 1)) +/// Space Complexity: O(log(n - k + 1)) +class Solution { +public: + int countDistinctStrings(string s, int k) { + + int n = s.size(); + return quick_pow(2ll, n - k + 1, 1e9 + 7ll); + } + +private: + long long quick_pow(long long a, long long k, long long MOD) { + long long res = 1ll; + while (k) { + if (k & 1) res = res * a % MOD; + a = a * a % MOD; + k >>= 1; + } + return res % MOD; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 10da990f..6797bb07 100644 --- a/readme.md +++ b/readme.md @@ -2318,7 +2318,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2447 | [Number of Subarrays With GCD Equal to K](https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/) | [无] | [C++](2001-2500/2447-Number-of-Subarrays-With-GCD-Equal-to-K/cpp-2447/) | | | | 2448 | [Minimum Cost to Make Array Equal](https://leetcode.com/problems/minimum-cost-to-make-array-equal/) | [无] | [C++](2001-2500/2448-Minimum-Cost-to-Make-Array-Equal/cpp-2448/) | | | | 2449 | [Minimum Number of Operations to Make Arrays Similar](https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/) | [无] | [C++](2001-2500/2449-Minimum-Number-of-Operations-to-Make-Arrays-Similar/cpp-2449/) | | | -| | | | | | | +| 2450 | [Number of Distinct Binary Strings After Applying Operations](https://leetcode.com/problems/number-of-distinct-binary-strings-after-applying-operations/) | [无] | [C++](2001-2500/2450-Number-of-Distinct-Binary-Strings-After-Applying-Operations/cpp-2450/) | | | | 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference/) | [无] | [C++](2001-2500/2451-Odd-String-Difference/cpp-2451/) | | | | 2452 | [Words Within Two Edits of Dictionary](https://leetcode.com/problems/words-within-two-edits-of-dictionary/) | [无] | [C++](2001-2500/2452-Words-Within-Two-Edits-of-Dictionary/cpp-2452/) | | | | 2453 | [Destroy Sequential Targets](https://leetcode.com/problems/destroy-sequential-targets/) | [无] | [C++](2001-2500/2453-Destroy-Sequential-Targets/cpp-2453/) | | | From f26458042ff0bbb081d85243367f00333471eae8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 1 Nov 2022 23:12:35 -0700 Subject: [PATCH 232/390] 1620 bug fixed. --- .../cpp-1620/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/1501-2000/1620-Coordinate-With-Maximum-Network-Quality/cpp-1620/main.cpp b/1501-2000/1620-Coordinate-With-Maximum-Network-Quality/cpp-1620/main.cpp index 71cef4cc..31a4c336 100644 --- a/1501-2000/1620-Coordinate-With-Maximum-Network-Quality/cpp-1620/main.cpp +++ b/1501-2000/1620-Coordinate-With-Maximum-Network-Quality/cpp-1620/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/coordinate-with-maximum-network-quality/ /// Author : liuyubobobo /// Time : 2020-10-18 +/// Updated: 2022-11-01 #include #include @@ -30,7 +31,7 @@ class Solution { int curq = getq(towers, x, y, radius); if(curq > maxq) res = {x, y}, maxq = curq; } - return res; + return maxq == 0 ? vector(2, 0) : res; } private: From 940c8c73a3452ee7cf69bee200f93377fc9b48f6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 5 Nov 2022 00:36:14 -0700 Subject: [PATCH 233/390] 0212 codes updated. --- .../0212-Word-Search-II/cpp-0212/main.cpp | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/0001-0500/0212-Word-Search-II/cpp-0212/main.cpp b/0001-0500/0212-Word-Search-II/cpp-0212/main.cpp index 532fbf01..684b8b1c 100644 --- a/0001-0500/0212-Word-Search-II/cpp-0212/main.cpp +++ b/0001-0500/0212-Word-Search-II/cpp-0212/main.cpp @@ -1,16 +1,18 @@ /// Source : https://leetcode.com/problems/word-search-ii/ /// Author : liuyubobobo /// Time : 2019-02-08 -/// Updated: 2021-09-15 +/// Updated: 2022-11-05 #include #include #include +#include using namespace std; /// Trie + DFS +/// Shrink the Trie while searching is a big optimization /// Time Complexity: O(4 ^ (m * n) * maxlen) /// Space Complexity: O(m * n + total_letters_in_words) class Solution { @@ -19,29 +21,26 @@ class Solution { class Node{ public: vector next; - bool end = false; + int sz = 0; + bool end = false, found = false; Node() : next(26, nullptr){}; }; Node* root = nullptr; const int d[4][2] = {{-1, 0}, {0,1}, {1, 0}, {0, -1}}; - int R, C, maxlen = 0; + int R, C; public: vector findWords(vector>& board, vector& words) { root = new Node; - for(const string& word: words){ + for(const string& word: words) insert(word); - maxlen = max(maxlen, (int)word.size()); - } - set res; + R = board.size(), C = board[0].size(); + vector res; - R = board.size(); - assert(R > 0); - C = board[0].size(); for(int i = 0 ; i < R ; i ++) for(int j = 0 ; j < C ; j ++){ vector> visited(R, vector(C, false)); @@ -49,22 +48,24 @@ class Solution { string s = ""; searchWord(board, i, j, cur, s, visited, res); } - - return vector(res.begin(), res.end()); + return res; } private: // start from board[x][y], find word s void searchWord(const vector> &board, int x, int y, Node* cur, string& s, - vector>& visited, set& res){ + vector>& visited, vector& res){ if(cur->next[board[x][y] - 'a'] == nullptr) return; s += board[x][y]; - if(s.size() > maxlen) return; + Node* parent = cur; cur = cur->next[board[x][y] - 'a']; - if(cur->end) res.insert(s); + if(cur->end && !cur->found){ + res.push_back(s); + cur->found = true; + } visited[x][y] = true; for(int i = 0 ; i < 4 ; i ++){ @@ -75,6 +76,11 @@ class Solution { } visited[x][y] = false; s.pop_back(); + + if(cur->sz == 0){ + parent->next[board[x][y] - 'a'] = nullptr; + parent->sz --; + } } void insert(const string& word){ @@ -82,7 +88,7 @@ class Solution { Node* cur = root; for(char c: word){ if(cur->next[c - 'a'] == nullptr) - cur->next[c - 'a'] = new Node(); + cur->next[c - 'a'] = new Node(), cur->sz ++; cur = cur->next[c - 'a']; } cur->end = true; From 884ef1134b126d4d2a186a5a7ae1976adb9a04ea Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 5 Nov 2022 23:48:15 -0700 Subject: [PATCH 234/390] 2460-2463 solved. --- .../cpp-2460/CMakeLists.txt | 6 ++ .../cpp-2460/main.cpp | 33 +++++++++++ .../cpp-2461/CMakeLists.txt | 6 ++ .../cpp-2461/main.cpp | 46 +++++++++++++++ .../cpp-2462/CMakeLists.txt | 6 ++ .../cpp-2462/main.cpp | 59 +++++++++++++++++++ .../cpp-2463/CMakeLists.txt | 6 ++ .../cpp-2463/main.cpp | 52 ++++++++++++++++ readme.md | 5 ++ 9 files changed, 219 insertions(+) create mode 100644 2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/CMakeLists.txt create mode 100644 2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/main.cpp create mode 100644 2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/CMakeLists.txt create mode 100644 2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/main.cpp create mode 100644 2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/CMakeLists.txt create mode 100644 2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/main.cpp create mode 100644 2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/CMakeLists.txt create mode 100644 2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/main.cpp diff --git a/2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/CMakeLists.txt b/2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/main.cpp b/2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/main.cpp new file mode 100644 index 00000000..8ee443ab --- /dev/null +++ b/2001-2500/2460-Apply-Operations-o an-Array/cpp-2460/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/apply-operations-to-an-array/ +/// Author : liuyubobobo +/// Time : 2022-11-05 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector applyOperations(vector& nums) { + + int n = nums.size(); + for(int i = 0; i + 1 < n; i ++) + if(nums[i] == nums[i + 1]) nums[i] *= 2, nums[i + 1] = 0; + + vector res(n, 0); + for(int i = 0, k = 0; i < n; i ++) + if(nums[i]) res[k ++] = nums[i]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/CMakeLists.txt b/2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/main.cpp b/2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/main.cpp new file mode 100644 index 00000000..aebd87fd --- /dev/null +++ b/2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/description/ +/// Author : liuyubobobo +/// Time : 2022-11-05 + +#include +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(max_nums) +class Solution { +public: + long long maximumSubarraySum(vector& nums, int k) { + + int n = nums.size(); + vector f(1e5 + 1); + int not_unique = 0; + long long sum = 0, res = 0; + + for(int i = 0; i < n; i ++){ + sum += nums[i]; + f[nums[i]] ++; + if(f[nums[i]] == 2) not_unique ++; + + if(i >= k - 1){ + if(not_unique == 0) res = max(res, sum); + + sum -= nums[i - (k - 1)]; + f[nums[i - (k - 1)]] --; + if(f[nums[i - (k - 1)]] == 1) not_unique --; + } + } + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/CMakeLists.txt b/2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/main.cpp b/2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/main.cpp new file mode 100644 index 00000000..114831d3 --- /dev/null +++ b/2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/total-cost-to-hire-k-workers/description/ +/// Author : liuyubobobo +/// Time : 2022-11-05 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Using MinHeap +/// Time Complexity: O((candidates + k) * log(candidates)) +/// Space Complexity: O(candidates) +class Solution { +public: + long long totalCost(vector& costs, int k, int candidates) { + + int n = costs.size(); + + priority_queue, greater<>> front_pq, tail_pq; + for(int i = 0; i < candidates; i ++) front_pq.push(costs[i]); + for(int i = n - 1; i >= n - candidates && i >= candidates; i --) tail_pq.push(costs[i]); + + int front = candidates, tail = n - candidates - 1; + long long res = 0; + for(int i = 0; i < k; i ++){ + int front_min = front_pq.empty() ? INT_MAX : front_pq.top(); + int tail_min = tail_pq.empty() ? INT_MAX : tail_pq.top(); + if(front_min <= tail_min){ + res += front_min; + front_pq.pop(); + if(front <= tail) front_pq.push(costs[front ++]); + } + else{ + res += tail_min; + tail_pq.pop(); + if(front <= tail) tail_pq.push(costs[tail --]); + } + } + return res; + } +}; + + +int main() { + + vector costs1 = {17,12,10,2,7,2,11,20,8}; + cout << Solution().totalCost(costs1, 3, 4) << '\n'; + // 11 + + vector costs2 = {57,33,26,76,14,67,24,90,72,37,30}; + cout << Solution().totalCost(costs2, 11, 2) << '\n'; + // 526 + + return 0; +} diff --git a/2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/CMakeLists.txt b/2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/main.cpp b/2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/main.cpp new file mode 100644 index 00000000..3669388b --- /dev/null +++ b/2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/minimum-total-distance-traveled/ +/// Author : liuyubobobo +/// Time : 2022-11-05 + +#include +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(|robot| * |factory| * |max_limit|) +/// Space Complexity: O(|robot| * |factory|) +class Solution { +public: + long long minimumTotalDistance(vector& robot, vector>& factory) { + + sort(robot.begin(), robot.end()); + sort(factory.begin(), factory.end()); + + int rn = robot.size(), fn = factory.size(); + vector> dp(rn, vector(fn, -1)); + return dfs(rn, robot, fn, factory, 0, 0, dp); + } + +private: + long long dfs(int rn, const vector& robot, int fn, const vector>& factory, + int rindex, int findex, vector>& dp){ + + if(rindex == rn) return 0; + if(findex == fn) return LONG_LONG_MAX / 2; + if(dp[rindex][findex] != -1) return dp[rindex][findex]; + + long long res = dfs(rn, robot, fn, factory, rindex, findex + 1, dp); + + int fpos = factory[findex][0], limit = factory[findex][1]; + long long cur = 0; + for(int i = 0; i < limit && rindex + i < rn; i ++){ + cur += abs(fpos - robot[rindex + i]); + res = min(res, cur + dfs(rn, robot, fn, factory, rindex + i + 1, findex + 1, dp)); + } + return dp[rindex][findex] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 6797bb07..36bc217b 100644 --- a/readme.md +++ b/readme.md @@ -2328,6 +2328,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2457 | [Minimum Addition to Make Integer Beautiful](https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/) | [无] | [C++](2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/) | | | | 2458 | [Height of Binary Tree After Subtree Removal Queries](https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/) | [无] | [C++](2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/) | | | | | | | | | | +| 2460 | [Apply Operations to an Array](https://leetcode.com/problems/apply-operations-to-an-array/) | [无] | [C++](2001-2500/2460-Apply-Operations-to-an-Array/cpp-2460/) | | | +| 2461 | [Maximum Sum of Distinct Subarrays With Length K](https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/) | [无] | [C++](2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/) | | | +| 2462 | [Total Cost to Hire K Workers](https://leetcode.com/problems/total-cost-to-hire-k-workers/) | [无] | [C++](2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/) | | | +| 2463 | [Minimum Total Distance Traveled](https://leetcode.com/problems/minimum-total-distance-traveled/) | [无] | [C++](2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 1a571ce3bcf345a830eee0de05994422862ab9c4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 7 Nov 2022 03:34:31 -0800 Subject: [PATCH 235/390] 1323 solved. --- .../cpp-1323/CMakeLists.txt | 6 ++++ .../1323-Maximum-69-Number/cpp-1323/main.cpp | 31 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 39 insertions(+) create mode 100644 1001-1500/1323-Maximum-69-Number/cpp-1323/CMakeLists.txt create mode 100644 1001-1500/1323-Maximum-69-Number/cpp-1323/main.cpp diff --git a/1001-1500/1323-Maximum-69-Number/cpp-1323/CMakeLists.txt b/1001-1500/1323-Maximum-69-Number/cpp-1323/CMakeLists.txt new file mode 100644 index 00000000..39d32ac5 --- /dev/null +++ b/1001-1500/1323-Maximum-69-Number/cpp-1323/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_1323) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1323 main.cpp) diff --git a/1001-1500/1323-Maximum-69-Number/cpp-1323/main.cpp b/1001-1500/1323-Maximum-69-Number/cpp-1323/main.cpp new file mode 100644 index 00000000..295d2468 --- /dev/null +++ b/1001-1500/1323-Maximum-69-Number/cpp-1323/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/maximum-69-number/description/ +/// Author : liuyubobobo +/// Time : 2022-11-07 + +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(log(num)) +/// Space Complexity: O(log(num)) +class Solution { +public: + int maximum69Number (int num) { + + string s = to_string(num); + for(char& c: s) + if(c == '6'){ + c = '9'; + break; + } + return atoi(s.c_str()); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 36bc217b..a46b4bf1 100644 --- a/readme.md +++ b/readme.md @@ -1259,6 +1259,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1318 | [Minimum Flips to Make a OR b Equal to c](https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/) | [无] | [C++](1001-1500/1318-Minimum-Flips-to-Make-a-OR-b-Equal-to-c/cpp-1318/) | | | | 1319 | [Number of Operations to Make Network Connected](https://leetcode.com/problems/number-of-operations-to-make-network-connected/) | [无] | [C++](1001-1500/1319-Number-of-Operations-to-Make-Network-Connected/cpp-1319/) | | | | | | | | | | +| 1323 | [Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/description/) | [无] | [C++](1001-1500/1323-Maximum-69-Number/cpp-1323/) | | | +| | | | | | | | 1328 | [Break a Palindrome](https://leetcode.com/problems/break-a-palindrome/) | [solution](https://leetcode.com/problems/break-a-palindrome/solution/) | [C++](1001-1500/1328-Break-a-Palindrome/cpp-1328/) | | | | 1329 | [Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [solution](https://leetcode.com/problems/sort-the-matrix-diagonally/solution/) | [C++](1001-1500/1329-Sort-the-Matrix-Diagonally/cpp-1329/) | | | | | | | | | | From 4075a073b14535ba22737751406837068afe0f4a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 9 Nov 2022 11:08:47 -0800 Subject: [PATCH 236/390] 0864 codes updated. --- .../cpp-0864/main.cpp | 180 +++++------------- 1 file changed, 44 insertions(+), 136 deletions(-) diff --git a/0501-1000/0864-Shortest-Path-to-Get-All-Keys/cpp-0864/main.cpp b/0501-1000/0864-Shortest-Path-to-Get-All-Keys/cpp-0864/main.cpp index bda3c571..d9545fb0 100644 --- a/0501-1000/0864-Shortest-Path-to-Get-All-Keys/cpp-0864/main.cpp +++ b/0501-1000/0864-Shortest-Path-to-Get-All-Keys/cpp-0864/main.cpp @@ -1,135 +1,74 @@ /// Source : https://leetcode.com/problems/shortest-path-to-get-all-keys/description/ /// Author : liuyubobobo /// Time : 2018-07-09 +/// Updated: 2022-11-09 #include #include -#include -#include -#include #include #include using namespace std; -/// Brute Force all Keys Permutation -/// Time Complexity: O(keys!*keys*n^2) -/// Space Complexity:O(keys + n^2) +/// BFS +/// Time Complexity: O(R * C * (2 ^ key_cnt)) +/// Space Complexity:O(R * C * (2 ^ key_cnt)) class Solution { private: - int n, m; - const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + const int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public: int shortestPathAllKeys(vector& grid) { - n = grid.size(); - m = grid[0].size(); + int R = grid.size(), C = grid[0].size(); - unordered_map> poi = getPOI(grid); - - vector keys = getAllKeys(grid); - sort(keys.begin(), keys.end()); - - int res = INT_MAX; - do{ - res = min(res, go(grid, keys, poi, res)); - }while(next_permutation(keys.begin(), keys.end())); + int sx = -1, sy = -1, key_cnt = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(grid[i][j] == '@'){ + sx = i, sy = j; + } + else if(islower(grid[i][j])){ + key_cnt ++; + } + } + assert(sx != -1 && sy != -1); - if(res == INT_MAX) - return -1; - return res; - } + vector> dis(R * C, vector((1 << key_cnt), -1)); + dis[sx * C + sy][0] = 0; + queue> q; + q.push({sx * C + sy, 0}); + while(!q.empty()){ + int cpos = q.front().first, key_state = q.front().second; + int cx = cpos / C, cy = cpos % C; + q.pop(); -private: - int go(const vector& grid, const vector& keys, - unordered_map>& poi, int curMinRes){ - - vector> pos = {poi['@']}; - for(char key: keys) - pos.push_back(poi[key]); - - int res = 0; - unordered_set ownKeys; - for(int i = 1; i < pos.size() ; i ++){ - int need = go(grid, pos[i-1], pos[i], ownKeys); - if(need == -1) - return INT_MAX; - - res += need; - if(res >= curMinRes) - return INT_MAX; - ownKeys.insert(keys[i-1]); - } - return res; - } + if(key_state == (1 << key_cnt) - 1) return dis[cpos][key_state]; - int go(const vector& grid, - const pair& start, const pair& end, - const unordered_set& ownKeys){ + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(!in_area(R, C, nx, ny) || grid[nx][ny] == '#') continue; - queue, int>> q; - q.push(make_pair(start, 0)); + if(isupper(grid[nx][ny]) && ((1 << (grid[nx][ny] - 'A')) & key_state) == 0) + continue; - unordered_set visited; - visited.insert(start.first * m + start.second); + int npos = nx * C + ny; + int next_key_state = key_state; + if(islower(grid[nx][ny])) next_key_state |= (1 << (grid[nx][ny] - 'a')); - while(!q.empty()){ - pair cur = q.front().first; - int step = q.front().second; - q.pop(); + if(dis[npos][next_key_state] != -1) continue; - for(int i = 0 ; i < 4 ; i ++){ - int nextX = cur.first + d[i][0]; - int nextY = cur.second + d[i][1]; - if(nextX == end.first && nextY == end.second) - return step + 1; - if(inArea(nextX, nextY) && grid[nextX][nextY] != '#' && - visited.find(nextX * m + nextY) == visited.end()){ - - if(!isLock(grid[nextX][nextY]) || - (isLock(grid[nextX][nextY]) && ownKeys.find(grid[nextX][nextY] - 'A' + 'a') != ownKeys.end())){ - q.push(make_pair(make_pair(nextX, nextY), step + 1)); - visited.insert(nextX * m + nextY); - } - } + dis[npos][next_key_state] = dis[cpos][key_state] + 1; + q.push({npos, next_key_state}); } } return -1; } - vector getAllKeys(const vector& grid){ - vector res; - for(int i = 0 ; i < n ; i ++) - for(int j = 0 ; j < m ; j ++) - if(isKey(grid[i][j])) - res.push_back(grid[i][j]); - return res; - } - - unordered_map> getPOI(const vector& grid){ - unordered_map> res; - for(int i = 0 ; i < n ; i ++) - for(int j = 0 ; j < m ; j ++) - if(grid[i][j] != '.' && grid[i][j] != '#'){ - assert(grid[i][j] == '@' || isKey(grid[i][j]) || isLock(grid[i][j])); - assert(res.find(grid[i][j]) == res.end()); - res[grid[i][j]] = make_pair(i, j); - } - return res; - }; - - bool isKey(char c){ - return c >= 'a' && c <= 'z'; - } - - bool isLock(char c){ - return c >= 'A' && c <= 'Z'; - } - - bool inArea(int x, int y){ - return x >= 0 && x < n && y >= 0 && y < m; +private: + bool in_area(int R, int C, int x, int y){ + return x >= 0 && x < R && y >= 0 && y < C; } }; @@ -140,48 +79,17 @@ int main() { "###.#", "b.A.B"}; cout << Solution().shortestPathAllKeys(grid1) << endl; + // 8 vector grid2 = {"@..aA", "..B#.", "....b"}; cout << Solution().shortestPathAllKeys(grid2) << endl; + // 6 - vector grid3 = {"@abcdeABCDEFf"}; + vector grid3 = {"@Aa"}; cout << Solution().shortestPathAllKeys(grid3) << endl; - - vector grid4 = { - "#..#.#.#..#.#.#.....#......#..", - ".#.......#....#A.....#.#......", - "#....#.....#.........#........", - "...#.#.........#..@....#....#.", - ".#.#.##...#.........##....#..#", - "..........#..#..###....##..#.#", - ".......#......#...#...#.....c#", - ".#...#.##......#...#.###...#..", - "..........##...#.......#......", - "#...#.........a#....#.#.##....", - "..#..#...#...#..#....#.....##.", - "..........#...#.##............", - "...#....#..#.........#..D.....", - "....#E.#....##................", - "...........##.#.......#.#....#", - "...#..#...#.#............#e...", - "..#####....#.#...........##..#", - "##......##......#.#...#..#.#..", - ".#F.......#..##.......#....#..", - "............#....#..#..#...#..", - ".............#...#f...#..##...", - "....#..#...##.........#..#..#.", - ".....#.....##.###..##.#......#", - ".#..#.#...#.....#........###..", - ".....#.#...#...#.....#.....#..", - "##.....#....B.....#..#b.......", - ".####....##..#.##..d.#......#.", - "..#.....#....##........##...##", - "...#...#...C..#..#....#.......", - "#.....##.....#.#......#......." - }; - cout << Solution().shortestPathAllKeys(grid4) << endl; + // -1 return 0; } \ No newline at end of file From 65a897d5e0ba7946b29b8c985f9ce9a0e4c2512b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 10 Nov 2022 17:07:11 -0800 Subject: [PATCH 237/390] 2464 solved. --- .../cpp-2464/CMakeLists.txt | 6 +++ .../cpp-2464/main.cpp | 42 +++++++++++++++++++ readme.md | 1 + 3 files changed, 49 insertions(+) create mode 100644 2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/CMakeLists.txt create mode 100644 2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/main.cpp diff --git a/2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/CMakeLists.txt b/2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/CMakeLists.txt new file mode 100644 index 00000000..bf6e4681 --- /dev/null +++ b/2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2464) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2464 main.cpp) diff --git a/2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/main.cpp b/2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/main.cpp new file mode 100644 index 00000000..d0a84df4 --- /dev/null +++ b/2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/minimum-subarrays-in-a-valid-split/description/ +/// Author : liuyubobobo +/// Time : 2022-11-10 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2 * log(max_num)) +/// Space Complexity: O(n) +class Solution { +public: + int validSubarraySplit(vector& nums) { + + int n = nums.size(); + + vector dp(n + 1, INT_MAX / 2); + dp[n] = 0; + for(int i = n - 1; i >= 0; i --){ + for(int j = i; j < n; j ++) + if(gcd(nums[i], nums[j]) > 1) + dp[i] = min(dp[i], 1 + dp[j + 1]); + } + return dp[0] >= INT_MAX / 2 ? -1 : dp[0]; + } + +private: + int gcd(int a, int b){ + if(a > b) swap(a, b); + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a46b4bf1..6c543afc 100644 --- a/readme.md +++ b/readme.md @@ -2334,6 +2334,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2461 | [Maximum Sum of Distinct Subarrays With Length K](https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/) | [无] | [C++](2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/) | | | | 2462 | [Total Cost to Hire K Workers](https://leetcode.com/problems/total-cost-to-hire-k-workers/) | [无] | [C++](2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/) | | | | 2463 | [Minimum Total Distance Traveled](https://leetcode.com/problems/minimum-total-distance-traveled/) | [无] | [C++](2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/) | | | +| 2464 | [Minimum Subarrays in a Valid Split](https://leetcode.com/problems/minimum-subarrays-in-a-valid-split/description/) | [无] | [C++](2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 1520fc0c631dc8c266c8d0d31d1b8039439ddcc9 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 13 Nov 2022 01:29:33 -0800 Subject: [PATCH 238/390] 0211 new algo added. --- .../java-0211/src/WordDictionary.java | 2 +- .../java-0211/src/WordDictionary2.java | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java diff --git a/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary.java b/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary.java index 2e5ee186..41b7d3b0 100644 --- a/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary.java +++ b/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary.java @@ -2,7 +2,7 @@ /// Author : liuyubobobo /// Time : 2019-09-01 -/// Trie +/// Trie + Recursive DFS /// Time Complexity: addWord: O(len(word)) /// search: O(size(trie)) /// Space Complexity: O(sum(len(wi))) where wi is the length of the ith word diff --git a/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java b/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java new file mode 100644 index 00000000..847251d3 --- /dev/null +++ b/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java @@ -0,0 +1,72 @@ +/// Source : https://leetcode.com/problems/add-and-search-word-data-structure-design/ +/// Author : liuyubobobo +/// Time : 2022-11-13 + +import java.util.*; + +/// Trie + Recursive DFS +/// Time Complexity: addWord: O(len(word)) +/// search: O(size(trie)) +/// Space Complexity: O(sum(len(wi))) where wi is the length of the ith word +public class WordDictionary2 { + + private class Node{ + + public boolean isWord = false; + public int index; + public Node next[]; + + public Node(int index){ + this.index = index; + next = new Node[26]; + } + } + + private Node root; + + /** Initialize your data structure here. */ + public WordDictionary2() { + root = new Node(0); + } + + /** Adds a word into the data structure. */ + public void addWord(String word) { + + Node cur = root; + for(int i = 0 ; i < word.length() ; i ++){ + char c = word.charAt(i); + if(cur.next[c - 'a'] == null) + cur.next[c - 'a'] = new Node(i + 1); + cur = cur.next[c - 'a']; + } + cur.isWord = true; + } + + /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ + public boolean search(String word) { + + LinkedList nodeStack = new LinkedList<>(); + + nodeStack.push(root); + while(!nodeStack.isEmpty()){ + Node node = nodeStack.pollLast(); + if(node.index == word.length()){ + if(node.isWord) return true; + continue; + } + + char c = word.charAt(node.index); + if(c != '.'){ + if(node.next[c - 'a'] != null) + nodeStack.push(node.next[c - 'a']); + } + else{ + for(char nextChar = 0; nextChar < 26; nextChar ++){ + if(node.next[nextChar] != null) + nodeStack.push(node.next[nextChar]); + } + } + } + return false; + } +} \ No newline at end of file From e927ded4069c34fa3adcb85b0d5e94ba3db13ba6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 13 Nov 2022 09:38:26 -0800 Subject: [PATCH 239/390] 0211 comments updated. --- .../java-0211/src/WordDictionary2.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java b/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java index 847251d3..b97f26cc 100644 --- a/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java +++ b/0001-0500/0211-Add-and-Search-Word-Data-structure-design/java-0211/src/WordDictionary2.java @@ -4,7 +4,7 @@ import java.util.*; -/// Trie + Recursive DFS +/// Trie + Non-Recursive DFS /// Time Complexity: addWord: O(len(word)) /// search: O(size(trie)) /// Space Complexity: O(sum(len(wi))) where wi is the length of the ith word @@ -45,11 +45,11 @@ public void addWord(String word) { /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ public boolean search(String word) { - LinkedList nodeStack = new LinkedList<>(); + Deque nodeStack = new LinkedList<>(); nodeStack.push(root); while(!nodeStack.isEmpty()){ - Node node = nodeStack.pollLast(); + Node node = nodeStack.poll(); if(node.index == word.length()){ if(node.isWord) return true; continue; From a42f2f392a59a6bb98a8c59c591fcaa9b313e854 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 13 Nov 2022 23:42:53 -0800 Subject: [PATCH 240/390] 0805 codes updated. --- .../cpp-0805/CMakeLists.txt | 2 +- .../cpp-0805/main.cpp | 13 +- .../cpp-0805/main2.cpp | 166 ------------------ 3 files changed, 6 insertions(+), 175 deletions(-) delete mode 100644 0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main2.cpp diff --git a/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/CMakeLists.txt b/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/CMakeLists.txt index ade1f6b8..1c3e7bb6 100644 --- a/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/CMakeLists.txt +++ b/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/CMakeLists.txt @@ -3,5 +3,5 @@ project(D) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main2.cpp) +set(SOURCE_FILES main.cpp) add_executable(D ${SOURCE_FILES}) \ No newline at end of file diff --git a/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main.cpp b/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main.cpp index 34763118..4c038299 100644 --- a/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main.cpp +++ b/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main.cpp @@ -9,6 +9,7 @@ using namespace std; + /// Dynamic Programming /// /// Suppose the same average is ave, then ave = sum(A) / len(A) @@ -57,27 +58,23 @@ class Solution { } }; -void print_bool(bool res){ - cout << (res ? "True" : "False") << endl; -} - int main() { vector A1 = {1,2,3,4,5,6,7,8}; - print_bool(Solution().splitArraySameAverage(A1)); + cout << Solution().splitArraySameAverage(A1) << '\n'; // true vector A2 = {3, 1}; - print_bool(Solution().splitArraySameAverage(A2)); + cout << Solution().splitArraySameAverage(A2) << '\n'; // false vector A3 = {18, 10, 5, 3}; - print_bool(Solution().splitArraySameAverage(A3)); + cout << Solution().splitArraySameAverage(A3) << '\n'; // false vector A4 = {2,0,5,6,16,12,15,12,4}; - print_bool(Solution().splitArraySameAverage(A4)); + cout << Solution().splitArraySameAverage(A4) << '\n'; // true return 0; diff --git a/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main2.cpp b/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main2.cpp deleted file mode 100644 index 0f7a95bf..00000000 --- a/0501-1000/0805-Split-Array-With-Same-Average/cpp-0805/main2.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/// Source : https://leetcode.com/problems/split-array-with-same-average/description/ -/// Author : liuyubobobo -/// Time : 2018-03-24 - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -/// Meet in the Middle -/// -/// Using a Custom Fraction class -/// The Algorithm is based on the official solution: -/// https://leetcode.com/problems/split-array-with-same-average/solution/ -/// -/// Time Complexity: O(2^(N/2)) -/// Space Complexity: O(2^(N/2)) - -class Fraction{ - -private: - int num, denom; - -public: - Fraction(int a = 0, int b = 1): num(a), denom(b){ - - if(num == 0) - denom = 1; - else{ - if(denom < 0){ - num = -num; - denom = -denom; - } - int g = gcd(abs(num), denom); - num /= g; - denom /= g; - } - } - - Fraction operator-(const Fraction& another){ - return Fraction(num * another.denom - another.num * denom, - denom * another.denom); - } - - Fraction operator-(){ - return Fraction(-num, denom); - } - - Fraction operator+(const Fraction& another){ - return Fraction(num * another.denom + another.num * denom, - denom * another.denom); - } - - string hash_string() const{ - return to_string(num) + "/" + to_string(denom); - } - -private: - int gcd(int a, int b){ - - if(a < b) - swap(a, b); - // a > b - if(a % b == 0) - return b; - return gcd(b, a % b); - } -}; - -bool operator==(const Fraction& f1, const Fraction& f2) { - return f1.hash_string() == f2.hash_string(); -} - -bool operator!=(const Fraction& f1, const Fraction& f2) { - return !(f1 == f2); -} - -// Custom Hash Function for Fraction -namespace std { - template<> - struct hash { - size_t operator()(Fraction const &f) const noexcept { - return std::hash{}(f.hash_string()); - } - }; -} - -class Solution { -public: - bool splitArraySameAverage(vector& A) { - - int sum = accumulate(A.begin(), A.end(), 0); - Fraction average(sum , A.size()); - - vector v; - for(int a: A) - v.push_back(Fraction(a, 1) - average); - - unordered_set left_bag = get_sum(v, 0, A.size()/2); - if(left_bag.find(Fraction(0, 1)) != left_bag.end()) - return true; - - unordered_set right_bag = get_sum(v, A.size()/2, A.size()); - if(right_bag.find(Fraction(0, 1)) != right_bag.end()) - return true; - - Fraction leftsum = accumulate(v.begin(), v.begin() + v.size() / 2, Fraction(0, 1)); - Fraction rightsum = accumulate(v.begin() + v.size() / 2, v.end(), Fraction(0, 1)); - for(Fraction num: left_bag) - if(num != leftsum && right_bag.find(-num) != right_bag.end()) - return true; - - return false; - } - -private: - unordered_set get_sum(const vector& v, int l, int r){ - - unordered_set bag; - for(int i = l ; i < r ; i ++){ - unordered_set new_bag; - for(Fraction e: bag) - new_bag.insert(e + v[i]); - bag.insert(new_bag.begin(), new_bag.end()); - bag.insert(v[i]); - } - - return bag; - } -}; - -void print_bool(bool res){ - cout << (res ? "True" : "False") << endl; -} - - -int main() { - - vector A1 = {1,2,3,4,5,6,7,8}; - print_bool(Solution().splitArraySameAverage(A1)); - // true - - vector A2 = {3, 1}; - print_bool(Solution().splitArraySameAverage(A2)); - // false - - vector A3 = {18, 10, 5, 3}; - print_bool(Solution().splitArraySameAverage(A3)); - // false - - vector A4 = {2,0,5,6,16,12,15,12,4}; - print_bool(Solution().splitArraySameAverage(A4)); - // true - - vector A5 = {10,29,13,53,33,48,76,70,5,5}; - print_bool(Solution().splitArraySameAverage(A5)); - // true - - return 0; -} \ No newline at end of file From 5811289a1a8ba7c7ff13a01dd31eb132c6bff283 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 13 Nov 2022 23:56:57 -0800 Subject: [PATCH 241/390] 2469-2472 added. --- .../cpp-2469/CMakeLists.txt | 6 ++ .../cpp-2469/main.cpp | 28 +++++++ .../cpp-2470/CMakeLists.txt | 6 ++ .../cpp-2470/main.cpp | 52 ++++++++++++ .../cpp-2471/CMakeLists.txt | 6 ++ .../cpp-2471/main.cpp | 84 +++++++++++++++++++ .../cpp-2472/CMakeLists.txt | 6 ++ .../cpp-2472/main.cpp | 52 ++++++++++++ readme.md | 5 ++ 9 files changed, 245 insertions(+) create mode 100644 2001-2500/2469-Convert-the-Temperature/cpp-2469/CMakeLists.txt create mode 100644 2001-2500/2469-Convert-the-Temperature/cpp-2469/main.cpp create mode 100644 2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/CMakeLists.txt create mode 100644 2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/main.cpp create mode 100644 2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/CMakeLists.txt create mode 100644 2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/main.cpp create mode 100644 2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/CMakeLists.txt create mode 100644 2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/main.cpp diff --git a/2001-2500/2469-Convert-the-Temperature/cpp-2469/CMakeLists.txt b/2001-2500/2469-Convert-the-Temperature/cpp-2469/CMakeLists.txt new file mode 100644 index 00000000..415e0a92 --- /dev/null +++ b/2001-2500/2469-Convert-the-Temperature/cpp-2469/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2469-Convert-the-Temperature/cpp-2469/main.cpp b/2001-2500/2469-Convert-the-Temperature/cpp-2469/main.cpp new file mode 100644 index 00000000..93e2851d --- /dev/null +++ b/2001-2500/2469-Convert-the-Temperature/cpp-2469/main.cpp @@ -0,0 +1,28 @@ +/// Source : https://leetcode.com/problems/convert-the-temperature/ +/// Author : liuyubobobo +/// Time : 2022-11-12 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + vector convertTemperature(double celsius) { + + double k = celsius + 273.15; + double f = celsius * 1.80 + 32.00; + return {k, f}; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/CMakeLists.txt b/2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/CMakeLists.txt new file mode 100644 index 00000000..b1c82ab8 --- /dev/null +++ b/2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/main.cpp b/2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/main.cpp new file mode 100644 index 00000000..d7b52a57 --- /dev/null +++ b/2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/ +/// Author : liuyubobobo +/// Time : 2022-11-12 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n^2 * logk) +/// Space Complexity: O(1) +class Solution { +public: + int subarrayLCM(vector& nums, int k) { + + int n = nums.size(), res = 0; + for(int l = 0; l < n; l ++){ + int lcm = nums[l]; + if(lcm == k) res ++; + else if(lcm > k) continue; + for(int r = l + 1; r < n && lcm <= k; r ++){ + lcm = lcm * nums[r] / gcd(lcm, nums[r]); + if(lcm == k) res ++; + } + } + return res; + } + +private: + int gcd(int a, int b){ + + if(a < b) swap(a, b); + int c; + while (b){ + c = a % b; + a = b; + b = c; + } + return a; + } +}; + + +int main() { + + vector nums1 = {3, 6, 2, 7, 1}; + cout << Solution().subarrayLCM(nums1, 6) << '\n'; + + return 0; +} diff --git a/2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/CMakeLists.txt b/2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/CMakeLists.txt new file mode 100644 index 00000000..d0dc1506 --- /dev/null +++ b/2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/main.cpp b/2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/main.cpp new file mode 100644 index 00000000..35422c71 --- /dev/null +++ b/2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/main.cpp @@ -0,0 +1,84 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/ +/// Author : liuyubobobo +/// Time : 2022-11-13 + +#include +#include +#include + +using namespace std; + + +/// BFS + Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + int minimumOperations(TreeNode* root) { + + queue q; + q.push(root); + int res = 0; + vector num2index(1e5 + 1, -1); + while(!q.empty()){ + + vector v; + while(!q.empty()) v.push_back(q.front()), q.pop(); + + vector data(v.size()); + for(int i = 0; i < v.size(); i ++) data[i] = v[i]->val; + + res += solve(data.size(), data, num2index); + + for(TreeNode* node: v){ + if(node->left) q.push(node->left); + if(node->right) q.push(node->right); + } + } + return res; + } + +private: + int solve(int n, vector& data, vector& num2index){ + + for(int i = 0; i < n; i ++) num2index[data[i]] = i; + + vector sorted_data = data; + sort(sorted_data.begin(), sorted_data.end()); + + int res = 0; + for(int i = 0; i < n; i ++){ + int j = num2index[sorted_data[i]]; + if(i != j){ + res ++; + swap(data[i], data[j]); + num2index[data[j]] = j; + } + } + return res; + } +}; + + +int main() { + + TreeNode* left = new TreeNode(4, new TreeNode(7), new TreeNode(6)); + TreeNode* right = new TreeNode(3, new TreeNode(8, new TreeNode(9), nullptr), new TreeNode(5, new TreeNode(10), + nullptr)); + TreeNode* root = new TreeNode(1, left, right); + cout << Solution().minimumOperations(root) << '\n'; + // 3 + + return 0; +} diff --git a/2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/CMakeLists.txt b/2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/CMakeLists.txt new file mode 100644 index 00000000..8148fadd --- /dev/null +++ b/2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/main.cpp b/2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/main.cpp new file mode 100644 index 00000000..2b6896ae --- /dev/null +++ b/2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/description/ +/// Author : liuyubobobo +/// Time : 2022-11-12 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2 + nk) +/// Space Complexity: O(n^2) +class Solution { +public: + int maxPalindromes(string s, int k) { + + int n = s.size(); + + vector> is_palindrome(n, vector(n + 1, false)); // start, len + for(int i = 0; i < n; i ++) is_palindrome[i][1] = is_palindrome[i][0] = true; + for(int len = 2; len <= n; len ++) + for(int i = 0; i + len <= n; i ++) + if(s[i] == s[i + len - 1] && is_palindrome[i + 1][len - 2]) is_palindrome[i][len] = true; + + vector dp(n + 1, INT_MIN / 2); + dp[n] = 0; + dp[n - 1] = (k == 1); + for(int i = n - 2; i >= 0; i --){ + dp[i] = dp[i + 1]; + for(int len = k; i + len <= n; len ++) + if(is_palindrome[i][len]) dp[i] = max(dp[i], 1 + dp[i + len]); + } + return dp[0] >= 0 ? dp[0] : 0; + } +}; + + +int main() { + + cout << Solution().maxPalindromes("abaccdbbd", 3) << '\n'; + // 2 + + cout << Solution().maxPalindromes("adbcda", 2) << '\n'; + // 0 + + cout << Solution().maxPalindromes("fttfjofpnpfydwdwdnns", 2) << '\n'; + // 4 + + return 0; +} diff --git a/readme.md b/readme.md index 6c543afc..a9f15f9b 100644 --- a/readme.md +++ b/readme.md @@ -2336,6 +2336,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2463 | [Minimum Total Distance Traveled](https://leetcode.com/problems/minimum-total-distance-traveled/) | [无] | [C++](2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/) | | | | 2464 | [Minimum Subarrays in a Valid Split](https://leetcode.com/problems/minimum-subarrays-in-a-valid-split/description/) | [无] | [C++](2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/) | | | | | | | | | | +| 2469 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [无] | [C++](2001-2500/2469-Convert-the-Temperature/cpp-2469/) | | | +| 2470 | [Number of Subarrays With LCM Equal to K](https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/) | [无] | [C++](2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/) | | | +| 2471 | [Minimum Number of Operations to Sort a Binary Tree by Level](https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/) | [无] | [C++](2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/) | | | +| 2472 | [Maximum Number of Non-overlapping Palindrome Substrings](https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/) | [无] | [C++](2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 0dbe0d7aec1bdc29e1162c18eff5843908db6728 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 15 Nov 2022 12:38:37 -0800 Subject: [PATCH 242/390] 0775 solved. --- .../cpp-0775/CMakeLists.txt | 6 ++ .../cpp-0775/main.cpp | 84 +++++++++++++++++++ readme.md | 1 + 3 files changed, 91 insertions(+) create mode 100644 0501-1000/0775-Global-and-Local-Inversions/cpp-0775/CMakeLists.txt create mode 100644 0501-1000/0775-Global-and-Local-Inversions/cpp-0775/main.cpp diff --git a/0501-1000/0775-Global-and-Local-Inversions/cpp-0775/CMakeLists.txt b/0501-1000/0775-Global-and-Local-Inversions/cpp-0775/CMakeLists.txt new file mode 100644 index 00000000..073258ca --- /dev/null +++ b/0501-1000/0775-Global-and-Local-Inversions/cpp-0775/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0775) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0775 main.cpp) diff --git a/0501-1000/0775-Global-and-Local-Inversions/cpp-0775/main.cpp b/0501-1000/0775-Global-and-Local-Inversions/cpp-0775/main.cpp new file mode 100644 index 00000000..3d057512 --- /dev/null +++ b/0501-1000/0775-Global-and-Local-Inversions/cpp-0775/main.cpp @@ -0,0 +1,84 @@ +/// Source : https://leetcode.com/problems/global-and-local-inversions/ +/// Author : liuyubobobo +/// Time : 2022-11-15 + +#include +#include + +using namespace std; + + +/// Merge Sort +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + bool isIdealPermutation(vector& A) { + + vector A_copy(A.begin(), A.end()); + long long global_inverse_count = globalInversionCount(A_copy); + + long long local_inverse_count = 0; + for(int i = 1 ; i < A.size() ; i ++) + if(A[i-1] > A[i]) + local_inverse_count ++; + + return global_inverse_count == local_inverse_count; + } + +private: + int globalInversionCount(vector& vec){ + + return globalInversionCount(vec, 0, vec.size()-1); + } + + long long globalInversionCount(vector& vec, int l, int r){ + + if( l >= r ) + return 0; + + int mid = l + (r-l)/2; + + long long res1 = globalInversionCount(vec, l, mid); + long long res2 = globalInversionCount(vec, mid + 1, r); + + return res1 + res2 + merge(vec, l, mid, r); + } + + long long merge(vector& vec, int l, int mid, int r){ + + vector aux; + for(int i = l ; i <= r ; i ++ ) + aux.push_back(vec[i]); + + long long res = 0; + int j = l, k = mid + 1; + for( int i = l ; i <= r ; i ++ ){ + if( j > mid ){ + vec[i] = aux[k-l]; + k ++; + } + else if( k > r ){ + vec[i] = aux[j-l]; + j ++; + } + else if( aux[j-l] <= aux[k-l] ){ + vec[i] = aux[j-l]; + j ++; + } + else{ + vec[i] = aux[k-l]; + k ++; + res += (mid - j + 1); + } + } + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a9f15f9b..cf85c1be 100644 --- a/readme.md +++ b/readme.md @@ -780,6 +780,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 772 | [Basic Calculator III](https://leetcode.com/problems/basic-calculator-iii/description/) | [无] | [C++](0501-1000/0772-Basic-Calculator-III/cpp-0772/) | | | | | | | | | | | 774 | [Minimize Max Distance to Gas Station](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | [solution](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | [C++](0501-1000/0774-Minimize-Max-Distance-to-Gas-Station/cpp-0774/) | | | +| 775 | [Global and Local Inversions](https://leetcode.com/problems/global-and-local-inversions/) | [C++](0501-1000/0775-Global-and-Local-Inversions/cpp-0775/) | | | | | | | | | | | | 779 | [K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/) | [无] | [C++](0501-1000/0779-K-th-Symbol-in-Grammar/cpp-0779/) | | | | 780 | [Reaching Points](https://leetcode.com/problems/reaching-points/description/) | [solution](https://leetcode.com/problems/reaching-points/solution/) | [C++](0501-1000/0780-Reaching-Points/cpp-0780/) | | | From 8bf0089943de41f8f002ec389c752efcc6e2bf8c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 20 Nov 2022 00:22:37 -0800 Subject: [PATCH 243/390] 0224 bug fixed. --- .../0224-Basic-Calculator/cpp-0224/main.cpp | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/0001-0500/0224-Basic-Calculator/cpp-0224/main.cpp b/0001-0500/0224-Basic-Calculator/cpp-0224/main.cpp index cc2eb2f2..cff2b971 100644 --- a/0001-0500/0224-Basic-Calculator/cpp-0224/main.cpp +++ b/0001-0500/0224-Basic-Calculator/cpp-0224/main.cpp @@ -1,7 +1,7 @@ /// Source : https://leetcode.com/problems/basic-calculator/description/ /// Author : liuyubobobo /// Time : 2018-09-03 -/// Updated: 2021-03-09 +/// Updated: 2022-11-20 #include #include @@ -16,41 +16,53 @@ using namespace std; /// Space Complexity: O(n) class Solution { public: - int calculate(string s) { + int calculate(string s2) { + + string s; + for(char c: s2) if(c != ' ') s += c; + + int n = s.size(); + vector right(n, -1); + vector left; + for(int i = 0; i < n; i ++) { + if (s[i] == '(') left.push_back(i); + else if (s[i] == ')') right[left.back()] = i, left.pop_back(); + } + + return parse(s, 0, n - 1, right); + } + +private: + int parse(const string& s, int l, int r, const vector& right){ vector nums; vector ops; - for(int i = 0; i < s.size(); ){ - if(s[i] == '+' || s[i] == '-' || s[i] == '(') + for(int i = l; i <= r; ){ + if(s[i] == '+' || s[i] == '-'){ ops.push_back(s[i++]); - else if(s[i] == ')'){ - assert(!ops.empty() && ops.back() == '('); - ops.pop_back(); - i ++; - + if(nums.empty()) nums.push_back(0); + } + else if(s[i] == '('){ + nums.push_back(parse(s, i + 1, right[i] - 1, right)); cal(nums, ops); + i = right[i] + 1; } - else if(isdigit(s[i])){ - - int num = s[i] - '0'; - int j; - for(j = i + 1; j < s.size() && isdigit(s[j]); j ++) + else{ + int j = i, num = 0; + for(; j <= r && isdigit(s[j]); j ++) num = num * 10 + (s[j] - '0'); - i = j; nums.push_back(num); cal(nums, ops); + i = j; } - else - i ++; } assert(nums.size() == 1); return nums.back(); } -private: void cal(vector& nums, vector& ops){ if(!ops.empty() && (ops.back() == '+' || ops.back() == '-')){ @@ -74,6 +86,8 @@ int main() { cout << Solution().calculate(" 2-1 + 2 ") << endl; // 3 cout << Solution().calculate("(1+(4+5+2)-3)+(6+8)") << endl; // 23 cout << Solution().calculate("-2+ 1") << endl; // -1 + cout << Solution().calculate("1-(-2)") << endl; // 3 + cout << Solution().calculate("-(3+(4+5))") << endl; // -12 return 0; } \ No newline at end of file From 2d1f0345d949740c6d68acb7a59b008b5958d118 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 27 Nov 2022 21:36:06 -0800 Subject: [PATCH 244/390] 0813 solved. --- .../cpp-0813/CMakeLists.txt | 6 +++ .../cpp-0813/main.cpp | 44 +++++++++++++++++++ readme.md | 1 + 3 files changed, 51 insertions(+) create mode 100644 0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/CMakeLists.txt create mode 100644 0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp diff --git a/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/CMakeLists.txt b/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/CMakeLists.txt new file mode 100644 index 00000000..91dd3d6f --- /dev/null +++ b/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_0813) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0813 main.cpp) diff --git a/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp b/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp new file mode 100644 index 00000000..d2c6d73c --- /dev/null +++ b/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp @@ -0,0 +1,44 @@ +#include +#include + +using namespace std; + + +class Solution { +public: + double largestSumOfAverages(vector& nums, int k) { + + int n = nums.size(); + vector> dp(k + 1, vector(n, 0.0)); + + int sum = 0; + for(int i = n - 1; i >= 0; i --){ + sum += nums[i]; + dp[1][i] = (double)sum / (n - i); + } + + for(int seg = 2; seg <= k; seg ++){ + for(int l = 0; n - l >= seg; l ++){ + int sum = 0; + for(int r = l; n - r >= seg; r ++){ + sum += nums[r]; + dp[seg][l] = max(dp[seg][l], (double)sum / (r - l + 1) + dp[seg - 1][r + 1]); + } + } + } + + double res = dp[1][0]; + for(int seg = 2; seg <= k; seg ++) res = max(res, dp[seg][0]); + return res; + } +}; + + +int main() { + + vector nums1 = {9,1,2,3,9}; + cout << Solution().largestSumOfAverages(nums1, 3) << '\n'; + // 20 + + return 0; +} diff --git a/readme.md b/readme.md index cf85c1be..8fa2e840 100644 --- a/readme.md +++ b/readme.md @@ -816,6 +816,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 810 | [Chalkboard XOR Game](https://leetcode.com/problems/chalkboard-xor-game/) | [solution](https://leetcode.com/problems/chalkboard-xor-game/solution/) | [C++](0501-1000/0810-Chalkboard-XOR-Game/cpp-0810/) | | | | 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/description/) | [solution](https://leetcode.com/problems/subdomain-visit-count/solution/) | [C++](0501-1000/0811-Subdomain-Visit-Count/cpp-0811/) | | | | | | | | | | +| 813 | [Largest Sum of Averages](https://leetcode.com/problems/largest-sum-of-averages/description/) | [solution](https://leetcode.com/problems/largest-sum-of-averages/solutions/) | [C++](0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/) | | | | 814 | [Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/) | [solution](https://leetcode.com/problems/binary-tree-pruning/solution/) | [C++](0501-1000/0814-Binary-Tree-Pruning/cpp-0814/) | | | | 815 | [Bus Routes](https://leetcode.com/problems/bus-routes/) | [solution](https://leetcode.com/problems/bus-routes/solution/) | [C++](0501-1000/0815-Bus-Routes/cpp-0815/) | | | | | | | | | | From 1373daef8356f74fae2756467fd3f47c46710b89 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 28 Nov 2022 17:16:04 -0800 Subject: [PATCH 245/390] 2465-2468 solved. --- .../cpp-0813/main.cpp | 7 ++ .../cpp-2465/CMakeLists.txt | 6 + .../cpp-2465/main.cpp | 33 ++++++ .../cpp-2466/CMakeLists.txt | 6 + .../cpp-2466/main.cpp | 40 +++++++ .../cpp-2467/CMakeLists.txt | 6 + .../cpp-2467/main.cpp | 107 ++++++++++++++++++ .../cpp-2468/CMakeLists.txt | 6 + .../cpp-2468/main.cpp | 61 ++++++++++ readme.md | 5 +- 10 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/CMakeLists.txt create mode 100644 2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/main.cpp create mode 100644 2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/CMakeLists.txt create mode 100644 2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/main.cpp create mode 100644 2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/CMakeLists.txt create mode 100644 2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/main.cpp create mode 100644 2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/CMakeLists.txt create mode 100644 2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/main.cpp diff --git a/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp b/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp index d2c6d73c..3c239dd1 100644 --- a/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp +++ b/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp @@ -1,9 +1,16 @@ +/// Source : https://leetcode.com/problems/largest-sum-of-averages/ +/// Author : liuyubobobo +/// Time : 2022-11-27 + #include #include using namespace std; +/// DP +/// Time Compleixty: O(n^2 * k) +/// Space Complexity: O(nk) class Solution { public: double largestSumOfAverages(vector& nums, int k) { diff --git a/2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/CMakeLists.txt b/2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/CMakeLists.txt new file mode 100644 index 00000000..3d4364fe --- /dev/null +++ b/2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2465) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2465 main.cpp) diff --git a/2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/main.cpp b/2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/main.cpp new file mode 100644 index 00000000..a89aab84 --- /dev/null +++ b/2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/number-of-distinct-averages/description/ +/// Author : liuyubobobo +/// Time : 2022-11-27 + +#include +#include +#include +#include + +using namespace std; + + +/// Sorting and using set +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int distinctAverages(vector& nums) { + + sort(nums.begin(), nums.end()); + + set res; + for(int i = 0, j = nums.size() - 1; i < j; i ++, j --) + res.insert(nums[i] + nums[j]); + return res.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/CMakeLists.txt b/2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/CMakeLists.txt new file mode 100644 index 00000000..6377af39 --- /dev/null +++ b/2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2466) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2466 main.cpp) diff --git a/2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/main.cpp b/2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/main.cpp new file mode 100644 index 00000000..1ae9b515 --- /dev/null +++ b/2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/count-ways-to-build-good-strings/ +/// Author : liuyubobobo +/// Time : 2022-11-27 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int countGoodStrings(int low, int high, int zero, int one) { + + vector dp(high + 1, 0); + dp[0] = 1; + for(int i = 1; i <= high; i ++){ + if(i - zero >= 0) dp[i] += dp[i - zero]; + if(i - one >= 0) dp[i] += dp[i - one]; + dp[i] %= MOD; + } + + long long res = 0; + for(int i = low; i <= high; i ++) res += dp[i]; + return res % MOD; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/CMakeLists.txt b/2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/CMakeLists.txt new file mode 100644 index 00000000..1c935fb3 --- /dev/null +++ b/2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2467) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2467 main.cpp) diff --git a/2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/main.cpp b/2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/main.cpp new file mode 100644 index 00000000..01f29a9e --- /dev/null +++ b/2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/main.cpp @@ -0,0 +1,107 @@ +/// Source : https://leetcode.com/problems/most-profitable-path-in-a-tree/description/ +/// Author : liuyubobobo +/// Time : 2022-11-28 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +public: + int mostProfitablePath(vector>& edges, int bob, vector& amount) { + + int n = edges.size() + 1; + + vector> tree(n); + for(const vector& edge: edges){ + int a = edge[0], b = edge[1]; + tree[a].push_back(b), tree[b].push_back(a); + } + + vector parent(n, 0); + dfs_parent(tree, 0, 0, parent); + + vector bob_path = {bob}; + while(bob_path.back() != 0) bob_path.push_back(parent[bob_path.back()]); + + int res = INT_MIN, ares = 0, bres = 0; + vector visited(n, 0); + dfs(tree, 0, 0, 0, visited, bob_path, amount, ares, bres, res); + return res; + } + +private: + void dfs(const vector>& tree, int u, int p, int index, + vector& visited, const vector& bob_path, const vector& amount, + int& ares, int& bres, int& res){ + + if(index < bob_path.size()){ + if(u == bob_path[index]){ + ares += amount[u] / 2, bres += amount[u] / 2; + assert(visited[u] == 0); + visited[u] |= 3; + } + else{ + if(!visited[u]) + ares += amount[u], visited[u] |= 1; + if(!visited[bob_path[index]]) + bres += amount[bob_path[index]], visited[bob_path[index]] |= 2; + } + } + else{ + if(!visited[u]) ares += amount[u], visited[u] |= 1; + } + + if(tree[u].size() == 1 && u){ + res = max(res, ares); + } + + for(int v: tree[u]){ + if(v == p) continue; + dfs(tree, v, u, index + 1, visited, bob_path, amount, ares, bres, res); + } + + if(index < bob_path.size()){ + if(u == bob_path[index]){ + assert(visited[u] == 3); + ares -= amount[u] / 2, bres -= amount[u] / 2; + visited[u] = 0; + } + else{ + if(visited[u] & 1) ares -= amount[u], visited[u] -= 1; + if(visited[bob_path[index]] & 2) bres -= amount[bob_path[index]], visited[bob_path[index]] -= 2; + } + } + else{ + if(visited[u] & 1) ares -= amount[u], visited[u] -= 1; + } + } + + void dfs_parent(const vector>& tree, int u, int p, vector& parent){ + + parent[u] = p; + for(int v: tree[u]){ + if(v == p) continue; + dfs_parent(tree, v, u, parent); + } + } +}; + + +int main() { + + vector> edges1 = {{0, 1}, {1, 2}, {2, 3}}; + vector amount1 = {-5644,-6018,1188,-8502}; + cout << Solution().mostProfitablePath(edges1, 3, amount1) << '\n'; + + return 0; +} diff --git a/2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/CMakeLists.txt b/2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/CMakeLists.txt new file mode 100644 index 00000000..1ebd4187 --- /dev/null +++ b/2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2468) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2468 main.cpp) diff --git a/2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/main.cpp b/2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/main.cpp new file mode 100644 index 00000000..3e2695b4 --- /dev/null +++ b/2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/main.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode.com/problems/split-message-based-on-limit/description/ +/// Author : liuyubobobo +/// Time : 2022-11-28 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector splitMessage(string message, int limit) { + + int n = message.size(); + for(int part_len = 1;part_len + part_len + 3 < limit; part_len ++){ + vector res; + if(ok(n, message, part_len, limit, res)) return res; + } + return {}; + } + +private: + bool ok(int n, const string& message, int part_len, int limit, vector& res){ + + string part_num(part_len, '#'); + for(int i = 1, j = 0; j < n; i ++){ + string i_str = to_string(i); + if(i_str.size() > part_len) return false; + + string suf = "<" + i_str + "/" + part_num + ">"; + int content_len = limit - suf.size(); + string content = message.substr(j, content_len); + j += content_len; + + res.push_back(content + suf); + } + + string len = to_string(res.size()); + if(len.size() != part_len) return false; + + for(string& s: res) s.replace(s.find(part_num), part_len, len); + return true; + } +}; + + +void print_vec(const vector& v){ + for(const string& e: v) cout << e << '\n'; +} + +int main() { + + string message1 = "this is really a very awesome message"; + print_vec(Solution().splitMessage("this is really a very awesome message", 9)); + + return 0; +} diff --git a/readme.md b/readme.md index 8fa2e840..1c9d7f0f 100644 --- a/readme.md +++ b/readme.md @@ -2337,7 +2337,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2462 | [Total Cost to Hire K Workers](https://leetcode.com/problems/total-cost-to-hire-k-workers/) | [无] | [C++](2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/) | | | | 2463 | [Minimum Total Distance Traveled](https://leetcode.com/problems/minimum-total-distance-traveled/) | [无] | [C++](2001-2500/2463-Minimum-Total-Distance-Traveled/cpp-2463/) | | | | 2464 | [Minimum Subarrays in a Valid Split](https://leetcode.com/problems/minimum-subarrays-in-a-valid-split/description/) | [无] | [C++](2001-2500/2464-Minimum-Subarrays-in-a-Valid-Split/cpp-2464/) | | | -| | | | | | | +| 2465 | [Number of Distinct Averages](https://leetcode.com/problems/number-of-distinct-averages/) | [无] | [C++](2001-2500/2465-Number-of-Distinct-Averages/cpp-2465/) | | | +| 2466 | [Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings/) | [无] | [C++](2001-2500/2466-Count-Ways-To-Build-Good-Strings/cpp-2466/) | | | +| 2467 | [Most Profitable Path in a Tree](https://leetcode.com/problems/most-profitable-path-in-a-tree/) | [无] | [C++](2001-2500/2467-Most-Profitable-Path-in-a-Tree/cpp-2467/) | | | +| 2468 | [Split Message Based on Limit](https://leetcode.com/problems/split-message-based-on-limit/) | [无] | [C++](2001-2500/2468-Split-Message-Based-on-Limit/cpp-2468/) | | | | 2469 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [无] | [C++](2001-2500/2469-Convert-the-Temperature/cpp-2469/) | | | | 2470 | [Number of Subarrays With LCM Equal to K](https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/) | [无] | [C++](2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/) | | | | 2471 | [Minimum Number of Operations to Sort a Binary Tree by Level](https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/) | [无] | [C++](2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/) | | | From 631b998d2bfd72b393e5beddaeaf727de29e6827 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 28 Nov 2022 22:04:04 -0800 Subject: [PATCH 246/390] 2473 solved. --- .../cpp-2473/CMakeLists.txt | 6 ++ .../cpp-2473/main.cpp | 82 +++++++++++++++++++ readme.md | 1 + 3 files changed, 89 insertions(+) create mode 100644 2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/CMakeLists.txt create mode 100644 2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/main.cpp diff --git a/2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/CMakeLists.txt b/2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/CMakeLists.txt new file mode 100644 index 00000000..1c1b6186 --- /dev/null +++ b/2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2473) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2473 main.cpp) diff --git a/2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/main.cpp b/2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/main.cpp new file mode 100644 index 00000000..01c41481 --- /dev/null +++ b/2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/main.cpp @@ -0,0 +1,82 @@ +/// Source : https://leetcode.com/problems/minimum-cost-to-buy-apples/description/ +/// Author : liuyubobobo +/// Time : 2022-11-28s + +#include +#include +#include +#include + +using namespace std; + + +/// Dij +/// Time Complexity: O(V^2*logE + V^2) +/// Space Complexity: O(V^2) +class Solution { +public: + vector minCost(int n, vector>& roads, vector& appleCost, int k) { + + vector>> g(n); + for(const vector& e: roads){ + int u = e[0], v = e[1], w = e[2]; + u --, v --; + g[u].push_back({v, w}); + g[v].push_back({u, w}); + } + + vector> dis(n); + for(int u = 0; u < n; u ++) dis[u] = dij(n, g, u); + + vector res(n, LONG_LONG_MAX); + for(int s = 0; s < n; s ++){ + for(int t = 0; t < n; t ++) + if(dis[s][t] != LONG_LONG_MAX / 2) + res[s] = min(res[s], dis[s][t] * (k + 1) + appleCost[t]); + } + return res; + } + +private: + vector dij(int n, const vector>>& g, int s){ + + priority_queue, vector>, greater<>> pq; + pq.push({0, s}); + vector dis(n, LONG_LONG_MAX / 2); + dis[s] = 0; + vector visited(n, false); + while(!pq.empty()){ + long long d = pq.top().first; + int u = pq.top().second; + pq.pop(); + + if(visited[u]) continue; + visited[u] = true; + + for(const pair& p: g[u]){ + int v = p.first; long long w = p.second; + if(!visited[v] && d + w < dis[v]){ + dis[v] = d + w; + pq.push({dis[v], v}); + } + } + } + return dis; + } +}; + + +void print_vec(const vector& v){ + for(long long e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector> roads1 = { + {1,2,4},{2,3,2},{2,4,5},{3,4,1},{1,3,4} + }; + vector appleCost1 = {56,42,102,301}; + print_vec(Solution().minCost(4, roads1, appleCost1, 2)); + + return 0; +} diff --git a/readme.md b/readme.md index 1c9d7f0f..99012a77 100644 --- a/readme.md +++ b/readme.md @@ -2345,6 +2345,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2470 | [Number of Subarrays With LCM Equal to K](https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/) | [无] | [C++](2001-2500/2470-Number-of-Subarrays-With-LCM-Equal-to-K/cpp-2470/) | | | | 2471 | [Minimum Number of Operations to Sort a Binary Tree by Level](https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/) | [无] | [C++](2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/) | | | | 2472 | [Maximum Number of Non-overlapping Palindrome Substrings](https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/) | [无] | [C++](2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/) | | | +| 2473 | [Minimum Cost to Buy Apples](https://leetcode.com/problems/minimum-cost-to-buy-apples/description/) | [无] | [C++](2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 334fcfd7368650d2481ed93d271577762ad131bc Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 1 Dec 2022 16:06:57 -0800 Subject: [PATCH 247/390] 2475-2478 solved. --- .../cpp-2475/CMakeLists.txt | 6 ++ .../cpp-2475/main.cpp | 31 ++++++++ .../cpp-2476/CMakeLists.txt | 6 ++ .../cpp-2476/main.cpp | 64 +++++++++++++++++ .../cpp-2477/CMakeLists.txt | 6 ++ .../cpp-2477/main.cpp | 51 +++++++++++++ .../cpp-2478/CMakeLists.txt | 6 ++ .../cpp-2478/main.cpp | 71 +++++++++++++++++++ readme.md | 7 ++ 9 files changed, 248 insertions(+) create mode 100644 2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/CMakeLists.txt create mode 100644 2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/main.cpp create mode 100644 2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/CMakeLists.txt create mode 100644 2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/main.cpp create mode 100644 2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/CMakeLists.txt create mode 100644 2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/main.cpp create mode 100644 2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/CMakeLists.txt create mode 100644 2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/main.cpp diff --git a/2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/CMakeLists.txt b/2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/CMakeLists.txt new file mode 100644 index 00000000..b7b6f031 --- /dev/null +++ b/2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2475) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2475 main.cpp) diff --git a/2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/main.cpp b/2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/main.cpp new file mode 100644 index 00000000..6cc3e925 --- /dev/null +++ b/2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/number-of-unequal-triplets-in-array/ +/// Author : liuyubobobo +/// Time : 2022-11-28 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^3) +/// Space Complexity: O(1) +class Solution { +public: + int unequalTriplets(vector& nums) { + + int n = nums.size(), res = 0; + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + for(int k = j + 1; k < n; k ++) + res += (nums[i] != nums[j] && nums[j] != nums[k] && nums[i] != nums[k]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/CMakeLists.txt b/2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/CMakeLists.txt new file mode 100644 index 00000000..a5f89477 --- /dev/null +++ b/2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2476) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2476 main.cpp) diff --git a/2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/main.cpp b/2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/main.cpp new file mode 100644 index 00000000..fab611bc --- /dev/null +++ b/2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/main.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/description/ +/// Author : liuyubobobo +/// Time : 2022-11-28 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector> closestNodes(TreeNode* root, vector& queries) { + + vector data; + dfs(root, data); + + int q = queries.size(); + vector> res(q, {-1, -1}); + for(int i = 0; i < q; i ++){ + int e = queries[i]; + + auto iter = upper_bound(data.begin(), data.end(), e); + if(iter != data.begin()){ + iter --; + res[i][0] = *iter; + } + + iter = lower_bound(data.begin(), data.end(), e); + if(iter != data.end()) res[i][1] = *iter; + } + return res; + } + +private: + void dfs(TreeNode* node, vector& data){ + if(!node) return; + + dfs(node->left, data); + data.push_back(node->val); + dfs(node->right, data); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/CMakeLists.txt b/2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/CMakeLists.txt new file mode 100644 index 00000000..343aa694 --- /dev/null +++ b/2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2477) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2477 main.cpp) diff --git a/2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/main.cpp b/2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/main.cpp new file mode 100644 index 00000000..09bc5c0c --- /dev/null +++ b/2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/description/ +/// Author : liuyubobobo +/// Time : 2022-11-29 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long minimumFuelCost(vector>& roads, int seats) { + + int n = roads.size() + 1; + vector> tree(n); + for(const vector& road: roads){ + int a = road[0], b = road[1]; + tree[a].push_back(b), tree[b].push_back(a); + } + + vector sz(n, 0); + dfs_sz(tree, 0, 0, sz); + + long long res = 0; + for(int i = 1; i < n; i ++) + res += sz[i] / seats + !!(sz[i] % seats); + return res; + } + +private: + int dfs_sz(const vector>& tree, int u, int p, vector& sz){ + + int res = 1; + for(int v: tree[u]){ + if(v == p) continue; + res += dfs_sz(tree, v, u, sz); + } + return sz[u] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/CMakeLists.txt b/2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/CMakeLists.txt new file mode 100644 index 00000000..3bf294af --- /dev/null +++ b/2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(cpp_2478) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2478 main.cpp) diff --git a/2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/main.cpp b/2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/main.cpp new file mode 100644 index 00000000..39468d11 --- /dev/null +++ b/2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/main.cpp @@ -0,0 +1,71 @@ +/// Source : https://leetcode.com/problems/number-of-beautiful-partitions/description/ +/// Author : liuyubobobo +/// Time : 2022-12-01 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int beautifulPartitions(string s, int k, int minLength) { + + int n = s.size(); + + vector dp(n, 0), sufsum(n, 0); + for(int i = n - minLength; i >= 0; i --) + if(can_be_start(s, i) && !is_prime(s[n - 1])) dp[i] = 1; + + sufsum[n - 1] = dp[n - 1]; + for(int i = n - 2; i >= 0; i --) sufsum[i] = dp[i] + sufsum[i + 1]; + + for(int cnt = 2; cnt <= k; cnt ++){ + vector tdp(n, 0); + for(int i = n - cnt * minLength; i >= 0; i --){ + if(can_be_start(s, i)) tdp[i] = sufsum[i + minLength]; + } + + sufsum[n - 1]= tdp[n - 1]; + for(int i = n - 2; i >= 0; i --) + sufsum[i] = (tdp[i] + sufsum[i + 1]) % MOD; + dp = tdp; + } + return dp[0]; + } + +private: + bool can_be_start(const string& s, int index){ + return is_prime(s[index]) && (index == 0 || !is_prime(s[index - 1])); + } + + bool is_prime(char c){ + return c == '2' || c == '3' || c == '5' || c == '7'; + } +}; + + +int main() { + + string s1 = "23542185131"; + cout << Solution().beautifulPartitions(s1, 3, 2) << '\n'; + // 3 + + string s2 = "23542185131"; + cout << Solution().beautifulPartitions(s2, 3, 3) << '\n'; + // 1 + + string s3 = "3312958"; + cout << Solution().beautifulPartitions(s3, 3, 1) << '\n'; + // 1 + + return 0; +} diff --git a/readme.md b/readme.md index 99012a77..e8dbb89f 100644 --- a/readme.md +++ b/readme.md @@ -2346,6 +2346,13 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2471 | [Minimum Number of Operations to Sort a Binary Tree by Level](https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/) | [无] | [C++](2001-2500/2471-Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/cpp-2471/) | | | | 2472 | [Maximum Number of Non-overlapping Palindrome Substrings](https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/) | [无] | [C++](2001-2500/2472-Maximum-Number-of-Non-overlapping-Palindrome-Substrings/cpp-2472/) | | | | 2473 | [Minimum Cost to Buy Apples](https://leetcode.com/problems/minimum-cost-to-buy-apples/description/) | [无] | [C++](2001-2500/2473-Minimum-Cost-to-Buy-Apples/cpp-2473/) | | | +| 2474 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2475 | [Number of Unequal Triplets in Array](https://leetcode.com/problems/number-of-unequal-triplets-in-array/) | [无] | [C++](2001-2500/2475-Number-of-Unequal-Triplets-in-Array/cpp-2475/) | | | +| 2476 | [Closest Nodes Queries in a Binary Search Tree](https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/) | [无] | [C++](2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/) | | | +| 2477 | [Minimum Fuel Cost to Report to the Capital](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/) | [无] | [C++](2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/) | | | +| 2478 | [Number of Beautiful Partitions](https://leetcode.com/problems/number-of-beautiful-partitions/) | [无] | [C++](2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/) | | | +| | | | | | | +| 2480 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 79f1bb6f06eb309b6a90113bdf7ead1f6ff00ea4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 2 Dec 2022 14:46:51 -0800 Subject: [PATCH 248/390] 2479 solved. --- .../cpp-2479/CMakeLists.txt | 6 + .../cpp-2479/main.cpp | 119 ++++++++++++++++++ readme.md | 2 +- 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/CMakeLists.txt create mode 100644 2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/main.cpp diff --git a/2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/CMakeLists.txt b/2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/CMakeLists.txt new file mode 100644 index 00000000..f55bf4c6 --- /dev/null +++ b/2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2479) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2479 main.cpp) diff --git a/2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/main.cpp b/2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/main.cpp new file mode 100644 index 00000000..650b1083 --- /dev/null +++ b/2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/main.cpp @@ -0,0 +1,119 @@ +/// Source : https://leetcode.com/problems/maximum-xor-of-two-non-overlapping-subtrees/description/ +/// Author : liuyubobobo +/// Time : 2022-12-02 + +#include +#include +#include + +using namespace std; + + +/// DFS + Trie +/// Time Complexity: O(nlogn(LONG_LONG_MAX)) +/// Space Complexity: O(n * LONG_LONG_MAX) +class BinaryTrie { + +private: + class Node{ + + public: + Node* next[2]; + int sz; + + Node() : sz(0) { + next[0] = next[1] = nullptr; + }; + }; + + int L; + Node* root; + +public: + BinaryTrie(int L = 63) : root(new Node()), L(L){} + + void insert(long long x) { + + Node* cur = root; + cur->sz ++; + for(int i = L - 1; i >= 0; i --){ + int e = ((x >> i) & 1); + if(cur->next[e] == nullptr) + cur->next[e] = new Node(); + cur = cur->next[e]; + cur->sz ++; + } + } + + // 返回 Binary Trie 中存储的数字和 x 异或的最大值 + long long query(long long x){ + + Node* cur = root; + long long res = 0; + for(int i = L - 1; i >= 0; i --){ + int e = ((x >> i) & 1); + if(cur->next[1 - e] && cur->next[1 - e]->sz){ + res = res * 2 + 1; + cur = cur->next[1 - e]; + } + else if(cur->next[e]){ + res = res * 2; + cur = cur->next[e]; + } + else return 0; + } + return res; + } +}; + +class Solution { + +public: + long long maxXor(int n, vector>& edges, vector& values) { + + vector> tree(n); + for(const vector& e: edges){ + int a = e[0], b = e[1]; + tree[a].push_back(b), tree[b].push_back(a); + } + + vector sum(n, 0); + dfs_sum(tree, 0, 0, values, sum); + + long long res = 0; + BinaryTrie trie; + dfs(tree, 0, 0, sum, trie, res); + return res; + } + +private: + void dfs(const vector>& tree, int u, int p, + const vector& sum, BinaryTrie& trie, long long& res){ + + res = max(res, trie.query(sum[u])); + + for(int v: tree[u]){ + if(v == p) continue; + dfs(tree, v, u, sum, trie, res); + } + + trie.insert(sum[u]); + } + + long long dfs_sum(const vector>& tree, int u, int p, + const vector& values, vector& sum){ + + long long res = values[u]; + for(int v: tree[u]){ + if(v == p) continue; + res += dfs_sum(tree, v, u, values, sum); + } + return sum[u] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index e8dbb89f..b72c61d3 100644 --- a/readme.md +++ b/readme.md @@ -2351,7 +2351,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2476 | [Closest Nodes Queries in a Binary Search Tree](https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/) | [无] | [C++](2001-2500/2476-Closest-Nodes-Queries-in-a-Binary-Search-Tree/cpp-2476/) | | | | 2477 | [Minimum Fuel Cost to Report to the Capital](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/) | [无] | [C++](2001-2500/2477-Minimum-Fuel-Cost-to-Report-to-the-Capital/cpp-2477/) | | | | 2478 | [Number of Beautiful Partitions](https://leetcode.com/problems/number-of-beautiful-partitions/) | [无] | [C++](2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/) | | | -| | | | | | | +| 2479 | [Maximum XOR of Two Non-Overlapping Subtrees](https://leetcode.com/problems/maximum-xor-of-two-non-overlapping-subtrees/description/) | [无] | [C++](2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/) | | | | 2480 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | From 506cdda024f255f3184e96d87d33597463520d2c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 15 Dec 2022 13:08:18 -0800 Subject: [PATCH 249/390] 2500 added. --- .../cpp-2500/CMakeLists.txt | 6 ++++ .../cpp-2500/main.cpp | 35 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/CMakeLists.txt create mode 100644 2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/main.cpp diff --git a/2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/CMakeLists.txt b/2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/CMakeLists.txt new file mode 100644 index 00000000..2b9df6f0 --- /dev/null +++ b/2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(A) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(A main.cpp) diff --git a/2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/main.cpp b/2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/main.cpp new file mode 100644 index 00000000..02c1ba57 --- /dev/null +++ b/2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/delete-greatest-value-in-each-row/ +/// Author : liuyubobobo +/// Time : 2022-12-10 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(R * ClogC) +/// Space Complexity: O(1) +class Solution { +public: + int deleteGreatestValue(vector>& grid) { + + for(vector& row: grid) sort(row.begin(), row.end()); + + int res = 0; + for(int j = grid[0].size() - 1; j >= 0; j --){ + int maxv = 0; + for(int i = 0; i < grid.size(); i ++) maxv = max(maxv, grid[i][j]); + res += maxv; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index b72c61d3..a968f2b9 100644 --- a/readme.md +++ b/readme.md @@ -2354,6 +2354,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2479 | [Maximum XOR of Two Non-Overlapping Subtrees](https://leetcode.com/problems/maximum-xor-of-two-non-overlapping-subtrees/description/) | [无] | [C++](2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/) | | | | 2480 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 321ef6584fca2cb47064df80cc80852e8ccfd9c9 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 15 Dec 2022 17:22:26 -0800 Subject: [PATCH 250/390] 2501-3000 folder added. --- .../cpp-2501/CMakeLists.txt | 6 + .../cpp-2501/main.cpp | 49 ++++++++ .../cpp-2502/CMakeLists.txt | 6 + .../cpp-2502/main.cpp | 89 +++++++++++++++ .../cpp-2502/main2.cpp | 104 +++++++++++++++++ .../cpp-2503/CMakeLists.txt | 6 + .../cpp-2503/main.cpp | 106 ++++++++++++++++++ readme.md | 4 + 8 files changed, 370 insertions(+) create mode 100644 2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/CMakeLists.txt create mode 100644 2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/main.cpp create mode 100644 2501-3000/2502-Design-Memory-Allocator/cpp-2502/CMakeLists.txt create mode 100644 2501-3000/2502-Design-Memory-Allocator/cpp-2502/main.cpp create mode 100644 2501-3000/2502-Design-Memory-Allocator/cpp-2502/main2.cpp create mode 100644 2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/CMakeLists.txt create mode 100644 2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/main.cpp diff --git a/2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/CMakeLists.txt b/2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/CMakeLists.txt new file mode 100644 index 00000000..f9dab828 --- /dev/null +++ b/2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(B) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(B main.cpp) diff --git a/2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/main.cpp b/2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/main.cpp new file mode 100644 index 00000000..b2d7753e --- /dev/null +++ b/2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/longest-square-streak-in-an-array/description/ +/// Author : liuyubobobo +/// Time : 2022-12-10 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlogn) +/// Space Compelxity: O(1) +class Solution { +public: + int longestSquareStreak(vector& nums) { + + int n = nums.size(); + vector v(n); + for(int i = 0; i < n; i ++) v[i] = nums[i]; + + sort(v.begin(), v.end()); + int res = 0; + for(int start = 0, i = 1; i <= n; i ++){ + if(i == n || v[i] != v[start]){ + int len = 1; + long long cur = v[start], cur_index = start; + while(true){ + auto iter = lower_bound(v.begin() + cur_index, v.end(), cur * cur); + if(iter == v.end() || *iter != cur * cur) break; + + len ++; + cur = cur * cur; + cur_index = iter - v.begin(); + } + res = max(res, len); + + start = i; + } + } + return res >= 2 ? res : -1; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2502-Design-Memory-Allocator/cpp-2502/CMakeLists.txt b/2501-3000/2502-Design-Memory-Allocator/cpp-2502/CMakeLists.txt new file mode 100644 index 00000000..e3dedae7 --- /dev/null +++ b/2501-3000/2502-Design-Memory-Allocator/cpp-2502/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(C) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(C main2.cpp) diff --git a/2501-3000/2502-Design-Memory-Allocator/cpp-2502/main.cpp b/2501-3000/2502-Design-Memory-Allocator/cpp-2502/main.cpp new file mode 100644 index 00000000..6dc76321 --- /dev/null +++ b/2501-3000/2502-Design-Memory-Allocator/cpp-2502/main.cpp @@ -0,0 +1,89 @@ +/// Source : https://leetcode.com/problems/design-memory-allocator/description/ +/// Author : liuyubobobo +/// Time : 2022-12-10 + +#include +#include +#include + +using namespace std; + + +/// Using TreeSet +/// Time Complexity: init: O(1) +/// allocate: O(n) +/// free: O(n) +/// Space Complexity: O(n) +class Allocator { + +private: + set> mem; + vector>> id2mem; + +public: + Allocator(int n) : id2mem(1001){ + mem.insert({0, n - 1}); + } + + int allocate(int size, int mID) { + + pair target = {-1, -1}; + for(const pair& block: mem){ + int l = block.first, r = block.second, len = r - l + 1; + if(len >= size){ + target = block; + break; + } + } + + if(target.first == -1) return -1; + + id2mem[mID].push_back({target.first, target.first + size - 1}); + mem.erase(target); + if(target.first + size <= target.second) + mem.insert({target.first + size, target.second}); + return target.first; + } + + int free(int mID) { + + int ret = 0; + for(const pair& p: id2mem[mID]) { + pair target = p; + ret += target.second - target.first + 1; + + auto iter = mem.upper_bound(target); + if (iter != mem.end() && iter->first == target.second + 1) { + target.second = iter->second; + mem.erase(iter); + } + + iter = mem.upper_bound(target); + if (iter != mem.begin()) { + iter--; + if (iter->second + 1 == target.first) { + target.first = iter->first; + mem.erase(iter); + } + } + + mem.insert(target); + } + id2mem[mID].clear(); + return ret; + } +}; + + +int main() { + + Allocator allocator(10); + cout << allocator.allocate(1, 1) << '\n'; // 0 + cout << allocator.allocate(1, 2) << '\n'; // 1 + cout << allocator.allocate(1, 3) << '\n'; // 2 + cout << allocator.free(2) << '\n'; // 1 + cout << allocator.allocate(3, 4) << '\n'; // 3 + cout << allocator.allocate(1, 3) << '\n'; // 2 + + return 0; +} diff --git a/2501-3000/2502-Design-Memory-Allocator/cpp-2502/main2.cpp b/2501-3000/2502-Design-Memory-Allocator/cpp-2502/main2.cpp new file mode 100644 index 00000000..5c8f8fe6 --- /dev/null +++ b/2501-3000/2502-Design-Memory-Allocator/cpp-2502/main2.cpp @@ -0,0 +1,104 @@ +/// Source : https://leetcode.com/problems/design-memory-allocator/description/ +/// Author : liuyubobobo +/// Time : 2022-12-15 + +#include +#include +#include + +using namespace std; + + +/// Using LinkedList +/// Time Complexity: init: O(1) +/// allocate: O(n) +/// free: O(n) +/// Space Complexity: O(n) +class Allocator { + +private: + list> available; // start, end + vector>> id2mem; + +public: + Allocator(int n) : id2mem(1001){ + available.push_back({0, n - 1}); + } + + int allocate(int size, int mID) { + + list>::iterator alloc_iter = available.end(); + for(auto iter = available.begin(); iter != available.end(); iter ++){ + int l = iter->first, r = iter->second, len = r - l + 1; + if(len >= size){ + alloc_iter = iter; + break; + } + } + + if(alloc_iter == available.end()) return -1; + + add(id2mem[mID], {alloc_iter->first, alloc_iter->first + size - 1}); + int ret = alloc_iter->first; + alloc_iter->first += size; + if(alloc_iter->first > alloc_iter->second) available.erase(alloc_iter); + return ret; + } + + int free(int mID) { + + int ret = 0; + for(const pair& p: id2mem[mID]) { + ret += p.second - p.first + 1; + add(available, p); + } + id2mem[mID].clear(); + return ret; + } + +private: + void add(list>& l, const pair& p){ + + auto insert_pos_iter = l.end(); + for(auto iter = l.begin(); iter != l.end(); iter ++){ + if(iter->second + 1 == p.first){ + iter->second = p.second; + insert_pos_iter = iter; + break; + } + else if(iter->first > p.second){ + insert_pos_iter = iter; +// insert_pos_iter --; + insert_pos_iter = l.insert(insert_pos_iter, p); + break; + } + } + + if(insert_pos_iter == l.end()){ + l.push_back(p); + return; + } + + auto next_iter = insert_pos_iter; + next_iter ++; + if(next_iter == l.end() || next_iter->first != insert_pos_iter->second + 1) return; + + insert_pos_iter->second = next_iter->second; + l.erase(next_iter); + return; + } +}; + + +int main() { + + Allocator allocator(10); + cout << allocator.allocate(1, 1) << '\n'; // 0 + cout << allocator.allocate(1, 2) << '\n'; // 1 + cout << allocator.allocate(1, 3) << '\n'; // 2 + cout << allocator.free(2) << '\n'; // 1 + cout << allocator.allocate(3, 4) << '\n'; // 3 + cout << allocator.allocate(1, 3) << '\n'; // 1 + + return 0; +} diff --git a/2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/CMakeLists.txt b/2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/CMakeLists.txt new file mode 100644 index 00000000..d2d08329 --- /dev/null +++ b/2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(D) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(D main.cpp) diff --git a/2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/main.cpp b/2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/main.cpp new file mode 100644 index 00000000..d2750c0a --- /dev/null +++ b/2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/main.cpp @@ -0,0 +1,106 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/ +/// Author : liuyubobobo +/// Time : 2022-12-15 + +#include +#include +#include +#include + +using namespace std; + + +/// Offline query, using UF +/// Time Complexity: O(nlogn + qlogq + q) +/// Space Complexity: O(n) +class UF{ + +private: + vector parent, sz; + +public: + UF(int n) : parent(n), sz(n, 1){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p , int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + sz[q_root] += sz[p_root]; + } + + int size(int p){ + return sz[find(p)]; + } +}; + +class Solution { + +private: + const int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + int R, C; + +public: + vector maxPoints(vector>& grid, vector& q) { + + R = grid.size(), C = grid[0].size(); + int n = R * C; + vector> v(R * C); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + int index = i * C + j; + v[index] = {grid[i][j], index}; + } + sort(v.begin(), v.end()); + + vector> queries(q.size()); + for(int i = 0; i < q.size(); i ++) queries[i] = {q[i], i}; + sort(queries.begin(), queries.end()); + + vector res(q.size()); + + UF uf(n); + int i = 0; + for(const pair& p: queries){ + int t = p.first, q_index = p.second; + while(i < n && v[i].first < t){ + int cx = v[i].second / C, cy = v[i].second % C; + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && grid[nx][ny] < t){ + uf.union_elements(v[i].second, nx * C + ny); + } + } + i ++; + } + res[q_index] = grid[0][0] < t ? uf.size(0) : 0; + } + return res; + } + +private: + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a968f2b9..bc948d42 100644 --- a/readme.md +++ b/readme.md @@ -2355,6 +2355,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2480 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | +| 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | +| 2502 | [Design Memory Allocator](https://leetcode.com/problems/design-memory-allocator/) | [无] | [C++](2001-2500/2502-Design-Memory-Allocator/cpp-2502/) | | | +| 2503 | [Maximum Number of Points From Grid Queries](https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/) | [无] | [C++](2001-2500/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/) | | | +| 2504 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 093e2746d9dc703ac700474d27103b495134069b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 15 Dec 2022 17:51:52 -0800 Subject: [PATCH 251/390] 2505 solved. --- .../cpp-2505/CMakeLists.txt | 6 +++ .../cpp-2505/main.cpp | 38 +++++++++++++++++++ readme.md | 1 + 3 files changed, 45 insertions(+) create mode 100644 2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/CMakeLists.txt create mode 100644 2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/main.cpp diff --git a/2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/CMakeLists.txt b/2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/CMakeLists.txt new file mode 100644 index 00000000..e671836c --- /dev/null +++ b/2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2505) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2505 main.cpp) diff --git a/2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/main.cpp b/2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/main.cpp new file mode 100644 index 00000000..b643a94f --- /dev/null +++ b/2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/bitwise-or-of-all-subsequence-sums/description/ +/// Author : liuyubobobo +/// Time : 2022-12-15 + +#include +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long subsequenceSumOr(vector& nums) { + + long long res = 0; + + int cnt = 0; + for(int p = 0; p < 62; p ++){ + cnt /= 2; + if(p <= 31) + for(int num: nums) cnt += ((num >> p) & 1); + if(cnt) res += (1ll << p); + } + + cnt /= 2; + if(cnt) res += (1ll << 62); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index bc948d42..5063edcf 100644 --- a/readme.md +++ b/readme.md @@ -2359,6 +2359,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2502 | [Design Memory Allocator](https://leetcode.com/problems/design-memory-allocator/) | [无] | [C++](2001-2500/2502-Design-Memory-Allocator/cpp-2502/) | | | | 2503 | [Maximum Number of Points From Grid Queries](https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/) | [无] | [C++](2001-2500/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/) | | | | 2504 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2505 | [Bitwise OR of All Subsequence Sums](https://leetcode.com/problems/bitwise-or-of-all-subsequence-sums/description/) | [无] | [C++](2001-2500/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 5c26d9cf53301594620db79b75789e47241fb27c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 15 Dec 2022 18:07:39 -0800 Subject: [PATCH 252/390] 2481-2484 solved. --- .../cpp-2481/CMakeLists.txt | 6 ++ .../cpp-2481/main.cpp | 29 +++++++ .../cpp-2482/CMakeLists.txt | 6 ++ .../cpp-2482/main.cpp | 40 ++++++++++ .../cpp-2483/CMakeLists.txt | 6 ++ .../cpp-2483/main.cpp | 35 ++++++++ .../cpp-2484/CMakeLists.txt | 6 ++ .../cpp-2484/main.cpp | 79 +++++++++++++++++++ readme.md | 4 + 9 files changed, 211 insertions(+) create mode 100644 2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/CMakeLists.txt create mode 100644 2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/main.cpp create mode 100644 2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/CMakeLists.txt create mode 100644 2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/main.cpp create mode 100644 2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/CMakeLists.txt create mode 100644 2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/main.cpp create mode 100644 2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/CMakeLists.txt create mode 100644 2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/main.cpp diff --git a/2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/CMakeLists.txt b/2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/CMakeLists.txt new file mode 100644 index 00000000..acad2941 --- /dev/null +++ b/2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2481) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2481 main.cpp) diff --git a/2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/main.cpp b/2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/main.cpp new file mode 100644 index 00000000..4983215f --- /dev/null +++ b/2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/minimum-cuts-to-divide-a-circle/description/ +/// Author : liuyubobobo +/// Time : 2022-12-02 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + int numberOfCuts(int n) { + + if(n == 1) return 0; + + int k = 1; + while(n % 2 == 0) k <<= 1, n >>= 1; + return n * ((k + 1) / 2); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/CMakeLists.txt b/2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/CMakeLists.txt new file mode 100644 index 00000000..89189094 --- /dev/null +++ b/2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2482) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2482 main.cpp) diff --git a/2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/main.cpp b/2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/main.cpp new file mode 100644 index 00000000..6305e93d --- /dev/null +++ b/2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/description/ +/// Author : liuyubobobo +/// Time : 2022-12-02 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(R * C) +/// Space Complexity: O(R + C) +class Solution { +public: + vector> onesMinusZeros(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + vector> res(R, vector(C)); + + vector onesRow(R, 0), zerosRow(R, 0); + vector onesCol(C, 0), zerosCol(C, 0); + for(int i = 0; i < R; i ++){ + for(int j = 0; j < C; j ++) + if(grid[i][j] == 0) zerosRow[i] ++, zerosCol[j] ++; + else onesRow[i] ++, onesCol[j] ++; + } + + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + res[i][j] = onesRow[i] + onesCol[j] - zerosRow[i] - zerosCol[j]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/CMakeLists.txt b/2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/CMakeLists.txt new file mode 100644 index 00000000..d6f67351 --- /dev/null +++ b/2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2483) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2483 main.cpp) diff --git a/2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/main.cpp b/2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/main.cpp new file mode 100644 index 00000000..9f3e3934 --- /dev/null +++ b/2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/minimum-penalty-for-a-shop/description/ +/// Author : liuyubobobo +/// Time : 2022-12-02 + +#include + +using namespace std; + + +/// Linear Scan +/// Time Compelxity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int bestClosingTime(string customers) { + + int res = 0, p = 0; + for(char c: customers) p += c == 'Y'; + + int best = p; + for(int i = 0; i < customers.size(); i ++){ + char c = customers[i]; + if(c == 'Y') p --; + else p ++; + if(p < best) best = p, res = i + 1; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/CMakeLists.txt b/2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/CMakeLists.txt new file mode 100644 index 00000000..7a42a496 --- /dev/null +++ b/2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2484) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2484 main.cpp) diff --git a/2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/main.cpp b/2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/main.cpp new file mode 100644 index 00000000..e9a8140a --- /dev/null +++ b/2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/main.cpp @@ -0,0 +1,79 @@ +/// Source : https://leetcode.com/problems/count-palindromic-subsequences/description/ +/// Author : liuyubobobo +/// Time : 2022-12-15 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + const int D = 10; + const long long MOD = 1e9 + 7; + +public: + int countPalindromes(string s) { + + int n = s.size(); + + vector> pre1(D, vector(n)), suf1(D, vector(n)); + for(char c = 0; c < D; c ++){ + pre1[c][0] = (s[0] - '0' == c); + for(int i = 1; i < n; i ++) + pre1[c][i] = pre1[c][i - 1] + (s[i] - '0' == c); + + suf1[c][n - 1] = (s[n - 1] - '0' == c); + for(int i = n - 2; i >= 0; i --) + suf1[c][i] = suf1[c][i + 1] + (s[i] - '0' == c); + } + + vector>> pre2(10, vector>(10, vector(n, 0))); + vector>> suf2(10, vector>(10, vector(n, 0))); + for(int i = 1; i < n; i ++){ + // c1, c2, x + for(char c1 = 0; c1 < D; c1 ++) + for(char c2 = 0; c2 < D; c2 ++){ + if(s[i] - '0' == c2) pre2[c2][c1][i] += pre1[c1][i - 1]; + pre2[c2][c1][i] += pre2[c2][c1][i - 1]; + } + } + for(int i = n - 2; i >= 0; i --){ + // x, c1, c2 + for(char c1 = 0; c1 <= 9; c1 ++) + for(char c2 = 0; c2 <= 9; c2 ++){ + if(s[i] - '0' == c1) suf2[c1][c2][i] += suf1[c2][i + 1]; + suf2[c1][c2][i] += suf2[c1][c2][i + 1]; + } + } + + long long res = 0, MOD = 1e9 + 7; + for(int i = 2; i < n - 2; i ++){ + // a, b, x, b, a + for(int a = 0; a < D; a ++) + for(int b = 0; b < D; b ++) + res += (1ll * pre2[b][a][i - 1] * suf2[b][a][i + 1]) % MOD, res %= MOD; + } + return res; + } +}; + + +int main() { + + cout << Solution().countPalindromes("103301") << '\n'; + // 2 + + cout << Solution().countPalindromes("0000000") << '\n'; + // 21 + + cout << Solution().countPalindromes("9999900000") << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 5063edcf..1ab3dc05 100644 --- a/readme.md +++ b/readme.md @@ -2353,6 +2353,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2478 | [Number of Beautiful Partitions](https://leetcode.com/problems/number-of-beautiful-partitions/) | [无] | [C++](2001-2500/2478-Number-of-Beautiful-Partitions/cpp-2478/) | | | | 2479 | [Maximum XOR of Two Non-Overlapping Subtrees](https://leetcode.com/problems/maximum-xor-of-two-non-overlapping-subtrees/description/) | [无] | [C++](2001-2500/2479-Maximum-XOR-of-Two-Non-Overlapping-Subtrees/cpp-2479/) | | | | 2480 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2481 | [Minimum Cuts to Divide a Circle](https://leetcode.com/problems/minimum-cuts-to-divide-a-circle/) | [无] | [C++](2001-2500/2481-Minimum-Cuts-to-Divide-a-Circle/cpp-2481/) | | | +| 2482 | [Difference Between Ones and Zeros in Row and Column](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/) | [无] | [C++](2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/) | | | +| 2483 | [Minimum Penalty for a Shop](https://leetcode.com/problems/minimum-penalty-for-a-shop/) | [无] | [C++](2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/) | | | +| 2484 | [Count Palindromic Subsequences](https://leetcode.com/problems/count-palindromic-subsequences/) | [无] | [C++](2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/) | | | | | | | | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | | 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | From b747812edbc1c8f9d7db41f7eb8eee99cf199187 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 17 Dec 2022 15:17:19 -0800 Subject: [PATCH 253/390] 1066 solved. --- .../cpp-1066/CMakeLists.txt | 6 ++ .../1066-Campus-Bikes-II/cpp-1066/main.cpp | 55 +++++++++++++++++++ readme.md | 2 + 3 files changed, 63 insertions(+) create mode 100644 1001-1500/1066-Campus-Bikes-II/cpp-1066/CMakeLists.txt create mode 100644 1001-1500/1066-Campus-Bikes-II/cpp-1066/main.cpp diff --git a/1001-1500/1066-Campus-Bikes-II/cpp-1066/CMakeLists.txt b/1001-1500/1066-Campus-Bikes-II/cpp-1066/CMakeLists.txt new file mode 100644 index 00000000..5266da59 --- /dev/null +++ b/1001-1500/1066-Campus-Bikes-II/cpp-1066/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1066) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1066 main.cpp) diff --git a/1001-1500/1066-Campus-Bikes-II/cpp-1066/main.cpp b/1001-1500/1066-Campus-Bikes-II/cpp-1066/main.cpp new file mode 100644 index 00000000..e2c4da89 --- /dev/null +++ b/1001-1500/1066-Campus-Bikes-II/cpp-1066/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/campus-bikes-ii/ +/// Author : liuyubobobo +/// Time : 2022-12-17 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * m * 2^m) +/// Space Complexity: O(n * 2^m) +class Solution { +public: + int assignBikes(vector>& workers, vector>& bikes) { + + int n = workers.size(), m = bikes.size(); + vector> dis(n, vector(m)); + for(int i = 0; i < n; i ++) + for(int j = 0; j < m; j ++) + dis[i][j] = get_dis(workers[i], bikes[j]); + + vector> dp(n, vector(1 << m, -1)); + return dfs(n, m, dis, 0, 0, dp); + } + +private: + int dfs(int n, int m, const vector>& dis, int index, int state, + vector>& dp){ + + if(index == n) return 0; + if(dp[index][state] != -1) return dp[index][state]; + + int res = INT_MAX; + for(int j = 0; j < m; j ++){ + if(state & (1 << j)) continue; + res = min(res, dis[index][j] + dfs(n, m, dis, index + 1, state | (1 << j), dp)); + } + return dp[index][state] = res; + } + + int get_dis(const vector& v1, const vector& v2){ + int x1 = v1[0], y1 = v1[1]; + int x2 = v2[0], y2 = v2[1]; + return abs(x1 - x2) + abs(y1 - y2); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 1ab3dc05..75326088 100644 --- a/readme.md +++ b/readme.md @@ -1057,6 +1057,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1062 | [Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/) | [无] | [C++](1001-1500/1062-Longest-Repeating-Substring/cpp-1062/) | | | | | | | | | | +| 1066 | [Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/) | [solution](https://leetcode.com/problems/campus-bikes-ii/solutions/1451959/campus-bikes-ii/) | [C++](1001-1500/1066-Campus-Bikes-II/cpp-1066/) | | | +| | | | | | | | 1072 | [Flip Columns For Maximum Number of Equal Rows](https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/) | [无] | [C++](1001-1500/1072-Flip-Columns-For-Maximum-Number-of-Equal-Rows/cpp-1072/) | | | | | | | | | | | 1074 | [Number of Submatrices That Sum to Target](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/) | [无] | [C++](1001-1500/1074-Number-of-Submatrices-That-Sum-to-Target/cpp-1074/) | | | From 5a80c2d58015fa4baeba854c8632027941053049 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 18 Dec 2022 15:16:43 -0800 Subject: [PATCH 254/390] 2506-2509 solved. --- .../cpp-2506/CMakeLists.txt | 6 + .../cpp-2506/main.cpp | 37 +++++ .../cpp-2507/CMakeLists.txt | 6 + .../cpp-2507/main.cpp | 69 ++++++++++ .../cpp-2508/CMakeLists.txt | 6 + .../cpp-2508/main.cpp | 126 ++++++++++++++++++ .../cpp-2509/CMakeLists.txt | 6 + .../cpp-2509/main.cpp | 52 ++++++++ readme.md | 4 + 9 files changed, 312 insertions(+) create mode 100644 2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/CMakeLists.txt create mode 100644 2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/main.cpp create mode 100644 2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/CMakeLists.txt create mode 100644 2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/main.cpp create mode 100644 2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/CMakeLists.txt create mode 100644 2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/main.cpp create mode 100644 2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/CMakeLists.txt create mode 100644 2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp diff --git a/2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/CMakeLists.txt b/2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/CMakeLists.txt new file mode 100644 index 00000000..41b4f523 --- /dev/null +++ b/2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2506) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2506 main.cpp) diff --git a/2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/main.cpp b/2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/main.cpp new file mode 100644 index 00000000..fcd3ace8 --- /dev/null +++ b/2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/count-pairs-of-similar-strings/description/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int similarPairs(vector& words) { + + int n = words.size(); + vector> f(n, vector(26, false)); + for(int i = 0; i < n; i ++){ + const string& word = words[i]; + for(char c: word) f[i][c - 'a'] = true; + } + + int res = 0; + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + res += f[i] == f[j]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/CMakeLists.txt b/2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/CMakeLists.txt new file mode 100644 index 00000000..aec8d639 --- /dev/null +++ b/2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2507) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2507 main.cpp) diff --git a/2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/main.cpp b/2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/main.cpp new file mode 100644 index 00000000..ff82cbf0 --- /dev/null +++ b/2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/main.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.com/problems/smallest-value-after-replacing-with-sum-of-prime-factors/description/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include +#include + +using namespace std; + + +/// Sieve + Simulation +/// Time Complexity: O(MAXN * logMAXN) +/// Space Compelxity: O(MAXN) +class Solution { + +private: + const int MAXN = 1e5; + +public: + int smallestValue(int n) { + + vector sieve_table = sieve(MAXN); + vector visited(MAXN + 1, false); + while(!visited[n]){ + visited[n] = true; + n = calc(n, sieve_table); + } + + for(int i = 0; i <= MAXN; i ++) + if(visited[i]) return i; + return -1; + } + +private: + int calc(int x, const vector& sieve_table){ + + int res = 0; + while(x != 1){ + int p = sieve_table[x]; + res += p; + x /= p; + } + return res; + } + + vector sieve(int n){ + + vector table(n + 1, 0); + vector primes; + table[0] = table[1] = 1; + for(int i = 2; i <= n; i ++){ + if(table[i] == 0) { + primes.push_back(i); + table[i] = i; + } + for(int j = 0; j < primes.size() && primes[j] <= table[i] && i * primes[j] <= n; j ++) + table[i * primes[j]] = primes[j]; + } + return table; + } +}; + + +int main() { + + cout << Solution().smallestValue(64) << '\n'; + + return 0; +} diff --git a/2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/CMakeLists.txt b/2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/CMakeLists.txt new file mode 100644 index 00000000..452024e8 --- /dev/null +++ b/2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2508) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2508 main.cpp) diff --git a/2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/main.cpp b/2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/main.cpp new file mode 100644 index 00000000..704b72a1 --- /dev/null +++ b/2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/main.cpp @@ -0,0 +1,126 @@ +/// Source : https://leetcode.com/problems/add-edges-to-make-degrees-of-all-nodes-even/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(V + E) +/// Space Complexity: O(V + E) +class Solution { +public: + bool isPossible(int n, vector>& edges) { + + vector> g(n); + for(const vector& e: edges){ + int u = e[0] - 1, v = e[1] - 1; + g[u].insert(v), g[v].insert(u); + } + + int is_full = 0; + vector oddv; + for(int i = 0; i < n; i ++){ + if(g[i].size() == n - 1) is_full ++; + else if(g[i].size() % 2 == 1) oddv.push_back(i); + } + +// cout << "is_full : " << is_full << '\n'; +// for(int e: oddv) cout << e << ' '; cout << '\n'; +// cout << g[1].count(2) << ' ' << g[2].count(1) << '\n'; + if(is_full && (n - 1) % 2) return false; + + if(oddv.empty()) return true; + if(oddv.size() == 2) { + if(ok_to_connect(g, oddv[0], oddv[1])) return true; + for(int i = 0; i < n; i ++) + if(g[i].size() + 2 <= n - 1 && ok_to_connect(g, i, oddv[0]) && ok_to_connect(g, i, oddv[1])) + return true; + return false; + } + + if(oddv.size() != 4) return false; + + do{ + if(ok_to_connect(g, oddv[0], oddv[1]) && ok_to_connect(g, oddv[2], oddv[3])) + return true; + }while(next_permutation(oddv.begin(), oddv.end())); + return false; + } + +private: + static bool ok_to_connect(const vector>& g, int u, int v){ + return g[u].count(v) == 0 && g[v].count(u) == 0; + } +}; + + +int main() { + + vector> edges1 = {{1, 2}, {3, 4}}; + cout << Solution().isPossible(4, edges1) << '\n'; + // 1 + + vector> edges2 = { + {5, 9}, + {8, 1}, + {2, 3}, + {7, 10}, + {3, 6}, + {6, 7}, + {7, 8}, + {5, 1}, + {5, 7}, + {10, 11}, + {3, 7}, + {6, 11}, + {8, 11}, + {3, 4}, + {8, 9}, + {9, 1}, + {2, 10}, + {9, 11}, + {5, 11}, + {2, 5}, + {8, 10}, + {2, 7}, + {4, 1}, + {3, 10}, + {6, 1}, + {4, 9}, + {4, 6}, + {4, 5}, + {2, 4}, + {2, 11}, + {5, 8}, + {6, 9}, + {4, 10}, + {3, 11}, + {4, 7}, + {3, 5}, + {7, 1}, + {2, 9}, + {6, 10}, + {10, 1}, + {5, 6}, + {3, 9}, + {2, 6}, + {7, 9}, + {4, 11}, + {4, 8}, + {6, 8}, + {3, 8}, + {9, 10}, + {5, 10}, + {2, 8}, + {7, 11} + }; + cout << Solution().isPossible(11, edges2) << '\n'; + // 0 + + return 0; +} diff --git a/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/CMakeLists.txt b/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/CMakeLists.txt new file mode 100644 index 00000000..1afa23dd --- /dev/null +++ b/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2509) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2509 main.cpp) diff --git a/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp b/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp new file mode 100644 index 00000000..5e512465 --- /dev/null +++ b/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; + + +class Solution { +public: + vector cycleLengthQueries(int n, vector>& queries) { + + int m = queries.size(); + + vector res(m); + for(int qindex = 0; qindex < m; qindex ++){ + int u = queries[qindex][0], v = queries[qindex][1]; + + vector upath, vpath; + while(u) upath.push_back(u), u /= 2; + while(v) vpath.push_back(v), v /= 2; + + reverse(upath.begin(), upath.end()); + reverse(vpath.begin(), vpath.end()); + + int lca_pos = -1; + for(int i = 0; i < upath.size() && i < vpath.size(); i ++) + if(upath[i] != vpath[i]){ + lca_pos = i; break; + } + + if(lca_pos == -1) lca_pos = min(upath.size(), vpath.size()); + int tres = 0; + tres += (int)upath.size() - lca_pos; + tres += (int)vpath.size() - lca_pos; + tres ++; + res[qindex] = tres; + } + return res; + } +}; + + +int main() { + + vector> queries1 = {{5, 3}, {4, 7}, {2, 3}}; + Solution().cycleLengthQueries(3, queries1); + + vector> queries2 = {{1, 2}}; + Solution().cycleLengthQueries(2, queries2); + + return 0; +} diff --git a/readme.md b/readme.md index 75326088..481f00bc 100644 --- a/readme.md +++ b/readme.md @@ -2366,6 +2366,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2503 | [Maximum Number of Points From Grid Queries](https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/) | [无] | [C++](2001-2500/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/) | | | | 2504 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2505 | [Bitwise OR of All Subsequence Sums](https://leetcode.com/problems/bitwise-or-of-all-subsequence-sums/description/) | [无] | [C++](2001-2500/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/) | | | +| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [无] | [C++](2001-2500/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/) | | | +| 2507 | [Smallest Value After Replacing With Sum of Prime Factors](https://leetcode.com/problems/smallest-value-after-replacing-with-sum-of-prime-factors/) | [无] | [C++](2001-2500/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/) | | | +| 2508 | [Add Edges to Make Degrees of All Nodes Even](https://leetcode.com/problems/add-edges-to-make-degrees-of-all-nodes-even/) | [无] | [C++](2001-2500/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/) | | | +| 2509 | [Cycle Length Queries in a Tree](https://leetcode.com/problems/cycle-length-queries-in-a-tree/) | [无] | [C++](2001-2500/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From a2d94d4199b15b82a0c0da227d6c3237179f79c0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 18 Dec 2022 17:23:11 -0800 Subject: [PATCH 255/390] 2485-2488 solved. --- .../cpp-2485/CMakeLists.txt | 6 ++ .../cpp-2485/main.cpp | 30 ++++++++++ .../cpp-2486/CMakeLists.txt | 6 ++ .../cpp-2486/main.cpp | 28 ++++++++++ .../cpp-2487/CMakeLists.txt | 6 ++ .../cpp-2487/main.cpp | 45 +++++++++++++++ .../cpp-2488/CMakeLists.txt | 6 ++ .../cpp-2488/main.cpp | 55 +++++++++++++++++++ .../cpp-2509/main.cpp | 7 +++ readme.md | 4 ++ 10 files changed, 193 insertions(+) create mode 100644 2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/CMakeLists.txt create mode 100644 2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/main.cpp create mode 100644 2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/CMakeLists.txt create mode 100644 2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/main.cpp create mode 100644 2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/CMakeLists.txt create mode 100644 2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/main.cpp create mode 100644 2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/CMakeLists.txt create mode 100644 2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/main.cpp diff --git a/2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/CMakeLists.txt b/2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/CMakeLists.txt new file mode 100644 index 00000000..8c8003d2 --- /dev/null +++ b/2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2485) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2485 main.cpp) diff --git a/2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/main.cpp b/2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/main.cpp new file mode 100644 index 00000000..8bf648d5 --- /dev/null +++ b/2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/find-the-pivot-integer/description/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int pivotInteger(int n) { + + // [1...x] and [x ... n] + for(int x = 1; x <= n; x ++){ + if((1 + x) * x / 2 == (x + n) * (n - x + 1) / 2) return x; + } + return -1; + } +}; + + +int main() { + + + return 0; +} diff --git a/2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/CMakeLists.txt b/2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/CMakeLists.txt new file mode 100644 index 00000000..95348428 --- /dev/null +++ b/2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2486) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2486 main.cpp) diff --git a/2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/main.cpp b/2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/main.cpp new file mode 100644 index 00000000..230f3528 --- /dev/null +++ b/2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/main.cpp @@ -0,0 +1,28 @@ +/// Source : https://leetcode.com/problems/append-characters-to-string-to-make-subsequence/description/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int appendCharacters(string s, string t) { + + int j = 0; + for(int i = 0; i < s.size(); i ++) + if(j < t.size() && s[i] == t[j]) j ++; + return t.size() - j; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/CMakeLists.txt b/2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/CMakeLists.txt new file mode 100644 index 00000000..2a40371c --- /dev/null +++ b/2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2487) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2487 main.cpp) diff --git a/2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/main.cpp b/2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/main.cpp new file mode 100644 index 00000000..23f6d690 --- /dev/null +++ b/2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/remove-nodes-from-linked-list/description/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include +#include + +using namespace std; + + +/// Mono Stack +/// Time Complexity: O(n) +/// Space Compelxity: O(n) + +/// Definition for singly-linked list. +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution { +public: + ListNode* removeNodes(ListNode* head) { + + vector stack; + for(ListNode* node = head; node; node = node->next){ + while(!stack.empty() && stack.back()->val < node->val) stack.pop_back(); + stack.push_back(node); + } + + for(int i = 0; i + 1 < stack.size(); i ++) + stack[i]->next = stack[i + 1]; + stack.back()->next = nullptr; + return stack[0]; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/CMakeLists.txt b/2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/CMakeLists.txt new file mode 100644 index 00000000..2e2a145f --- /dev/null +++ b/2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2488) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2488 main.cpp) diff --git a/2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/main.cpp b/2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/main.cpp new file mode 100644 index 00000000..cea29bf4 --- /dev/null +++ b/2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/count-subarrays-with-median-k/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include +#include +#include +#include + +using namespace std; + + +/// Median Classic +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int countSubarrays(vector& nums, int k) { + + int n = nums.size(); + + vector data(n, 0); + int kpos = -1; + for(int i = 0; i < n; i ++) { + if (nums[i] < k) data[i] = -1; + else if (nums[i] > k) data[i] = 1; + else kpos = i; + } + + map f; + f[0] = 1; + int cur = 0; + for(int i = kpos + 1; i < n; i ++){ + cur += data[i]; + f[cur] ++; + } + + int lsum = accumulate(data.begin(), data.begin() + kpos + 1, 0); + int res = 0; + for(int l = 0; l <= kpos; l ++){ + auto iter = f.find(-lsum); + if(iter != f.end()) res += iter->second; + iter = f.find(1-lsum); + if(iter != f.end()) res += iter->second; + lsum -= data[l]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp b/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp index 5e512465..92405cab 100644 --- a/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp +++ b/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp @@ -1,3 +1,7 @@ +/// Source : https://leetcode.com/problems/cycle-length-queries-in-a-tree/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + #include #include #include @@ -5,6 +9,9 @@ using namespace std; +/// Math +/// Time Complexity: O(qlogn) +/// Space Complexity: O(logn) class Solution { public: vector cycleLengthQueries(int n, vector>& queries) { diff --git a/readme.md b/readme.md index 481f00bc..4a520b8f 100644 --- a/readme.md +++ b/readme.md @@ -2359,6 +2359,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2482 | [Difference Between Ones and Zeros in Row and Column](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/) | [无] | [C++](2001-2500/2482-Difference-Between-Ones-and-Zeros-in-Row-and-Column/cpp-2482/) | | | | 2483 | [Minimum Penalty for a Shop](https://leetcode.com/problems/minimum-penalty-for-a-shop/) | [无] | [C++](2001-2500/2483-Minimum-Penalty-for-a-Shop/cpp-2483/) | | | | 2484 | [Count Palindromic Subsequences](https://leetcode.com/problems/count-palindromic-subsequences/) | [无] | [C++](2001-2500/2484-Count-Palindromic-Subsequences/cpp-2484/) | | | +| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer/) | [无] | [C++](2001-2500/2485-Find-the-Pivot-Integer/cpp-2485/) | | | +| 2486 | [Append Characters to String to Make Subsequence](https://leetcode.com/problems/append-characters-to-string-to-make-subsequence/) | [无] | [C++](2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/) | | | +| 2487 | [Remove Nodes From Linked List](https://leetcode.com/problems/remove-nodes-from-linked-list/) | [无] | [C++](2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/) | | | +| 2488 | [Count Subarrays With Median K](https://leetcode.com/problems/count-subarrays-with-median-k/) | [无] | [C++](2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/) | | | | | | | | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | | 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | From 1c3fc999c1e6f010d7c207c09870724b07f478aa Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 19 Dec 2022 13:09:36 -0800 Subject: [PATCH 256/390] 2489 solved. --- .../cpp-2489/CMakeLists.txt | 6 +++ .../cpp-2489/main.cpp | 40 +++++++++++++++++++ readme.md | 1 + 3 files changed, 47 insertions(+) create mode 100644 2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/CMakeLists.txt create mode 100644 2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/main.cpp diff --git a/2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/CMakeLists.txt b/2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/CMakeLists.txt new file mode 100644 index 00000000..ab10eb3d --- /dev/null +++ b/2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2489) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2489 main.cpp) diff --git a/2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/main.cpp b/2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/main.cpp new file mode 100644 index 00000000..d4c0334d --- /dev/null +++ b/2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/number-of-substrings-with-fixed-ratio/description/ +/// Author : liuyubobobo +/// Time : 2022-12-19 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long fixedRatio(string s, int num1, int num2) { + + int n = s.size(); + vector presum1(n + 1, 0); + for(int i = 0; i < n; i ++) presum1[i + 1] = presum1[i] + (s[i] == '1'); + + long long res = 0; + map table; + table[0] = 1; + for(int i = 0; i < n; i ++){ + int t = presum1[i + 1] * num1 - (i + 1 - presum1[i + 1]) * num2; + auto iter = table.find(t); + if(iter != table.end()) res += iter->second; + table[t] ++; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 4a520b8f..c2c90103 100644 --- a/readme.md +++ b/readme.md @@ -2363,6 +2363,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2486 | [Append Characters to String to Make Subsequence](https://leetcode.com/problems/append-characters-to-string-to-make-subsequence/) | [无] | [C++](2001-2500/2486-Append-Characters-to-String-to-Make-Subsequence/cpp-2486/) | | | | 2487 | [Remove Nodes From Linked List](https://leetcode.com/problems/remove-nodes-from-linked-list/) | [无] | [C++](2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/) | | | | 2488 | [Count Subarrays With Median K](https://leetcode.com/problems/count-subarrays-with-median-k/) | [无] | [C++](2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/) | | | +| 2489 | [Number of Substrings With Fixed Ratio](https://leetcode.com/problems/number-of-substrings-with-fixed-ratio/description/) | [无] | [C++](2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/) | | | | | | | | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | | 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | From 4843b221b621cff95f419d3d22cd63667ae2aeec Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 22 Dec 2022 15:21:12 -0800 Subject: [PATCH 257/390] 2510 solved. --- .../cpp-2510/CMakeLists.txt | 6 +++ .../cpp-2510/main.cpp | 44 +++++++++++++++++++ readme.md | 1 + 3 files changed, 51 insertions(+) create mode 100644 2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/CMakeLists.txt create mode 100644 2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/main.cpp diff --git a/2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/CMakeLists.txt b/2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/CMakeLists.txt new file mode 100644 index 00000000..b317dc14 --- /dev/null +++ b/2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2510) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2510 main.cpp) diff --git a/2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/main.cpp b/2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/main.cpp new file mode 100644 index 00000000..e29004b1 --- /dev/null +++ b/2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/check-if-there-is-a-path-with-equal-number-of-0s-and-1s/description/ +/// Author : liuyubobobo +/// Time : 2022-12-22 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * m * (n + m)) +/// Space Complexity: O(n * m * (n + m)) +class Solution { +public: + bool isThereAPath(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + if((n + m - 1) & 1) return false; + + const int OFFSET = n + m - 1; + vector>> dp(n, vector>(m, vector(2 * OFFSET + 1, false))); + + // zero - one + dp[0][0][(grid[0][0] == 0 ? 1 : -1) + OFFSET] = true; + for(int i = 0; i < n; i ++) + for(int j = 0; j < m; j ++) + for(int value = -OFFSET; value <= OFFSET; value ++){ + if(!dp[i][j][ value + OFFSET]) continue; + if(i + 1 < n && value + (grid[i + 1][j] ? -1 : 1) <= OFFSET) + dp[i + 1][j][value + (grid[i + 1][j] ? -1 : 1) + OFFSET] = true; + if(j + 1 < m && value + (grid[i][j + 1] ? -1 : 1) <= OFFSET) + dp[i][j + 1][value + (grid[i][j + 1] ? -1 : 1) + OFFSET] = true; + } + return dp[n - 1][m - 1][OFFSET]; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index c2c90103..ddf60e3a 100644 --- a/readme.md +++ b/readme.md @@ -2375,6 +2375,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2507 | [Smallest Value After Replacing With Sum of Prime Factors](https://leetcode.com/problems/smallest-value-after-replacing-with-sum-of-prime-factors/) | [无] | [C++](2001-2500/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/) | | | | 2508 | [Add Edges to Make Degrees of All Nodes Even](https://leetcode.com/problems/add-edges-to-make-degrees-of-all-nodes-even/) | [无] | [C++](2001-2500/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/) | | | | 2509 | [Cycle Length Queries in a Tree](https://leetcode.com/problems/cycle-length-queries-in-a-tree/) | [无] | [C++](2001-2500/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/) | | | +| 2510 | [Check if There is a Path With Equal Number of 0's And 1's](https://leetcode.com/problems/check-if-there-is-a-path-with-equal-number-of-0s-and-1s/description/) | [无] | [C++](2001-2500/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From a014f0e7c0d60c9ed8f76e3a5e2401359ff4b92c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 22 Dec 2022 16:36:30 -0800 Subject: [PATCH 258/390] 2490-2493 solved. --- .../cpp-2490/CMakeLists.txt | 6 ++ .../2490-Circular-Sentence/cpp-2490/main.cpp | 38 ++++++++ .../cpp-2491/CMakeLists.txt | 6 ++ .../cpp-2491/main.cpp | 46 +++++++++ .../cpp-2492/CMakeLists.txt | 6 ++ .../cpp-2492/main.cpp | 60 ++++++++++++ .../cpp-2493/CMakeLists.txt | 6 ++ .../cpp-2493/main.cpp | 95 +++++++++++++++++++ readme.md | 5 + 9 files changed, 268 insertions(+) create mode 100644 2001-2500/2490-Circular-Sentence/cpp-2490/CMakeLists.txt create mode 100644 2001-2500/2490-Circular-Sentence/cpp-2490/main.cpp create mode 100644 2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/CMakeLists.txt create mode 100644 2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/main.cpp create mode 100644 2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/CMakeLists.txt create mode 100644 2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/main.cpp create mode 100644 2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/CMakeLists.txt create mode 100644 2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/main.cpp diff --git a/2001-2500/2490-Circular-Sentence/cpp-2490/CMakeLists.txt b/2001-2500/2490-Circular-Sentence/cpp-2490/CMakeLists.txt new file mode 100644 index 00000000..4673bfac --- /dev/null +++ b/2001-2500/2490-Circular-Sentence/cpp-2490/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2490) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2490 main.cpp) diff --git a/2001-2500/2490-Circular-Sentence/cpp-2490/main.cpp b/2001-2500/2490-Circular-Sentence/cpp-2490/main.cpp new file mode 100644 index 00000000..c7ed13d0 --- /dev/null +++ b/2001-2500/2490-Circular-Sentence/cpp-2490/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/circular-sentence/description/ +/// Author : liuyubobobo +/// Time : 2022-12-19 + +#include +#include +#include + +using namespace std; + + +/// Simualtion +/// Time Complexity: O(|n|) +/// Space Complexity: O(|n|) +class Solution { +public: + bool isCircularSentence(string sentence) { + + vector words; + for(int start = 0, i = 1; i <= sentence.size(); i ++){ + if(i == sentence.size() || sentence[i] == ' '){ + words.push_back(sentence.substr(start, i - start)); + start = i + 1; + i = start; + } + } + + for(int i = 1; i < words.size(); i ++) + if(words[i - 1].back() != words[i][0]) return false; + return words.back().back() == words[0][0]; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/CMakeLists.txt b/2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/CMakeLists.txt new file mode 100644 index 00000000..8c7ca595 --- /dev/null +++ b/2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2491) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2491 main.cpp) diff --git a/2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/main.cpp b/2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/main.cpp new file mode 100644 index 00000000..1e4b6bb4 --- /dev/null +++ b/2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/divide-players-into-teams-of-equal-skill/description/ +/// Author : liuyubobobo +/// Time : 2022-12-19 + +#include +#include +#include + +using namespace std; + + +/// Using Set +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long dividePlayers(vector& skill) { + + int n = skill.size(); + + multiset s; + int sum = 0; + for(int e: skill) s.insert(e), sum += e; + + if(sum % (n / 2)) return -1; + int t = sum / (n / 2); + + long long res = 0; + while(!s.empty()){ + int a = *s.begin(); + s.erase(s.begin()); + int b = t - a; + auto iter = s.find(b); + if(iter == s.end()) return -1; + s.erase(iter); + res += a * b; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/CMakeLists.txt b/2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/CMakeLists.txt new file mode 100644 index 00000000..a31202df --- /dev/null +++ b/2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2492) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2492 main.cpp) diff --git a/2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/main.cpp b/2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/main.cpp new file mode 100644 index 00000000..2aa82c8f --- /dev/null +++ b/2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/description/ +/// Author : liuyubobobo +/// Time : 2022-12-19 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(eloge + (v + e) + e) +/// Space Complexity: O(v + e) +class Solution { +public: + int minScore(int n, vector>& roads) { + + sort(roads.begin(), roads.end(), + [](const vector& road1, const vector& road2){ + return road1[2] < road2[2]; + }); + + vector> g(n); + for(int i = 0; i < roads.size(); i ++){ + int u = roads[i][0] - 1, v = roads[i][1] - 1; + g[u].push_back(v), g[v].push_back(u); + } + + vector visited_s(n, false); + dfs(g, 0, 0, visited_s); + + vector visited_t(n, false); + dfs(g, n - 1, n - 1, visited_t); + + for(const vector& road: roads){ + int u = road[0] - 1, v = road[1] - 1, w = road[2]; + if(visited_s[u] && visited_t[v]) return w; + if(visited_s[v] && visited_t[u]) return w; + } + return -1; + } + +private: + void dfs(const vector>& g, int u, int p, vector& visited){ + visited[u] = true; + for(int v: g[u]){ + if(v == p || visited[v]) continue; + dfs(g, v, u, visited); + } + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/CMakeLists.txt b/2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/CMakeLists.txt new file mode 100644 index 00000000..c65d3701 --- /dev/null +++ b/2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2493) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2493 main.cpp) diff --git a/2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/main.cpp b/2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/main.cpp new file mode 100644 index 00000000..2c70d63b --- /dev/null +++ b/2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/main.cpp @@ -0,0 +1,95 @@ +/// Source : https://leetcode.com/problems/divide-nodes-into-the-maximum-number-of-groups/description/ +/// Author : liuyubobobo +/// Time : 2022-12-22 + +#include +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(V * (V + E)) +/// Space Complexity: O(V + E) +class Solution { +public: + int magnificentSets(int n, vector>& edges) { + + vector> g(n); + for(const vector& e: edges){ + int a = e[0] - 1, b = e[1] - 1; + g[a].push_back(b), g[b].push_back(a); + } + + vector cc(n, -1); + int cc_index = 0, res = 0; + for(int u = 0; u < n; u ++){ + if(cc[u] != -1) continue; + dfs(g, u, cc_index, cc); + + int tres = solve(n, g, cc_index, cc); + if(tres == -1) return -1; + res += tres; + + cc_index ++; + } + return res; + } + +private: + int solve(int n, const vector>& g, int cc_index, const vector& cc){ + + int res = -1; + for(int s = 0; s < n; s ++){ + if(cc[s] != cc_index) continue; + res = max(res, solve(n, g, s)); + } + + return res; + } + + int solve(int n, const vector>& g, int s){ + + vector level(n, -1), pre(n, -1); + + queue q; + q.push(s); + level[s] = 1; + pre[s] = 0; + while(!q.empty()){ + int u = q.front(); q.pop(); + for(int v: g[u]){ + if(level[v] != -1){ + if(abs(level[v] - level[u]) == 1) continue; + else return -1; + } + level[v] = level[u] + 1; + pre[v] = u; + q.push(v); + } + } + return *max_element(level.begin(), level.end()); + } + + void dfs(const vector>& g, int u, int cc_index, vector& cc){ + + cc[u] = cc_index; + for(int v: g[u]){ + if(cc[v] == -1) dfs(g, v, cc_index, cc); + } + } +}; + + +int main() { + + vector> edges1 = { + {1,2},{1,4},{1,5},{2,6},{2,3},{4,6} + }; + cout << Solution().magnificentSets(6, edges1) << '\n'; + // 4 + + return 0; +} diff --git a/readme.md b/readme.md index ddf60e3a..495bb6cd 100644 --- a/readme.md +++ b/readme.md @@ -2364,6 +2364,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2487 | [Remove Nodes From Linked List](https://leetcode.com/problems/remove-nodes-from-linked-list/) | [无] | [C++](2001-2500/2487-Remove-Nodes-From-Linked-List/cpp-2487/) | | | | 2488 | [Count Subarrays With Median K](https://leetcode.com/problems/count-subarrays-with-median-k/) | [无] | [C++](2001-2500/2488-Count-Subarrays-With-Median-K/cpp-2488/) | | | | 2489 | [Number of Substrings With Fixed Ratio](https://leetcode.com/problems/number-of-substrings-with-fixed-ratio/description/) | [无] | [C++](2001-2500/2489-Number-of-Substrings-With-Fixed-Ratio/cpp-2489/) | | | +| 2490 | [Circular Sentence](https://leetcode.com/problems/circular-sentence/) | [无] | [C++](2001-2500/2490-Circular-Sentence/cpp-2490/) | | | +| 2491 | [Divide Players Into Teams of Equal Skill](https://leetcode.com/problems/divide-players-into-teams-of-equal-skill/) | [无] | [C++](2001-2500/2491-Divide-Players-Into-Teams-of-Equal-Skill/cpp-2491/) | | | +| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/) | [无] | [C++](2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/) | | | +| 2493 | [Divide Nodes Into the Maximum Number of Groups](https://leetcode.com/problems/divide-nodes-into-the-maximum-number-of-groups/) | [无] | [C++](2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/) | | | +| 2494 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | | 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | From 351fcbd53fc8582db3499dd204b1021879ac66db Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 22 Dec 2022 17:10:55 -0800 Subject: [PATCH 259/390] 2495 solved. --- .../cpp-2495/CMakeLists.txt | 6 +++ .../cpp-2495/main.cpp | 42 +++++++++++++++++++ readme.md | 1 + 3 files changed, 49 insertions(+) create mode 100644 2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/CMakeLists.txt create mode 100644 2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/main.cpp diff --git a/2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/CMakeLists.txt b/2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/CMakeLists.txt new file mode 100644 index 00000000..9ff5c9ce --- /dev/null +++ b/2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2495) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2495 main.cpp) diff --git a/2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/main.cpp b/2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/main.cpp new file mode 100644 index 00000000..93b26744 --- /dev/null +++ b/2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/number-of-subarrays-having-even-product/description/ +/// Author : liuyubobobo +/// Time : 2022-12-22 + +#include +#include + +using namespace std; + + +/// Mono Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long evenProduct(vector& nums) { + + int n = nums.size(); + vector next_even(n, n), stack; + + for(int i = 0; i < n; i ++){ + stack.push_back(i); + if(nums[i] % 2 == 0){ + while(!stack.empty()){ + next_even[stack.back()] = i; + stack.pop_back(); + } + } + } + + long long res = 0; + for(int l = 0; l < n; l ++) + res += n - next_even[l]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 495bb6cd..c0e8fb3b 100644 --- a/readme.md +++ b/readme.md @@ -2369,6 +2369,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/) | [无] | [C++](2001-2500/2492-Minimum-Score-of-a-Path-Between-Two-Cities/cpp-2492/) | | | | 2493 | [Divide Nodes Into the Maximum Number of Groups](https://leetcode.com/problems/divide-nodes-into-the-maximum-number-of-groups/) | [无] | [C++](2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/) | | | | 2494 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2495 | [Number of Subarrays Having Even Product](https://leetcode.com/problems/number-of-subarrays-having-even-product/description/) | [无] | [C++](2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495) | | | | | | | | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | | 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | From b145ccdc3660bea46f48db7b124adc2663cb9834 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 28 Dec 2022 14:14:59 -0800 Subject: [PATCH 260/390] 2515-2518 solved. --- .../cpp-2515/CMakeLists.txt | 6 ++ .../cpp-2515/main.cpp | 43 +++++++++++ .../cpp-2516/CMakeLists.txt | 6 ++ .../cpp-2516/main.cpp | 64 ++++++++++++++++ .../cpp-2517/CMakeLists.txt | 6 ++ .../cpp-2517/main.cpp | 50 +++++++++++++ .../cpp-2518/CMakeLists.txt | 6 ++ .../cpp-2518/main.cpp | 74 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 260 insertions(+) create mode 100644 2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/CMakeLists.txt create mode 100644 2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/main.cpp create mode 100644 2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/CMakeLists.txt create mode 100644 2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/main.cpp create mode 100644 2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/CMakeLists.txt create mode 100644 2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/main.cpp create mode 100644 2501-3000/2518-Number-of-Great-Partitions/cpp-2518/CMakeLists.txt create mode 100644 2501-3000/2518-Number-of-Great-Partitions/cpp-2518/main.cpp diff --git a/2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/CMakeLists.txt b/2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/CMakeLists.txt new file mode 100644 index 00000000..4263d09b --- /dev/null +++ b/2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2515) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2515 main.cpp) diff --git a/2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/main.cpp b/2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/main.cpp new file mode 100644 index 00000000..3dfcced9 --- /dev/null +++ b/2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/description/ +/// Author : liuyubobobo +/// Time : 2022-12-26 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int closetTarget(vector& words, string target, int startIndex) { + + auto iter = find(words.begin(), words.end(), target); + if(iter == words.end()) return -1; + + vector v; + while(iter != words.end()) { + int targetIndex = iter - words.begin(); + v.push_back(targetIndex); + iter = find(words.begin() + targetIndex + 1, words.end(), target); + } + + int n = words.size(), res = INT_MAX; + for(int targetIndex: v){ + int a = (targetIndex - startIndex + n) % n; + int b = (startIndex - targetIndex + n) % n; + res = min(res, min(a, b)); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/CMakeLists.txt b/2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/CMakeLists.txt new file mode 100644 index 00000000..a00c5b49 --- /dev/null +++ b/2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2516) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2516 main.cpp) diff --git a/2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/main.cpp b/2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/main.cpp new file mode 100644 index 00000000..ae976a9f --- /dev/null +++ b/2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/main.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/ +/// Author : liuyubobobo +/// Time : 2022-12-26 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +public: + int takeCharacters(string s, int k) { + + int n = s.size(); + + vector prea(n + 1, 0), preb(n + 1, 0), prec(n + 1, 0); + for(int i = 0; i < n; i ++){ + prea[i + 1] = prea[i] + (s[i] == 'a'); + preb[i + 1] = preb[i] + (s[i] == 'b'); + prec[i + 1] = prec[i] + (s[i] == 'c'); + } + + if(prea.back() < k || preb.back() < k || prec.back() < k) return -1; + + int res = max3(get(prea, k), get(preb, k), get(prec, k)); + + int a = 0, b = 0, c = 0; + for(int i = n - 1; i >= 0; i --){ + a += s[i] == 'a'; + b += s[i] == 'b'; + c += s[i] == 'c'; + + int t = max3(get(prea, max(k - a, 0)), get(preb, max(k - b, 0)), get(prec, max(k - c, 0))); + res = min(res, t + n - i); + } + return res; + } + +private: + int max3(int a, int b, int c){ + return max(a, max(b, c)); + } + + int get(const vector& v, int k){ + auto iter = lower_bound(v.begin(), v.end(), k); + assert(iter != v.end()); + return iter - v.begin(); + } +}; + + +int main() { + + string s = "aabaaaacaabc"; + cout << Solution().takeCharacters(s, 2) << '\n'; + // 8 + + return 0; +} diff --git a/2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/CMakeLists.txt b/2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/CMakeLists.txt new file mode 100644 index 00000000..d2460ca5 --- /dev/null +++ b/2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2517) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2517 main.cpp) diff --git a/2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/main.cpp b/2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/main.cpp new file mode 100644 index 00000000..7ac1220d --- /dev/null +++ b/2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/maximum-tastiness-of-candy-basket/description/ +/// Author : liuyubobobo +/// Time : 2022-12-26 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn + nlong(max_price - min_price)) +/// Space Complexity: O(1) +class Solution { +public: + int maximumTastiness(vector& price, int k) { + + sort(price.begin(), price.end()); + + int n = price.size(); + int l = 0, r = price.back() - price[0]; + while(l < r){ + int mid = (l + r + 1) / 2; + if(ok(price, mid, k)) l = mid; + else r = mid - 1; + } + return l; + } + +private: + bool ok(const vector& v, int t, int k){ + + int cnt = 1, cur = v[0]; + while(cnt < k){ + cur += t; + auto iter = lower_bound(v.begin(), v.end(), cur); + if(iter == v.end()) return false; + cur = *iter; + cnt ++; + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2518-Number-of-Great-Partitions/cpp-2518/CMakeLists.txt b/2501-3000/2518-Number-of-Great-Partitions/cpp-2518/CMakeLists.txt new file mode 100644 index 00000000..2ab5083e --- /dev/null +++ b/2501-3000/2518-Number-of-Great-Partitions/cpp-2518/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2518) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2518 main.cpp) diff --git a/2501-3000/2518-Number-of-Great-Partitions/cpp-2518/main.cpp b/2501-3000/2518-Number-of-Great-Partitions/cpp-2518/main.cpp new file mode 100644 index 00000000..09a2f984 --- /dev/null +++ b/2501-3000/2518-Number-of-Great-Partitions/cpp-2518/main.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/number-of-great-partitions/description/ +/// Author : liuyubobobo +/// Time : 2022-12-28 + +#include +#include + +using namespace std; + + +/// Knapback +/// Time Complexity: O(nk) +/// Space Complexity: O(nk) +class Solution { + +private: + long long MOD = 1e9 + 7; + +public: + int countPartitions(vector& nums, int k) { + + int n = nums.size(); + vector> dp(n + 1, vector(k, 0)); + + dp[0][0] = 1; + for(int i = 0; i < n; i ++){ + for(int C = 0; C < k; C ++){ + dp[i + 1][C] = dp[i][C]; + if(C - nums[i] >= 0) + dp[i + 1][C] += dp[i][C - nums[i]], + dp[i + 1][C] %= MOD; + } + } + + long long sum = 0; + for(int e: nums) sum += e; + + long long t = 0; + for(int C = 0; C < k; C ++){ + t += dp[n][C]; + if(sum - C >= k) t += dp[n][C]; + t %= MOD; + } +// cout << t << '\n'; + + long long res = 1; + for(int i = 0; i < n; i ++) res = res * 2 % MOD; + + res = (res - t + MOD) % MOD; + return res; + } +}; + + +int main() { + + vector nums1 = {1, 2, 3, 4}; + cout << Solution().countPartitions(nums1, 4) << '\n'; + // 6 + + vector nums2 = {3, 3, 3}; + cout << Solution().countPartitions(nums2, 4) << '\n'; + // 0 + + vector nums3 = {6, 6}; + cout << Solution().countPartitions(nums3, 2) << '\n'; + // 2 + + vector nums4 = {96,40,22,98,9,97,45,22,79,57,95,62}; + cout << Solution().countPartitions(nums4, 505) << '\n'; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index c0e8fb3b..ff484e3a 100644 --- a/readme.md +++ b/readme.md @@ -2383,6 +2383,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2509 | [Cycle Length Queries in a Tree](https://leetcode.com/problems/cycle-length-queries-in-a-tree/) | [无] | [C++](2001-2500/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/) | | | | 2510 | [Check if There is a Path With Equal Number of 0's And 1's](https://leetcode.com/problems/check-if-there-is-a-path-with-equal-number-of-0s-and-1s/description/) | [无] | [C++](2001-2500/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/) | | | | | | | | | | +| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [无] | [C++](2001-2500/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/) | | | +| 2516 | [Take K of Each Character From Left and Right](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | [无] | [C++](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | | | +| 2517 | [Maximum Tastiness of Candy Basket](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | [无] | [C++](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | | | +| 2518 | [Number of Great Partitions](https://leetcode.com/problems/number-of-great-partitions/) | [无] | [C++](2001-2500/2518-Number-of-Great-Partitions/cpp-2518/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 606dcc0be3c44b990624cf26479c8797a8a12571 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 28 Dec 2022 14:38:03 -0800 Subject: [PATCH 261/390] 2519 solved. --- .../cpp-2519/CMakeLists.txt | 6 ++ .../cpp-2519/main.cpp | 99 +++++++++++++++++++ readme.md | 1 + 3 files changed, 106 insertions(+) create mode 100644 2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/CMakeLists.txt create mode 100644 2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/main.cpp diff --git a/2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/CMakeLists.txt b/2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/CMakeLists.txt new file mode 100644 index 00000000..bd78dc34 --- /dev/null +++ b/2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2519) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2519 main.cpp) diff --git a/2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/main.cpp b/2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/main.cpp new file mode 100644 index 00000000..e8fbdfd2 --- /dev/null +++ b/2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/main.cpp @@ -0,0 +1,99 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-k-big-indices/ +/// Author : liuyubobobo +/// Time : 2022-12-28 + +#include +#include +#include + +using namespace std; + + +/// Using BIT +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class BIT{ + +private: + int n; + vector data; + vector tree; + +public: + BIT(const vector& arr) : n(arr.size()), data(arr.begin(), arr.end()), tree(n + 1){ + + for(int i = 0; i < n; i ++) + tree[i + 1]= data[i]; + + for(int i = 1; i <= n; i ++){ + int parent = i + (i & -i); + if(parent <= n) tree[parent] += tree[i]; + } + } + + void add(int index, T v){ + assert(0 <= index && index < n); + + data[index] += v; + + index ++; + for(;index <= n; index += (index & -index)) + tree[index] += v; + } + + T query(int l, int r){ + if(!(0 <= l && l < n) || !(0 <= r && r < n) || !(l <= r)) return 0; + return prefix_sum(r + 1) - prefix_sum(l); + } + + void set(int index, T v){ + assert(0 <= index && index < n); + add(index, v - data[index]); + } + +private: + // r is 1-based + T prefix_sum(int r){ + + T res = 0; + for(; r; r -= (r & -r)) + res += tree[r]; + return res; + } +}; + + +class Solution { +public: + int kBigIndices(vector& nums, int k) { + + int maxv = *max_element(nums.begin(), nums.end()); + int n = nums.size(); + + BIT bit1(vector(maxv + 1, 0)); + vector left(n, 0); + for(int i = 0; i < n; i ++){ + left[i] = bit1.query(0, nums[i] - 1); + bit1.add(nums[i], 1); + } + + BIT bit2(vector(maxv + 1, 0)); + vector right(n, 0); + for(int i = n - 1; i >= 0; i --){ + right[i] = bit2.query(0, nums[i] - 1); + bit2.add(nums[i], 1); + } + + int res = 0; + for(int i = 0; i < n; i ++) + res += (left[i] >= k && right[i] >= k); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ff484e3a..5243bd38 100644 --- a/readme.md +++ b/readme.md @@ -2387,6 +2387,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2516 | [Take K of Each Character From Left and Right](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | [无] | [C++](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | | | | 2517 | [Maximum Tastiness of Candy Basket](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | [无] | [C++](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | | | | 2518 | [Number of Great Partitions](https://leetcode.com/problems/number-of-great-partitions/) | [无] | [C++](2001-2500/2518-Number-of-Great-Partitions/cpp-2518/) | | | +| 2519 | [Count the Number of K-Big Indices](https://leetcode.com/problems/count-the-number-of-k-big-indices/description/) | [无] | [C++](2001-2500/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From d9e7c1c471c281c79a013e8acd6334e9a137f46f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 28 Dec 2022 15:39:09 -0800 Subject: [PATCH 262/390] 2511-2514 solved. --- .../cpp-2511/CMakeLists.txt | 6 ++ .../cpp-2511/main.cpp | 37 +++++++++ .../cpp-2512/CMakeLists.txt | 6 ++ .../cpp-2512/main.cpp | 60 +++++++++++++++ .../cpp-2513/CMakeLists.txt | 6 ++ .../cpp-2513/main.cpp | 59 +++++++++++++++ .../cpp-2514/CMakeLists.txt | 6 ++ .../2514-Count-Anagrams/cpp-2514/main.cpp | 75 +++++++++++++++++++ readme.md | 5 +- 9 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/CMakeLists.txt create mode 100644 2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/main.cpp create mode 100644 2501-3000/2512-Reward-Top-K-Students/cpp-2512/CMakeLists.txt create mode 100644 2501-3000/2512-Reward-Top-K-Students/cpp-2512/main.cpp create mode 100644 2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/CMakeLists.txt create mode 100644 2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/main.cpp create mode 100644 2501-3000/2514-Count-Anagrams/cpp-2514/CMakeLists.txt create mode 100644 2501-3000/2514-Count-Anagrams/cpp-2514/main.cpp diff --git a/2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/CMakeLists.txt b/2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/CMakeLists.txt new file mode 100644 index 00000000..7d43c2ef --- /dev/null +++ b/2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2511) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2511 main.cpp) diff --git a/2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/main.cpp b/2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/main.cpp new file mode 100644 index 00000000..9c305c66 --- /dev/null +++ b/2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/description/ +/// Author : liuyubobobo +/// Time : 2022-12-28 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int captureForts(vector& forts) { + + int n = forts.size(), res = 0; + for(int i = 0; i < n; i ++){ + if(forts[i] != 1) continue; + + int j; + for(j = i - 1; j >= 0 && forts[j] == 0; j --); + if(j >= 0 && forts[j] == -1) res = max(res, i - j - 1); + + for(j = i + 1; j < n && forts[j] == 0; j ++); + if(j < n && forts[j] == -1) res = max(res, j - i - 1); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2512-Reward-Top-K-Students/cpp-2512/CMakeLists.txt b/2501-3000/2512-Reward-Top-K-Students/cpp-2512/CMakeLists.txt new file mode 100644 index 00000000..615ae582 --- /dev/null +++ b/2501-3000/2512-Reward-Top-K-Students/cpp-2512/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2512) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2512 main.cpp) diff --git a/2501-3000/2512-Reward-Top-K-Students/cpp-2512/main.cpp b/2501-3000/2512-Reward-Top-K-Students/cpp-2512/main.cpp new file mode 100644 index 00000000..d8d68ad0 --- /dev/null +++ b/2501-3000/2512-Reward-Top-K-Students/cpp-2512/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/reward-top-k-students/description/ +/// Author : liuyubobobo +/// Time : 2022-10-28 + +#include +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlogn) +/// Space Complexity: O(|pset| + |nset| + n) +class Solution { +public: + vector topStudents(vector& positive_feedback, vector& negative_feedback, vector& report, vector& student_id, int k) { + + set pset(positive_feedback.begin(), positive_feedback.end()); + set nset(negative_feedback.begin(), negative_feedback.end()); + + int n = report.size(); + vector> v(n); // score, id; + for(int i = 0; i < n; i ++) + v[i] = {get_score(report[i], pset, nset), student_id[i]}; + + sort(v.begin(), v.end(), [](const pair& p1, const pair& p2){ + if(p1.first != p2.first) return p1.first > p2.first; + return p1.second < p2.second; + }); + + vector res(k); + for(int i = 0; i < k; i ++) res[i] = v[i].second; + return res; + } + +private: + int get_score(const string& s, const set& pset, const set& nset){ + + int res = 0, n = s.size(); + for(int start = 0, i = 1; i <= n; i ++){ + if(i == n || s[i] == ' '){ + string word = s.substr(start, i - start); + if(pset.count(word)) res += 3; + if(nset.count(word)) res -= 1; + + start = i + 1; + i = start; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/CMakeLists.txt b/2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/CMakeLists.txt new file mode 100644 index 00000000..1d5ee00d --- /dev/null +++ b/2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2513) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2513 main.cpp) diff --git a/2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/main.cpp b/2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/main.cpp new file mode 100644 index 00000000..8b9fb370 --- /dev/null +++ b/2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/minimize-the-maximum-of-two-arrays/description/ +/// Author : liuyubobobo +/// Time : 2022-12-28 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(log(MAX_LONG_LONG)) +/// Space Complexity: O(1) +class Solution { +public: + int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) { + + long long l = 1, r = 1000000000000000000ll; + while(l < r){ + long long mid = (l + r) / 2; + if(ok(mid, divisor1, divisor2, uniqueCnt1, uniqueCnt2)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(long long n, long long d1, long long d2, long long n1, long long n2){ + + long long only1 = n / d1; + long long only2 = n / d2; + long long both12 = n / ((d1 * d2) / gcd(min(d1, d2), max(d1, d2))); + + long long left = n - only1 - only2 + both12; + only1 -= both12, only2 -= both12; + + long long cnt1 = 0, cnt2 = 0; + cnt1 += only2; + if(cnt1 < n1) left -= (n1 - cnt1); + + if(left < 0) return false; + + cnt2 += only1 + left; + return cnt2 >= n2; + } + + long long gcd(long long a, long long b){ + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + cout << Solution().minimizeSet(2, 4, 8, 2) << '\n'; + + return 0; +} diff --git a/2501-3000/2514-Count-Anagrams/cpp-2514/CMakeLists.txt b/2501-3000/2514-Count-Anagrams/cpp-2514/CMakeLists.txt new file mode 100644 index 00000000..0bcd1108 --- /dev/null +++ b/2501-3000/2514-Count-Anagrams/cpp-2514/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2514) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2514 main.cpp) diff --git a/2501-3000/2514-Count-Anagrams/cpp-2514/main.cpp b/2501-3000/2514-Count-Anagrams/cpp-2514/main.cpp new file mode 100644 index 00000000..10955d46 --- /dev/null +++ b/2501-3000/2514-Count-Anagrams/cpp-2514/main.cpp @@ -0,0 +1,75 @@ +/// Source : https://leetcode.com/problems/count-anagrams/ +/// Author : liuyubobobo +/// Time : 2022-12-28 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(n) +/// Space Compelxity: O(n) +class Solution { + +private: + long long MOD = 1e9 + 7; + +public: + int countAnagrams(string s) { + + int n = s.size(); + vector fac(n + 1, 1); + for(int i = 2; i <= n; i ++) fac[i] = fac[i - 1] * i % MOD; + + vector ifac(n + 1); + for(int i = 0; i <= n; i ++) ifac[i] = quick_pow(fac[i], MOD - 2); + + long long res = 1; + for(int start = 0, i = 1; i <= n; i ++){ + if(i == n || s[i] == ' '){ + string word = s.substr(start, i - start); + res *= get_cnt(word, fac, ifac); + res %= MOD; + + start = i + 1; + i = start; + } + } + return res; + } + +private: + long long get_cnt(const string& word, + const vector& fac, const vector& ifac){ + + vector f(26, 0); + for(char c: word) f[c - 'a'] ++; + + int n = word.size(); + long long res = fac[n]; + for(int i = 0; i < 26; i ++) + res = res * ifac[f[i]] % MOD; + return res; + } + + long long quick_pow(long long a, long long k) { + long long res = 1ll; + while (k) { + if (k & 1) res = res * a % MOD; + a = a * a % MOD; + k >>= 1; + } + return res % MOD; + } +}; + + +int main() { + + string s1 = "aa"; + cout << Solution().countAnagrams(s1) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index 5243bd38..fa97f757 100644 --- a/readme.md +++ b/readme.md @@ -2382,7 +2382,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2508 | [Add Edges to Make Degrees of All Nodes Even](https://leetcode.com/problems/add-edges-to-make-degrees-of-all-nodes-even/) | [无] | [C++](2001-2500/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/) | | | | 2509 | [Cycle Length Queries in a Tree](https://leetcode.com/problems/cycle-length-queries-in-a-tree/) | [无] | [C++](2001-2500/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/) | | | | 2510 | [Check if There is a Path With Equal Number of 0's And 1's](https://leetcode.com/problems/check-if-there-is-a-path-with-equal-number-of-0s-and-1s/description/) | [无] | [C++](2001-2500/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/) | | | -| | | | | | | +| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/) | [无] | [C++](2001-2500/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/) | | | +| 2512 | [Reward Top K Students](https://leetcode.com/problems/reward-top-k-students/) | [无] | [C++](2001-2500/2512-Reward-Top-K-Students/cpp-2512/) | | | +| 2513 | [Minimize the Maximum of Two Arrays](https://leetcode.com/problems/minimize-the-maximum-of-two-arrays/) | [无] | [C++](2001-2500/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/) | | | +| 2514 | [Count Anagrams](https://leetcode.com/problems/count-anagrams/) | [无] | [C++](2001-2500/2514-Count-Anagrams/cpp-2514/) | | | | 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [无] | [C++](2001-2500/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/) | | | | 2516 | [Take K of Each Character From Left and Right](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | [无] | [C++](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | | | | 2517 | [Maximum Tastiness of Candy Basket](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | [无] | [C++](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | | | From bae95c2b9aa0abe475a5110c395574bd244bbbaa Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 31 Dec 2022 21:10:40 -0800 Subject: [PATCH 263/390] 2520-2523 solved. --- .../cpp-2520/CMakeLists.txt | 6 ++ .../cpp-2520/main.cpp | 30 ++++++++++ .../cpp-2521/CMakeLists.txt | 6 ++ .../cpp-2521/main.cpp | 53 ++++++++++++++++++ .../cpp-2522/CMakeLists.txt | 6 ++ .../cpp-2522/main.cpp | 49 +++++++++++++++++ .../cpp-2523/CMakeLists.txt | 6 ++ .../cpp-2523/main.cpp | 55 +++++++++++++++++++ readme.md | 4 ++ 9 files changed, 215 insertions(+) create mode 100644 2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/CMakeLists.txt create mode 100644 2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/main.cpp create mode 100644 2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/CMakeLists.txt create mode 100644 2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/main.cpp create mode 100644 2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/CMakeLists.txt create mode 100644 2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/main.cpp create mode 100644 2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/CMakeLists.txt create mode 100644 2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/main.cpp diff --git a/2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/CMakeLists.txt b/2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/CMakeLists.txt new file mode 100644 index 00000000..27018f78 --- /dev/null +++ b/2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2520) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2520 main.cpp) diff --git a/2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/main.cpp b/2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/main.cpp new file mode 100644 index 00000000..974cb288 --- /dev/null +++ b/2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/count-the-digits-that-divide-a-number/description/ +/// Author : liuyubobobo +/// Time : 2022-12-31 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(log(num)) +/// Space Complexity: O(log(num)) +class Solution { +public: + int countDigits(int num) { + + int res = 0; + + string num_str = to_string(num); + for(char c: num_str) + res += num % (c - '0') == 0; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/CMakeLists.txt b/2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/CMakeLists.txt new file mode 100644 index 00000000..3be708aa --- /dev/null +++ b/2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2521) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2521 main.cpp) diff --git a/2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/main.cpp b/2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/main.cpp new file mode 100644 index 00000000..3cdeb763 --- /dev/null +++ b/2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/distinct-prime-factors-of-product-of-array/description/ +/// Author : liuyubobobo +/// Time : 2022-12-31 + +#include +#include +#include + +using namespace std; + + +/// Sieve +/// Time Complexity: O(MAX_NUM*log(MAX_NUM) + nlog(MAX_NUM)) +/// Space Compelxity: O(MAX_NUM) +class Solution { +public: + int distinctPrimeFactors(vector& nums) { + + vector sieve_table = sieve(1000); + set res; + for(int num: nums){ + + while(num != 1){ + res.insert(sieve_table[num]); + num /= sieve_table[num]; + } + } + return res.size(); + } + +private: + vector sieve(int n){ + + vector table(n + 1, 0); + vector primes; + table[0] = table[1] = 1; + for(int i = 2; i <= n; i ++){ + if(table[i] == 0) { + primes.push_back(i); + table[i] = i; + } + for(int j = 0; j < primes.size() && primes[j] <= table[i] && i * primes[j] <= n; j ++) + table[i * primes[j]] = primes[j]; + } + return table; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/CMakeLists.txt b/2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/CMakeLists.txt new file mode 100644 index 00000000..b702502d --- /dev/null +++ b/2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2522) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2522 main.cpp) diff --git a/2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/main.cpp b/2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/main.cpp new file mode 100644 index 00000000..6e55ce40 --- /dev/null +++ b/2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/partition-string-into-substrings-with-values-at-most-k/description/ +/// Author : liuyubobobo +/// Time : 2022-12-31 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(nlogk) +/// Space Complexity: O(n) +class Solution { + +private: + const int INF = INT_MAX / 2; +public: + int minimumPartition(string s, int k) { + + int n = s.size(); + vector dp(n, -1); + dfs(n, s, 0, k, dp); + return dp[0] == INF ? -1 : dp[0]; + } + +private: + int dfs(int n, const string& s, int index, long long k, vector& dp){ + + if(index == n) return 0; + if(dp[index] != -1) return dp[index]; + + long long cur = 0; + int res = INF; + for(int i = index; i < n && cur * 10 + (s[i] - '0') <= k; i ++){ + cur = cur * 10 + (s[i] - '0'); + res = min(res, 1 + dfs(n, s, i + 1, k, dp)); + } + return dp[index] = res; + } +}; + + +int main() { + + + return 0; +} diff --git a/2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/CMakeLists.txt b/2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/CMakeLists.txt new file mode 100644 index 00000000..339a9757 --- /dev/null +++ b/2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2523) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2523 main.cpp) diff --git a/2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/main.cpp b/2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/main.cpp new file mode 100644 index 00000000..725f5901 --- /dev/null +++ b/2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/closest-prime-numbers-in-range/description/ +/// Author : liuyubobobo +/// Time : 2022-12-31 + +#include +#include + +using namespace std; + + +/// Sieve +/// Time Compelxity: O(right * log(right) + (right - left) +/// Space Compelxity: O(right) +class Solution { + +public: + vector closestPrimes(int left, int right) { + + vector sieve_table = sieve(right); + vector v; + for(int i = left; i <= right; i ++) + if(sieve_table[i] == i && i != 1) v.push_back(i); + + if(v.size() <= 1) return {-1, -1}; + + int min_interval = v[1] - v[0], l = v[0]; + for(int i = 2; i < (int)v.size(); i ++) + if(v[i] - v[i - 1] < min_interval) + min_interval = v[i] - v[i - 1], l = v[i - 1]; + return {l, l + min_interval}; + } + +private: + vector sieve(int n){ + + vector table(n + 1, 0); + vector primes; + table[0] = table[1] = 1; + for(int i = 2; i <= n; i ++){ + if(table[i] == 0) { + primes.push_back(i); + table[i] = i; + } + for(int j = 0; j < primes.size() && primes[j] <= table[i] && i * primes[j] <= n; j ++) + table[i * primes[j]] = primes[j]; + } + return table; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index fa97f757..69c11ff9 100644 --- a/readme.md +++ b/readme.md @@ -2391,6 +2391,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2517 | [Maximum Tastiness of Candy Basket](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | [无] | [C++](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | | | | 2518 | [Number of Great Partitions](https://leetcode.com/problems/number-of-great-partitions/) | [无] | [C++](2001-2500/2518-Number-of-Great-Partitions/cpp-2518/) | | | | 2519 | [Count the Number of K-Big Indices](https://leetcode.com/problems/count-the-number-of-k-big-indices/description/) | [无] | [C++](2001-2500/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/) | | | +| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number/) | [无] | [C++](2001-2500/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/) | | | +| 2521 | [Distinct Prime Factors of Product of Array](https://leetcode.com/problems/distinct-prime-factors-of-product-of-array/) | [无] | [C++](2001-2500/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/) | | | +| 2522 | [Partition String Into Substrings With Values at Most K](https://leetcode.com/problems/partition-string-into-substrings-with-values-at-most-k/) | [无] | [C++](2001-2500/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/) | | | +| 2523 | [Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/) | [无] | [C++](2001-2500/2523-Closest-Prime-Numbers-in-Range/cpp-2523/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From c59e1c5e067297a9c47ac17630280e13638c7643 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 2 Jan 2023 12:17:56 -0800 Subject: [PATCH 264/390] 2496-2498 solved. --- .../cpp-2496/CMakeLists.txt | 6 +++ .../cpp-2496/main.cpp | 48 +++++++++++++++++++ .../cpp-2497/CMakeLists.txt | 6 +++ .../cpp-2497/main.cpp | 43 +++++++++++++++++ .../2498-Frog-Jump-II/cpp-2498/CMakeLists.txt | 6 +++ 2001-2500/2498-Frog-Jump-II/cpp-2498/main.cpp | 33 +++++++++++++ readme.md | 3 ++ 7 files changed, 145 insertions(+) create mode 100644 2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/CMakeLists.txt create mode 100644 2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/main.cpp create mode 100644 2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/CMakeLists.txt create mode 100644 2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/main.cpp create mode 100644 2001-2500/2498-Frog-Jump-II/cpp-2498/CMakeLists.txt create mode 100644 2001-2500/2498-Frog-Jump-II/cpp-2498/main.cpp diff --git a/2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/CMakeLists.txt b/2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/CMakeLists.txt new file mode 100644 index 00000000..ff1e9fd5 --- /dev/null +++ b/2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2496) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2496 main.cpp) diff --git a/2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/main.cpp b/2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/main.cpp new file mode 100644 index 00000000..723b6bbb --- /dev/null +++ b/2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/frog-jump-ii/description/ +/// Author : liuyubobobo +/// Time : 2023-01-02 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n * |s|) +/// Space Complexity: O(1) +class Solution { +public: + int maximumValue(vector& strs) { + + int res = 0; + for(const string& s: strs) + res = max(res, get_value(s)); + return res; + } + +private: + int get_value(const string& s){ + + if(all_digits(s)){ + long long res = 0; + for(char c: s) + res = res * 10 + (c - '0'); + return res; + } + return s.size(); + } + + bool all_digits(const string& s){ + + for(char c: s) + if(!isdigit(c)) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/CMakeLists.txt b/2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/CMakeLists.txt new file mode 100644 index 00000000..bfcd001f --- /dev/null +++ b/2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2497) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2497 main.cpp) diff --git a/2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/main.cpp b/2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/main.cpp new file mode 100644 index 00000000..32bd1135 --- /dev/null +++ b/2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/maximum-star-sum-of-a-graph/description/ +/// Author : liuyubobobo +/// Time : 2022-12-31 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(V + E) +/// Space Complexity: O(V + E) +class Solution { +public: + int maxStarSum(vector& vals, vector>& edges, int k) { + + int n = vals.size(); + vector> g(n); + for(const vector& e: edges){ + int a = e[0], b = e[1]; + g[a].push_back(vals[b]); + g[b].push_back(vals[a]); + } + + for(vector& v: g) sort(v.begin(), v.end(), greater()); + + int res = INT_MIN; + for(int u = 0; u < n; u ++){ + int sum = vals[u]; + for(int i = 0; i < g[u].size() && i < k && g[u][i] > 0; i ++) sum += g[u][i]; + res = max(res, sum); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2498-Frog-Jump-II/cpp-2498/CMakeLists.txt b/2001-2500/2498-Frog-Jump-II/cpp-2498/CMakeLists.txt new file mode 100644 index 00000000..7521f191 --- /dev/null +++ b/2001-2500/2498-Frog-Jump-II/cpp-2498/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2498) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2498 main.cpp) diff --git a/2001-2500/2498-Frog-Jump-II/cpp-2498/main.cpp b/2001-2500/2498-Frog-Jump-II/cpp-2498/main.cpp new file mode 100644 index 00000000..67f138f3 --- /dev/null +++ b/2001-2500/2498-Frog-Jump-II/cpp-2498/main.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + + +class Solution { +public: + int maxJump(vector& stones) { + + stones.push_back(stones.back()); + + int n = stones.size(), res = 0; + for(int i = 2; i < n; i += 2) + res = max(res, stones[i] - stones[i - 2]); + + res = max(res, stones[1]); + for(int i = 3; i < n; i += 2) + res = max(res, stones[i] - stones[i - 2]); + return res; + } +}; + + +int main() { + + vector stones1 = {0, 3, 9}; + cout << Solution().maxJump(stones1) << '\n'; + // 9 + + return 0; +} diff --git a/readme.md b/readme.md index 69c11ff9..9b5821e1 100644 --- a/readme.md +++ b/readme.md @@ -2370,6 +2370,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2493 | [Divide Nodes Into the Maximum Number of Groups](https://leetcode.com/problems/divide-nodes-into-the-maximum-number-of-groups/) | [无] | [C++](2001-2500/2493-Divide-Nodes-Into-the-Maximum-Number-of-Groups/cpp-2493/) | | | | 2494 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2495 | [Number of Subarrays Having Even Product](https://leetcode.com/problems/number-of-subarrays-having-even-product/description/) | [无] | [C++](2001-2500/2495-Number-of-Subarrays-Having-Even-Product/cpp-2495) | | | +| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [无] | [C++](2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496) | | | +| 2497 | [Maximum Star Sum of a Graph](https://leetcode.com/problems/maximum-star-sum-of-a-graph/) | [无] | [C++](2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497) | | | +| 2498 | [Frog Jump II](https://leetcode.com/problems/frog-jump-ii/) | [无] | [C++](2001-2500/2498-Frog-Jump-II/cpp-2498/) | | | | | | | | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | | 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | From 66495fbd365abf067947c6f3de4907ffbda81916 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 4 Jan 2023 22:59:14 -0800 Subject: [PATCH 265/390] 1056 solved. --- .../cpp-1056/CMakeLists.txt | 6 ++++ .../1056-Confusing-Number/cpp-1056/main.cpp | 33 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 41 insertions(+) create mode 100644 1001-1500/1056-Confusing-Number/cpp-1056/CMakeLists.txt create mode 100644 1001-1500/1056-Confusing-Number/cpp-1056/main.cpp diff --git a/1001-1500/1056-Confusing-Number/cpp-1056/CMakeLists.txt b/1001-1500/1056-Confusing-Number/cpp-1056/CMakeLists.txt new file mode 100644 index 00000000..500ba285 --- /dev/null +++ b/1001-1500/1056-Confusing-Number/cpp-1056/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1056) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1056 main.cpp) diff --git a/1001-1500/1056-Confusing-Number/cpp-1056/main.cpp b/1001-1500/1056-Confusing-Number/cpp-1056/main.cpp new file mode 100644 index 00000000..8dffe08a --- /dev/null +++ b/1001-1500/1056-Confusing-Number/cpp-1056/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/confusing-number/description/ +/// Author : liuyubobobo +/// Time : 2023-01-04 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(logn) +/// Space Compelxity: O(logn) +class Solution { +public: + bool confusingNumber(int n) { + + string s1 = to_string(n); + string s2 = s1; + reverse(s2.begin(), s2.end()); + + for(char& c: s2){ + if(c != '0' && c != '1' && c != '6' && c != '8' && c != '9') return false; + if(c == '6') c = '9'; else if(c == '9') c = '6'; + } + return s1 != s2; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9b5821e1..9816789e 100644 --- a/readme.md +++ b/readme.md @@ -1053,6 +1053,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1051 | [Height Checker](https://leetcode.com/problems/height-checker/) | [无] | [C++](1001-1500/1051-Height-Checker/cpp-1051/) | | | | 1052 | [Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner/) | [无] | [C++](1001-1500/1052-Grumpy-Bookstore-Owner/cpp-1052/) | | | | | | | | | | +| 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number/description/) | [无] | [C++](1001-1500/1056-Confusing-Number/cpp-1056/) | | | +| | | | | | | | 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [solution](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/solution/) | [C++](1001-1500/1059-All-Paths-from-Source-Lead-to-Destination/cpp-1059/) | | | | | | | | | | | 1062 | [Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/) | [无] | [C++](1001-1500/1062-Longest-Repeating-Substring/cpp-1062/) | | | From 1a1ede1e769be95fe34358f255eb0b6a6d1baebf Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 6 Jan 2023 03:35:04 -0800 Subject: [PATCH 266/390] 2524 solved. --- .../cpp-2524/CMakeLists.txt | 6 ++ .../cpp-2524/main.cpp | 62 +++++++++++++++++++ readme.md | 1 + 3 files changed, 69 insertions(+) create mode 100644 2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/CMakeLists.txt create mode 100644 2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/main.cpp diff --git a/2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/CMakeLists.txt b/2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/CMakeLists.txt new file mode 100644 index 00000000..2270b059 --- /dev/null +++ b/2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2524) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2524 main.cpp) diff --git a/2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/main.cpp b/2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/main.cpp new file mode 100644 index 00000000..544c1d03 --- /dev/null +++ b/2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/maximum-frequency-score-of-a-subarray/description/ +/// Author : liuyubobobo +/// Time : 2022-01-06 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(maxv + nlog(maxv)) +/// Space Compelxity: O(maxv) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int maxFrequencyScore(vector& nums, int k) { + + int maxv = *max_element(nums.begin(), nums.end()); + vector f(maxv + 1, 0); + for(int i = 0; i + 1 < k; i ++) f[nums[i]] ++; + + long long cur = 0; + for(int i = 0; i <= maxv; i ++) + if(f[i]) cur += quick_pow(i, f[i]), cur %= MOD; + + long long res = 0; + for(int i = k - 1; i < nums.size(); i ++){ + int num = nums[i]; + if(f[num]){cur -= quick_pow(num, f[num]); cur = (cur + MOD) % MOD;} + f[num] ++; + cur += quick_pow(num, f[num]); cur %= MOD; + + res = max(res, cur); + + num = nums[i - (k - 1)]; + cur -= quick_pow(num, f[num]); cur = (cur + MOD) % MOD; + f[num] --; + if(f[num]){cur += quick_pow(num, f[num]); cur %= MOD;} + } + return res; + } + +private: + long long quick_pow(long long a, long long k) { + long long res = 1ll; + while (k) { + if (k & 1) res = res * a % MOD; + a = a * a % MOD; + k >>= 1; + } + return res % MOD; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9816789e..53604bfa 100644 --- a/readme.md +++ b/readme.md @@ -2400,6 +2400,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2521 | [Distinct Prime Factors of Product of Array](https://leetcode.com/problems/distinct-prime-factors-of-product-of-array/) | [无] | [C++](2001-2500/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/) | | | | 2522 | [Partition String Into Substrings With Values at Most K](https://leetcode.com/problems/partition-string-into-substrings-with-values-at-most-k/) | [无] | [C++](2001-2500/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/) | | | | 2523 | [Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/) | [无] | [C++](2001-2500/2523-Closest-Prime-Numbers-in-Range/cpp-2523/) | | | +| 2524 | [Maximum Frequency Score of a Subarray](https://leetcode.com/problems/maximum-frequency-score-of-a-subarray/description/) | [无] | [C++](2001-2500/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 9e44b3fd1534d085da1e66c64f12ddcdb13abafe Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 7 Jan 2023 21:37:50 -0800 Subject: [PATCH 267/390] 2525-2528 solved. --- .../cpp-2525/CMakeLists.txt | 6 ++ .../cpp-2525/main.cpp | 36 ++++++++++++ .../cpp-2526/CMakeLists.txt | 6 ++ .../cpp-2526/main.cpp | 43 ++++++++++++++ .../cpp-2527/CMakeLists.txt | 6 ++ .../cpp-2527/main.cpp | 37 ++++++++++++ .../cpp-2528/CMakeLists.txt | 6 ++ .../cpp-2528/main.cpp | 56 +++++++++++++++++++ readme.md | 4 ++ 9 files changed, 200 insertions(+) create mode 100644 2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/CMakeLists.txt create mode 100644 2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/main.cpp create mode 100644 2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/CMakeLists.txt create mode 100644 2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/main.cpp create mode 100644 2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/CMakeLists.txt create mode 100644 2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/main.cpp create mode 100644 2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/CMakeLists.txt create mode 100644 2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/main.cpp diff --git a/2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/CMakeLists.txt b/2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/CMakeLists.txt new file mode 100644 index 00000000..e91fd64a --- /dev/null +++ b/2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2525) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2525 main.cpp) diff --git a/2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/main.cpp b/2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/main.cpp new file mode 100644 index 00000000..d55e03ac --- /dev/null +++ b/2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/categorize-box-according-to-criteria/description/ +/// Author : liuyubobobo +/// Time : 2023-01-07 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + string categorizeBox(long long length, long long width, long long height, long long mass) { + + bool is_bulky = false; + if(length >= 10000 || width >= 10000 || height >= 10000 || mass >= 10000) + is_bulky = true; + + long long volume = length * width * height; + if(volume >= 1000000000) is_bulky = true; + + bool is_heavy = mass >= 100; + + if(is_bulky && is_heavy) return "Both"; + if(!is_bulky && !is_heavy) return "Neither"; + return is_bulky ? "Bulky" : "Heavy"; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/CMakeLists.txt b/2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/CMakeLists.txt new file mode 100644 index 00000000..0beb385b --- /dev/null +++ b/2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2526) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2526 main.cpp) diff --git a/2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/main.cpp b/2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/main.cpp new file mode 100644 index 00000000..ad6e835f --- /dev/null +++ b/2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/find-consecutive-integers-from-a-data-stream/description/ +/// Author : liuyubobobo +/// Time : 2023-01-07 + +#include +#include + +using namespace std; + + +/// Using Deque +/// Time Complexity: O(n) +/// Space Complexity: O(k) +class DataStream { + +private: + int value, k; + deque q; + int equal_value = 0; + +public: + DataStream(int value, int k) : value(value), k(k) { + } + + bool consec(int num) { + q.push_back(num); + equal_value += (num == value); + + if(q.size() > k){ + int front = q.front(); + q.pop_front(); + equal_value -= (front == value); + } + + return equal_value == k; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/CMakeLists.txt b/2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/CMakeLists.txt new file mode 100644 index 00000000..4bd25232 --- /dev/null +++ b/2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2527) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2527 main.cpp) diff --git a/2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/main.cpp b/2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/main.cpp new file mode 100644 index 00000000..bacfd456 --- /dev/null +++ b/2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/find-xor-beauty-of-array/description/ +/// Author : liuyubobobo +/// Time : 2023-01-07 + +#include +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int xorBeauty(vector& nums) { + + long long n = nums.size(); + + int res = 0; + for(int p = 0; p < 30; p ++){ + vector cnt(2, 0); + for(int num: nums) cnt[(num >> p) & 1] ++; + + long long a = n * n - cnt[0] * cnt[0]; + long long b = a * cnt[1]; + if(b & 1) res += (1 << p); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/CMakeLists.txt b/2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/CMakeLists.txt new file mode 100644 index 00000000..e9658cae --- /dev/null +++ b/2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2528) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2528 main.cpp) diff --git a/2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/main.cpp b/2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/main.cpp new file mode 100644 index 00000000..7e68eda1 --- /dev/null +++ b/2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/maximize-the-minimum-powered-city/description/ +/// Author : liuyubobobo +/// Time : 2023-01-07 + +#include +#include + +using namespace std; + + +/// Binary Search + Diff Array +/// Time Complexity: O(n * log(max)) +/// Space Complexity: O(n) +class Solution { +public: + long long maxPower(vector& stations, int radius, int k) { + + long long l = 0, r = 1e11; + while(l < r){ + long long mid = (l + r + 1) / 2; + if(ok(stations.size(), stations, radius, k, mid)) + l = mid; + else + r = mid - 1; + } + return l; + } + +private: + bool ok(int n, const vector& stations, int radius, int k, long long min_power){ + + vector diff_arr(n + 1, 0); + for(int i = 0; i < n; i ++){ + int l = max(0, i - radius), r = min(n - 1, i + radius); + diff_arr[l] += stations[i], diff_arr[r + 1] -= stations[i]; + } + + long long cur = 0, need = 0; + for(int i = 0; i < n; i ++){ + cur += diff_arr[i]; + if(cur < min_power){ + long long d = min_power - cur, l = i, r = min(i + radius + radius, n - 1); + diff_arr[l] += d, diff_arr[r + 1] -= d; + cur = min_power; + need += d; + } + } + return need <= k; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 53604bfa..d4668d2d 100644 --- a/readme.md +++ b/readme.md @@ -2401,6 +2401,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2522 | [Partition String Into Substrings With Values at Most K](https://leetcode.com/problems/partition-string-into-substrings-with-values-at-most-k/) | [无] | [C++](2001-2500/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/) | | | | 2523 | [Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/) | [无] | [C++](2001-2500/2523-Closest-Prime-Numbers-in-Range/cpp-2523/) | | | | 2524 | [Maximum Frequency Score of a Subarray](https://leetcode.com/problems/maximum-frequency-score-of-a-subarray/description/) | [无] | [C++](2001-2500/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/) | | | +| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria/) | [无] | [C++](2001-2500/2525-Categorize-Box-According-to-Criteria/cpp-2525/) | | | +| 2526 | [Find Consecutive Integers from a Data Stream](https://leetcode.com/problems/find-consecutive-integers-from-a-data-stream/) | [无] | [C++](2001-2500/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/) | | | +| 2527 | [Find Xor-Beauty of Array](https://leetcode.com/problems/find-xor-beauty-of-array/) | [无] | [C++](2001-2500/2527-Find-Xor-Beauty-of-Array/cpp-2527/) | | | +| 2528 | [Maximize the Minimum Powered City](https://leetcode.com/problems/maximize-the-minimum-powered-city/) | [无] | [C++](2001-2500/2528-Maximize-the-Minimum-Powered-City/cpp-2528/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From bbb5671fc900aa5ddbe661e6dd1d4cfc50f7b719 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 8 Jan 2023 12:23:10 -0800 Subject: [PATCH 268/390] 2529-2531 solved. --- .../cpp-2529/CMakeLists.txt | 6 +++ .../cpp-2529/main.cpp | 33 +++++++++++++ .../cpp-2530/CMakeLists.txt | 6 +++ .../cpp-2530/main.cpp | 38 +++++++++++++++ .../cpp-2531/CMakeLists.txt | 6 +++ .../cpp-2531/main.cpp | 47 +++++++++++++++++++ readme.md | 4 ++ 7 files changed, 140 insertions(+) create mode 100644 2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/CMakeLists.txt create mode 100644 2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/main.cpp create mode 100644 2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/CMakeLists.txt create mode 100644 2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/main.cpp create mode 100644 2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/CMakeLists.txt create mode 100644 2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/main.cpp diff --git a/2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/CMakeLists.txt b/2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/CMakeLists.txt new file mode 100644 index 00000000..0585437f --- /dev/null +++ b/2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2529) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2529 main.cpp) diff --git a/2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/main.cpp b/2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/main.cpp new file mode 100644 index 00000000..be662ed1 --- /dev/null +++ b/2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/description/ +/// Author : liuyubobobo +/// Time : 2023-01-08 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + int maximumCount(vector& nums) { + + auto iter = lower_bound(nums.begin(), nums.end(), 0); + int neg = iter - nums.begin(); + + iter = lower_bound(nums.begin(), nums.end(), 1); + int pos = nums.end() - iter; + + return max(neg, pos); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/CMakeLists.txt b/2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/CMakeLists.txt new file mode 100644 index 00000000..fecf5aa6 --- /dev/null +++ b/2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2530) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2530 main.cpp) diff --git a/2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/main.cpp b/2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/main.cpp new file mode 100644 index 00000000..f7d5f0c0 --- /dev/null +++ b/2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/maximal-score-after-applying-k-operations/description/ +/// Author : liuyubobobo +/// Time : 2023-01-08 + +#include +#include +#include + +using namespace std; + + +/// PQ +/// Time Complexity: O(klogn) +/// Space Complexity: O(n) +class Solution { +public: + long long maxKelements(vector& nums, int k) { + + priority_queue pq; + for(int e: nums) pq.push(e); + + long long res = 0; + for(int i = 0; i < k && !pq.empty(); i ++){ + int e = pq.top(); pq.pop(); + res += e; + + e = e / 3 + !!(e % 3); + if(e) pq.push(e); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/CMakeLists.txt b/2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/CMakeLists.txt new file mode 100644 index 00000000..e062bdc9 --- /dev/null +++ b/2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2531) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2531 main.cpp) diff --git a/2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/main.cpp b/2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/main.cpp new file mode 100644 index 00000000..715a8d69 --- /dev/null +++ b/2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/make-number-of-distinct-characters-equal/description/ +/// Author : liuyubobobo +/// Time : 2023-01-08 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(|word1| + |word2| + CHAR_SET^3) +/// Space Complexity: O(1) +class Solution { +public: + bool isItPossible(string word1, string word2) { + + vector f1(26, 0), f2(26, 0); + for(char c: word1) f1[c - 'a'] ++; + for(char c: word2) f2[c - 'a'] ++; + + for(int i = 0; i < 26; i ++) if(f1[i]) + for(int j = 0; j < 26; j ++) if(f2[j]){ + f1[i] --, f2[j] --; + f1[j] ++, f2[i] ++; + + if(get_unique(f1) == get_unique(f2)) return true; + + f1[j] --, f2[i] --; + f1[i] ++, f2[j] ++; + } + return false; + } + +private: + int get_unique(const vector& f){ + + int res = 0; + for(int e: f) res += !!e; + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d4668d2d..04c93883 100644 --- a/readme.md +++ b/readme.md @@ -2405,6 +2405,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2526 | [Find Consecutive Integers from a Data Stream](https://leetcode.com/problems/find-consecutive-integers-from-a-data-stream/) | [无] | [C++](2001-2500/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/) | | | | 2527 | [Find Xor-Beauty of Array](https://leetcode.com/problems/find-xor-beauty-of-array/) | [无] | [C++](2001-2500/2527-Find-Xor-Beauty-of-Array/cpp-2527/) | | | | 2528 | [Maximize the Minimum Powered City](https://leetcode.com/problems/maximize-the-minimum-powered-city/) | [无] | [C++](2001-2500/2528-Maximize-the-Minimum-Powered-City/cpp-2528/) | | | +| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | [无] | [C++](2001-2500/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/) | | | +| 2530 | [Maximal Score After Applying K Operations](https://leetcode.com/problems/maximal-score-after-applying-k-operations/) | [无] | [C++](2001-2500/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/) | | | +| 2531 | [Make Number of Distinct Characters Equal](https://leetcode.com/problems/make-number-of-distinct-characters-equal/) | [无] | [C++](2001-2500/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/) | | | +| | | | | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 2d69874b6c6249d2ab621408b1a22a04311fc793 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 9 Jan 2023 13:56:11 -0800 Subject: [PATCH 269/390] 2532 solved. --- .../cpp-2532/CMakeLists.txt | 6 ++ .../cpp-2532/main.cpp | 84 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/CMakeLists.txt create mode 100644 2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/main.cpp diff --git a/2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/CMakeLists.txt b/2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/CMakeLists.txt new file mode 100644 index 00000000..522a4447 --- /dev/null +++ b/2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2532) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2532 main.cpp) diff --git a/2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/main.cpp b/2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/main.cpp new file mode 100644 index 00000000..62be1ebc --- /dev/null +++ b/2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/main.cpp @@ -0,0 +1,84 @@ +/// Source : https://leetcode.com/problems/time-to-cross-a-bridge/description/s +/// Author : liuyubobobo +/// Time : 2023-01-09 + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// Event Simulation, Using PQ +/// Time Complexity: O(nlogk) +/// Space Complexity: O(k) +class Solution { +public: + int findCrossingTime(int n, int k, vector>& time) { + + priority_queue, vector>, greater>> left_ready_q, right_ready_q; // ready_time, index + priority_queue> bridge_left_q, bridge_right_q; // efficient, index + for(int i = 0; i < k; i ++) + bridge_left_q.push({time[i][0] + time[i][2], i}); + + int cur_time = 0, left = n, complete = 0; + while(true){ + + while(!left_ready_q.empty() && cur_time >= left_ready_q.top().first){ + int index = left_ready_q.top().second; + left_ready_q.pop(); + bridge_left_q.push({time[index][0] + time[index][2], index}); + } + + while(!right_ready_q.empty() && cur_time >= right_ready_q.top().first){ + int index = right_ready_q.top().second; + right_ready_q.pop(); + bridge_right_q.push({time[index][0] + time[index][2], index}); + } + + if(!bridge_right_q.empty()){ + int index = bridge_right_q.top().second; + bridge_right_q.pop(); + + cur_time += time[index][2]; + + left_ready_q.push({cur_time + time[index][3], index}); + + complete ++; + if(complete == n) return cur_time; + } + else if(left && !bridge_left_q.empty()){ + int index = bridge_left_q.top().second; + bridge_left_q.pop(); + + cur_time += time[index][0]; + + right_ready_q.push({cur_time + time[index][1], index}); + + left --; + } + else{ + int next_time = INT_MAX; + if(!left_ready_q.empty()) next_time = min(next_time, left_ready_q.top().first); + if(!right_ready_q.empty()) next_time = min(next_time, right_ready_q.top().first); + assert(next_time != INT_MAX); + cur_time = next_time; + } + } + return -1; + } +}; + + +int main() { + + vector> time1 = {{1, 1, 2, 1}, {1, 1, 3, 1}, {1, 1, 4, 1}}; + cout << Solution().findCrossingTime(1, 3, time1) << '\n'; + // 6 + + return 0; +} diff --git a/readme.md b/readme.md index 04c93883..5d1dd255 100644 --- a/readme.md +++ b/readme.md @@ -2408,7 +2408,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | [无] | [C++](2001-2500/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/) | | | | 2530 | [Maximal Score After Applying K Operations](https://leetcode.com/problems/maximal-score-after-applying-k-operations/) | [无] | [C++](2001-2500/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/) | | | | 2531 | [Make Number of Distinct Characters Equal](https://leetcode.com/problems/make-number-of-distinct-characters-equal/) | [无] | [C++](2001-2500/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/) | | | -| | | | | | | +| 2532 | [Time to Cross a Bridge](https://leetcode.com/problems/time-to-cross-a-bridge/description/) | [无] | [C++](2001-2500/2532-Time-to-Cross-a-Bridge/cpp-2532/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 9cf1c2810532b842367430fcf89c66dfe13147be Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 9 Jan 2023 14:18:00 -0800 Subject: [PATCH 270/390] 2499 solved. --- .../cpp-2499/CMakeLists.txt | 6 +++ .../cpp-2499/main.cpp | 43 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/CMakeLists.txt create mode 100644 2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/main.cpp diff --git a/2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/CMakeLists.txt b/2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/CMakeLists.txt new file mode 100644 index 00000000..33689c88 --- /dev/null +++ b/2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2499) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2499 main.cpp) diff --git a/2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/main.cpp b/2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/main.cpp new file mode 100644 index 00000000..9695c39f --- /dev/null +++ b/2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/minimum-total-cost-to-make-arrays-unequal/description/ +/// Author : liuyubobobo +/// Time : 2023-01-09 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long minimumTotalCost(vector& nums1, vector& nums2) { + + int n = nums1.size(); + + map f; + int total = 0; + long long res = 0; + for(int i = 0; i < n; i ++) + if(nums1[i] == nums2[i]) f[nums1[i]] ++, total ++, res += i; + + int maxf = 0, maxf_value; + for(const pair& p: f) + if(p.second > maxf) maxf = p.second, maxf_value = p.first; + + for(int i = 0; i < n && maxf > total / 2; i ++) + if(nums1[i] != nums2[i] && nums1[i] != maxf_value && nums2[i] != maxf_value) + res += i, total ++; + + return maxf > total / 2 ? -1 : res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 5d1dd255..eeb148d1 100644 --- a/readme.md +++ b/readme.md @@ -2375,7 +2375,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [无] | [C++](2001-2500/2496-Maximum-Value-of-a-String-in-an-Array/cpp-2496) | | | | 2497 | [Maximum Star Sum of a Graph](https://leetcode.com/problems/maximum-star-sum-of-a-graph/) | [无] | [C++](2001-2500/2497-Maximum-Star-Sum-of-a-Graph/cpp-2497) | | | | 2498 | [Frog Jump II](https://leetcode.com/problems/frog-jump-ii/) | [无] | [C++](2001-2500/2498-Frog-Jump-II/cpp-2498/) | | | -| | | | | | | +| 2499 | [Minimum Total Cost to Make Arrays Unequal](https://leetcode.com/problems/minimum-total-cost-to-make-arrays-unequal/description/) | [无] | [C++](2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/) | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | | 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | | 2502 | [Design Memory Allocator](https://leetcode.com/problems/design-memory-allocator/) | [无] | [C++](2001-2500/2502-Design-Memory-Allocator/cpp-2502/) | | | From 124f1f5f0bd7c361d4aaa1961728bc93a675d06b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 9 Jan 2023 14:43:39 -0800 Subject: [PATCH 271/390] 2459 solved. --- .../cpp-2459/CMakeLists.txt | 6 ++ .../cpp-2459/main.cpp | 66 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/CMakeLists.txt create mode 100644 2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/main.cpp diff --git a/2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/CMakeLists.txt b/2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/CMakeLists.txt new file mode 100644 index 00000000..1d476010 --- /dev/null +++ b/2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2459) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2459 main.cpp) diff --git a/2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/main.cpp b/2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/main.cpp new file mode 100644 index 00000000..ccf15376 --- /dev/null +++ b/2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/sort-array-by-moving-items-to-empty-space/description/ +/// Author : liuyubobobo +/// Time : 2023-01-09 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int sortArray(vector& nums) { + + int n = nums.size(); + + vector target(n); + for(int i = 0; i < n; i ++) target[i] = i; + + int res = solve(n, nums, target); + + for(int i = 0; i < n; i ++) target[i] = i + 1; + target.back() = 0; + res = min(res, solve(n, nums, target)); + + return res; + } + +private: + int solve(int n, const vector& nums, const vector& target){ + + vector pos(n); + for(int i = 0; i < n; i ++) pos[target[i]] = i; + + vector visited(n, false); + int res = 0; + for(int i = 0; i < n; i ++){ + if(visited[i]) continue; + + int cur = i, len = 0; + bool contain_zero = false; + while(!visited[cur]){ + len ++; + visited[cur] = true; + + if(nums[cur] == 0) contain_zero = true; + cur = pos[nums[cur]]; + } + + if(len == 1) continue; + + if(contain_zero) res += len - 1; + else res += len + 1; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index eeb148d1..54c7ee87 100644 --- a/readme.md +++ b/readme.md @@ -2335,7 +2335,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2456 | [Most Popular Video Creator](https://leetcode.com/problems/most-popular-video-creator/) | [无] | [C++](2001-2500/2456-Most-Popular-Video-Creator/cpp-2456/) | | | | 2457 | [Minimum Addition to Make Integer Beautiful](https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/) | [无] | [C++](2001-2500/2457-Minimum-Addition-to-Make-Integer-Beautiful/cpp-2457/) | | | | 2458 | [Height of Binary Tree After Subtree Removal Queries](https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/) | [无] | [C++](2001-2500/2458-Height-of-Binary-Tree-After-Subtree-Removal-Queries/cpp-2458/) | | | -| | | | | | | +| 2459 | [Sort Array by Moving Items to Empty Space](https://leetcode.com/problems/sort-array-by-moving-items-to-empty-space/description/) | [无] | [C++](2001-2500/2459-Sort-Array-by-Moving-Items-to-Empty-Space/cpp-2459/) | | | | 2460 | [Apply Operations to an Array](https://leetcode.com/problems/apply-operations-to-an-array/) | [无] | [C++](2001-2500/2460-Apply-Operations-to-an-Array/cpp-2460/) | | | | 2461 | [Maximum Sum of Distinct Subarrays With Length K](https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/) | [无] | [C++](2001-2500/2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K/cpp-2461/) | | | | 2462 | [Total Cost to Hire K Workers](https://leetcode.com/problems/total-cost-to-hire-k-workers/) | [无] | [C++](2001-2500/2462-Total-Cost-to-Hire-K-Workers/cpp-2462/) | | | From 3b22c12b06a1fc74442b519fbbfc672c8ab52c30 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 9 Jan 2023 15:52:29 -0800 Subject: [PATCH 272/390] 2412 solved. --- .../cpp-2412/CMakeLists.txt | 6 +++ .../cpp-2412/main.cpp | 49 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/CMakeLists.txt create mode 100644 2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/main.cpp diff --git a/2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/CMakeLists.txt b/2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/CMakeLists.txt new file mode 100644 index 00000000..39417c0c --- /dev/null +++ b/2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2412) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2412 main.cpp) diff --git a/2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/main.cpp b/2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/main.cpp new file mode 100644 index 00000000..4b6f9205 --- /dev/null +++ b/2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/minimum-money-required-before-transactions/description/ +/// Author : liuyubobobo +/// Time : 2023-01-09 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + long long minimumMoney(vector>& transactions) { + + vector> neg, pos; + for(const vector& e: transactions) { + if (e[0] > e[1]) neg.push_back({e[0], e[1]}); + else pos.push_back({e[0], e[1]}); + } + + sort(neg.begin(), neg.end(), [](const pair& e1, const pair& e2){ + if(e1.second != e2.second) return e1.second < e2.second; + return e1.first > e2.first; + }); + + sort(pos.begin(), pos.end(), [](const pair& e1, const pair& e2){ + return e1.first > e2.first; + }); + + long long res = 0, cur = 0; + for(const pair& e: neg){ + cur -= e.first; + res = max(res, -cur); + cur += e.second; + } + if(!pos.empty()) cur -= pos[0].first; + return max(res, max(0ll, -cur)); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 54c7ee87..d6b453ff 100644 --- a/readme.md +++ b/readme.md @@ -2288,7 +2288,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [无] | [C++](2001-2500/2409-Count-Days-Spent-Together/cpp2409/) | | | | 2410 | [Maximum Matching of Players With Trainers](https://leetcode.com/problems/maximum-matching-of-players-with-trainers/) | [无] | [C++](2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp2410/) | | | | 2411 | [Smallest Subarrays With Maximum Bitwise OR](https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/) | [无][缺:O(n) 算法] | [C++](2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp2411/) | | | -| | | | | | | +| 2412 | [Minimum Money Required Before Transactions](https://leetcode.com/problems/minimum-money-required-before-transactions/description/) | [无] | [C++](2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/) | | | | 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [无] | [C++](2001-2500/2413-Smallest-Even-Multiple/cpp-2413/) | | | | 2414 | [Length of the Longest Alphabetical Continuous Substring](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) | [无] | [C++](2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/) | | | | 2415 | [Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | [无] | [C++](2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/) | | | From 1c551690dd2951e5e08076466c83bf2e24d91900 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 12 Jan 2023 00:48:49 -0800 Subject: [PATCH 273/390] 1519 solved. --- .../cpp-1519/CMakeLists.txt | 6 +++ .../cpp-1519/main.cpp | 52 +++++++++++++++++++ readme.md | 4 +- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/CMakeLists.txt create mode 100644 1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/main.cpp diff --git a/1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/CMakeLists.txt b/1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/CMakeLists.txt new file mode 100644 index 00000000..e30161c9 --- /dev/null +++ b/1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1519) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1519 main.cpp) diff --git a/1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/main.cpp b/1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/main.cpp new file mode 100644 index 00000000..1a7ff35a --- /dev/null +++ b/1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/description/ +/// Author : liuyubobobo +/// Time : 2023-01-12 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector countSubTrees(int n, vector>& edges, string labels) { + + vector> tree(n); + for(const vector& e: edges){ + int u = e[0], v = e[1]; + tree[u].push_back(v), tree[v].push_back(u); + } + + vector> table(n, vector(26, 0)); + dfs(tree, 0, -1, labels, table); + + vector res(n); + for(int i = 0; i < n; i ++) + res[i] = table[i][labels[i] - 'a']; + return res; + } + +private: + void dfs(const vector>& tree, int u, int p, + const string& label, vector>& table){ + + table[u][label[u] - 'a'] ++; + for(int v: tree[u]){ + if(v == p) continue; + dfs(tree, v, u, label, table); + for(int i = 0; i < 26; i ++) + table[u][i] += table[v][i]; + } + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d6b453ff..d3fd7bca 100644 --- a/readme.md +++ b/readme.md @@ -1437,6 +1437,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1506 | [Find Root of N-Ary Tree](https://leetcode.com/problems/find-root-of-n-ary-tree/) | [solution](https://leetcode.com/problems/find-root-of-n-ary-tree/solution/) | [C++](1501-2000/1506-Find-Root-of-N-Ary-Tree/cpp-1506/) | | | | | | | | | | +| 1519 | [Number of Nodes in the Sub-Tree With the Same Label](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/description/) | [solution](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/solutions/2864718/number-of-nodes-in-the-sub-tree-with-the-same-label/?orderBy=most_votes) | [C++](1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/) | | | +| | | | | | | | 1522 | [Diameter of N-Ary Tree](https://leetcode.com/problems/diameter-of-n-ary-tree/) | [无] | [C++](1501-2000/1522-Diameter-of-N-Ary-Tree/cpp-1522/) | | | | | | | | | | | 1527 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | @@ -2288,7 +2290,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [无] | [C++](2001-2500/2409-Count-Days-Spent-Together/cpp2409/) | | | | 2410 | [Maximum Matching of Players With Trainers](https://leetcode.com/problems/maximum-matching-of-players-with-trainers/) | [无] | [C++](2001-2500/2410-Maximum-Matching-of-Players-With-Trainers/cpp2410/) | | | | 2411 | [Smallest Subarrays With Maximum Bitwise OR](https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/) | [无][缺:O(n) 算法] | [C++](2001-2500/2411-Smallest-Subarrays-With-Maximum-Bitwise-OR/cpp2411/) | | | -| 2412 | [Minimum Money Required Before Transactions](https://leetcode.com/problems/minimum-money-required-before-transactions/description/) | [无] | [C++](2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/) | | | +| 2412 | [Minimum Money Required Before Transactions](https://leetcode.com/problems/minimum-money-required-before-transactions/description/) | [无] [缺:不用 sort] | [C++](2001-2500/2412-Minimum-Money-Required-Before-Transactions/cpp-2412/) | | | | 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [无] | [C++](2001-2500/2413-Smallest-Even-Multiple/cpp-2413/) | | | | 2414 | [Length of the Longest Alphabetical Continuous Substring](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) | [无] | [C++](2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/) | | | | 2415 | [Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | [无] | [C++](2001-2500/2415-Reverse-Odd-Levels-of-Binary-Tree/cpp-2415/) | | | From 8f01b3de447412daf975c20f5a49a4ea03745520 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 14 Jan 2023 19:30:27 -0800 Subject: [PATCH 274/390] 1061 solved. --- .../cpp-1061/CMakeLists.txt | 6 ++ .../cpp-1061/main.cpp | 70 +++++++++++++++++++ readme.md | 1 + 3 files changed, 77 insertions(+) create mode 100644 1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/CMakeLists.txt create mode 100644 1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/main.cpp diff --git a/1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/CMakeLists.txt b/1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/CMakeLists.txt new file mode 100644 index 00000000..000615a7 --- /dev/null +++ b/1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1061) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1061 main.cpp) diff --git a/1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/main.cpp b/1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/main.cpp new file mode 100644 index 00000000..df32fc80 --- /dev/null +++ b/1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/main.cpp @@ -0,0 +1,70 @@ +/// Source : https://leetcode.com/problems/lexicographically-smallest-equivalent-string/description/ +/// Author : liuyubobobo +/// Time : 2023-01-14 + +#include +#include + +using namespace std; + + +/// UF +/// Time Complexity: O(n) +/// Space Compelxity: O(1) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + string smallestEquivalentString(string s1, string s2, string baseStr) { + + UF uf(26); + for(int i = 0; i < s1.size(); i ++) + uf.union_elements(s1[i] - 'a', s2[i] - 'a'); + + vector min_char(26, 'z'); + for(int i = 0; i < 26; i ++){ + int p = uf.find(i); + min_char[p] = min(min_char[p], (char)('a' + i)); + } + + string res(baseStr.size(), ' '); + for(int i = 0; i < baseStr.size(); i ++) + res[i] = min_char[uf.find(baseStr[i] - 'a')]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d3fd7bca..63d13253 100644 --- a/readme.md +++ b/readme.md @@ -1057,6 +1057,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [solution](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/solution/) | [C++](1001-1500/1059-All-Paths-from-Source-Lead-to-Destination/cpp-1059/) | | | | | | | | | | +| 1061 | [Lexicographically Smallest Equivalent String](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/description/) | [无] | [C++](1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/) | | | | 1062 | [Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/) | [无] | [C++](1001-1500/1062-Longest-Repeating-Substring/cpp-1062/) | | | | | | | | | | | 1066 | [Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/) | [solution](https://leetcode.com/problems/campus-bikes-ii/solutions/1451959/campus-bikes-ii/) | [C++](1001-1500/1066-Campus-Bikes-II/cpp-1066/) | | | From 14edeaa8bda1ccac0eba554c3691d5b54a8a9683 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 14 Jan 2023 19:51:35 -0800 Subject: [PATCH 275/390] 1533 solved. --- .../cpp-1533/CMakeLists.txt | 6 ++ .../cpp-1533/main.cpp | 88 +++++++++++++++++++ readme.md | 2 + 3 files changed, 96 insertions(+) create mode 100644 1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/CMakeLists.txt create mode 100644 1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/main.cpp diff --git a/1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/CMakeLists.txt b/1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/CMakeLists.txt new file mode 100644 index 00000000..ef8a6543 --- /dev/null +++ b/1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1533) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1533 main.cpp) diff --git a/1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/main.cpp b/1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/main.cpp new file mode 100644 index 00000000..fa81143c --- /dev/null +++ b/1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/main.cpp @@ -0,0 +1,88 @@ +/// Source : https://leetcode.com/problems/find-the-index-of-the-large-integer/ +/// Author : liuyubobobo +/// Time : 2023-01-14 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Compelxity: O(logn) +/// Space Compelxity: O(1) + +// This is the ArrayReader's API interface. +// You should not implement it, or speculate about its implementation +class ArrayReader { + +private: + vector data; + vector presum; + +public: + ArrayReader(const vector& arr): data(arr), presum(data.size() + 1, 0){ + + int n = arr.size(); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + data[i]; + } + + // Compares the sum of arr[l..r] with the sum of arr[x..y] + // return 1 if sum(arr[l..r]) > sum(arr[x..y]) + // return 0 if sum(arr[l..r]) == sum(arr[x..y]) + // return -1 if sum(arr[l..r]) < sum(arr[x..y]) + int compareSub(int l, int r, int x, int y){ + int sum1 = presum[r + 1] - presum[l], sum2 = presum[y + 1] - presum[x]; + if(sum1 > sum2) return 1; + return sum1 == sum2 ? 0 : -1; + } + + // Returns the length of the array + int length(){ + return data.size(); + } +}; + +class Solution { +public: + int getIndex(ArrayReader &reader) { + + int n = reader.length(); + return search(reader, 0, n - 1); + } + +private: + int search(ArrayReader &reader, int l, int r){ + + if(l == r) return l; + + int len = (r - l + 1); + if(len & 1){ + int res = search(reader, l, r - 1); + return res == -1 ? r : res; + } + + int mid = (l + r) / 2; + int x = reader.compareSub(l, mid, mid + 1, r); + if(x == 1) return search(reader, l, mid); + else if(x == 0) return -1; + else return search(reader, mid + 1, r); + } +}; + + +int main() { + + vector nums1 = {6, 6, 12}; + ArrayReader reader1(nums1); + cout << Solution().getIndex(reader1) << '\n'; + // 2 + + vector nums2 = {7, 7, 7, 7, 10, 7, 7, 7}; + ArrayReader reader2(nums2); + cout << Solution().getIndex(reader2) << '\n'; + // 4 + + return 0; +} diff --git a/readme.md b/readme.md index 63d13253..7108c040 100644 --- a/readme.md +++ b/readme.md @@ -1446,6 +1446,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1531 | [String Compression II](https://leetcode.com/problems/string-compression-ii/) | [无][缺:其他解法] | [C++](1501-2000/1531-String-Compression-II/cpp-1531/) | | | | | | | | | | +| 1533 | [Find the Index of the Large Integer](https://leetcode.com/problems/find-the-index-of-the-large-integer/) | [solution](https://leetcode.com/problems/find-the-index-of-the-large-integer/solutions/2885830/find-the-index-of-the-large-integer/?orderBy=most_votes) | [C++](1501-2000/1533-Find-the-Index-of-the-Large-Integer/cpp-1533/) | | | +| | | | | | | | 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [solution](https://leetcode.com/problems/kth-missing-positive-number/solution/) | [C++](1501-2000/1539-Kth-Missing-Positive-Number/cpp-1539/) | | | | | | | | | | | 1559 | [Detect Cycles in 2D Grid](https://leetcode.com/problems/detect-cycles-in-2d-grid/) | [无] | [C++](1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/) | | | From 4ce34c2dc037da78a4e75cfaa23be8019ffdc8a8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 14 Jan 2023 20:11:24 -0800 Subject: [PATCH 276/390] 2533 solved. --- .../cpp-2533/CMakeLists.txt | 6 +++ .../cpp-2533/main.cpp | 49 +++++++++++++++++++ readme.md | 1 + 3 files changed, 56 insertions(+) create mode 100644 2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/CMakeLists.txt create mode 100644 2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/main.cpp diff --git a/2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/CMakeLists.txt b/2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/CMakeLists.txt new file mode 100644 index 00000000..cac4ab16 --- /dev/null +++ b/2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2533) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2533 main.cpp) diff --git a/2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/main.cpp b/2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/main.cpp new file mode 100644 index 00000000..03f64446 --- /dev/null +++ b/2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/number-of-good-binary-strings/ +/// Author : liuyubobobo +/// Time : 2023-01-14 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(maxLength) +/// Space Complexity: O(maxLength) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int goodBinaryStrings(int minLength, int maxLength, int oneGroup, int zeroGroup) { + + vector dp(maxLength + 1, -1); + long long res = 0; + for(int len = minLength; len <= maxLength; len ++) + res += solve(len, oneGroup, zeroGroup, dp), res %= MOD; + return res; + } + +private: + long long solve(int len, int one, int zero, vector& dp){ + + if(len < 0) return 0; + if(len == 0) return 1; + if(dp[len] != -1) return dp[len]; + + long long res = solve(len - one, one, zero, dp); + res += solve(len - zero, one, zero, dp); + return dp[len] = res % MOD; + } +}; + + +int main() { + + cout << Solution().goodBinaryStrings(2, 3, 1, 2) << '\n'; + // 5 + + return 0; +} diff --git a/readme.md b/readme.md index 7108c040..aa9177a1 100644 --- a/readme.md +++ b/readme.md @@ -2414,6 +2414,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2530 | [Maximal Score After Applying K Operations](https://leetcode.com/problems/maximal-score-after-applying-k-operations/) | [无] | [C++](2001-2500/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/) | | | | 2531 | [Make Number of Distinct Characters Equal](https://leetcode.com/problems/make-number-of-distinct-characters-equal/) | [无] | [C++](2001-2500/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/) | | | | 2532 | [Time to Cross a Bridge](https://leetcode.com/problems/time-to-cross-a-bridge/description/) | [无] | [C++](2001-2500/2532-Time-to-Cross-a-Bridge/cpp-2532/) | | | +| 2533 | [Number of Good Binary Strings](https://leetcode.com/problems/number-of-good-binary-strings/) | [无] | [C++](2001-2500/2533-Number-of-Good-Binary-Strings/cpp-2533/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 758af449c54814f0aea0953c58e9cd5d2990a8a8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 14 Jan 2023 20:47:02 -0800 Subject: [PATCH 277/390] 2534 solved. --- .../cpp-2534/CMakeLists.txt | 6 ++ .../cpp-2534/main.cpp | 81 +++++++++++++++++++ readme.md | 1 + 3 files changed, 88 insertions(+) create mode 100644 2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/CMakeLists.txt create mode 100644 2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/main.cpp diff --git a/2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/CMakeLists.txt b/2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/CMakeLists.txt new file mode 100644 index 00000000..8dfa2c4e --- /dev/null +++ b/2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2534) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2534 main.cpp) diff --git a/2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/main.cpp b/2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/main.cpp new file mode 100644 index 00000000..aa68e311 --- /dev/null +++ b/2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/main.cpp @@ -0,0 +1,81 @@ +/// Source : https://leetcode.com/problems/time-taken-to-cross-the-door/description/ +/// Author : liuyubobobo +/// Time : 2023-01-14 + +#include +#include +#include + +using namespace std; + + +/// Event Simulation +/// Time Compelxity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector timeTaken(vector& arrival, vector& state) { + + int n = arrival.size(); + + vector res(n, -1); + queue enter_list, leave_list; + int arrival_index = 0, t = 0, pre_state = -1; + while(arrival_index < n || !enter_list.empty() || !leave_list.empty()){ + while(arrival_index < n && arrival[arrival_index] <= t){ + if(state[arrival_index] == 0) enter_list.push(arrival_index); + else leave_list.push(arrival_index); + arrival_index ++; + } + + if(pre_state == -1 || pre_state == 1){ + if(!leave_list.empty()){ + int index = leave_list.front(); leave_list.pop(); + res[index] = t; + pre_state = 1; + } + else if(!enter_list.empty()){ + int index = enter_list.front(); enter_list.pop(); + res[index] = t; + pre_state = 0; + } + else pre_state = -1; + } + else{ + if(!enter_list.empty()){ + int index = enter_list.front(); enter_list.pop(); + res[index] = t; + pre_state = 0; + } + else if(!leave_list.empty()){ + int index = leave_list.front(); leave_list.pop(); + res[index] = t; + pre_state = 1; + } + else pre_state = -1; + } + t ++; + } + + return res; + } +}; + + +void print_vec(const vector& res){ + for(int e: res) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector arrival1 = {0, 1, 1, 2, 4}, state1 = {0, 1, 0, 0, 1}; + print_vec(Solution().timeTaken(arrival1, state1)); + // 0 3 1 2 4 + + vector arrival2 = {0,0,6,9,10,10,11,11,11,12,14,14,15,15,15,15,15,16,18,18}, + state2 = {0,0,0,0,0,0,1,1,1,1,0,1,1,1,0,1,0,1,0,0}; + print_vec(Solution().timeTaken(arrival2, state2)); + // 0 3 1 2 4 + + return 0; +} diff --git a/readme.md b/readme.md index aa9177a1..7ee260de 100644 --- a/readme.md +++ b/readme.md @@ -2415,6 +2415,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2531 | [Make Number of Distinct Characters Equal](https://leetcode.com/problems/make-number-of-distinct-characters-equal/) | [无] | [C++](2001-2500/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/) | | | | 2532 | [Time to Cross a Bridge](https://leetcode.com/problems/time-to-cross-a-bridge/description/) | [无] | [C++](2001-2500/2532-Time-to-Cross-a-Bridge/cpp-2532/) | | | | 2533 | [Number of Good Binary Strings](https://leetcode.com/problems/number-of-good-binary-strings/) | [无] | [C++](2001-2500/2533-Number-of-Good-Binary-Strings/cpp-2533/) | | | +| 2534 | [Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door/description/) | [无] | [C++](2001-2500/2534-Time-Taken-to-Cross-the-Door/cpp-2534/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 3d43225e28528c2efea62d7104f8344d36a09dae Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 15 Jan 2023 22:40:02 -0800 Subject: [PATCH 278/390] 0057 solved. --- .../cpp-0057/CMakeLists.txt | 6 +++ .../0057-Insert-Interval/cpp-0057/main.cpp | 49 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0057-Insert-Interval/cpp-0057/CMakeLists.txt create mode 100644 0001-0500/0057-Insert-Interval/cpp-0057/main.cpp diff --git a/0001-0500/0057-Insert-Interval/cpp-0057/CMakeLists.txt b/0001-0500/0057-Insert-Interval/cpp-0057/CMakeLists.txt new file mode 100644 index 00000000..15527718 --- /dev/null +++ b/0001-0500/0057-Insert-Interval/cpp-0057/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_0057) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0057 main.cpp) diff --git a/0001-0500/0057-Insert-Interval/cpp-0057/main.cpp b/0001-0500/0057-Insert-Interval/cpp-0057/main.cpp new file mode 100644 index 00000000..23304fb3 --- /dev/null +++ b/0001-0500/0057-Insert-Interval/cpp-0057/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/insert-interval/ +/// Author : liuyubobobo +/// Time : 2023-01-15 + +#include +#include + +using namespace std; + + +/// Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector> insert(vector>& intervals, vector& newInterval) { + + intervals.push_back(newInterval); + sort(intervals.begin(), intervals.end()); + + vector> res; + for(const vector& v: intervals){ + if(!res.empty() && is_intersect(res.back(), v)){ + vector x = res.back(); + res.pop_back(); + res.push_back(intersect(x, v)); + } + else res.push_back(v); + } + return res; + } + +private: + bool is_intersect(const vector& a, const vector& b){ + if(a[0] <= b[0] && b[0] <= a[1]) return true; + if(b[0] <= a[0] && a[0] <= b[1]) return true; + return false; + } + + vector intersect(const vector& a, const vector& b){ + return {min(a[0], b[0]), max(a[1], b[1])}; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 7ee260de..053dfc9c 100644 --- a/readme.md +++ b/readme.md @@ -103,7 +103,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 054 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/description/) | [solution](https://leetcode.com/problems/spiral-matrix/solution/) | [C++](0001-0500/0054-Spiral-Matrix/cpp-0054/) | | | | 055 | [Jump Game](https://leetcode.com/problems/jump-game/) | [solution](https://leetcode.com/problems/jump-game/solution/) | [C++](0001-0500/0055-Jump-Game/cpp-0055/) | | | | 056 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/description/) | [solution](https://leetcode.com/problems/merge-intervals/solution/) | [C++](0001-0500/0056-Merge-Intervals/cpp-0056/) | | | -| | | | | | | +| 057 | [Insert Interval](https://leetcode.com/problems/insert-interval/) | [solution](https://leetcode.com/problems/insert-interval/solutions/2914120/insert-intervals/?orderBy=most_votes) | [C++](0001-0500/0057-Insert-Interval/cpp-0057/) | | | | 058 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | [solution](https://leetcode.com/problems/length-of-last-word/solution/) | [C++](0001-0500/0058-Length-of-Last-Word/cpp-0058/) | | | | 059 | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/description/) | [无] | [C++](0001-0500/0059-Spiral-Matrix-II/cpp-0059/) | | | | | | | | | | From 2d4be0662d259fad4b967bc7fce75ec0a0bfcf01 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 19 Jan 2023 00:53:18 -0800 Subject: [PATCH 279/390] 2539 solved. --- .../cpp-2539/CMakeLists.txt | 6 ++ .../cpp-2539/main.cpp | 80 +++++++++++++++++++ readme.md | 2 + 3 files changed, 88 insertions(+) create mode 100644 2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/CMakeLists.txt create mode 100644 2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/main.cpp diff --git a/2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/CMakeLists.txt b/2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/CMakeLists.txt new file mode 100644 index 00000000..17f76ecb --- /dev/null +++ b/2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2539) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2539 main.cpp) diff --git a/2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/main.cpp b/2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/main.cpp new file mode 100644 index 00000000..9e08f006 --- /dev/null +++ b/2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/main.cpp @@ -0,0 +1,80 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-good-subsequences/description/ +/// Author : liuyubobobo +/// Time : 2023-01-19 + +#include +#include +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(n) +/// Space Complexity: O(maxn) +class Solution { + +private: + const int MAXN = 1e4; + const long long MOD = 1e9 + 7; + vector fac, ifac; + +public: + Solution() : fac(MAXN + 1, 1), ifac(MAXN + 1){ + for(int i = 2; i <= MAXN; i ++) fac[i] = fac[i - 1] * i % MOD; + for(int i = 0; i <= MAXN; i ++) ifac[i] = quick_pow(fac[i], MOD - 2); + } + + int countGoodSubsequences(string s) { + + int n = s.size(); + + vector f(26, 0); + for(char c: s) f[c - 'a'] ++; + + vector a; + for(int e: f) if(e) a.push_back(e); + int maxf = *max_element(a.begin(), a.end()); + + long long total = 0; + for(int i = 1; i <= maxf; i ++){ + long long tres = 1; + for(int e: a) tres = tres * (C(e, i) + C(e, 0)) % MOD; + tres = (tres - 1 + MOD) % MOD; + total += tres; + } + return total % MOD; + } + +private: + long long C(int n, int r){ + if(r > n) return 0; + return fac[n] * ifac[r] % MOD * ifac[n - r] % MOD; + } + + long long quick_pow(long long a, long long k) { + long long res = 1ll; + while (k) { + if (k & 1) res = res * a % MOD; + a = a * a % MOD; + k >>= 1; + } + return res % MOD; + } +}; + + +int main() { + + cout << Solution().countGoodSubsequences("aabb") << '\n'; + // 11 + + cout << Solution().countGoodSubsequences("leet") << '\n'; + // 12 + + cout << Solution().countGoodSubsequences("abcd") << '\n'; + // 15 + + return 0; +} diff --git a/readme.md b/readme.md index 053dfc9c..dbb854ea 100644 --- a/readme.md +++ b/readme.md @@ -2417,6 +2417,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2533 | [Number of Good Binary Strings](https://leetcode.com/problems/number-of-good-binary-strings/) | [无] | [C++](2001-2500/2533-Number-of-Good-Binary-Strings/cpp-2533/) | | | | 2534 | [Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door/description/) | [无] | [C++](2001-2500/2534-Time-Taken-to-Cross-the-Door/cpp-2534/) | | | | | | | | | | +| 2539 | [Count the Number of Good Subsequences](https://leetcode.com/problems/count-the-number-of-good-subsequences/) | [无] | [C++](2001-2500/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 4c98a35c8af6cd8632c68b7215d656876d734425 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 19 Jan 2023 23:26:27 -0800 Subject: [PATCH 280/390] 0491 solved. --- .../cpp-0491/CMakeLists.txt | 6 +++ .../cpp-0491/main.cpp | 49 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/CMakeLists.txt create mode 100644 0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/main.cpp diff --git a/0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/CMakeLists.txt b/0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/CMakeLists.txt new file mode 100644 index 00000000..77a0db9f --- /dev/null +++ b/0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_0491) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0491 main.cpp) diff --git a/0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/main.cpp b/0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/main.cpp new file mode 100644 index 00000000..7d170d85 --- /dev/null +++ b/0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/non-decreasing-subsequences/description/ +/// Author : liuyubobobo +/// Time : 2023-01-19 + +#include +#include +#include + +using namespace std; + + +/// Backtrack +/// Time Compelxity: O(2^n) +/// Space Compelxity: O(n) +class Solution { +public: + vector> findSubsequences(vector& nums) { + + int n = nums.size(); + set> res; + vector cur; + dfs(n, nums, 0, cur, res); + return vector>(res.begin(), res.end()); + } + +private: + void dfs(int n, const vector& nums, int start, + vector& cur, set>& res){ + + if(start == n){ + if(cur.size() >= 2) res.insert(cur); + return; + } + + dfs(n, nums, start + 1, cur, res); + + if(cur.empty() || nums[start] >= cur.back()){ + cur.push_back(nums[start]); + dfs(n, nums, start + 1, cur, res); + cur.pop_back(); + } + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index dbb854ea..305cb099 100644 --- a/readme.md +++ b/readme.md @@ -522,7 +522,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 488 | [Zuma Game](https://leetcode.com/problems/zuma-game/) | [无] | [C++](0001-0500/0488-Zuma-Game/cpp-0488/) | | | | 489 | [Robot Room Cleaner](https://leetcode.com/problems/robot-room-cleaner/) | [solution](https://leetcode.com/problems/robot-room-cleaner/solution/) | [C++](0001-0500/0489-Robot-Room-Cleaner/cpp-0489/) | | | | 490 | [The Maze](https://leetcode.com/problems/the-maze/description/) | [solution](https://leetcode.com/problems/the-maze/solution/) | [C++](0001-0500/0490-The-Maze/cpp-0490/) | | | -| | | | | | | +| 491 | [Non-decreasing Subsequences](https://leetcode.com/problems/non-decreasing-subsequences/description/) | [无] | [C++](0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/) | | | | 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/) | [无] | [C++](0001-0500/0492-Construct-the-Rectangle/cpp-0492/) | | | | | | | | | | | 494 | [Target Sum](https://leetcode.com/problems/target-sum/description/) | [solution](https://leetcode.com/problems/target-sum/solution/) | [C++](0001-0500/0494-Target-Sum/cpp-0494/) | | | From b836f8382df55ec3a52a635072dbaf52a05f9fee Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 21 Jan 2023 22:10:55 -0800 Subject: [PATCH 281/390] 1088 solved. --- .../cpp-1088/CMakeLists.txt | 6 ++ .../cpp-1088/main.cpp | 68 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1088-Confusing-Number-II/cpp-1088/CMakeLists.txt create mode 100644 1001-1500/1088-Confusing-Number-II/cpp-1088/main.cpp diff --git a/1001-1500/1088-Confusing-Number-II/cpp-1088/CMakeLists.txt b/1001-1500/1088-Confusing-Number-II/cpp-1088/CMakeLists.txt new file mode 100644 index 00000000..7d08cc44 --- /dev/null +++ b/1001-1500/1088-Confusing-Number-II/cpp-1088/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1088) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1088 main.cpp) diff --git a/1001-1500/1088-Confusing-Number-II/cpp-1088/main.cpp b/1001-1500/1088-Confusing-Number-II/cpp-1088/main.cpp new file mode 100644 index 00000000..d9992184 --- /dev/null +++ b/1001-1500/1088-Confusing-Number-II/cpp-1088/main.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/confusing-number-ii/description/ +/// Author : liuyubobobo +/// Time : 2023-01-21 + +#include +#include +#include + +using namespace std; + + +/// Backtrack +/// Time Complexity: O(5^9) +/// Space Complexity: O(1) +class Solution { + +private: + const vector valid_digits = {0, 1, 6, 8, 9}; + const vector r_digits = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6}; + +public: + int confusingNumberII(int n) { + + int len = to_string(n).size(); + vector nums(len); + return dfs(len, nums, 0, 0, n); + } + +private: + int dfs(int len, vector& nums, int index, long long cur, long long n){ + + if(cur > n) return 0; + + if(index == len){ + + int i = 0; + for(i = 0; i < len && nums[i] == 0; i ++); + + long long r = 0; + for(int j = len - 1; j >= i; j --) + r = r * 10ll + r_digits[nums[j]]; +// if(cur != r) cout << cur << '\n'; + return cur != r; + } + + int res = 0; + for(int d: valid_digits){ + nums[index] = d; + res += dfs(len, nums, index + 1, cur * 10ll + d, n); + } + return res; + } +}; + + +int main() { + + cout << Solution().confusingNumberII(20) << '\n'; + // 6 + + cout << Solution().confusingNumberII(100) << '\n'; + // 19 + + cout << Solution().confusingNumberII(1000000000) << '\n'; + // 1950627 + + return 0; +} diff --git a/readme.md b/readme.md index 305cb099..1226a7a0 100644 --- a/readme.md +++ b/readme.md @@ -1074,7 +1074,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1084 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 1087 | [Brace Expansion](https://leetcode.com/problems/brace-expansion/) | [无] | [C++](1001-1500/1087-Brace-Expansion/cpp-1087/) | | | -| | | | | | | +| 1088 | [Confusing Number II](https://leetcode.com/problems/confusing-number-ii/description/) | [无] | [C++](1001-1500/1088-Confusing-Number-II/cpp-1088/) | | | | 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [无] | [C++](1001-1500/1089-Duplicate-Zeros/cpp-1089/) | | | | 1090 | [Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/) | [无] | [C++](1001-1500/1090-Largest-Values-From-Labels/cpp-1090/) | | | | 1091 | [Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/) | [无] | [C++](1001-1500/1091-Shortest-Path-in-Binary-Matrix/cpp-1091/) | | | From 02b831312393e5ca8b07ea5bdac0c8c4b83d9c9b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 21 Jan 2023 22:14:48 -0800 Subject: [PATCH 282/390] 1815 solved. --- .../cpp-1815/main.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/1501-2000/1815-Maximum-Number-of-Groups-Getting-Fresh-Donuts/cpp-1815/main.cpp b/1501-2000/1815-Maximum-Number-of-Groups-Getting-Fresh-Donuts/cpp-1815/main.cpp index 939cebf0..09c71f3c 100644 --- a/1501-2000/1815-Maximum-Number-of-Groups-Getting-Fresh-Donuts/cpp-1815/main.cpp +++ b/1501-2000/1815-Maximum-Number-of-Groups-Getting-Fresh-Donuts/cpp-1815/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/maximum-number-of-groups-getting-fresh-donuts/ /// Author : liuyubobobo /// Time : 2020-06-11 +/// Updaetd: 2023-01-21 #include #include @@ -10,7 +11,7 @@ using namespace std; -/// Memory Search - vector as key +/// Memory Search /// Time Complexity: O(2^n) /// Space Complexity: O(2^n) class Solution { @@ -23,15 +24,18 @@ class Solution { int res = f[0]; f[0] = 0; - map, int> dp; + map dp; res += dfs(f, batchSize, dp); return res; } private: - int dfs(vector& f, int k, map, int>& dp){ + int dfs(vector& f, int k, map& dp){ - auto iter = dp.find(f); + long long h = 0; + for(int e: f) h = h * 10ll + e; + + auto iter = dp.find(h); if(iter != dp.end()) return iter->second; int pre = f[0]; @@ -44,7 +48,7 @@ class Solution { f[0] = pre; f[i] ++; } - return dp[f] = res; + return dp[h] = res; } }; From 1d169a1d4c1679689e4b2183e3e7e3d278a72ae3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 21 Jan 2023 22:40:26 -0800 Subject: [PATCH 283/390] 2540-2542 solved. --- .../cpp-2540/CMakeLists.txt | 6 +++ .../cpp-2540/main.cpp | 32 ++++++++++++ .../cpp-2541/CMakeLists.txt | 6 +++ .../cpp-2541/main.cpp | 48 +++++++++++++++++ .../cpp-2542/CMakeLists.txt | 6 +++ .../cpp-2542/main.cpp | 52 +++++++++++++++++++ readme.md | 3 ++ 7 files changed, 153 insertions(+) create mode 100644 2501-3000/2540-Minimum-Common-Value/cpp-2540/CMakeLists.txt create mode 100644 2501-3000/2540-Minimum-Common-Value/cpp-2540/main.cpp create mode 100644 2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/CMakeLists.txt create mode 100644 2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/main.cpp create mode 100644 2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/CMakeLists.txt create mode 100644 2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/main.cpp diff --git a/2501-3000/2540-Minimum-Common-Value/cpp-2540/CMakeLists.txt b/2501-3000/2540-Minimum-Common-Value/cpp-2540/CMakeLists.txt new file mode 100644 index 00000000..c8903efb --- /dev/null +++ b/2501-3000/2540-Minimum-Common-Value/cpp-2540/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2540) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2540 main.cpp) diff --git a/2501-3000/2540-Minimum-Common-Value/cpp-2540/main.cpp b/2501-3000/2540-Minimum-Common-Value/cpp-2540/main.cpp new file mode 100644 index 00000000..ea4a5035 --- /dev/null +++ b/2501-3000/2540-Minimum-Common-Value/cpp-2540/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/minimum-common-value/description/ +/// Author : liuyubobobo +/// Time : 2023-01-21 + +#include +#include + +using namespace std; + + +/// Two Pointers +/// Time Complexity: O(|nums1| + |nums2|) +/// Space Complexity: O(1) +class Solution { +public: + int getCommon(vector& nums1, vector& nums2) { + + int i = 0, j = 0; + while(i < nums1.size() && j < nums2.size()){ + if(nums1[i] == nums2[j]) return nums1[i]; + else if(nums1[i] < nums2[j]) i ++; + else j ++; + } + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/CMakeLists.txt b/2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/CMakeLists.txt new file mode 100644 index 00000000..189c45e8 --- /dev/null +++ b/2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2541) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2541 main.cpp) diff --git a/2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/main.cpp b/2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/main.cpp new file mode 100644 index 00000000..95e40d1f --- /dev/null +++ b/2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/description/ +/// Author : liuyubobobo +/// Time : 2023-01-21 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long minOperations(vector& nums1, vector& nums2, int k) { + + int n = nums1.size(); + if(k == 0){ + for(int i = 0; i < n; i ++) + if(nums1[i] != nums2[i]) return -1; + return 0; + } + + long long a = 0, b = 0; + for(int i = 0; i < n; i ++){ + if(nums1[i] > nums2[i]){ + long long d = nums1[i] - nums2[i]; + if(d % k != 0) return -1; + a += d / k; + } + else if(nums1[i] < nums2[i]){ + long long d = nums2[i] - nums1[i]; + if(d % k != 0) return -1; + b += d / k; + } + } + + return a == b ? a : -1; + } +}; + + +int main() { + + + return 0; +} diff --git a/2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/CMakeLists.txt b/2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/CMakeLists.txt new file mode 100644 index 00000000..c62f244f --- /dev/null +++ b/2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2542) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2542 main.cpp) diff --git a/2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/main.cpp b/2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/main.cpp new file mode 100644 index 00000000..eca07bae --- /dev/null +++ b/2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/maximum-subsequence-score/description/ +/// Author : liuyubobobo +/// Time : 2023-01-21 + +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + + int n = nums1.size(); + + vector> v(n); + for(int i = 0; i < n; i ++) v[i] = {nums1[i], nums2[i]}; + + sort(v.begin(), v.end(), + [](const pair& p1, const pair& p2){ + return p1.second < p2.second; + }); + + priority_queue, greater<>> pq; + long long sum = 0; + for(int i = n - 1; i >= n - k; i --) + pq.push(v[i].first), sum += v[i].first; + + long long res = v[n - k].second * sum; + for(int i = n - k - 1; i >= 0; i --){ + long long t = pq.top(); + pq.pop(); + sum -= t; + pq.push(v[i].first); + sum += v[i].first; + res = max(res, v[i].second * sum); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 1226a7a0..65222b56 100644 --- a/readme.md +++ b/readme.md @@ -2418,6 +2418,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2534 | [Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door/description/) | [无] | [C++](2001-2500/2534-Time-Taken-to-Cross-the-Door/cpp-2534/) | | | | | | | | | | | 2539 | [Count the Number of Good Subsequences](https://leetcode.com/problems/count-the-number-of-good-subsequences/) | [无] | [C++](2001-2500/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/) | | | +| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value/) | [无] | [C++](2001-2500/2540-Minimum-Common-Value/cpp-2540/) | | | +| 2541 | [Minimum Operations to Make Array Equal II](https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/) | [无] | [C++](2001-2500/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/) | | | +| 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) | [无] | [C++](2001-2500/2542-Maximum-Subsequence-Score/cpp-2542/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 1808b8720bc8605568da033861c22b0ec3b67f7d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 21 Jan 2023 22:43:57 -0800 Subject: [PATCH 284/390] 2535-2537 solved. --- .../cpp-2535/CMakeLists.txt | 6 +++ .../cpp-2535/main.cpp | 31 +++++++++++++ .../cpp-2536/CMakeLists.txt | 6 +++ .../cpp-2536/main.cpp | 41 ++++++++++++++++++ .../cpp-2537/CMakeLists.txt | 6 +++ .../cpp-2537/main.cpp | 43 +++++++++++++++++++ readme.md | 3 ++ 7 files changed, 136 insertions(+) create mode 100644 2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/CMakeLists.txt create mode 100644 2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/main.cpp create mode 100644 2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/CMakeLists.txt create mode 100644 2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/main.cpp create mode 100644 2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/CMakeLists.txt create mode 100644 2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/main.cpp diff --git a/2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/CMakeLists.txt b/2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/CMakeLists.txt new file mode 100644 index 00000000..4b2bfe28 --- /dev/null +++ b/2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2535) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2535 main.cpp) diff --git a/2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/main.cpp b/2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/main.cpp new file mode 100644 index 00000000..9a89b7fe --- /dev/null +++ b/2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/description/ +/// Author : liuyubobobo +/// Time : 2023-01-14 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Compelxity: O(nlog(max_num)) +/// Space Complexity: O(1) +class Solution { +public: + int differenceOfSum(vector& nums) { + + int sum1 = 0, sum2 = 0; + for(int num: nums){ + sum1 += num; + while(num) sum2 += num % 10, num /= 10; + } + return abs(sum1 - sum2); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/CMakeLists.txt b/2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/CMakeLists.txt new file mode 100644 index 00000000..dc6f5345 --- /dev/null +++ b/2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2536) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2536 main.cpp) diff --git a/2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/main.cpp b/2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/main.cpp new file mode 100644 index 00000000..6127abba --- /dev/null +++ b/2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/increment-submatrices-by-one/description/ +/// Author : liuyubobobo +/// Time : 2023-01-14 + +#include +#include + +using namespace std; + + +/// Diff Array +/// Time Complexity: O(|q| * n) +/// Space Complexity: O(n^2) +class Solution { +public: + vector> rangeAddQueries(int n, vector>& queries) { + + vector> diff(n, vector(n + 1, 0)); + for(const vector& q: queries){ + int a = q[0], b = q[2], l = q[1], r = q[3]; + for(int i = a; i <= b; i ++) + diff[i][l] += 1, diff[i][r + 1] -= 1; + } + + vector> res(n, vector(n)); + for(int i = 0; i < n; i ++){ + int presum = 0; + for(int j = 0; j < n; j ++){ + presum += diff[i][j]; + res[i][j] = presum; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/CMakeLists.txt b/2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/CMakeLists.txt new file mode 100644 index 00000000..266c11b5 --- /dev/null +++ b/2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2537) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2537 main.cpp) diff --git a/2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/main.cpp b/2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/main.cpp new file mode 100644 index 00000000..d286e5f4 --- /dev/null +++ b/2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-good-subarrays/description/ +/// Author : liuyubobobo +/// Time : 2023-01-14 + +#include +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long countGood(vector& nums, int k) { + + map f; + long long cur = 0, res = 0; + int n = nums.size(), l = 0, r = -1; + while(l < n){ + if(r + 1 < n && cur < k){ + cur += f[nums[r + 1]]; + f[nums[r + 1]] ++; + r ++; + } + else{ + f[nums[l]] --; + cur -= f[nums[l]]; + l ++; + } + if(cur >= k) res += n - r; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 65222b56..a6cd7a60 100644 --- a/readme.md +++ b/readme.md @@ -2416,6 +2416,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2532 | [Time to Cross a Bridge](https://leetcode.com/problems/time-to-cross-a-bridge/description/) | [无] | [C++](2001-2500/2532-Time-to-Cross-a-Bridge/cpp-2532/) | | | | 2533 | [Number of Good Binary Strings](https://leetcode.com/problems/number-of-good-binary-strings/) | [无] | [C++](2001-2500/2533-Number-of-Good-Binary-Strings/cpp-2533/) | | | | 2534 | [Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door/description/) | [无] | [C++](2001-2500/2534-Time-Taken-to-Cross-the-Door/cpp-2534/) | | | +| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/) | [无] | [C++](2001-2500/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/) | | | +| 2536 | [Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/) | [无] | [C++](2001-2500/2536-Increment-Submatrices-by-One/cpp-2536/) | | | +| 2537 | [Count the Number of Good Subarrays](https://leetcode.com/problems/count-the-number-of-good-subarrays/) | [无] | [C++](2001-2500/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/) | | | | | | | | | | | 2539 | [Count the Number of Good Subsequences](https://leetcode.com/problems/count-the-number-of-good-subsequences/) | [无] | [C++](2001-2500/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/) | | | | 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value/) | [无] | [C++](2001-2500/2540-Minimum-Common-Value/cpp-2540/) | | | From e7a7615d6ee9fb1b4ddadaad5ec6a19a0ea3d18a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 22 Jan 2023 01:04:04 -0800 Subject: [PATCH 285/390] 2544-2547 solved. --- .../cpp-2544/CMakeLists.txt | 6 +++ .../cpp-2544/main.cpp | 29 +++++++++++++ .../cpp-2545/CMakeLists.txt | 6 +++ .../cpp-2545/main.cpp | 31 +++++++++++++ .../cpp-2546/CMakeLists.txt | 6 +++ .../cpp-2546/main.cpp | 38 ++++++++++++++++ .../cpp-2547/CMakeLists.txt | 6 +++ .../cpp-2547/main.cpp | 43 +++++++++++++++++++ readme.md | 5 +++ 9 files changed, 170 insertions(+) create mode 100644 2501-3000/2544-Alternating-Digit-Sum/cpp-2544/CMakeLists.txt create mode 100644 2501-3000/2544-Alternating-Digit-Sum/cpp-2544/main.cpp create mode 100644 2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/CMakeLists.txt create mode 100644 2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/main.cpp create mode 100644 2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/CMakeLists.txt create mode 100644 2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/main.cpp create mode 100644 2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/CMakeLists.txt create mode 100644 2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp diff --git a/2501-3000/2544-Alternating-Digit-Sum/cpp-2544/CMakeLists.txt b/2501-3000/2544-Alternating-Digit-Sum/cpp-2544/CMakeLists.txt new file mode 100644 index 00000000..4a2a5823 --- /dev/null +++ b/2501-3000/2544-Alternating-Digit-Sum/cpp-2544/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2544) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2544 main.cpp) diff --git a/2501-3000/2544-Alternating-Digit-Sum/cpp-2544/main.cpp b/2501-3000/2544-Alternating-Digit-Sum/cpp-2544/main.cpp new file mode 100644 index 00000000..10e9fb6e --- /dev/null +++ b/2501-3000/2544-Alternating-Digit-Sum/cpp-2544/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/alternating-digit-sum/description/ +/// Author : liuyubobobo +/// Time : 2023-01-21 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(logn) +/// Space Complexity: O(logn) +class Solution { +public: + int alternateDigitSum(int n) { + + string s = to_string(n); + int res = 0; + for(int i = 0; i < s.size(); i ++) + res += (i % 2 ? -1 : 1) * (s[i] - '0'); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/CMakeLists.txt b/2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/CMakeLists.txt new file mode 100644 index 00000000..95c038f1 --- /dev/null +++ b/2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2545) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2545 main.cpp) diff --git a/2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/main.cpp b/2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/main.cpp new file mode 100644 index 00000000..c4faa74b --- /dev/null +++ b/2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/sort-the-students-by-their-kth-score/description/ +/// Author : liuyubobobo +/// Time : 2023-01-21 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(mlogm * n) +/// Space Cpomplexity: O(n) +class Solution { +public: + vector> sortTheStudents(vector>& score, int k) { + + sort(score.begin(), score.end(), + [k](const vector& s1, const vector& s2){ + return s1[k] > s2[k]; + }); + return score; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/CMakeLists.txt b/2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/CMakeLists.txt new file mode 100644 index 00000000..35963fd1 --- /dev/null +++ b/2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2546) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2546 main.cpp) diff --git a/2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/main.cpp b/2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/main.cpp new file mode 100644 index 00000000..19dd9be4 --- /dev/null +++ b/2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/description/ +/// Author : liuyubobobo +/// Time : 2023-01-22 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool makeStringsEqual(string s, string target) { + + int n = s.size() - 1; + if(s == target) return true; + + bool s_has1 = false, s_all0 = true; + for(char c: s) if(c == '1') s_has1 = true; + for(char c: s) if(c == '1') s_all0 = false; + + bool t_has1 = false, t_all0 = true; + for(char c: target) if(c == '1') t_has1 = true; + for(char c: target) if(c == '1') t_all0 = false; + + if(s_has1 && t_all0) return false; + if(t_has1 && s_all0) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/CMakeLists.txt b/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/CMakeLists.txt new file mode 100644 index 00000000..48bbea51 --- /dev/null +++ b/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2547) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2547 main.cpp) diff --git a/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp b/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp new file mode 100644 index 00000000..bdef60b0 --- /dev/null +++ b/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/minimum-cost-to-split-an-array/description/ +/// Author : liuyubobobo +/// Time : 2023-01-22 + +#include +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int minCost(vector& nums, int k) { + + int n = nums.size(); + + vector dp(n + 1, INT_MAX / 2); + dp[n] = 0; + for(int i = n - 1; i >= 0; i --){ + map f; + int trim_value = 0; + for(int j = i; j >= 0; j --){ + f[nums[j]] ++; + int len = f[nums[j]]; + if(len == 2) trim_value += 2; + else if(len > 2) trim_value ++; + dp[j] = min(dp[j], trim_value + k + dp[i + 1]); + } + } + return dp[0]; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a6cd7a60..645255cc 100644 --- a/readme.md +++ b/readme.md @@ -2425,6 +2425,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2541 | [Minimum Operations to Make Array Equal II](https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/) | [无] | [C++](2001-2500/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/) | | | | 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) | [无] | [C++](2001-2500/2542-Maximum-Subsequence-Score/cpp-2542/) | | | | | | | | | | +| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/) | [无] | [C++](2001-2500/2544-Alternating-Digit-Sum/cpp-2544/) | | | +| 2545 | [Sort the Students by Their Kth Score](https://leetcode.com/problems/sort-the-students-by-their-kth-score/) | [无] | [C++](2001-2500/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/) | | | +| 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [无] | [C++](2001-2500/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | | +| 2547 | [Minimum Cost to Split an Array](https://leetcode.com/problems/minimum-cost-to-split-an-array/) | [无] | [C++](2001-2500/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From e25d0f41178885565452a2761020acfad6c66560 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 23 Jan 2023 22:10:50 -0800 Subject: [PATCH 286/390] 2547 codes updated. --- .../2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp b/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp index bdef60b0..f98e2983 100644 --- a/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp +++ b/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp @@ -18,11 +18,12 @@ class Solution { int minCost(vector& nums, int k) { int n = nums.size(); + vector f(n, 0); vector dp(n + 1, INT_MAX / 2); dp[n] = 0; for(int i = n - 1; i >= 0; i --){ - map f; + f.assign(n, 0); int trim_value = 0; for(int j = i; j >= 0; j --){ f[nums[j]] ++; From 3a799c4282da36725487b87b97baee1cb6a3063b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 24 Jan 2023 09:28:56 -0800 Subject: [PATCH 287/390] 2543 solved. --- .../cpp-2543/CMakeLists.txt | 6 ++++ .../cpp-2543/main.cpp | 35 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/CMakeLists.txt create mode 100644 2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/main.cpp diff --git a/2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/CMakeLists.txt b/2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/CMakeLists.txt new file mode 100644 index 00000000..4db2fffb --- /dev/null +++ b/2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2543) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2543 main.cpp) diff --git a/2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/main.cpp b/2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/main.cpp new file mode 100644 index 00000000..6732efdb --- /dev/null +++ b/2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/check-if-point-is-reachable/description/ +/// Author : liuyubobobo +/// Time : 2023-01-24 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(log(max(targetX, targetY))) +/// Space Complexity: O(log(max(targetX, targetY))) +class Solution { +public: + bool isReachable(int targetX, int targetY) { + + int g = gcd(targetX, targetY); + return (g & (g - 1)) == 0; + } + +private: + int gcd(int a, int b){ + if(a > b) swap(a, b); + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + cout << Solution().isReachable(6, 9) << '\n'; + return 0; +} diff --git a/readme.md b/readme.md index 645255cc..ded2763e 100644 --- a/readme.md +++ b/readme.md @@ -2424,7 +2424,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value/) | [无] | [C++](2001-2500/2540-Minimum-Common-Value/cpp-2540/) | | | | 2541 | [Minimum Operations to Make Array Equal II](https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/) | [无] | [C++](2001-2500/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/) | | | | 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) | [无] | [C++](2001-2500/2542-Maximum-Subsequence-Score/cpp-2542/) | | | -| | | | | | | +| 2543 | [Check if Point Is Reachable](https://leetcode.com/problems/check-if-point-is-reachable/description/) | [无] | [C++](2001-2500/2543-Check-if-Point-Is-Reachable/cpp-2543/) | | | | 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/) | [无] | [C++](2001-2500/2544-Alternating-Digit-Sum/cpp-2544/) | | | | 2545 | [Sort the Students by Their Kth Score](https://leetcode.com/problems/sort-the-students-by-their-kth-score/) | [无] | [C++](2001-2500/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/) | | | | 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [无] | [C++](2001-2500/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | | From 22bb734c30cad2b338333e5e83529166fe0b7acb Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 28 Jan 2023 09:30:30 -0800 Subject: [PATCH 288/390] 2548 solved. --- .../cpp-2548/CMakeLists.txt | 6 +++ .../cpp-2548/main.cpp | 46 +++++++++++++++++++ readme.md | 1 + 3 files changed, 53 insertions(+) create mode 100644 2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/CMakeLists.txt create mode 100644 2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/main.cpp diff --git a/2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/CMakeLists.txt b/2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/CMakeLists.txt new file mode 100644 index 00000000..5fcd588b --- /dev/null +++ b/2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2548) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2548 main.cpp) diff --git a/2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/main.cpp b/2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/main.cpp new file mode 100644 index 00000000..b302f6dd --- /dev/null +++ b/2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/maximum-price-to-fill-a-bag/description/ +/// Author : liuyubobobo +/// Time : 2023-01-28 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + double maxPrice(vector>& items, int capacity) { + + int total_weight = 0; + for(const vector& item: items) total_weight += item[1]; + if(total_weight < capacity) return -1; + + sort(items.begin(), items.end(), + [](const vector& item1, const vector& item2){ + double p1 = item1[0], w1 = item1[1]; + double p2 = item2[0], w2 = item2[1]; + return p1 / w1 > p2 / w2; + }); + + double price = 0; + for(const vector& item: items){ + double p = item[0]; int w = item[1]; + if(w <= capacity) price += p, capacity -= w; + else{ + price += capacity / (double)w * p; + break; + } + } + return price; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ded2763e..2f617295 100644 --- a/readme.md +++ b/readme.md @@ -2429,6 +2429,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2545 | [Sort the Students by Their Kth Score](https://leetcode.com/problems/sort-the-students-by-their-kth-score/) | [无] | [C++](2001-2500/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/) | | | | 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [无] | [C++](2001-2500/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | | | 2547 | [Minimum Cost to Split an Array](https://leetcode.com/problems/minimum-cost-to-split-an-array/) | [无] | [C++](2001-2500/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/) | | | +| 2548 | [Maximum Price to Fill a Bag](https://leetcode.com/problems/maximum-price-to-fill-a-bag/) | [无] | [C++](2001-2500/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 1e0ad4c2483d4f98257ce2f274238b013da4967f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 28 Jan 2023 20:50:25 -0800 Subject: [PATCH 289/390] 0460 solved. --- .../0460-LFU-Cache/cpp-0460/CMakeLists.txt | 6 ++ 0001-0500/0460-LFU-Cache/cpp-0460/main.cpp | 90 +++++++++++++++++++ readme.md | 1 + 3 files changed, 97 insertions(+) create mode 100644 0001-0500/0460-LFU-Cache/cpp-0460/CMakeLists.txt create mode 100644 0001-0500/0460-LFU-Cache/cpp-0460/main.cpp diff --git a/0001-0500/0460-LFU-Cache/cpp-0460/CMakeLists.txt b/0001-0500/0460-LFU-Cache/cpp-0460/CMakeLists.txt new file mode 100644 index 00000000..1cdb4aa5 --- /dev/null +++ b/0001-0500/0460-LFU-Cache/cpp-0460/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_0460) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0460 main.cpp) diff --git a/0001-0500/0460-LFU-Cache/cpp-0460/main.cpp b/0001-0500/0460-LFU-Cache/cpp-0460/main.cpp new file mode 100644 index 00000000..b7cfdb61 --- /dev/null +++ b/0001-0500/0460-LFU-Cache/cpp-0460/main.cpp @@ -0,0 +1,90 @@ +/// Source : https://leetcode.com/problems/lfu-cache/description/ +/// Author : liuyubobobo +/// Time : 2023-01-28 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class LFUCache { + +private: + map cache; // key->value + map> f; // key->{f, timer} + map, int> f2; // {f, timer}->key + int capacity, timer; + +public: + LFUCache(int capacity) : capacity(capacity), timer(0) {} + + int get(int key) { + auto iter = cache.find(key); + if(iter == cache.end()) return -1; + + assert(f.count(key)); + pair old_f = f[key]; + pair new_f = {old_f.first + 1, timer ++}; + f[key] = new_f; + + assert(f2.count(old_f)); + f2.erase(old_f); + f2[new_f] = key; + + return iter->second; + } + + void put(int key, int value) { + + auto iter = cache.find(key); + if(iter != cache.end()){ + update(key, value); + return; + } + + if(cache.size() == capacity){ + pair del_f = f2.begin()->first; + int key = f2[del_f]; + + cache.erase(key); + f.erase(key); + f2.erase(f2.begin()); + } + + if(cache.size() < capacity){ + cache[key] = value; + pair new_f = {1, timer ++}; + f[key] = new_f; + f2[new_f] = key; + } + } + +private: + void update(int key, int value){ + + auto iter = cache.find(key); + assert(iter != cache.end()); + + assert(f.count(key)); + pair old_f = f[key]; + pair new_f = {old_f.first + 1, timer ++}; + f[key] = new_f; + + assert(f2.count(old_f)); + f2.erase(old_f); + f2[new_f] = key; + + cache[key] = value; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 2f617295..1238bb50 100644 --- a/readme.md +++ b/readme.md @@ -493,6 +493,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 457 | [Circular Array Loop](https://leetcode.com/problems/circular-array-loop/) | [无] | [C++](0001-0500/0457-Circular-Array-Loop/cpp-0457/) | | | | 458 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | [solution](https://leetcode.com/problems/poor-pigs/solution/) | [C++](0001-0500/0458-Poor-Pigs/) | | | | | | | | | | +| 460 | [LFU Cache](https://leetcode.com/problems/lfu-cache/description/) | [solution](https://leetcode.com/problems/lfu-cache/solutions/2815229/lfu-cache/?orderBy=most_votes) | [C++](0001-0500/0460-LFU-Cache/) | | | | 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [solution](https://leetcode.com/problems/hamming-distance/solution/) | [C++](0001-0500/0461-Hamming-Distance/cpp-0461/) | | | | 462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [solution](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/solution/) | [C++](0001-0500/0462-Minimum-Moves-to-Equal-Array-Elements-II/cpp-0462/) | | | | 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [solution](https://leetcode.com/problems/island-perimeter/solution/) | [C++](0001-0500/0463-Island-Perimeter/cpp-0463/) | | | From edd916a9035d4c678ec807d077d56c223b1ca2c3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 29 Jan 2023 10:05:43 -0800 Subject: [PATCH 290/390] 2549-2551 solved. --- .../cpp-2549/CMakeLists.txt | 6 +++ .../cpp-2549/main.cpp | 25 ++++++++++++ .../cpp-2550/CMakeLists.txt | 6 +++ .../cpp-2550/main.cpp | 39 +++++++++++++++++++ .../cpp-2551/CMakeLists.txt | 6 +++ .../cpp-2551/main.cpp | 37 ++++++++++++++++++ readme.md | 3 ++ 7 files changed, 122 insertions(+) create mode 100644 2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/CMakeLists.txt create mode 100644 2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/main.cpp create mode 100644 2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/CMakeLists.txt create mode 100644 2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/main.cpp create mode 100644 2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/CMakeLists.txt create mode 100644 2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/main.cpp diff --git a/2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/CMakeLists.txt b/2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/CMakeLists.txt new file mode 100644 index 00000000..af4c5c79 --- /dev/null +++ b/2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2549) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2549 main.cpp) diff --git a/2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/main.cpp b/2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/main.cpp new file mode 100644 index 00000000..dd7e64e0 --- /dev/null +++ b/2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/main.cpp @@ -0,0 +1,25 @@ +/// Source : https://leetcode.com/problems/count-distinct-numbers-on-board/description/ +/// Author : liuyubobobo +/// Time : 2023-01-28 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int distinctIntegers(int n) { + return n == 1 ? 1 : n - 1; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/CMakeLists.txt b/2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/CMakeLists.txt new file mode 100644 index 00000000..ba55a941 --- /dev/null +++ b/2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2550) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2550 main.cpp) diff --git a/2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/main.cpp b/2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/main.cpp new file mode 100644 index 00000000..f67abb2f --- /dev/null +++ b/2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/count-collisions-of-monkeys-on-a-polygon/description/ +/// Author : liuyubobobo +/// Time : 2023-01-28 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int monkeyMove(int n) { + return (quick_pow(2, n) - 2 + MOD) % MOD; + } + +private: + long long quick_pow(long long a, long long k) { + long long res = 1ll; + while (k) { + if (k & 1) res = res * a % MOD; + a = a * a % MOD; + k >>= 1; + } + return res % MOD; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/CMakeLists.txt b/2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/CMakeLists.txt new file mode 100644 index 00000000..0a4259ea --- /dev/null +++ b/2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2551) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2551 main.cpp) diff --git a/2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/main.cpp b/2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/main.cpp new file mode 100644 index 00000000..5b315e00 --- /dev/null +++ b/2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/put-marbles-in-bags/description/ +/// Author : liuyubobobo +/// Time : 2023-01-29 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long putMarbles(vector& weights, int k) { + + int n = weights.size(); + + vector v; + for(int i = 0; i + 1 < n; i ++) v.push_back(weights[i] + weights[i + 1]); + sort(v.begin(), v.end()); + + long long maxv = 0, minv = 0; + for(int i = (int)v.size() - 1, j = 0; j < k - 1; j ++, i --) maxv += v[i]; + for(int i = 0, j = 0; j < k - 1; j ++, i ++) minv += v[i]; + + return maxv - minv; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 1238bb50..27bbdff0 100644 --- a/readme.md +++ b/readme.md @@ -2431,6 +2431,9 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [无] | [C++](2001-2500/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | | | 2547 | [Minimum Cost to Split an Array](https://leetcode.com/problems/minimum-cost-to-split-an-array/) | [无] | [C++](2001-2500/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/) | | | | 2548 | [Maximum Price to Fill a Bag](https://leetcode.com/problems/maximum-price-to-fill-a-bag/) | [无] | [C++](2001-2500/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/) | | | +| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board/) | [无] | [C++](2001-2500/2549-Count-Distinct-Numbers-on-Board/cpp-2549/) | | | +| 2550 | [Count Collisions of Monkeys on a Polygon](https://leetcode.com/problems/count-collisions-of-monkeys-on-a-polygon/) | [无] | [C++](2001-2500/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/) | | | +| 2551 | [Put Marbles in Bags](https://leetcode.com/problems/put-marbles-in-bags/) | [无] | [C++](2001-2500/2551-Put-Marbles-in-Bags/cpp-2551/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From b83e1d8fca5037c00185ccb7c1bd4f2e3ca7e71c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 5 Feb 2023 11:41:20 -0800 Subject: [PATCH 291/390] 2553-2556 solved. --- .../cpp-2553/CMakeLists.txt | 6 ++ .../cpp-2553/main.cpp | 40 ++++++++ .../cpp-2554/CMakeLists.txt | 6 ++ .../cpp-2554/main.cpp | 41 ++++++++ .../cpp-2555/CMakeLists.txt | 6 ++ .../cpp-2555/main.cpp | 39 ++++++++ .../cpp-2556/CMakeLists.txt | 6 ++ .../cpp-2556/main.cpp | 93 +++++++++++++++++++ readme.md | 5 + 9 files changed, 242 insertions(+) create mode 100644 2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/CMakeLists.txt create mode 100644 2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/main.cpp create mode 100644 2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/CMakeLists.txt create mode 100644 2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/main.cpp create mode 100644 2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/CMakeLists.txt create mode 100644 2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/main.cpp create mode 100644 2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/CMakeLists.txt create mode 100644 2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/main.cpp diff --git a/2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/CMakeLists.txt b/2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/CMakeLists.txt new file mode 100644 index 00000000..25110586 --- /dev/null +++ b/2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2553) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2553 main.cpp) diff --git a/2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/main.cpp b/2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/main.cpp new file mode 100644 index 00000000..7a6acf3c --- /dev/null +++ b/2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/separate-the-digits-in-an-array/ +/// Author : liuyubobobo +/// Time : 2023-02-04 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlog(MAX_NUM)) +/// Space Complexity: O(1) +class Solution { +public: + vector separateDigits(vector& nums) { + + vector res; + for(int num: nums){ + vector digits = get_digits(num); + for(int e: digits) res.push_back(e); + } + return res; + } + +private: + vector get_digits(int x){ + vector res; + while(x) res.push_back(x % 10), x /= 10; + if(res.empty()) res.push_back(0); + reverse(res.begin(), res.end()); + return res; + } +}; + + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/CMakeLists.txt b/2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/CMakeLists.txt new file mode 100644 index 00000000..932c1463 --- /dev/null +++ b/2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2554) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2554 main.cpp) diff --git a/2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/main.cpp b/2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/main.cpp new file mode 100644 index 00000000..b16c0b36 --- /dev/null +++ b/2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/description/ +/// Author : liuyubobobo +/// Time : 2023-02-04 + +#include +#include +#include + +using namespace std; + + +/// Two Pointers +/// Time Complexity: O(|banned| * log|banned| + n + |banned|) +/// Space Complexity: O(1) +class Solution { +public: + int maxCount(vector& banned, int n, int maxSum) { + + sort(banned.begin(), banned.end()); + + int banned_index = 0, res = 0, sum = 0; + for(int i = 1; i <= n; i ++){ + if(banned_index < banned.size() && i == banned[banned_index]){ + while(banned_index < banned.size() && i == banned[banned_index]) + banned_index ++; + } + else if(sum + i > maxSum) break; + else{ + res ++; + sum += i; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/CMakeLists.txt b/2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/CMakeLists.txt new file mode 100644 index 00000000..6fbe7afc --- /dev/null +++ b/2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2555) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2555 main.cpp) diff --git a/2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/main.cpp b/2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/main.cpp new file mode 100644 index 00000000..f9e8c47a --- /dev/null +++ b/2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/maximize-win-from-two-segments/description/ +/// Author : liuyubobobo +/// Time : 2023-02-04 + +#include +#include +#include + +using namespace std; + + +/// DP + Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maximizeWin(vector& pos, int k) { + + int n = pos.size(); + + vector dp(n + 1, 0); + int res = 0; + for(int i = n - 1; i >= 0; i --){ + auto iter = upper_bound(pos.begin() + i, pos.end(), pos[i] + k); + int r = (iter - pos.begin()); + int tres = r - i; + res = max(res, tres + dp[r]); + dp[i] = max(tres, dp[i + 1]); + } + return res; + } +}; + + +int main() { + + + return 0; +} diff --git a/2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/CMakeLists.txt b/2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/CMakeLists.txt new file mode 100644 index 00000000..fbad678b --- /dev/null +++ b/2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2556) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2556 main.cpp) diff --git a/2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/main.cpp b/2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/main.cpp new file mode 100644 index 00000000..fad86949 --- /dev/null +++ b/2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/main.cpp @@ -0,0 +1,93 @@ +/// Source : https://leetcode.com/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/description/ +/// Author : liuyubobobo +/// Time : 2023-02-05 + +#include +#include +#include + +using namespace std; + + +/// Tarjan critical points +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class TarjanCriticalPoint{ + +private: + int n; + vector> g; + set critical_points; + +public: + TarjanCriticalPoint(int n): n(n), g(n){} + + void add_edge(int u, int v){ + g[u].insert(v); + g[v].insert(u); + } + + vector get_critical_points(){ + return vector(critical_points.begin(), critical_points.end()); + } + + bool solve(){ + + critical_points.clear(); + + vector ids(n, -1), low(n, -1); + int id = 0; + return dfs(0, -1, id, ids, low); + } + + bool dfs(int u, int p, int& id, vector& ids, vector& low){ + + bool ret = (u == n - 1); + ids[u] = low[u] = id ++; + + int children = 0; + for(int v: g[u]){ + if(v == p) continue; + if(ids[v] == -1){ + if(dfs(v, u, id, ids, low)) ret = true; + low[u] = min(low[u], low[v]); + children ++; + + if(low[v] >= ids[u] && p != -1) + critical_points.insert(u); + } + else + low[u] = min(low[u], ids[v]); + } + + if(p == -1 && children > 1) + critical_points.insert(u); + return ret; + } +}; + +class Solution { +public: + bool isPossibleToCutPath(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + TarjanCriticalPoint cp(R * C); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(!grid[i][j]) continue; + int u = i * C + j; + + if(j + 1 < C && grid[i][j + 1]) cp.add_edge(u, i * C + j + 1); + if(i + 1 < R && grid[i + 1][j]) cp.add_edge(u, (i + 1) * C + j); + } + + return !cp.solve() || !cp.get_critical_points().empty(); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 27bbdff0..8bc17c06 100644 --- a/readme.md +++ b/readme.md @@ -2435,6 +2435,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2550 | [Count Collisions of Monkeys on a Polygon](https://leetcode.com/problems/count-collisions-of-monkeys-on-a-polygon/) | [无] | [C++](2001-2500/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/) | | | | 2551 | [Put Marbles in Bags](https://leetcode.com/problems/put-marbles-in-bags/) | [无] | [C++](2001-2500/2551-Put-Marbles-in-Bags/cpp-2551/) | | | | | | | | | | +| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array/) | [无] | [C++](2001-2500/2553-Separate-the-Digits-in-an-Array/cpp-2553/) | | | +| 2554 | [Maximum Number of Integers to Choose From a Range I](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/) | [无] | [C++](2001-2500/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/) | | | +| 2555 | [Maximize Win From Two Segments](https://leetcode.com/problems/maximize-win-from-two-segments/) | [无] | [C++](2001-2500/2555-Maximize-Win-From-Two-Segments/cpp-2555/) | | | +| 2556 | [Disconnect Path in a Binary Matrix by at Most One Flip](https://leetcode.com/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/) | [无] | [C++](2001-2500/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From fdd05626020ed8106ba5158afdf73e80a2b3e926 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 5 Feb 2023 14:24:18 -0800 Subject: [PATCH 292/390] 2558-2561 solved. --- .../cpp-2558/CMakeLists.txt | 6 ++ .../cpp-2558/main.cpp | 40 ++++++++ .../cpp-2559/CMakeLists.txt | 6 ++ .../cpp-2559/main.cpp | 45 +++++++++ .../cpp-2560/CMakeLists.txt | 6 ++ .../2560-House-Robber-IV/cpp-2560/main.cpp | 44 +++++++++ .../cpp-2561/CMakeLists.txt | 6 ++ .../2561-Rearranging-Fruits/cpp-2561/main.cpp | 94 +++++++++++++++++++ readme.md | 5 + 9 files changed, 252 insertions(+) create mode 100644 2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/CMakeLists.txt create mode 100644 2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/main.cpp create mode 100644 2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/CMakeLists.txt create mode 100644 2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/main.cpp create mode 100644 2501-3000/2560-House-Robber-IV/cpp-2560/CMakeLists.txt create mode 100644 2501-3000/2560-House-Robber-IV/cpp-2560/main.cpp create mode 100644 2501-3000/2561-Rearranging-Fruits/cpp-2561/CMakeLists.txt create mode 100644 2501-3000/2561-Rearranging-Fruits/cpp-2561/main.cpp diff --git a/2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/CMakeLists.txt b/2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/CMakeLists.txt new file mode 100644 index 00000000..f864e0a0 --- /dev/null +++ b/2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2558) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2558 main.cpp) diff --git a/2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/main.cpp b/2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/main.cpp new file mode 100644 index 00000000..b1c9f9f3 --- /dev/null +++ b/2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/take-gifts-from-the-richest-pile/description/ +/// Author : liuyubobobo +/// Time : 2023-02-05 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(klogn + nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long pickGifts(vector& gifts, int k) { + + priority_queue pq; + for(int e: gifts) pq.push(e); + for(int i = 0; i < k; i ++){ + int e = pq.top(); pq.pop(); + int x = sqrt(e); + pq.push(x); + } + + long long res = 0; + while(!pq.empty()){ + int e = pq.top(); pq.pop(); + res += e; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/CMakeLists.txt b/2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/CMakeLists.txt new file mode 100644 index 00000000..fe7a7609 --- /dev/null +++ b/2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2559) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2559 main.cpp) diff --git a/2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/main.cpp b/2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/main.cpp new file mode 100644 index 00000000..8ce01190 --- /dev/null +++ b/2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/count-vowel-strings-in-ranges/description/ +/// Author : liuyubobobo +/// Time : 2023-02-05 + +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n + q) +/// Space Complexity: O(n) +class Solution { +public: + vector vowelStrings(vector& words, vector>& queries) { + + int n = words.size(); + vector v(n, 0); + for(int i = 0; i < n; i ++) + v[i] = is_vowel(words[i][0]) && is_vowel(words[i].back()); + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) + presum[i + 1] = presum[i] + v[i]; + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i ++){ + int l = queries[i][0], r = queries[i][1]; + res[i] = presum[r + 1] - presum[l]; + } + return res; + } + +private: + bool is_vowel(char c){ + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2560-House-Robber-IV/cpp-2560/CMakeLists.txt b/2501-3000/2560-House-Robber-IV/cpp-2560/CMakeLists.txt new file mode 100644 index 00000000..decd4afc --- /dev/null +++ b/2501-3000/2560-House-Robber-IV/cpp-2560/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2560) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2560 main.cpp) diff --git a/2501-3000/2560-House-Robber-IV/cpp-2560/main.cpp b/2501-3000/2560-House-Robber-IV/cpp-2560/main.cpp new file mode 100644 index 00000000..accee50f --- /dev/null +++ b/2501-3000/2560-House-Robber-IV/cpp-2560/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/house-robber-iv/description/ +/// Author : liuyubobobo +/// Time : 2023-02-05 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlog(max_num)) +/// Space Complexity: O(1) +class Solution { +public: + int minCapability(vector& nums, int k) { + + int n = nums.size(); + int l = 0, r = *max_element(nums.begin(), nums.end()); + while(l < r){ + int mid = (l + r) / 2; + if(ok(n, nums, mid, k)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(int n, const vector& nums, int v, int k){ + + int cnt = 0; + for(int i = 0; i < n; i ++) + if(nums[i] <= v) cnt ++, i ++; + return cnt >= k; + } +}; + + +int main() { + + + return 0; +} diff --git a/2501-3000/2561-Rearranging-Fruits/cpp-2561/CMakeLists.txt b/2501-3000/2561-Rearranging-Fruits/cpp-2561/CMakeLists.txt new file mode 100644 index 00000000..74465431 --- /dev/null +++ b/2501-3000/2561-Rearranging-Fruits/cpp-2561/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2561) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2561 main.cpp) diff --git a/2501-3000/2561-Rearranging-Fruits/cpp-2561/main.cpp b/2501-3000/2561-Rearranging-Fruits/cpp-2561/main.cpp new file mode 100644 index 00000000..429c95e3 --- /dev/null +++ b/2501-3000/2561-Rearranging-Fruits/cpp-2561/main.cpp @@ -0,0 +1,94 @@ +/// Source : https://leetcode.com/problems/rearranging-fruits/description/ +/// Author : liuyubobobo +/// Time : 2023-02-05 + +#include +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n1logn1 + n2logn2) +/// Space Complexity: O(n1 + n2) +class Solution { +public: + long long minCost(vector& basket1, vector& basket2) { + + int n = basket1.size(); + sort(basket1.begin(), basket1.end()); + sort(basket2.begin(), basket2.end()); + + priority_queue, greater> pq; + for(int e: basket1) pq.push(e); + for(int e: basket2) pq.push(e); + + vector target; + while(!pq.empty()){ + int a = pq.top(); pq.pop(); + int b = pq.top(); pq.pop(); + if(a != b) return -1; + target.push_back(a); + } + + deque change1 = get_change(n, basket1, target); + deque change2 = get_change(n, basket2, target); + + if(change1.size() != change2.size()) return -1; + int len = change1.size(); + if(len == 0) return 0; + + long long res = 0, min_cost = min(basket1[0], basket2[0]); + while(!change1.empty()){ + if(min_cost * 2ll < min(change1.front(), change2.front())){ + res += min_cost * 2ll; + change1.pop_back(), change2.pop_back(); + } + else if(change1.front() <= change2.front()){ + res += change1.front(); + change1.pop_front(), change2.pop_back(); + } + else{ + res += change2.front(); + change2.pop_front(), change1.pop_back(); + } + } + return res; + } + +private: + deque get_change(int n, const vector& a, const vector& b){ + + deque res; + for(int i = 0, j = 0; i < n;){ + if(j == n) res.push_back(a[i ++]); + else if(a[i] < b[j]) res.push_back(a[i ++]); + else if(a[i] == b[j]) i ++, j ++; + else if(a[i] > b[j]) j ++; + } + return res; + } +}; + + +int main() { + + vector basket1_1 = {4, 4, 4, 4, 3}; + vector basket1_2 = {5, 5, 5, 5, 3}; + cout << Solution().minCost(basket1_1, basket1_2) << '\n'; + // 8 + + vector basket2_1 = {84,80,43,8,80,88,43,14,100,88}; + vector basket2_2 = {32,32,42,68,68,100,42,84,14,8}; + cout << Solution().minCost(basket2_1, basket2_2) << '\n'; + // 48 + + vector basket3_1 = {3350,1104,2004,1577,1365,2088,2249,1948,2621,750,31,2004,1749,3365,3350,3843,3365,1656,3168,3106,2820,3557,1095,2446,573,2464,2172,1326,2712,467,1104,1446,1577,53,2492,2638,1200,2997,3454,2492,1926,1452,2712,446,2997,2820,750,2529,3847,656,272,3873,530,1749,1743,251,3847,31,251,515,2858,126,2491}; + vector basket3_2 = {530,1920,2529,2317,1969,2317,1095,2249,2858,2636,3772,53,3106,2638,1267,1926,2882,515,3772,1969,3454,2446,656,2621,1365,1743,3557,1656,3447,446,1098,1446,467,2636,1088,1098,2882,1088,1326,644,3873,3843,3926,1920,2464,2088,205,1200,1267,272,925,925,2172,2491,3168,644,1452,573,1948,3926,205,126,3447}; + cout << Solution().minCost(basket3_1, basket3_2) << '\n'; + // 837 + + return 0; +} diff --git a/readme.md b/readme.md index 8bc17c06..0d27aa74 100644 --- a/readme.md +++ b/readme.md @@ -2440,6 +2440,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2555 | [Maximize Win From Two Segments](https://leetcode.com/problems/maximize-win-from-two-segments/) | [无] | [C++](2001-2500/2555-Maximize-Win-From-Two-Segments/cpp-2555/) | | | | 2556 | [Disconnect Path in a Binary Matrix by at Most One Flip](https://leetcode.com/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/) | [无] | [C++](2001-2500/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/) | | | | | | | | | | +| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | [无] | [C++](2001-2500/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/) | | | +| 2559 | [Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/) | [无] | [C++](2001-2500/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/) | | | +| 2560 | [House Robber IV](https://leetcode.com/problems/house-robber-iv/) | [无] | [C++](2001-2500/2560-House-Robber-IV/cpp-2560/) | | | +| 2561 | [Rearranging Fruits](https://leetcode.com/problems/rearranging-fruits/) | [无] | [C++](2001-2500/2561-Rearranging-Fruits/cpp-2561/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 1fb1df4b6376eab807c5d063abb0a4df840ecc03 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 5 Feb 2023 14:38:53 -0800 Subject: [PATCH 293/390] 2557 solved. --- .../cpp-2557/CMakeLists.txt | 6 +++ .../cpp-2557/main.cpp | 41 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/CMakeLists.txt create mode 100644 2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/main.cpp diff --git a/2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/CMakeLists.txt b/2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/CMakeLists.txt new file mode 100644 index 00000000..365f4d94 --- /dev/null +++ b/2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2557) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2557 main.cpp) diff --git a/2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/main.cpp b/2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/main.cpp new file mode 100644 index 00000000..b03cad5a --- /dev/null +++ b/2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-ii/description/ +/// Author : liuyubobobo +/// Time : 2023-02-05 + +#include +#include + +using namespace std; + + +/// Two Pointers +/// Time Complexity: O(|banned| * log|banned| + n + |banned|) +/// Space Complexity: O(1) +class Solution { +public: + int maxCount(vector& banned, int n, long long maxSum) { + + sort(banned.begin(), banned.end()); + + int banned_index = 0, res = 0; + long long sum = 0; + for(int i = 1; i <= n; i ++){ + if(banned_index < banned.size() && i == banned[banned_index]){ + while(banned_index < banned.size() && i == banned[banned_index]) + banned_index ++; + } + else if(sum + i > maxSum) break; + else{ + res ++; + sum += i; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 0d27aa74..35ae8519 100644 --- a/readme.md +++ b/readme.md @@ -2439,7 +2439,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2554 | [Maximum Number of Integers to Choose From a Range I](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/) | [无] | [C++](2001-2500/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/) | | | | 2555 | [Maximize Win From Two Segments](https://leetcode.com/problems/maximize-win-from-two-segments/) | [无] | [C++](2001-2500/2555-Maximize-Win-From-Two-Segments/cpp-2555/) | | | | 2556 | [Disconnect Path in a Binary Matrix by at Most One Flip](https://leetcode.com/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/) | [无] | [C++](2001-2500/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/) | | | -| | | | | | | +| 2557 | [Maximum Number of Integers to Choose From a Range II](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-ii/description/) | [无] | [C++](2001-2500/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/) | | | | 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | [无] | [C++](2001-2500/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/) | | | | 2559 | [Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/) | [无] | [C++](2001-2500/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/) | | | | 2560 | [House Robber IV](https://leetcode.com/problems/house-robber-iv/) | [无] | [C++](2001-2500/2560-House-Robber-IV/cpp-2560/) | | | From 7b37f513ce8cabe763137e1756b30c28efa523f6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 8 Feb 2023 13:55:48 -0800 Subject: [PATCH 294/390] 0280 solved. --- .../0280-Wiggle-Sort/cpp-0280/CMakeLists.txt | 6 ++++ 0001-0500/0280-Wiggle-Sort/cpp-0280/main.cpp | 29 +++++++++++++++++++ readme.md | 1 + 3 files changed, 36 insertions(+) create mode 100644 0001-0500/0280-Wiggle-Sort/cpp-0280/CMakeLists.txt create mode 100644 0001-0500/0280-Wiggle-Sort/cpp-0280/main.cpp diff --git a/0001-0500/0280-Wiggle-Sort/cpp-0280/CMakeLists.txt b/0001-0500/0280-Wiggle-Sort/cpp-0280/CMakeLists.txt new file mode 100644 index 00000000..e8dbab7b --- /dev/null +++ b/0001-0500/0280-Wiggle-Sort/cpp-0280/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_0280) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0280 main.cpp) diff --git a/0001-0500/0280-Wiggle-Sort/cpp-0280/main.cpp b/0001-0500/0280-Wiggle-Sort/cpp-0280/main.cpp new file mode 100644 index 00000000..3ed1b191 --- /dev/null +++ b/0001-0500/0280-Wiggle-Sort/cpp-0280/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/wiggle-sort/description/ +/// Author : liuyubobobo +/// Time : 2023-02-08 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + void wiggleSort(vector& nums) { + + sort(nums.begin(), nums.end()); + for(int i = 1; i + 1 < nums.size(); i += 2) + swap(nums[i], nums[i + 1]); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 35ae8519..b62b2f84 100644 --- a/readme.md +++ b/readme.md @@ -317,6 +317,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 277 | [Find the Celebrity](https://leetcode.com/problems/find-the-celebrity/) | [solution](https://leetcode.com/problems/find-the-celebrity/solution/)
[缺:O(n) 解法] | [C++](0001-0500/0277-Find-the-Celebrity/cpp-0277/) | | | | 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [solution](https://leetcode.com/problems/first-bad-version/solution/) | [C++](0001-0500/0278-First-Bad-Version/cpp-0278/) | | | | 279 | [Perfect Squares](https://leetcode.com/problems/perfect-squares/description/) | [无]
四平方和数学解 | [C++](0001-0500/0279-Perfect-Squares/cpp-0279/) | [Java](0001-0500/0279-Perfect-Squares/java-0279/src/) | | +| 280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/description/) | [solution](https://leetcode.com/problems/wiggle-sort/solutions/2961467/wiggle-sort/?orderBy=most_votes) | [C++](0001-0500/0280-Wiggle-Sort/cpp-0280/) | | | | | | | | | | | 282 | [Expression Add Operators](https://leetcode.com/problems/expression-add-operators/description/) | [solution](https://leetcode.com/problems/expression-add-operators/solution/) | [C++](0001-0500/0282-Expression-Add-Operators/cpp-0282/) | | | | 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/description/) | [solution](https://leetcode.com/problems/move-zeroes/solution/) | [C++](0001-0500/0283-Move-Zeroes/cpp-0283/) | [Java](0001-0500/0283-Move-Zeroes/java-0283/src/) | | From fbbbc911fa5657d0f72213b914f32761cf06c27b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 12 Feb 2023 10:35:07 -0800 Subject: [PATCH 295/390] 2562-2565 solved. --- .../cpp-2562/CMakeLists.txt | 6 ++ .../cpp-2562/main.cpp | 36 ++++++++++++ .../cpp-2563/CMakeLists.txt | 6 ++ .../cpp-2563/main.cpp | 34 +++++++++++ .../cpp-2564/CMakeLists.txt | 6 ++ .../cpp-2564/main.cpp | 56 +++++++++++++++++++ .../cpp-2565/CMakeLists.txt | 6 ++ .../cpp-2565/main.cpp | 41 ++++++++++++++ readme.md | 4 ++ 9 files changed, 195 insertions(+) create mode 100644 2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/CMakeLists.txt create mode 100644 2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/main.cpp create mode 100644 2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/CMakeLists.txt create mode 100644 2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/main.cpp create mode 100644 2501-3000/2564-Substring-XOR-Queries/cpp-2564/CMakeLists.txt create mode 100644 2501-3000/2564-Substring-XOR-Queries/cpp-2564/main.cpp create mode 100644 2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/CMakeLists.txt create mode 100644 2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/main.cpp diff --git a/2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/CMakeLists.txt b/2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/CMakeLists.txt new file mode 100644 index 00000000..13f9654c --- /dev/null +++ b/2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2562) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2562 main.cpp) diff --git a/2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/main.cpp b/2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/main.cpp new file mode 100644 index 00000000..17839784 --- /dev/null +++ b/2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/find-the-array-concatenation-value/description/ +/// Author : liuyubobobo +/// Time : 2023-02-11 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlog(MAX_NUMS)) +/// Space Complexity: O(log(MAX_NUMS)) +class Solution { +public: + long long findTheArrayConcVal(vector& nums) { + + int n = nums.size(); + long long res = 0; + for(int i = 0, j = n - 1; i <= j; i ++, j --){ + if(i == j) res += nums[i]; + else{ + long long a = nums[i], b = nums[j]; + string a_str = to_string(a), b_str = to_string(b); + res += atoll((a_str + b_str).c_str()); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/CMakeLists.txt b/2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/CMakeLists.txt new file mode 100644 index 00000000..c49b6754 --- /dev/null +++ b/2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2563) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2563 main.cpp) diff --git a/2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/main.cpp b/2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/main.cpp new file mode 100644 index 00000000..6d488e01 --- /dev/null +++ b/2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-fair-pairs/description/ +/// Author : liuyubobobo +/// Time : 2023-02-11 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + long long countFairPairs(vector& nums, int lower, int upper) { + + sort(nums.begin(), nums.end()); + long long res = 0; + for(int i = 0; i < nums.size(); i ++){ + auto iter1 = lower_bound(nums.begin() + (i + 1), nums.end(), lower - nums[i]); + auto iter2 = upper_bound(nums.begin() + (i + 1), nums.end(), upper - nums[i]); + res += iter2 - iter1; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2564-Substring-XOR-Queries/cpp-2564/CMakeLists.txt b/2501-3000/2564-Substring-XOR-Queries/cpp-2564/CMakeLists.txt new file mode 100644 index 00000000..f388d47a --- /dev/null +++ b/2501-3000/2564-Substring-XOR-Queries/cpp-2564/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2564) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2564 main.cpp) diff --git a/2501-3000/2564-Substring-XOR-Queries/cpp-2564/main.cpp b/2501-3000/2564-Substring-XOR-Queries/cpp-2564/main.cpp new file mode 100644 index 00000000..b1952538 --- /dev/null +++ b/2501-3000/2564-Substring-XOR-Queries/cpp-2564/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/substring-xor-queries/description/ +/// Author : liuyubobobo +/// Time : 2023-02-11 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector> substringXorQueries(string s, vector>& queries) { + + int n = s.size(), q = queries.size(); + vector> res(q, {-1, -1}); + map> res2index; + for(int i = 0; i < q; i ++){ + int first = queries[i][0], second = queries[i][1]; + int val = second ^ first; + + res2index[val].push_back(i); + } + + for(int len = 1; len <= min(31, n); len ++){ + long long cur = 0; + for(int i = 0; i < len - 1; i ++) cur = cur * 2 + (s[i] - '0'); + for(int i = len - 1; i < n; i ++){ + cur = cur * 2 + (s[i] - '0'); + auto iter = res2index.find(cur); + if(iter != res2index.end()){ + for(int index: iter->second) res[index] = {i - (len - 1), i}; + res2index.erase(iter); + } + + if(s[i - (len - 1)] == '1') cur -= (1ll << (len - 1)); + } + } + return res; + } +}; + +int main() { + + string s1 = "101101"; + vector> queries1 = {{0, 5}, {1, 2}}; + Solution().substringXorQueries(s1, queries1); + + return 0; +} diff --git a/2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/CMakeLists.txt b/2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/CMakeLists.txt new file mode 100644 index 00000000..d87fa24d --- /dev/null +++ b/2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2565) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2565 main.cpp) diff --git a/2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/main.cpp b/2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/main.cpp new file mode 100644 index 00000000..22dc670a --- /dev/null +++ b/2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/subsequence-with-the-minimum-score/description/ +/// Author : liuyubobobo +/// Time : 2023-02-12 + +#include +#include + +using namespace std; + + +/// Pre and Suf +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumScore(string s, string t) { + + int sn = s.size(), tn = t.size(); + + vector pre(sn, 0), suf(sn, 0); + for(int i = 0, j = 0; i < sn; i ++){ + if(j < tn && s[i] == t[j]) pre[i] = ++j; + else if(i > 0) pre[i] = pre[i - 1]; + } + for(int i = sn - 1, j = tn - 1; i >= 0; i --){ + if(j >= 0 && s[i] == t[j]) suf[i] = tn - (j --); + else if(i + 1 < sn) suf[i] = suf[i + 1]; + } + + int res = min(tn - suf[0], tn - pre.back()); + for(int i = 0; i + 1 < sn; i ++) + res = min(res, tn - pre[i] - suf[i + 1]); + return max(res, 0); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index b62b2f84..70928066 100644 --- a/readme.md +++ b/readme.md @@ -2445,6 +2445,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2559 | [Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/) | [无] | [C++](2001-2500/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/) | | | | 2560 | [House Robber IV](https://leetcode.com/problems/house-robber-iv/) | [无] | [C++](2001-2500/2560-House-Robber-IV/cpp-2560/) | | | | 2561 | [Rearranging Fruits](https://leetcode.com/problems/rearranging-fruits/) | [无] | [C++](2001-2500/2561-Rearranging-Fruits/cpp-2561/) | | | +| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value/) | [无] | [C++](2001-2500/2562-Find-the-Array-Concatenation-Value/cpp-2562/) | | | +| 2563 | [Count the Number of Fair Pairs](https://leetcode.com/problems/count-the-number-of-fair-pairs/) | [无] | [C++](2001-2500/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/) | | | +| 2564 | [Substring XOR Queries](https://leetcode.com/problems/substring-xor-queries/) | [无] | [C++](2001-2500/2564-Substring-XOR-Queries/cpp-2564/) | | | +| 2565 | [Subsequence With the Minimum Score](https://leetcode.com/problems/subsequence-with-the-minimum-score/) | [无] | [C++](2001-2500/2565-Subsequence-With-the-Minimum-Score/cpp-2565/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 1f9e3b3e3b82e15f906c8f3ccc0539f520c42fb2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 13 Feb 2023 11:26:49 -0800 Subject: [PATCH 296/390] 1523 solved. --- .../cpp-1523/CMakeLists.txt | 6 +++++ .../cpp-1523/main.cpp | 26 +++++++++++++++++++ readme.md | 1 + 3 files changed, 33 insertions(+) create mode 100644 1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/CMakeLists.txt create mode 100644 1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/main.cpp diff --git a/1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/CMakeLists.txt b/1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/CMakeLists.txt new file mode 100644 index 00000000..3a994f27 --- /dev/null +++ b/1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1523) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1523 main.cpp) diff --git a/1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/main.cpp b/1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/main.cpp new file mode 100644 index 00000000..d1b3d2b3 --- /dev/null +++ b/1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/main.cpp @@ -0,0 +1,26 @@ +/// Source : https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/description/ +/// Author : liuyubobobo +/// Time : 2023-02-13 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int countOdds(int low, int high) { + + int len = high - low + 1; + return low % 2 == 1 ? (len + 1) / 2 : len / 2; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 70928066..5967125d 100644 --- a/readme.md +++ b/readme.md @@ -1443,6 +1443,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1519 | [Number of Nodes in the Sub-Tree With the Same Label](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/description/) | [solution](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/solutions/2864718/number-of-nodes-in-the-sub-tree-with-the-same-label/?orderBy=most_votes) | [C++](1501-2000/1519-Number-of-Nodes-in-the-Sub-Tree-With-the-Same-Label/cpp-1519/) | | | | | | | | | | | 1522 | [Diameter of N-Ary Tree](https://leetcode.com/problems/diameter-of-n-ary-tree/) | [无] | [C++](1501-2000/1522-Diameter-of-N-Ary-Tree/cpp-1522/) | | | +| 1523 | [Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/description/) | [无] | [C++](1501-2000/1523-Count-Odd-Numbers-in-an-Interval-Range/cpp-1523/) | | | | | | | | | | | 1527 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | From 8267d7194d4e16e6276258fc5b588cb5ef7566da Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 14 Feb 2023 14:11:41 -0800 Subject: [PATCH 297/390] 1250 solved. --- .../cpp-1250/CMakeLists.txt | 6 ++++ .../cpp-1250/main.cpp | 36 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/CMakeLists.txt create mode 100644 1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/main.cpp diff --git a/1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/CMakeLists.txt b/1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/CMakeLists.txt new file mode 100644 index 00000000..3d294d36 --- /dev/null +++ b/1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1250) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1250 main.cpp) diff --git a/1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/main.cpp b/1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/main.cpp new file mode 100644 index 00000000..c11ec797 --- /dev/null +++ b/1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/check-if-it-is-a-good-array/description/ +/// Author : liuyubobobo +/// Time : 2023-02-14 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(nlog(MAX_NUM)) +/// Space Complexity: O(log(MAX_NUM)) +class Solution { +public: + bool isGoodArray(vector& nums) { + + int n = nums.size(), g = nums[0]; + for(int i = 1; i < n && g > 1; i ++) + g = gcd(g, nums[i]); + return g == 1; + } + +private: + int gcd(int a, int b){ + if(a > b) swap(a, b); + if (a == 0) return b; + return gcd(b % a, a); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 5967125d..bc8d946e 100644 --- a/readme.md +++ b/readme.md @@ -1222,7 +1222,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1248 | [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [无] | [C++](1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/) | [Java](1001-1500/1248-Count-Number-of-Nice-Subarrays/java-1248/) | | | 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [solution](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/solution/) | [C++](1001-1500/1249-Minimum-Remove-to-Make-Valid-Parentheses/cpp-1249/) | | | -| | | | | | | +| 1250 | [Check If It Is a Good Array](https://leetcode.com/problems/check-if-it-is-a-good-array/description/) | [无] | [C++](1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/) | | | | 1251 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [无] | [C++](1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/) | | | | | | | | | | From a3fed79c0c9fe4202d6614bab693b69bba6c5cc3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 15 Feb 2023 13:06:49 -0800 Subject: [PATCH 298/390] 0305 solved. --- .../cpp-0305/CMakeLists.txt | 6 ++ .../cpp-0305/main.cpp | 85 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0305-Number-of-Islands-II/cpp-0305/CMakeLists.txt create mode 100644 0001-0500/0305-Number-of-Islands-II/cpp-0305/main.cpp diff --git a/0001-0500/0305-Number-of-Islands-II/cpp-0305/CMakeLists.txt b/0001-0500/0305-Number-of-Islands-II/cpp-0305/CMakeLists.txt new file mode 100644 index 00000000..76fd5e50 --- /dev/null +++ b/0001-0500/0305-Number-of-Islands-II/cpp-0305/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_0305) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0305 main.cpp) diff --git a/0001-0500/0305-Number-of-Islands-II/cpp-0305/main.cpp b/0001-0500/0305-Number-of-Islands-II/cpp-0305/main.cpp new file mode 100644 index 00000000..49cbc366 --- /dev/null +++ b/0001-0500/0305-Number-of-Islands-II/cpp-0305/main.cpp @@ -0,0 +1,85 @@ +/// Source : https://leetcode.com/problems/number-of-islands-ii/description/ +/// Author : liuyubobobo +/// Time : 2023-02-15 + +#include +#include + +using namespace std; + + +/// Using UF +/// Time Complexity: O(|positions|) +/// Space Complexity: O(R * C) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { + +private: + const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + +public: + vector numIslands2(int R, int C, vector>& positions) { + + vector> g(R, vector(C, 0)); + int sz = 0; + + vector res; + + UF uf(R * C); + for(const vector& pos: positions){ + int x = pos[0], y = pos[1]; + if(g[x][y] != 1){ + g[x][y] = 1; sz ++; + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(!in_area(R, C, nx, ny) || g[nx][ny] == 0 || uf.is_connected(x * C + y, nx * C + ny)) + continue; + uf.union_elements(x * C + y, nx * C + ny); + sz --; + } + } + res.push_back(sz); + } + return res; + } + + bool in_area(int R, int C, int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index bc8d946e..a73170c4 100644 --- a/readme.md +++ b/readme.md @@ -341,7 +341,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/description/) | | [C++](0001-0500/0303/Range-Sum-Query-Immutable/cpp-0303/) | | | | 304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | [solution](https://leetcode.com/problems/range-sum-query-2d-immutable/solution/) | [C++](0001-0500/0304-Range-Sum-Query-2D-Immutable/cpp-0304/) | | | -| | | | | | | +| 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/description/) | [solution](https://leetcode.com/problems/number-of-islands-ii/solutions/3033683/number-of-islands-ii/?orderBy=most_votes) | [C++](0001-0500/0305-Number-of-Islands-II/cpp-0305/) | | | | 306 | [Additive Number](https://leetcode.com/problems/additive-number/) | [无] | [C++](0001-0500/0306-Additive-Number/cpp-0306/) | | | | 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/description/) | [缺:BIT] | [C++](0001-0500/0307-Range-Sum-Query-Mutable/cpp-0307/) | | | | 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/description/) | | [C++](0001-0500/0308-Range-Sum-Query-2D-Mutable/cpp-0308/) | | | From 784563683969ee9d74eaa6538c77c1348eb8966b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 20 Feb 2023 09:57:11 -0800 Subject: [PATCH 299/390] 1326 solved. --- .../cpp-1326/CMakeLists.txt | 6 ++++ .../cpp-1326/main.cpp | 35 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/CMakeLists.txt create mode 100644 1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/main.cpp diff --git a/1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/CMakeLists.txt b/1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/CMakeLists.txt new file mode 100644 index 00000000..eef8b694 --- /dev/null +++ b/1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1326) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1326 main.cpp) diff --git a/1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/main.cpp b/1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/main.cpp new file mode 100644 index 00000000..a8c09eaa --- /dev/null +++ b/1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/description/ +/// Author : liuyubobobo +/// Time : 2023-02-20 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * max_range) +/// Space Complexity: O(n) +class Solution { +public: + int minTaps(int n, vector& ranges) { + + vector dp(n, INT_MAX / 2); + for(int i = 0; i <= n; i ++){ + int l = max(0, i - ranges[i]), r = min(n, i + ranges[i]); + for(int j = l; j < r; j ++) + dp[j] = min(dp[j], (l - 1 >= 0 ? dp[l - 1] : 0) + 1); + } + return dp.back() == INT_MAX / 2 ? -1 : dp.back(); + } +}; + +int main() { + + vector ranges1 = {0, 0, 0, 0}; + cout << Solution().minTaps(3, ranges1) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index a73170c4..ff4e2959 100644 --- a/readme.md +++ b/readme.md @@ -1270,6 +1270,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1323 | [Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/description/) | [无] | [C++](1001-1500/1323-Maximum-69-Number/cpp-1323/) | | | | | | | | | | +| 1326 | [Minimum Number of Taps to Open to Water a Garden](https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/description/) | [无] | [C++](1001-1500/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/cpp-1326/) | | | +| | | | | | | | 1328 | [Break a Palindrome](https://leetcode.com/problems/break-a-palindrome/) | [solution](https://leetcode.com/problems/break-a-palindrome/solution/) | [C++](1001-1500/1328-Break-a-Palindrome/cpp-1328/) | | | | 1329 | [Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [solution](https://leetcode.com/problems/sort-the-matrix-diagonally/solution/) | [C++](1001-1500/1329-Sort-the-Matrix-Diagonally/cpp-1329/) | | | | | | | | | | From d0804457352e27572fb2f7c264b797c63cbf9db1 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 20 Feb 2023 23:15:14 -0800 Subject: [PATCH 300/390] 2566-2569 solved. --- .../cpp-2566/CMakeLists.txt | 6 + .../cpp-2566/main.cpp | 43 +++++ .../cpp-2567/CMakeLists.txt | 6 + .../cpp-2567/main.cpp | 35 ++++ .../cpp-2568/CMakeLists.txt | 6 + .../cpp-2568/main.cpp | 33 ++++ .../cpp-2569/CMakeLists.txt | 6 + .../cpp-2569/main.cpp | 173 ++++++++++++++++++ readme.md | 4 + 9 files changed, 312 insertions(+) create mode 100644 2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/CMakeLists.txt create mode 100644 2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/main.cpp create mode 100644 2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/CMakeLists.txt create mode 100644 2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/main.cpp create mode 100644 2501-3000/2568-Minimum-Impossible-OR/cpp-2568/CMakeLists.txt create mode 100644 2501-3000/2568-Minimum-Impossible-OR/cpp-2568/main.cpp create mode 100644 2501-3000/2569-Handling-Sum-Queries-After-Update/cpp-2569/CMakeLists.txt create mode 100644 2501-3000/2569-Handling-Sum-Queries-After-Update/cpp-2569/main.cpp diff --git a/2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/CMakeLists.txt b/2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/CMakeLists.txt new file mode 100644 index 00000000..957e935e --- /dev/null +++ b/2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2566) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2566 main.cpp) diff --git a/2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/main.cpp b/2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/main.cpp new file mode 100644 index 00000000..7bc05ece --- /dev/null +++ b/2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/description/ +/// Author : liuyubobobo +/// Time : 2023-02-19 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(10 * 10 * log(num)) +/// Space Complexity: O(log(num)) +class Solution { +public: + int minMaxDifference(int num) { + + int maxv = INT_MIN, minv = INT_MAX; + for(int from = 0; from <= 9; from ++) + for(int to = 0; to <= 9; to ++){ + int new_num = replace(num, from, to); + maxv = max(maxv, new_num); + minv = min(minv, new_num); + } + return maxv - minv; + } + +private: + int replace(int num, int from, int to) { + + string num_str = to_string(num); + for(char& c: num_str) + if(c == (char)(from + '0')) + c = (char)(to + '0'); + return atoi(num_str.c_str()); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/CMakeLists.txt b/2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/CMakeLists.txt new file mode 100644 index 00000000..902dee05 --- /dev/null +++ b/2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2567) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2567 main.cpp) diff --git a/2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/main.cpp b/2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/main.cpp new file mode 100644 index 00000000..3cd462d7 --- /dev/null +++ b/2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/minimum-score-by-changing-two-elements/description/ +/// Author : liuyubobobo +/// Time : 2023-02-20 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int minimizeSum(vector& nums) { + + sort(nums.begin(), nums.end()); + + int n = nums.size(); + return min3(nums[n - 2] - nums[1], nums[n - 1] - nums[2], nums[n - 3] - nums[0]); + } + +private: + int min3(int a, int b, int c) { + return min(a, min(b, c)); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2568-Minimum-Impossible-OR/cpp-2568/CMakeLists.txt b/2501-3000/2568-Minimum-Impossible-OR/cpp-2568/CMakeLists.txt new file mode 100644 index 00000000..6a782e74 --- /dev/null +++ b/2501-3000/2568-Minimum-Impossible-OR/cpp-2568/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2568) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2568 main.cpp) diff --git a/2501-3000/2568-Minimum-Impossible-OR/cpp-2568/main.cpp b/2501-3000/2568-Minimum-Impossible-OR/cpp-2568/main.cpp new file mode 100644 index 00000000..15418261 --- /dev/null +++ b/2501-3000/2568-Minimum-Impossible-OR/cpp-2568/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/minimum-impossible-or/description/ +/// Author : liuyubobobo +/// Time : 2023-02-20 + +#include +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int minImpossibleOR(vector& nums) { + + sort(nums.begin(), nums.end()); + for(int i = 0;;i ++){ + auto iter = lower_bound(nums.begin(), nums.end(), 1 << i); + if(iter == nums.end() || *iter != (1 << i)) + return 1 < +#include +#include + +using namespace std; + + +/// Segment Tree +/// Time Complexity: O(nlogn + qlogn) +/// Space Complexity: O(n) +template +class SegmentTree{ + +private: + int n; + vector tree, lazy; + T (*combine)(T a, T b); + +public: + SegmentTree(const vector& data, T (*combine)(T a, T b)): + n(data.size()), tree(4 * n, 0), lazy(4 * n, 0){ + this->combine = combine; + buildSegTree(0, 0, n - 1, data); + } + + SegmentTree(int n, T (*combine)(T a, T b)): n(n), tree(4 * n, 0), lazy(4 * n, 0){ + this->combine = combine; + } + + void flip(int uL, int uR){ + assert(0 <= uL && uL < n); + assert(0 <= uR && uR < n); + assert(uL <= uR); + flip(0, 0, n-1, uL, uR); + } + + T query(int qL, int qR){ + assert(0 <= qL && qL < n); + assert(0 <= qR && qR < n); + assert(qL <= qR); + return query(0, 0, n - 1, qL, qR); + } + +private: + void buildSegTree(int treeID, int l, int r, const vector& data){ + + if(l == r){ + tree[treeID] = data[l]; + return; + } + + int mid = (l + r) / 2; + buildSegTree(treeID * 2 + 1, l, mid, data); + buildSegTree(treeID * 2 + 2, mid + 1, r, data); + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + void flip(int treeID, int treeL, int treeR, int uL, int uR){ + + if(uL > treeR || uR < treeL) return; + + if(uL <= treeL && treeR <= uR){ + + int len = treeR - treeL + 1; + tree[treeID] = len - tree[treeID]; + lazy[treeID] ++; + + return; + } + + if(lazy[treeID]) + push(treeID, treeL, treeR); + + int mid = (treeL + treeR) / 2; + flip(2 * treeID + 1, treeL, mid, uL, uR); + flip(2 * treeID + 2, mid + 1, treeR, uL, uR); + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + T query(int treeID, int treeL, int treeR, int qL, int qR){ + + if(qL <= treeL && treeR <= qR) + return tree[treeID]; + + if(lazy[treeID]) + push(treeID, treeL, treeR); + + int mid = (treeL + treeR) / 2; + if(qR <= mid) return query(2 * treeID + 1, treeL, mid, qL, qR); + if(qL >= mid + 1) return query(2 * treeID + 2, mid + 1, treeR, qL, qR); + + T resl = query(2 * treeID + 1, treeL, mid, qL, qR); + T resr = query(2 * treeID + 2, mid + 1, treeR, qL, qR); + T res = combine(resl, resr); + return res; + } + +private: + void push(int treeID, int treeL, int treeR){ + + if(treeL == treeR) return; + + T v = lazy[treeID]; + + int mid = (treeL + treeR) / 2; + int tn = treeR - treeL + 1, tnl = mid - treeL + 1, tnr = tn - tnl; + + if(v & 1) { + tree[treeID * 2 + 1] = tnl - tree[treeID * 2 + 1]; + tree[treeID * 2 + 2] = tnr - tree[treeID * 2 + 2]; + } + + lazy[treeID * 2 + 1] += v; + lazy[treeID * 2 + 2] += v; + + lazy[treeID] = 0; + } +}; + +class Solution { +public: + vector handleQuery(vector& nums1, vector& nums2, vector>& queries) { + + int n = nums1.size(); + + SegmentTree seg_tree(nums1, [](int a, int b){return a + b;}); + + long long sum = 0; + for(int e: nums2) sum += e; + + vector res; + for(const vector& query: queries){ +// for(int i = 0; i < n; i ++) cout << seg_tree.query(i, i) << " "; cout << '\n'; + int type = query[0]; + if(type == 1){ + int l = query[1], r = query[2]; + seg_tree.flip(l, r); + } + else if(type == 2){ + long long p = query[1]; + sum += p * seg_tree.query(0, n - 1); + } + else res.push_back(sum); + } + return res; + } +}; + + +void print_vec(const vector& res){ + for(long long e: res) cout << e << " "; cout << '\n'; +} + +int main() { + + vector nums1_1 = {1, 0, 1}, nums1_2 = {0, 0, 0}; + vector>queries1 = {{1, 1, 1}, {2, 1, 0}, {3, 0, 0}}; + print_vec(Solution().handleQuery(nums1_1, nums1_2, queries1)); + // 3 + + vector nums2_1 = {0, 1, 0, 0, 0, 0}, nums2_2 = {14, 4, 13, 13, 47, 18}; + vector>queries2 = {{3,0,0},{1,4,4},{1,1,4},{1,3,4},{3,0,0},{2,5,0},{1,1,3},{2,16,0},{2,10,0},{3,0,0},{3,0,0},{2,6,0}}; + print_vec(Solution().handleQuery(nums2_1, nums2_2, queries2)); + // 109,109,197,197 + + return 0; +} diff --git a/readme.md b/readme.md index ff4e2959..4c31a95b 100644 --- a/readme.md +++ b/readme.md @@ -2452,6 +2452,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2563 | [Count the Number of Fair Pairs](https://leetcode.com/problems/count-the-number-of-fair-pairs/) | [无] | [C++](2001-2500/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/) | | | | 2564 | [Substring XOR Queries](https://leetcode.com/problems/substring-xor-queries/) | [无] | [C++](2001-2500/2564-Substring-XOR-Queries/cpp-2564/) | | | | 2565 | [Subsequence With the Minimum Score](https://leetcode.com/problems/subsequence-with-the-minimum-score/) | [无] | [C++](2001-2500/2565-Subsequence-With-the-Minimum-Score/cpp-2565/) | | | +| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/) | [无] | [C++](2001-2500/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/) | | | +| 2567 | [Minimum Score by Changing Two Elements](https://leetcode.com/problems/minimum-score-by-changing-two-elements/) | [无] | [C++](2001-2500/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/) | | | +| 2568 | [Minimum Impossible OR](https://leetcode.com/problems/minimum-impossible-or/) | [无] | [C++](2001-2500/2568-Minimum-Impossible-OR/cpp-2568/) | | | +| 2569 | [Handling Sum Queries After Update](https://leetcode.com/problems/handling-sum-queries-after-update/) | [无] | [C++](2001-2500/2569-Handling-Sum-Queries-After-Update/cpp-2569/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 9153ced82f308dece4e64b3e6eb4f45a858ed990 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 22 Feb 2023 14:20:24 -0800 Subject: [PATCH 301/390] 1259 solved. --- .../cpp-1259/CMakeLists.txt | 6 +++ .../cpp-1259/main.cpp | 40 +++++++++++++++++++ readme.md | 1 + 3 files changed, 47 insertions(+) create mode 100644 1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/CMakeLists.txt create mode 100644 1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/main.cpp diff --git a/1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/CMakeLists.txt b/1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/CMakeLists.txt new file mode 100644 index 00000000..2fd7d011 --- /dev/null +++ b/1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1259) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1259 main.cpp) diff --git a/1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/main.cpp b/1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/main.cpp new file mode 100644 index 00000000..269305c1 --- /dev/null +++ b/1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/handshakes-that-dont-cross/description/ +/// Author : liuyubobobo +/// Time : 2023-02-22 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int numberOfWays(int numPeople) { + + vector dp(numPeople + 1, 0); + dp[0] = 1; + for(int i = 2; i <= numPeople; i ++){ + if(i % 2) continue; + for(int j = 1; j < i; j ++){ + int len = j - 1; + if(len & 1) continue; + dp[i] += dp[len] * dp[i - (len + 2)] % MOD; + } + dp[i] %= MOD; + } + return dp.back(); + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 4c31a95b..ccd45ee3 100644 --- a/readme.md +++ b/readme.md @@ -1226,6 +1226,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1251 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [无] | [C++](1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/) | | | | | | | | | | +| 1259 | [Handshakes That Don't Cross](https://leetcode.com/problems/handshakes-that-dont-cross/description/) | [无] | [C++](1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/) | | | | 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [solution](https://leetcode.com/problems/shift-2d-grid/solution/) | [C++](1001-1500/1260-Shift-2D-Grid/cpp-1260/) | | | | 1261 | [Find Elements in a Contaminated Binary Tree](https://leetcode.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binary-tree/) | [C++](1001-1500/1261-Find-Elements-in-a-Contaminated-Binary-Tree/cpp-1261/) | | | | | 1262 | [Greatest Sum Divisible by Three](https://leetcode.com/problems/greatest-sum-divisible-by-three/) | [无] | [C++](1001-1500/1262-Greatest-Sum-Divisible-by-Three/cpp-1262/) | | | From 2c05f91d02e39a654e581e1055f1b8f5a58f0d68 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 22 Feb 2023 15:17:28 -0800 Subject: [PATCH 302/390] 2570-2573 solved. --- .../cpp-2570/CMakeLists.txt | 6 ++ .../cpp-2570/main.cpp | 35 +++++++ .../cpp-2571/CMakeLists.txt | 6 ++ .../cpp-2571/main.cpp | 54 +++++++++++ .../cpp-2572/CMakeLists.txt | 6 ++ .../cpp-2572/main.cpp | 68 +++++++++++++ .../cpp-2573/CMakeLists.txt | 6 ++ .../cpp-2573/main.cpp | 96 +++++++++++++++++++ readme.md | 4 + 9 files changed, 281 insertions(+) create mode 100644 2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/CMakeLists.txt create mode 100644 2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/main.cpp create mode 100644 2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/CMakeLists.txt create mode 100644 2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/main.cpp create mode 100644 2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/CMakeLists.txt create mode 100644 2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/main.cpp create mode 100644 2501-3000/2573-Find-the-String-with-LCP/cpp-2573/CMakeLists.txt create mode 100644 2501-3000/2573-Find-the-String-with-LCP/cpp-2573/main.cpp diff --git a/2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/CMakeLists.txt b/2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/CMakeLists.txt new file mode 100644 index 00000000..5fcbddbf --- /dev/null +++ b/2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2570) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2570 main.cpp) diff --git a/2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/main.cpp b/2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/main.cpp new file mode 100644 index 00000000..b512af34 --- /dev/null +++ b/2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/description/ +/// Author : liuyubobobo +/// Time : 2023-02-20 + +#include +#include + +using namespace std; + + +/// Merge +/// Time Complexity: O(n1 + n2) +/// Space Complexity: O(1) +class Solution { +public: + vector> mergeArrays(vector>& nums1, vector>& nums2) { + + vector> res; + int n1 = nums1.size(), n2 = nums2.size(); + for(int i = 0, j = 0; i < n1 || j < n2;){ + if(i == n1) res.push_back(nums2[j++]); + else if(j == n2) res.push_back(nums1[i++]); + else if(nums1[i][0] < nums2[j][0]) res.push_back(nums1[i++]); + else if(nums1[i][0] > nums2[j][0]) res.push_back(nums2[j++]); + else res.push_back({nums1[i][0], nums1[i ++][1] + nums2[j ++][1]}); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/CMakeLists.txt b/2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/CMakeLists.txt new file mode 100644 index 00000000..f51708f0 --- /dev/null +++ b/2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2571) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2571 main.cpp) diff --git a/2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/main.cpp b/2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/main.cpp new file mode 100644 index 00000000..24ee4844 --- /dev/null +++ b/2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-reduce-an-integer-to-0/description/ +/// Author : liuyubobobo +/// Time : 2023-02-20 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(logn) +/// Space Complexity: O(logn) +class Solution { +public: + int minOperations(int n) { + + vector v; + while(n) v.push_back(n & 1), n >>= 1; + + vector> dp(v.size(), vector(2, 0)); + return dfs(v, 0, 0, dp); + } + +private: + int dfs(const vector& v, int index, int last, vector>& dp){ + + if(index == v.size()) return last == 1 ? 2 : 0; + + if(dp[index][last] != 0) return dp[index][last]; + + int res = INT_MAX; + if(last == 0){ + if(v[index] == 0) res = min(res, dfs(v, index + 1, 0, dp)); + else{ + res = min(res, dfs(v, index + 1, 1, dp)); + res = min(res, dfs(v, index + 1, 0, dp) + 1); + } + } + else{ + if(v[index] == 0){ + res = min(res, dfs(v, index + 1, 0, dp) + 2); + res = min(res, dfs(v, index + 1, 1, dp) + 1); + } + else res = min(res, dfs(v, index + 1, 1, dp)); + } + return dp[index][last] = res; + } +}; + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/CMakeLists.txt b/2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/CMakeLists.txt new file mode 100644 index 00000000..6c6290a0 --- /dev/null +++ b/2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2572) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2572 main.cpp) diff --git a/2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/main.cpp b/2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/main.cpp new file mode 100644 index 00000000..a778d533 --- /dev/null +++ b/2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/main.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-square-free-subsets/description/ +/// Author : liuyubobobo +/// Time : 2023-02-22 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * 2^10 + 2^20) +/// Space Complexity: O(2^10) +class Solution { + +private: + const vector primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; + const int prime_sz = primes.size(); + const long long MOD = 1e9 + 7; + +public: + int squareFreeSubsets(vector& nums) { + + int n = nums.size(), ones = 0; + vector cnt(1 << prime_sz, 0); + for(int num: nums){ + if(contain_square_factor(num)) continue; + if(num == 1){ ones ++; continue;} + int mask = 0; + for(int i = 0; i < prime_sz; i ++) + if(num % primes[i] == 0) + mask |= (1 << i); + cnt[mask] ++; + } + + vector dp(1 << prime_sz, 0); + dp[0] = 1; + for(int mask = 1; mask < (1 << prime_sz); mask ++) { + for(int submask = mask; submask >= 1; submask --) + if((submask & mask) == submask && __builtin_ffs(submask) == __builtin_ffs(mask)) + dp[mask] += dp[mask - submask] * cnt[submask]; + dp[mask] %= MOD; + } + long long res = accumulate(dp.begin(), dp.end(), 0LL) % MOD; + + long long ones_res = 1; + for(int i = 0; i < ones; i ++) ones_res = ones_res * 2 % MOD; + return (res * ones_res % MOD - 1 + MOD) % MOD; + } + +private: + bool contain_square_factor(int num){ + for(int prime: primes) + if(num % (prime * prime) == 0) return true; + return false; + } +}; + + +int main() { + + vector nums1 = {3, 4, 4, 5}; + cout << Solution().squareFreeSubsets(nums1) << endl; + // 3 + + return 0; +} diff --git a/2501-3000/2573-Find-the-String-with-LCP/cpp-2573/CMakeLists.txt b/2501-3000/2573-Find-the-String-with-LCP/cpp-2573/CMakeLists.txt new file mode 100644 index 00000000..d080518b --- /dev/null +++ b/2501-3000/2573-Find-the-String-with-LCP/cpp-2573/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2573) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2573 main.cpp) diff --git a/2501-3000/2573-Find-the-String-with-LCP/cpp-2573/main.cpp b/2501-3000/2573-Find-the-String-with-LCP/cpp-2573/main.cpp new file mode 100644 index 00000000..d6a704c9 --- /dev/null +++ b/2501-3000/2573-Find-the-String-with-LCP/cpp-2573/main.cpp @@ -0,0 +1,96 @@ +/// Source : https://leetcode.com/problems/find-the-string-with-lcp/description/ +/// Author : liuyubobobo +/// Time : 2023-02-22 + +#include +#include + +using namespace std; + + +/// Construction and LCP +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class UF{ + +private: + vector parent; + +public: + UF(int n) : parent(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + } +}; + +class Solution { +public: + string findTheString(vector>& lcp) { + + int n = lcp.size(); + UF uf(n); + for(int i = 0 ; i < n; i ++) + for(int j = 0 ; j < n; j ++) + if(lcp[i][j] >= 1) + uf.union_elements(i, j); + + vector parent2char(n, ' '); + char next_char = 'a'; + string res(n, ' '); + bool ok = true; + for(int i = 0 ; i < n && ok; i ++){ + int p = uf.find(i); + if(parent2char[p] == ' '){ + if(next_char > 'z') ok = false; + else parent2char[p] = next_char ++; + } + res[i] = parent2char[p]; + } + if(!ok) return ""; + + if(check(n, res, lcp)) return res; + return ""; + } + +private: + bool check(int n, const string& s, const vector>& A){ + vector> lcp(n, vector(n, 0)); + for(int i = n - 1 ; i >= 0 ; i --) + for(int j = n - 1 ; j >= 0 ; j --) + if(s[i] == s[j]){ + lcp[i][j] = 1 + ((i + 1 < n && j + 1 < n) ? lcp[i + 1][j + 1] : 0); + } + return lcp == A; + } +}; + + +int main() { + + vector> lcp2 = {{4,3,2,1}, + {3,3,2,1}, + {2,2,2,1}, + {1,1,1,1}}; + cout << Solution().findTheString(lcp2) << endl; + + return 0; +} diff --git a/readme.md b/readme.md index ccd45ee3..2d45840b 100644 --- a/readme.md +++ b/readme.md @@ -2457,6 +2457,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2567 | [Minimum Score by Changing Two Elements](https://leetcode.com/problems/minimum-score-by-changing-two-elements/) | [无] | [C++](2001-2500/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/) | | | | 2568 | [Minimum Impossible OR](https://leetcode.com/problems/minimum-impossible-or/) | [无] | [C++](2001-2500/2568-Minimum-Impossible-OR/cpp-2568/) | | | | 2569 | [Handling Sum Queries After Update](https://leetcode.com/problems/handling-sum-queries-after-update/) | [无] | [C++](2001-2500/2569-Handling-Sum-Queries-After-Update/cpp-2569/) | | | +| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/) | [无] | [C++](2001-2500/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/) | | | +| 2571 | [Minimum Operations to Reduce an Integer to 0](https://leetcode.com/problems/minimum-operations-to-reduce-an-integer-to-0/) | [无] | [C++](2001-2500/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/) | | | +| 2572 | [Count the Number of Square-Free Subsets](https://leetcode.com/problems/count-the-number-of-square-free-subsets/) | [无] | [C++](2001-2500/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/) | | | +| 2573 | [Find the String with LCP](https://leetcode.com/problems/find-the-string-with-lcp/) | [无] | [C++](2001-2500/2573-Find-the-String-with-LCP/cpp-2573/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 419ae6002b599b37b6f0b83e6dd51b6ed2d645d0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 24 Feb 2023 16:58:45 -0800 Subject: [PATCH 303/390] 1247 solved. --- .../cpp-1247/CMakeLists.txt | 6 +++ .../cpp-1247/main.cpp | 47 +++++++++++++++++++ readme.md | 1 + 3 files changed, 54 insertions(+) create mode 100644 1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/CMakeLists.txt create mode 100644 1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/main.cpp diff --git a/1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/CMakeLists.txt b/1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/CMakeLists.txt new file mode 100644 index 00000000..98b83287 --- /dev/null +++ b/1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1247) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1247 main.cpp) diff --git a/1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/main.cpp b/1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/main.cpp new file mode 100644 index 00000000..2df53c62 --- /dev/null +++ b/1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/description/ +/// Author : liuyubobobo +/// Time : 2023-02-24 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumSwap(string s1, string s2) { + + int x1 = 0, y1 = 0, x2 = 0, y2 = 0; + for(char c: s1) if(c == 'x') x1 ++; else y1 ++; + for(char c: s2) if(c == 'x') x2 ++; else y2 ++; + + if((x1 + x2) & 1) return -1; + if((y1 + y2) & 1) return -1; + + int xy = 0, yx = 0; + for(int i = 0; i < s1.size(); i ++){ + if(s1[i] == 'x' && s2[i] == 'y') xy ++; + if(s1[i] == 'y' && s2[i] == 'x') yx ++; + } + + int res = 0; + res += xy / 2; xy %= 2; + res += yx / 2; yx %= 2; + if(xy){ + assert(yx); + res += 2; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 2d45840b..be72ef0b 100644 --- a/readme.md +++ b/readme.md @@ -1220,6 +1220,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1244 | [Design A Leaderboard](https://leetcode.com/problems/design-a-leaderboard/) | [无] | [C++](1001-1500/1244-Design-A-Leaderboard/cpp-1244/) | | | | 1245 | [Tree Diameter](https://leetcode.com/problems/tree-diameter/) | [无] | [C++](1001-1500/1245-Tree-Diameter/cpp-1245/) | | | | | | | | | | +| 1247 | [Minimum Swaps to Make Strings Equal](https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/) | [无] | [C++](1001-1500/1247-Minimum-Swaps-to-Make-Strings-Equal/cpp-1247/) | | | | 1248 | [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [无] | [C++](1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/) | [Java](1001-1500/1248-Count-Number-of-Nice-Subarrays/java-1248/) | | | 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [solution](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/solution/) | [C++](1001-1500/1249-Minimum-Remove-to-Make-Valid-Parentheses/cpp-1249/) | | | | 1250 | [Check If It Is a Good Array](https://leetcode.com/problems/check-if-it-is-a-good-array/description/) | [无] | [C++](1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/) | | | From 5b7015ebaaf8382bc65b5ba20a553e573a0c84ad Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 25 Feb 2023 18:09:57 -0800 Subject: [PATCH 304/390] 1255 solved. --- .../cpp-1255/CMakeLists.txt | 6 +++ .../cpp-1255/main.cpp | 44 +++++++++++++++++++ readme.md | 2 + 3 files changed, 52 insertions(+) create mode 100644 1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/CMakeLists.txt create mode 100644 1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp diff --git a/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/CMakeLists.txt b/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/CMakeLists.txt new file mode 100644 index 00000000..9b275a2f --- /dev/null +++ b/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1255) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1255 main.cpp) diff --git a/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp b/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp new file mode 100644 index 00000000..f637aa8e --- /dev/null +++ b/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp @@ -0,0 +1,44 @@ +#include +#include + +using namespace std; + + +class Solution { +public: + int maxScoreWords(vector& words, vector& letters, vector& score) { + + int n = words.size(); + + vector f(26, 0); + for(char c: letters) f[c - 'a'] ++; + + int res = 0; + for(int state = 0; state < (1 << n); state ++){ + + vector curf(26, 0); + for(int i = 0; i < n; i ++){ + if((state >> i) & 1) for(char c: words[i]) curf[c - 'a'] ++; + } + + if(ok(curf, f)){ + int cur_score = 0; + for(int i = 0; i < 26; i ++) cur_score += score[i] * curf[i]; + res = max(res, cur_score); + } + } + return res; + } + +private: + bool ok(const vector& v, const vector& f){ + for(int i = 0; i < 26; i ++) if(v[i] > f[i]) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index be72ef0b..8c509b29 100644 --- a/readme.md +++ b/readme.md @@ -1227,6 +1227,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1251 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [无] | [C++](1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/) | | | | | | | | | | +| 1255 | [Maximum Score Words Formed by Letters](https://leetcode.com/problems/maximum-score-words-formed-by-letters/description/) | [无] | [C++](1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/) | | | +| | | | | | | | 1259 | [Handshakes That Don't Cross](https://leetcode.com/problems/handshakes-that-dont-cross/description/) | [无] | [C++](1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/) | | | | 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [solution](https://leetcode.com/problems/shift-2d-grid/solution/) | [C++](1001-1500/1260-Shift-2D-Grid/cpp-1260/) | | | | 1261 | [Find Elements in a Contaminated Binary Tree](https://leetcode.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binary-tree/) | [C++](1001-1500/1261-Find-Elements-in-a-Contaminated-Binary-Tree/cpp-1261/) | | | | From 93d929a6696c25ad7710283bc094e0cc8a5b4902 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 25 Feb 2023 18:11:47 -0800 Subject: [PATCH 305/390] 1255 comments updated. --- .../cpp-1255/main.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp b/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp index f637aa8e..6c04b837 100644 --- a/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp +++ b/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp @@ -1,9 +1,16 @@ +/// Source : https://leetcode.com/problems/maximum-score-words-formed-by-letters/description/ +/// Author : liuyubobobo +/// Time : 2023-02-25 + #include #include using namespace std; +/// State Compression DP +/// Time Complexity: O(2^n * max_word_len) +/// Space Complexity: O(1) class Solution { public: int maxScoreWords(vector& words, vector& letters, vector& score) { From c43e169c292d736319229a1f92e1aa890c8fceeb Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 1 Mar 2023 12:09:04 -0800 Subject: [PATCH 306/390] Cracking the coding interview 05 02 solved. --- .../05-02/cpp-05-02/CMakeLists.txt | 6 ++++ .../05-02/cpp-05-02/main.cpp | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Cracking-The-Coding-Interview/05-02/cpp-05-02/CMakeLists.txt create mode 100644 Cracking-The-Coding-Interview/05-02/cpp-05-02/main.cpp diff --git a/Cracking-The-Coding-Interview/05-02/cpp-05-02/CMakeLists.txt b/Cracking-The-Coding-Interview/05-02/cpp-05-02/CMakeLists.txt new file mode 100644 index 00000000..94d41a68 --- /dev/null +++ b/Cracking-The-Coding-Interview/05-02/cpp-05-02/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_05_02) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_05_02 main.cpp) diff --git a/Cracking-The-Coding-Interview/05-02/cpp-05-02/main.cpp b/Cracking-The-Coding-Interview/05-02/cpp-05-02/main.cpp new file mode 100644 index 00000000..89a0dedb --- /dev/null +++ b/Cracking-The-Coding-Interview/05-02/cpp-05-02/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.cn/problems/bianry-number-to-string-lcci/ +/// Author : liuyubobobo +/// Time : 2023-03-01 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + string printBin(double num) { + + string res = "0."; + double x = 0.5; + for (int k = 0; k < 7 && num != 0; k ++) { + if (num >= x) { + res += '1'; + num -= x; + } else { + res += '0'; + } + x /= 2; + } + return num == 0.0 ? res : "ERROR"; + } +}; + + +int main() { + + return 0; +} From 5aa78abba2233c9ad3db88b0c655c93c17d5acb4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 1 Mar 2023 12:16:58 -0800 Subject: [PATCH 307/390] 0422 solved. --- .../cpp-0422/CMakeLists.txt | 6 ++++ .../0422-Valid-Word-Square/cpp-0422/main.cpp | 32 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0422-Valid-Word-Square/cpp-0422/CMakeLists.txt create mode 100644 0001-0500/0422-Valid-Word-Square/cpp-0422/main.cpp diff --git a/0001-0500/0422-Valid-Word-Square/cpp-0422/CMakeLists.txt b/0001-0500/0422-Valid-Word-Square/cpp-0422/CMakeLists.txt new file mode 100644 index 00000000..aa64cf96 --- /dev/null +++ b/0001-0500/0422-Valid-Word-Square/cpp-0422/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_0422) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0422 main.cpp) diff --git a/0001-0500/0422-Valid-Word-Square/cpp-0422/main.cpp b/0001-0500/0422-Valid-Word-Square/cpp-0422/main.cpp new file mode 100644 index 00000000..8a29dca5 --- /dev/null +++ b/0001-0500/0422-Valid-Word-Square/cpp-0422/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/valid-word-square/description/ +/// Author : liuyubobobo +/// Time : 2023-03-01 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(R * C) +/// Space Complexity: O(1) +class Solution { +public: + bool validWordSquare(vector& words) { + + int R = words.size(); + for(int i = 0; i < R; i++) + for(int j = 0; j < words[i].size(); j++){ + if(j >= R || i >= words[j].size()) return false; + if(words[i][j] != words[j][i]) return false; + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 8c509b29..f907493c 100644 --- a/readme.md +++ b/readme.md @@ -456,7 +456,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 419 | [Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/) | [无] | [C++](0001-0500/0419-Battleships-in-a-Board/cpp-0419/) | | | | 420 | [Strong Password Checker](https://leetcode.com/problems/strong-password-checker/) | [无] | [C++](0001-0500/0420-Strong-Password-Checker/cpp-0420/) | | | | 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [solution](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/solution/) | [C++](0001-0500/0421-Maximum-XOR-of-Two-Numbers-in-an-Array/cpp-0421/) | | | -| | | | | | | +| 422 | [Valid Word Square](https://leetcode.com/problems/valid-word-square/description/) | [无] | [C++](0001-0500/0422-Valid-Word-Square/cpp-0422/) | | | | 423 | [Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/) | [solution](https://leetcode.com/problems/reconstruct-original-digits-from-english/solution/) | [C++](0001-0500/0423-Reconstruct-Original-Digits-from-English/cpp-0423/) | | | | 424 | [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | [无] | [C++](0001-0500/0424-Longest-Repeating-Character-Replacement/cpp-0424/) | | | | | | | | | | From 117335fed793be00c4d4c3d0ba30326829c8dcf2 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 4 Mar 2023 11:39:13 -0800 Subject: [PATCH 308/390] 1599 bug fixed. --- .../cpp1599/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1501-2000/1599-Maximum-Profit-of-Operating-a-Centennial-Wheel/cpp1599/main.cpp b/1501-2000/1599-Maximum-Profit-of-Operating-a-Centennial-Wheel/cpp1599/main.cpp index 3b063895..ee947e67 100644 --- a/1501-2000/1599-Maximum-Profit-of-Operating-a-Centennial-Wheel/cpp1599/main.cpp +++ b/1501-2000/1599-Maximum-Profit-of-Operating-a-Centennial-Wheel/cpp1599/main.cpp @@ -15,7 +15,7 @@ class Solution { public: int minOperationsMaxProfit(vector& customers, int boardingCost, int runningCost) { - int k = 0, kres = -1, cp = 0, maxres = -1, curres = 0, waiting = 0; + int k = 0, kres = -1, cp = 0, maxres = 0, curres = 0, waiting = 0; while(cp < customers.size() || waiting){ if(cp < customers.size()) waiting += customers[cp]; cp ++; From ed6301952d0335d21b3cda33ae17a739c17e2de3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 4 Mar 2023 16:26:49 -0800 Subject: [PATCH 309/390] 2578-2581 solved. --- .../cpp-2578/CMakeLists.txt | 6 ++ .../cpp-2578/main.cpp | 38 ++++++++++ .../cpp-2579/CMakeLists.txt | 6 ++ .../cpp-2579/main.cpp | 31 ++++++++ .../cpp-2580/CMakeLists.txt | 6 ++ .../cpp-2580/main.cpp | 55 ++++++++++++++ .../cpp-2581/CMakeLists.txt | 6 ++ .../cpp-2581/main.cpp | 74 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 227 insertions(+) create mode 100644 2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/CMakeLists.txt create mode 100644 2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/main.cpp create mode 100644 2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/CMakeLists.txt create mode 100644 2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/main.cpp create mode 100644 2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/CMakeLists.txt create mode 100644 2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/main.cpp create mode 100644 2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/CMakeLists.txt create mode 100644 2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/main.cpp diff --git a/2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/CMakeLists.txt b/2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/CMakeLists.txt new file mode 100644 index 00000000..2d383c52 --- /dev/null +++ b/2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2578) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2578 main.cpp) diff --git a/2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/main.cpp b/2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/main.cpp new file mode 100644 index 00000000..0b06e6f3 --- /dev/null +++ b/2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/split-with-minimum-sum/ +/// Author : liuyubobobo +/// Time : 2023-03-04 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(log(num)) +/// Space Complexity: O(log(num)) +class Solution { +public: + int splitNum(int num) { + + vector f(10, 0); + while(num) f[num % 10] ++, num /= 10; + + vector res(2, ""); + for(int e = 1, index = 0; e <= 9; e ++){ + while(f[e] --) res[index] += to_string(e), index ^= 1; + } + + for(int index = 0; index < 2; index ++) + if(res[index] == "") res[index] = "0"; + + int a = stoi(res[0]), b = stoi(res[1]); + return a + b; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/CMakeLists.txt b/2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/CMakeLists.txt new file mode 100644 index 00000000..deb7ada6 --- /dev/null +++ b/2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2579) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2579 main.cpp) diff --git a/2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/main.cpp b/2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/main.cpp new file mode 100644 index 00000000..f331780c --- /dev/null +++ b/2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/count-total-number-of-colored-cells/description/ +/// Author : liuyubobobo +/// Time : 2023-03-04 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + long long coloredCells(long long n) { + + return n * n * 2 - (n * 2 - 1); + } +}; + + +int main() { + + cout << Solution().coloredCells(1) << '\n'; + // 1 + + cout << Solution().coloredCells(2) << '\n'; + // 5 + + return 0; +} diff --git a/2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/CMakeLists.txt b/2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/CMakeLists.txt new file mode 100644 index 00000000..763f119e --- /dev/null +++ b/2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2580) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2580 main.cpp) diff --git a/2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/main.cpp b/2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/main.cpp new file mode 100644 index 00000000..72396faa --- /dev/null +++ b/2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/description/ +/// Author : liuyubobobo +/// Time : 2023-03-04 + +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int countWays(vector>& ranges) { + + int n = ranges.size(); + sort(ranges.begin(), ranges.end()); + + int cnt = 0, l = ranges[0][0], r = ranges[0][1]; + for(int i = 1; i <= n; i ++){ + if(i < n && overlapped(l, r, ranges[i][0], ranges[i][1])){ + l = min(l, ranges[i][0]); + r = max(r, ranges[i][1]); + } + else{ + cnt ++; + if(i < n) l = ranges[i][0], r = ranges[i][1]; + } + } + + long long res = 1; + for(int i = 0; i < cnt; i ++) res = res * 2ll % MOD; + return res; + } + +private: + bool overlapped(int l1, int r1, int l2, int r2){ + assert(l2 >= l1); + return l1 <= l2 && l2 <= r1; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/CMakeLists.txt b/2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/CMakeLists.txt new file mode 100644 index 00000000..d52cbfc9 --- /dev/null +++ b/2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2581) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2581 main.cpp) diff --git a/2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/main.cpp b/2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/main.cpp new file mode 100644 index 00000000..e469d275 --- /dev/null +++ b/2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/main.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/count-number-of-possible-root-nodes/description/ +/// Author : liuyubobobo +/// Time : 2023-03-04 + +#include +#include +#include +#include + +using namespace std; + + +/// Rerooting +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int rootCount(vector>& edges, vector>& guesses, int k) { + + int n = edges.size() + 1; + vector> tree(n); + for(const vector& edge: edges){ + int u = edge[0], v = edge[1]; + tree[u].push_back(v), tree[v].push_back(u); + } + + set> guess_set; + for(const vector& guess: guesses){ + int u = guess[0], v = guess[1]; + guess_set.insert({u, v}); + } + + set> correct_guesses; + dfs(tree, 0, -1, guess_set, correct_guesses); + + int res = 0; + dfs_reroot(tree, 0, -1, guess_set, correct_guesses, k, res); + return res; + } + +private: + void dfs_reroot(const vector>& tree, int u, int p, + const set>& guess_set, set>& correct_guesses, + const int k, int& res){ + + if(correct_guesses.size() >= k) res ++; + + for(int v: tree[u]){ + if(v == p) continue; + + correct_guesses.erase({u, v}); + if(guess_set.count({v, u})) correct_guesses.insert({v, u}); + dfs_reroot(tree, v, u, guess_set, correct_guesses, k, res); + correct_guesses.erase({v, u}); + if(guess_set.count({u, v})) correct_guesses.insert({u, v}); + } + } + + void dfs(const vector>& tree, int u, int p, + const set>& guess_set, set>& correct_guesses){ + + for(int v: tree[u]){ + if(v == p) continue; + if(guess_set.count({u, v})) + correct_guesses.insert({u, v}); + dfs(tree, v, u, guess_set, correct_guesses); + } + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index f907493c..1367cc4e 100644 --- a/readme.md +++ b/readme.md @@ -2465,6 +2465,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2572 | [Count the Number of Square-Free Subsets](https://leetcode.com/problems/count-the-number-of-square-free-subsets/) | [无] | [C++](2001-2500/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/) | | | | 2573 | [Find the String with LCP](https://leetcode.com/problems/find-the-string-with-lcp/) | [无] | [C++](2001-2500/2573-Find-the-String-with-LCP/cpp-2573/) | | | | | | | | | | +| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum/) | [无] | [C++](2001-2500/2578-Split-With-Minimum-Sum/cpp-2578/) | | | +| 2579 | [Count Total Number of Colored Cells](https://leetcode.com/problems/count-total-number-of-colored-cells/) | [无] | [C++](2001-2500/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/) | | | +| 2580 | [Count Ways to Group Overlapping Ranges](https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/) | [无] | [C++](2001-2500/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/) | | | +| 2581 | [Count Number of Possible Root Nodes](https://leetcode.com/problems/count-number-of-possible-root-nodes/) | [无] | [C++](2001-2500/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 461e2bace66358ee84dbd81bdd14ab4c665e87cf Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 7 Mar 2023 11:36:47 -0800 Subject: [PATCH 310/390] coding interviews 047 solved. --- Coding-Interviews/047/cpp-047/CMakeLists.txt | 6 ++++ Coding-Interviews/047/cpp-047/main.cpp | 32 ++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 Coding-Interviews/047/cpp-047/CMakeLists.txt create mode 100644 Coding-Interviews/047/cpp-047/main.cpp diff --git a/Coding-Interviews/047/cpp-047/CMakeLists.txt b/Coding-Interviews/047/cpp-047/CMakeLists.txt new file mode 100644 index 00000000..30a85b8a --- /dev/null +++ b/Coding-Interviews/047/cpp-047/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_047) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_047 main.cpp) diff --git a/Coding-Interviews/047/cpp-047/main.cpp b/Coding-Interviews/047/cpp-047/main.cpp new file mode 100644 index 00000000..a294b567 --- /dev/null +++ b/Coding-Interviews/047/cpp-047/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/ +/// Author : liuyubobobo +/// Time : 2023-03-07 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { +public: + int maxValue(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + vector> dp(R + 1, vector(C + 1, 0)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]) + grid[i][j]; + return dp[R][C]; + } +}; + + +int main() { + + return 0; +} From 7264035bc3ed4a8e6bf1b8a8c8b60cdd100d744d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 9 Mar 2023 00:49:56 -0800 Subject: [PATCH 311/390] 1055 solved. --- .../cpp-1055/CMakeLists.txt | 6 +++ .../cpp-1055/main.cpp | 43 +++++++++++++++++++ readme.md | 1 + 3 files changed, 50 insertions(+) create mode 100644 1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/CMakeLists.txt create mode 100644 1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/main.cpp diff --git a/1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/CMakeLists.txt b/1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/CMakeLists.txt new file mode 100644 index 00000000..d16026a9 --- /dev/null +++ b/1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1055) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1055 main.cpp) diff --git a/1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/main.cpp b/1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/main.cpp new file mode 100644 index 00000000..10767f82 --- /dev/null +++ b/1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/shortest-way-to-form-string/description/ +/// Author : liuyubobobo +/// Time : 2023-03-09 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(|s| * |t|) +/// Space Complexity: O(1) +class Solution { +public: + int shortestWay(string source, string target) { + + int res = 0, start = 0; + while(start < target.size()) { + if(ok(source, target, start)) res++; + else return -1; + } + return res; + } + +private: + bool ok(const string& source, const string& target, int& start) { + + int j = start; + for(int i = 0; i < source.size() && j < target.size(); i ++) + if(source[i] == target[j]) j ++; + + if(j == start) return false; + start = j; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 1367cc4e..9cd98279 100644 --- a/readme.md +++ b/readme.md @@ -1055,6 +1055,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1051 | [Height Checker](https://leetcode.com/problems/height-checker/) | [无] | [C++](1001-1500/1051-Height-Checker/cpp-1051/) | | | | 1052 | [Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner/) | [无] | [C++](1001-1500/1052-Grumpy-Bookstore-Owner/cpp-1052/) | | | | | | | | | | +| 1055 | [Shortest Way to Form String](https://leetcode.com/problems/shortest-way-to-form-string/description/) | [无] | [C++](1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/) | | | | 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number/description/) | [无] | [C++](1001-1500/1056-Confusing-Number/cpp-1056/) | | | | | | | | | | | 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [solution](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/solution/) | [C++](1001-1500/1059-All-Paths-from-Source-Lead-to-Destination/cpp-1059/) | | | From a33e68fe0a187979dc07bae0a0cc207c74300267 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 10 Mar 2023 15:22:47 -0800 Subject: [PATCH 312/390] cracking the coding interview 17 05 solved. --- .../17-05/cpp-17-05/CMakeLists.txt | 6 +++ .../17-05/cpp-17-05/main.cpp | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Cracking-The-Coding-Interview/17-05/cpp-17-05/CMakeLists.txt create mode 100644 Cracking-The-Coding-Interview/17-05/cpp-17-05/main.cpp diff --git a/Cracking-The-Coding-Interview/17-05/cpp-17-05/CMakeLists.txt b/Cracking-The-Coding-Interview/17-05/cpp-17-05/CMakeLists.txt new file mode 100644 index 00000000..2ca9c494 --- /dev/null +++ b/Cracking-The-Coding-Interview/17-05/cpp-17-05/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_17_05) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_17_05 main.cpp) diff --git a/Cracking-The-Coding-Interview/17-05/cpp-17-05/main.cpp b/Cracking-The-Coding-Interview/17-05/cpp-17-05/main.cpp new file mode 100644 index 00000000..c1b73aba --- /dev/null +++ b/Cracking-The-Coding-Interview/17-05/cpp-17-05/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.cn/problems/find-longest-subarray-lcci/ +/// Author : liuyubobobo +/// Time : 2023-03-10 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector findLongestSubarray(vector& array) { + + int n = array.size(); + vector v(n, 0); + for (int i = 0; i < n; i++) + v[i] = (isdigit(array[i][0]) ? 1 : -1); + + map m; + m[0] = -1; + int cur = 0, best = 0, resl = -1, resr = -1; + for(int i = 0; i < n; i ++) { + cur += v[i]; + + auto iter = m.find(-cur); + if(iter != m.end()){ + int len = i - iter->second; + if(len > best){ + best = len; + resl = iter->second + 1; + resr = i; + } + } + else m[-cur] = i; + } + + return best == 0 ? vector() : vector(array.begin() + resl, array.begin() + resr + 1); + } +}; + + +int main() { + + return 0; +} From ddf27e30bd483818eae8ebe88dd4c5b69dea268a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 13 Mar 2023 11:08:11 -0700 Subject: [PATCH 313/390] 2586-2589 solved. --- .../cpp-2586/CMakeLists.txt | 6 +++ .../cpp-2586/main.cpp | 38 +++++++++++++++++ .../cpp-2587/CMakeLists.txt | 6 +++ .../cpp-2587/main.cpp | 35 ++++++++++++++++ .../cpp-2588/CMakeLists.txt | 6 +++ .../cpp-2588/main.cpp | 37 +++++++++++++++++ .../cpp-2589/CMakeLists.txt | 6 +++ .../cpp-2589/main.cpp | 41 +++++++++++++++++++ readme.md | 5 +++ 9 files changed, 180 insertions(+) create mode 100644 2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/CMakeLists.txt create mode 100644 2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/main.cpp create mode 100644 2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/CMakeLists.txt create mode 100644 2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/main.cpp create mode 100644 2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/CMakeLists.txt create mode 100644 2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/main.cpp create mode 100644 2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/CMakeLists.txt create mode 100644 2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/main.cpp diff --git a/2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/CMakeLists.txt b/2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/CMakeLists.txt new file mode 100644 index 00000000..b61aa603 --- /dev/null +++ b/2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2586) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2586 main.cpp) diff --git a/2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/main.cpp b/2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/main.cpp new file mode 100644 index 00000000..a5bce452 --- /dev/null +++ b/2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/description/ +/// Author : liuyubobobo +/// Time : 2023-03-13 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int vowelStrings(vector& words, int left, int right) { + + int res = 0; + for(int i = left; i <= right; i ++) + res += check(words[i]); + return res; + } + +private: + bool check(const string& s){ + return is_vowel(s[0]) && is_vowel(s.back()); + } + + bool is_vowel(char c){ + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/CMakeLists.txt b/2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/CMakeLists.txt new file mode 100644 index 00000000..5588fb09 --- /dev/null +++ b/2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2587) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2587 main.cpp) diff --git a/2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/main.cpp b/2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/main.cpp new file mode 100644 index 00000000..247cf30e --- /dev/null +++ b/2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/description/ +/// Author : liuyubobobo +/// Time : 2023-03-13 + +#include +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maxScore(vector& nums) { + + sort(nums.begin(), nums.end(), greater()); + + int n = nums.size(); + vector presum(n + 1, 0ll); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + nums[i]; + + int res = 0; + for(int i = 1; i <= n; i ++) res += presum[i] > 0; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/CMakeLists.txt b/2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/CMakeLists.txt new file mode 100644 index 00000000..cf7187b8 --- /dev/null +++ b/2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2588) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2588 main.cpp) diff --git a/2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/main.cpp b/2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/main.cpp new file mode 100644 index 00000000..ee634780 --- /dev/null +++ b/2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/description/ +/// Author : liuyubobobo +/// Time : 2023-03-13 + +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(max_num) +class Solution { +public: + long long beautifulSubarrays(vector& nums) { + + vector records(1 << 20, 0); + records[0] ++; + long long res = 0; + int x = 0; + for(int e: nums){ + for(int p = 0; p < 20; p++){ + if((e >> p) & 1) x ^= (1 << p); + } + res += records[x]; + records[x] ++; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/CMakeLists.txt b/2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/CMakeLists.txt new file mode 100644 index 00000000..f584459c --- /dev/null +++ b/2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2589) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2589 main.cpp) diff --git a/2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/main.cpp b/2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/main.cpp new file mode 100644 index 00000000..3f26ae7b --- /dev/null +++ b/2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-complete-all-tasks/description/ +/// Author : liuyubobobo +/// Time : 2023-03-13 + +#include +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn + n * max_time) +/// Space Complexity: O(max_time) +class Solution { +public: + int findMinimumTime(vector>& tasks) { + + sort(tasks.begin(), tasks.end(), [](vector& a, vector& b) { + if(a[1] != b[1]) return a[1] < b[1]; + return a[0] < b[0]; + }); + + vector used(2001, 0); + for(const vector& task: tasks){ + int start = task[0], end = task[1], duration = task[2]; + int cur = accumulate(used.begin() + start, used.begin() + (end + 1), 0); + for(int i = end; i >= start && cur < duration; i --) + if(!used[i]) used[i] = 1, cur ++; + } + + return accumulate(used.begin(), used.end(), 0); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9cd98279..511b58d2 100644 --- a/readme.md +++ b/readme.md @@ -2471,6 +2471,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2580 | [Count Ways to Group Overlapping Ranges](https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/) | [无] | [C++](2001-2500/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/) | | | | 2581 | [Count Number of Possible Root Nodes](https://leetcode.com/problems/count-number-of-possible-root-nodes/) | [无] | [C++](2001-2500/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/) | | | | | | | | | | +| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/) | [无] | [C++](2001-2500/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/) | | | +| 2587 | [Rearrange Array to Maximize Prefix Score](https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/) | [无] | [C++](2001-2500/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/) | | | +| 2588 | [Count the Number of Beautiful Subarrays](https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/) | [无] | [C++](2001-2500/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/) | | | +| 2589 | [Minimum Time to Complete All Tasks](https://leetcode.com/problems/minimum-time-to-complete-all-tasks/) | [无] | [C++](2001-2500/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From e723f118071a2dd4bff05266e40d718df4d18c4e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 13 Mar 2023 11:33:46 -0700 Subject: [PATCH 314/390] 2582-2585 solved. --- .../cpp-2582/CMakeLists.txt | 6 ++ .../2582-Pass-the-Pillow/cpp-2582/main.cpp | 31 ++++++++ .../cpp-2583/CMakeLists.txt | 6 ++ .../cpp-2583/main.cpp | 55 ++++++++++++++ .../cpp-2584/CMakeLists.txt | 6 ++ .../cpp-2584/main.cpp | 76 +++++++++++++++++++ .../cpp-2585/CMakeLists.txt | 6 ++ .../cpp-2585/main.cpp | 42 ++++++++++ readme.md | 5 +- 9 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2582-Pass-the-Pillow/cpp-2582/CMakeLists.txt create mode 100644 2501-3000/2582-Pass-the-Pillow/cpp-2582/main.cpp create mode 100644 2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/CMakeLists.txt create mode 100644 2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/main.cpp create mode 100644 2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/CMakeLists.txt create mode 100644 2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/main.cpp create mode 100644 2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/CMakeLists.txt create mode 100644 2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/main.cpp diff --git a/2501-3000/2582-Pass-the-Pillow/cpp-2582/CMakeLists.txt b/2501-3000/2582-Pass-the-Pillow/cpp-2582/CMakeLists.txt new file mode 100644 index 00000000..bccbfed5 --- /dev/null +++ b/2501-3000/2582-Pass-the-Pillow/cpp-2582/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2582) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2582 main.cpp) diff --git a/2501-3000/2582-Pass-the-Pillow/cpp-2582/main.cpp b/2501-3000/2582-Pass-the-Pillow/cpp-2582/main.cpp new file mode 100644 index 00000000..367d6477 --- /dev/null +++ b/2501-3000/2582-Pass-the-Pillow/cpp-2582/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/pass-the-pillow/description/ +/// Author : liuyubobobo +/// Time : 2023-03-04 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int passThePillow(int n, int time) { + + int k = 2 * (n - 1); + time %= k; + + if(time <= n - 1) return time + 1; + + time -= (n - 1); + return n - time; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/CMakeLists.txt b/2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/CMakeLists.txt new file mode 100644 index 00000000..f41f268e --- /dev/null +++ b/2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2583) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2583 main.cpp) diff --git a/2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/main.cpp b/2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/main.cpp new file mode 100644 index 00000000..60b0df7f --- /dev/null +++ b/2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/kth-largest-sum-in-a-binary-tree/description/ +/// Author : liuyubobobo +/// Time : 2023-03-04 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + long long kthLargestLevelSum(TreeNode* root, int k) { + + vector sums; + dfs(root, 0, sums); + sort(sums.begin(), sums.end(), greater<>()); + return k - 1 < sums.size() ? sums[k - 1] : -1; + } + +private: + void dfs(TreeNode* root, int level, vector& sums) { + + if (!root) return; + + if (level >= sums.size()){ + assert(level == sums.size()); + sums.push_back(root->val); + } + else sums[level] += root->val; + + dfs(root->left, level + 1, sums); + dfs(root->right, level + 1, sums); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/CMakeLists.txt b/2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/CMakeLists.txt new file mode 100644 index 00000000..7d06d034 --- /dev/null +++ b/2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2584) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2584 main.cpp) diff --git a/2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/main.cpp b/2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/main.cpp new file mode 100644 index 00000000..bcfa71be --- /dev/null +++ b/2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/main.cpp @@ -0,0 +1,76 @@ +/// Source : https://leetcode.com/problems/split-the-array-to-make-coprime-products/description/ +/// Author : liuyubobobo +/// Time : 2023-03-13 + +#include +#include +#include +#include + +using namespace std; + + +/// Sieve + Map +/// Time Complexity: O(max_num * log(max_num) + n * log(max_num)) +/// Space Complexity: O(max_num) +class Solution { +public: + int findValidSplit(vector& nums) { + + int n = nums.size(); + int max_num = *max_element(nums.begin(), nums.end()); + + vector sieve_table = sieve(max_num); + + vector f1(max_num + 1, 0), f2(max_num + 1, 0); + for(int i = 0; i < n; i ++){ + int x = nums[i]; + while(x > 1){ + int p = sieve_table[x]; + f2[p] ++; + while(x % p == 0) x /= p; + } + } + + int both = 0; + for(int i = 0; i < n; i ++){ + int x = nums[i]; + while(x > 1){ + int p = sieve_table[x]; + both -= (f1[p] && f2[p]); + + f2[p] --; + f1[p] ++; + while(x % p == 0) x /= p; + + both += (f1[p] && f2[p]); + } + if(both == 0 && i < n - 1) return i; + } + + return -1; + } + +private: + vector sieve(int n){ + + vector table(n + 1, 0); + vector primes; + table[0] = table[1] = 1; + for(int i = 2; i <= n; i ++){ + if(table[i] == 0) { + primes.push_back(i); + table[i] = i; + } + for(int j = 0; j < primes.size() && primes[j] <= table[i] && i * primes[j] <= n; j ++) + table[i * primes[j]] = primes[j]; + } + return table; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/CMakeLists.txt b/2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/CMakeLists.txt new file mode 100644 index 00000000..57bb80ca --- /dev/null +++ b/2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2585) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2585 main.cpp) diff --git a/2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/main.cpp b/2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/main.cpp new file mode 100644 index 00000000..f83a8ae4 --- /dev/null +++ b/2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-earn-points/description/ +/// Author : liuyubobobo +/// Time : 2023-03-13 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * target * max_count) +/// Space Complexity: O(n * target) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int waysToReachTarget(int target, vector>& types) { + + int n = types.size(); + vector> dp(n + 1, vector(target + 1, 0)); + dp[0][0] = 1; + for(int i = 0; i < n; i ++){ + int count = types[i][0], mark = types[i][1]; + for(int j = 0; j <= count; j ++){ + for(int t = mark * j; t <= target; t ++){ + dp[i + 1][t] += dp[i][t - mark * j]; + dp[i + 1][t] %= MOD; + } + } + } + return dp[n][target]; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 511b58d2..5866a2e8 100644 --- a/readme.md +++ b/readme.md @@ -2470,7 +2470,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2579 | [Count Total Number of Colored Cells](https://leetcode.com/problems/count-total-number-of-colored-cells/) | [无] | [C++](2001-2500/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/) | | | | 2580 | [Count Ways to Group Overlapping Ranges](https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/) | [无] | [C++](2001-2500/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/) | | | | 2581 | [Count Number of Possible Root Nodes](https://leetcode.com/problems/count-number-of-possible-root-nodes/) | [无] | [C++](2001-2500/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/) | | | -| | | | | | | +| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow/) | [无] | [C++](2001-2500/2582-Pass-the-Pillow/cpp-2582/) | | | +| 2583 | [Kth Largest Sum in a Binary Tree](https://leetcode.com/problems/kth-largest-sum-in-a-binary-tree/) | [无] | [C++](2001-2500/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/) | | | +| 2584 | [Split the Array to Make Coprime Products](https://leetcode.com/problems/split-the-array-to-make-coprime-products/) | [无] | [C++](2001-2500/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/) | | | +| 2585 | [Number of Ways to Earn Points](https://leetcode.com/problems/number-of-ways-to-earn-points/) | [无] | [C++](2001-2500/2585-Number-of-Ways-to-Earn-Points/cpp-2585/) | | | | 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/) | [无] | [C++](2001-2500/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/) | | | | 2587 | [Rearrange Array to Maximize Prefix Score](https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/) | [无] | [C++](2001-2500/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/) | | | | 2588 | [Count the Number of Beautiful Subarrays](https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/) | [无] | [C++](2001-2500/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/) | | | From 2810a72824259c03561a761f9976d957011aa941 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 24 Mar 2023 13:49:26 -0700 Subject: [PATCH 315/390] 1574 solved. --- .../cpp-1574/CMakeLists.txt | 6 ++ .../cpp-1574/main.cpp | 74 +++++++++++++++++++ .../cpp-1574/main2.cpp | 67 +++++++++++++++++ readme.md | 2 + 4 files changed, 149 insertions(+) create mode 100644 1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/CMakeLists.txt create mode 100644 1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main.cpp create mode 100644 1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main2.cpp diff --git a/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/CMakeLists.txt b/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/CMakeLists.txt new file mode 100644 index 00000000..d27c9c03 --- /dev/null +++ b/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1574) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1574 main2.cpp) diff --git a/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main.cpp b/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main.cpp new file mode 100644 index 00000000..c9ea9f0a --- /dev/null +++ b/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/description/ +/// Author : liuyubobobo +/// Time : 2023-03-24 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +public: + int findLengthOfShortestSubarray(vector& arr) { + + if(is_sorted(arr.begin(), arr.end())) return 0; + + int n = arr.size(); + + map table; + table[arr[n - 1]] = n - 1; + int res = 1; + for(int i = n - 2; i >= 0 && arr[i] <= arr[i + 1]; i --) + table[arr[i]] = i, res = n - i; + + for(int i = 0; i < n && (i == 0 || arr[i] >= arr[i - 1]); i ++){ + res = max(res, i + 1); + auto iter = table.lower_bound(arr[i]); + if(iter != table.end()){ + int j = iter->second; + res = max(res, i + 1 + (n - j)); + } + } + return n - res; + } +}; + + +int main() { + + vector arr1 = {1, 2, 3, 10, 4, 2, 3, 5}; + cout << Solution().findLengthOfShortestSubarray(arr1) << '\n'; + // 3 + + vector arr2 = {5, 4, 3, 2, 1}; + cout << Solution().findLengthOfShortestSubarray(arr2) << '\n'; + // 4 + + vector arr3 = {2, 2, 2, 1, 1, 1}; + cout << Solution().findLengthOfShortestSubarray(arr3) << '\n'; + // 3 + + vector arr4 = {1, 2, 3}; + cout << Solution().findLengthOfShortestSubarray(arr4) << '\n'; + // 0 + + vector arr5 = {10, 13, 17, 21, 15, 15, 9, 17, 22, 22, 13}; + cout << Solution().findLengthOfShortestSubarray(arr5) << '\n'; + // 7 + + vector arr6 = {16, 10, 0, 3, 22, 1, 14, 7, 1, 12, 15}; + cout << Solution().findLengthOfShortestSubarray(arr6) << '\n'; + // 8 + + vector arr7 = {22, 0, 23, 23, 27, 23, 12, 19, 18, 10, 25, 29, 15, 28, 0}; + cout << Solution().findLengthOfShortestSubarray(arr7) << '\n'; + // 14 + + return 0; +} diff --git a/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main2.cpp b/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main2.cpp new file mode 100644 index 00000000..de45855d --- /dev/null +++ b/1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/main2.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/description/ +/// Author : liuyubobobo +/// Time : 2023-03-24 + +#include +#include +#include + +using namespace std; + + +/// Two Pointers +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { + +public: + int findLengthOfShortestSubarray(vector& arr) { + + int n = arr.size(); + + int j = n - 1; + while(j - 1 >= 0 && arr[j - 1] <= arr[j]) j --; + + int res = n - j; + for(int i = 0; i < n && (i == 0 || arr[i] >= arr[i - 1]); i ++){ + res = max(res, i + 1); + while(j < n && (i == j || arr[i] > arr[j])) j ++; + if(j < n) res = max(res, i + 1 + (n - j)); + } + return n - res; + } +}; + + +int main() { + + vector arr1 = {1, 2, 3, 10, 4, 2, 3, 5}; + cout << Solution().findLengthOfShortestSubarray(arr1) << '\n'; + // 3 + + vector arr2 = {5, 4, 3, 2, 1}; + cout << Solution().findLengthOfShortestSubarray(arr2) << '\n'; + // 4 + + vector arr3 = {2, 2, 2, 1, 1, 1}; + cout << Solution().findLengthOfShortestSubarray(arr3) << '\n'; + // 3 + + vector arr4 = {1, 2, 3}; + cout << Solution().findLengthOfShortestSubarray(arr4) << '\n'; + // 0 + + vector arr5 = {10, 13, 17, 21, 15, 15, 9, 17, 22, 22, 13}; + cout << Solution().findLengthOfShortestSubarray(arr5) << '\n'; + // 7 + + vector arr6 = {16, 10, 0, 3, 22, 1, 14, 7, 1, 12, 15}; + cout << Solution().findLengthOfShortestSubarray(arr6) << '\n'; + // 8 + + vector arr7 = {22, 0, 23, 23, 27, 23, 12, 19, 18, 10, 25, 29, 15, 28, 0}; + cout << Solution().findLengthOfShortestSubarray(arr7) << '\n'; + // 14 + + return 0; +} diff --git a/readme.md b/readme.md index 5866a2e8..ae5c3cb2 100644 --- a/readme.md +++ b/readme.md @@ -1467,6 +1467,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1570 | [Dot Product of Two Sparse Vectors](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/) | [solution](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/) | [C++](1501-2000/1570-Dot-Product-of-Two-Sparse-Vectors/cpp-1570/) | | | | 1571 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | +| 1574 | [Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/description/) | [无] | [C++](1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/) | | | +| | | | | | | | 1576 | [Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [无] | [C++](1501-2000/1576-Replace-All-Question-Marks-to-Avoid-Consecutive-Repeating-Characters/cpp-1576/) | | | | 1577 | [Number of Ways Where Square of Number Is Equal to Product of Two Numbers](https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/) | [无] | [C++](1501-2000/1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp-1577/) | | | | 1578 | [Minimum Deletion Cost to Avoid Repeating Letters](https://leetcode.com/problems/minimum-deletion-cost-to-avoid-repeating-letters/) | [无] | [C++](1501-2000/1578-Minimum-Deletion-Cost-to-Avoid-Repeating-Letters/cpp-1578/) | | | From 17271dd754559f9e368d8125eca955d3de959fd6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 24 Mar 2023 14:01:26 -0700 Subject: [PATCH 316/390] 1236 solved. --- .../1236-Web-Crawler/cpp-1236/CMakeLists.txt | 6 ++ 1001-1500/1236-Web-Crawler/cpp-1236/main.cpp | 55 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1236-Web-Crawler/cpp-1236/CMakeLists.txt create mode 100644 1001-1500/1236-Web-Crawler/cpp-1236/main.cpp diff --git a/1001-1500/1236-Web-Crawler/cpp-1236/CMakeLists.txt b/1001-1500/1236-Web-Crawler/cpp-1236/CMakeLists.txt new file mode 100644 index 00000000..96241cea --- /dev/null +++ b/1001-1500/1236-Web-Crawler/cpp-1236/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1236) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1236 main.cpp) diff --git a/1001-1500/1236-Web-Crawler/cpp-1236/main.cpp b/1001-1500/1236-Web-Crawler/cpp-1236/main.cpp new file mode 100644 index 00000000..0ba3d9c0 --- /dev/null +++ b/1001-1500/1236-Web-Crawler/cpp-1236/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/web-crawler/description/ +/// Author : liuyubobobo +/// Time : 2023-03-24 + +#include +#include +#include +#include + +using namespace std; + + +// This is the HtmlParser's API interface. +// You should not implement it, or speculate about its implementation +class HtmlParser { + public: + vector getUrls(string url); +}; + + +/// BFS +/// Time Complexity: O(V + E) +/// Space Complexity: O(V) +class Solution { +public: + vector crawl(string startUrl, HtmlParser htmlParser) { + + string hostname = startUrl.substr(7); + int pos = hostname.find("/"); + if(pos != string::npos) + hostname = hostname.substr(0, pos); + + set visited; + queue q; + q.push(startUrl); + visited.insert(startUrl); + while(!q.empty()){ + string url = q.front(); + q.pop(); + vector urls = htmlParser.getUrls(url); + for(auto u : urls){ + if(visited.find(u) == visited.end() && u.find(hostname) != string::npos){ + visited.insert(u); + q.push(u); + } + } + } + return vector(visited.begin(), visited.end()); + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ae5c3cb2..c7ae7296 100644 --- a/readme.md +++ b/readme.md @@ -1211,7 +1211,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1233 | [Remove Sub-Folders from the Filesystem](https://leetcode.com/problems/remove-sub-folders-from-the-filesystem/) | [无] | [C++](1001-1500/1233-Remove-Sub-Folders-from-the-Filesystem/cpp-1233/) | | | | 1234 | [Replace the Substring for Balanced String](https://leetcode.com/problems/replace-the-substring-for-balanced-string/) | [无] | [C++](1001-1500/1234-Replace-the-Substring-for-Balanced-String/cpp-1234/) | | | | 1235 | [Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling/) | [无] | [C++](1001-1500/1235-Maximum-Profit-in-Job-Scheduling/cpp-1235/) | | | -| | | | | | | +| 1236 | [Web Crawler](https://leetcode.com/problems/web-crawler/description/) | [无] | [C++](1001-1500/1236-Web-Crawler/cpp-1236/) | | | | 1237 | [Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/) | [无] | [C++](1001-1500/1237-Find-Positive-Integer-Solution-for-a-Given-Equation/cpp-1237/) | | | | 1238 | [Circular Permutation in Binary Representation](https://leetcode.com/problems/circular-permutation-in-binary-representation/) | [无] | [C++](1001-1500/1238-Circular-Permutation-in-Binary-Representation/cpp-1238/) | | | | 1239 | [Maximum Length of a Concatenated String with Unique Characters](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | [无] | [C++](1001-1500/1239-Maximum-Length-of-a-Concatenated-String-with-Unique-Characters/cpp-1239/) | | | From c192e938e759ad5686626d489d381cb7931d7b5b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 24 Mar 2023 15:10:50 -0700 Subject: [PATCH 317/390] 2590 solved. --- .../cpp-2590/CMakeLists.txt | 6 ++ .../2590-Design-a-Todo-List/cpp-2590/main.cpp | 98 +++++++++++++++++++ readme.md | 1 + 3 files changed, 105 insertions(+) create mode 100644 2501-3000/2590-Design-a-Todo-List/cpp-2590/CMakeLists.txt create mode 100644 2501-3000/2590-Design-a-Todo-List/cpp-2590/main.cpp diff --git a/2501-3000/2590-Design-a-Todo-List/cpp-2590/CMakeLists.txt b/2501-3000/2590-Design-a-Todo-List/cpp-2590/CMakeLists.txt new file mode 100644 index 00000000..88d68a11 --- /dev/null +++ b/2501-3000/2590-Design-a-Todo-List/cpp-2590/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2590) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2590 main.cpp) diff --git a/2501-3000/2590-Design-a-Todo-List/cpp-2590/main.cpp b/2501-3000/2590-Design-a-Todo-List/cpp-2590/main.cpp new file mode 100644 index 00000000..fd7138ec --- /dev/null +++ b/2501-3000/2590-Design-a-Todo-List/cpp-2590/main.cpp @@ -0,0 +1,98 @@ +/// Source : https://leetcode.com/problems/design-a-todo-list/description/ +/// Author : liuyubobobo +/// Time : 2023-03-24 + +#include +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class TodoList { + +private: + class Task { + public: + string description; + int dueDate; + vector tags; + bool completed; + + Task(const string& description, int dueDate, const vector& tags, bool completed) + : description(description), dueDate(dueDate), tags(tags.begin(), tags.end()), completed(completed) {} + }; + + vector> user_tasks; + vector task_list; + +public: + TodoList() : user_tasks(101){} + + int addTask(int userId, const string& taskDescription, int dueDate, const vector& tags) { + task_list.push_back(Task(taskDescription, dueDate, tags, false)); + + int task_id = task_list.size(); + user_tasks[userId].push_back(task_id - 1); + return task_id; + } + + vector getAllTasks(int userId) { + vector> result; + for (int task_id : user_tasks[userId]) + if(!task_list[task_id].completed) + result.push_back({task_list[task_id].description, task_list[task_id].dueDate}); + return get_tasks_sorted_by_due_date(result); + } + + vector getTasksForTag(int userId, string tag) { + vector> result; + for (int task_id : user_tasks[userId]) { + if (find(task_list[task_id].tags.begin(), task_list[task_id].tags.end(), tag) != task_list[task_id].tags.end() && !task_list[task_id].completed) + result.push_back({task_list[task_id].description, task_list[task_id].dueDate}); + } + return get_tasks_sorted_by_due_date(result); + } + + void completeTask(int userId, int taskId) { + if(find(user_tasks[userId].begin(), user_tasks[userId].end(), taskId - 1) != user_tasks[userId].end()) + task_list[taskId - 1].completed = true; + } + +private: + vector get_tasks_sorted_by_due_date(vector>& v) { + sort(v.begin(), v.end(), [](pair a, pair b) { + return a.second < b.second; + }); + + vector ret; + for(auto task : v) + ret.push_back(task.first); + return ret; + } +}; + + +int main() { + + TodoList todoList; + cout << todoList.addTask(1, "Task1", 50, {}) << '\n'; // 1 + cout << todoList.addTask(1, "Task2", 100, {"P1"}) << '\n'; // 2 + + vector res = todoList.getAllTasks(1); + for(const string& e: res) cout << e << ' '; cout << '\n'; // Task1, Task2 + + res = todoList.getAllTasks(5); + for(const string& e: res) cout << e << ' '; cout << '\n'; // empty + + cout << todoList.addTask(1, "Task3", 30, {"P1"}) << '\n'; // 3 + + res = todoList.getTasksForTag(1, "P1"); + for(const string& e: res) cout << e << ' '; cout << '\n'; // Task3, Task2 + + return 0; +} diff --git a/readme.md b/readme.md index c7ae7296..d66e77b5 100644 --- a/readme.md +++ b/readme.md @@ -2480,6 +2480,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2587 | [Rearrange Array to Maximize Prefix Score](https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/) | [无] | [C++](2001-2500/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/) | | | | 2588 | [Count the Number of Beautiful Subarrays](https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/) | [无] | [C++](2001-2500/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/) | | | | 2589 | [Minimum Time to Complete All Tasks](https://leetcode.com/problems/minimum-time-to-complete-all-tasks/) | [无] | [C++](2001-2500/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/) | | | +| 2590 | [Design a Todo List](https://leetcode.com/problems/design-a-todo-list/description/) | [无] | [C++](2001-2500/2590-Design-a-Todo-List/cpp-2590/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 81fa3294376d35dfa8417a6ff742b61a2518997e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 24 Mar 2023 17:17:45 -0700 Subject: [PATCH 318/390] 2599 solved. --- .../cpp-2599/CMakeLists.txt | 6 +++ .../cpp-2599/main.cpp | 45 +++++++++++++++++++ readme.md | 2 + 3 files changed, 53 insertions(+) create mode 100644 2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/CMakeLists.txt create mode 100644 2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/main.cpp diff --git a/2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/CMakeLists.txt b/2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/CMakeLists.txt new file mode 100644 index 00000000..c5f0b184 --- /dev/null +++ b/2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2599) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2599 main.cpp) diff --git a/2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/main.cpp b/2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/main.cpp new file mode 100644 index 00000000..57e584a6 --- /dev/null +++ b/2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/make-the-prefix-sum-non-negative/description/ +/// Author : liuyubobobo +/// Time : 2023-03-24 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int makePrefSumNonNegative(vector& nums) { + + int n = nums.size(); + + vector last; + priority_queue, greater<>> pq; + long long cur_presum = 0; + for(int i = 0; i < n; i ++){ + + cur_presum += nums[i]; + if(nums[i] < 0) pq.push(nums[i]); + + while(cur_presum < 0){ + long long t = pq.top(); pq.pop(); + last.push_back(t), cur_presum -= t; + } + + } + return last.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d66e77b5..7425d2e4 100644 --- a/readme.md +++ b/readme.md @@ -2482,6 +2482,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2589 | [Minimum Time to Complete All Tasks](https://leetcode.com/problems/minimum-time-to-complete-all-tasks/) | [无] | [C++](2001-2500/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/) | | | | 2590 | [Design a Todo List](https://leetcode.com/problems/design-a-todo-list/description/) | [无] | [C++](2001-2500/2590-Design-a-Todo-List/cpp-2590/) | | | | | | | | | | +| 2599 | [Make the Prefix Sum Non-negative](https://leetcode.com/problems/make-the-prefix-sum-non-negative/description/) | [无] | [C++](2001-2500/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 3e658ad0d216a45d14f92f6d37752b79d5451a7a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 25 Mar 2023 19:10:45 -0700 Subject: [PATCH 319/390] 2591-2594 solved. --- .../cpp-2591/CMakeLists.txt | 6 +++ .../cpp-2591/main.cpp | 34 ++++++++++++++ .../cpp-2592/CMakeLists.txt | 6 +++ .../cpp-2592/main.cpp | 36 +++++++++++++++ .../cpp-2593/CMakeLists.txt | 6 +++ .../cpp-2593/main.cpp | 45 +++++++++++++++++++ .../cpp-2594/CMakeLists.txt | 6 +++ .../cpp-2594/main.cpp | 44 ++++++++++++++++++ readme.md | 4 ++ 9 files changed, 187 insertions(+) create mode 100644 2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/CMakeLists.txt create mode 100644 2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/main.cpp create mode 100644 2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/CMakeLists.txt create mode 100644 2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/main.cpp create mode 100644 2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/CMakeLists.txt create mode 100644 2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/main.cpp create mode 100644 2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/CMakeLists.txt create mode 100644 2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/main.cpp diff --git a/2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/CMakeLists.txt b/2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/CMakeLists.txt new file mode 100644 index 00000000..7e294415 --- /dev/null +++ b/2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2591) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2591 main.cpp) diff --git a/2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/main.cpp b/2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/main.cpp new file mode 100644 index 00000000..fb21aa61 --- /dev/null +++ b/2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/distribute-money-to-maximum-children/description/ +/// Author : liuyubobobo +/// Time : 2023-03-25 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(min(money / 8, children)) +/// Space Complexity: O(1) +class Solution { +public: + int distMoney(int money, int children) { + + for(int res = min(money / 8, children); res >= 0; res --){ + + int left_money = money - res * 8; + int left_children = children - res; + if(left_money && !left_children) continue; + if(left_children == 1 && left_money == 4) continue; + if(left_money < left_children) continue; + return res; + } + return -1; + } +}; + +int main() { + + return 0; +} diff --git a/2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/CMakeLists.txt b/2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/CMakeLists.txt new file mode 100644 index 00000000..b2be2054 --- /dev/null +++ b/2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2592) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2592 main.cpp) diff --git a/2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/main.cpp b/2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/main.cpp new file mode 100644 index 00000000..5f8a952f --- /dev/null +++ b/2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/maximize-greatness-of-an-array/description/ +/// Author : liuyubobobo +/// Time : 2023-03-25 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Compelxity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maximizeGreatness(vector& nums) { + + multiset s(nums.begin(), nums.end()); + + int res = 0; + for(int e: nums){ + auto iter = s.upper_bound(e); + if(iter != s.end()){ + res ++; s.erase(iter); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/CMakeLists.txt b/2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/CMakeLists.txt new file mode 100644 index 00000000..12acc0a5 --- /dev/null +++ b/2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2593) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2593 main.cpp) diff --git a/2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/main.cpp b/2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/main.cpp new file mode 100644 index 00000000..f354bba2 --- /dev/null +++ b/2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/description/ +/// Author : liuyubobobo +/// Time : 2023-03-25 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long findScore(vector& nums) { + + int n = nums.size(); + vector visited(n, false); + + long long score = 0; + priority_queue, vector>, greater<>> pq; + for(int i = 0; i < n; i++) pq.push({nums[i], i}); + + while(!pq.empty()) { + auto [val, idx] = pq.top(); + pq.pop(); + + if(visited[idx]) continue; + visited[idx] = true; + if(idx > 0) visited[idx - 1] = true; + if(idx + 1 < n) visited[idx + 1] = true; + + score += val; + } + return score; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/CMakeLists.txt b/2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/CMakeLists.txt new file mode 100644 index 00000000..5f796857 --- /dev/null +++ b/2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2594) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2594 main.cpp) diff --git a/2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/main.cpp b/2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/main.cpp new file mode 100644 index 00000000..30fa0536 --- /dev/null +++ b/2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-repair-cars/description/ +/// Author : liuyubobobo +/// Time : 2023-03-25 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlog(LONG_LONG_MAX)) +/// Space Complexity: O(1) +class Solution { +public: + long long repairCars(vector& ranks, int cars) { + + long long l = 0, r = LONG_LONG_MAX; + while(l < r){ + long long mid = l + (r - l) / 2; + if(check(ranks, cars, mid)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool check(vector& ranks, int cars, long long time){ + + long long max_cars = 0; + for(int r: ranks){ + long long x = (long long)sqrt((long double)time / r); + max_cars += x; + } + return max_cars >= cars; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 7425d2e4..d151ac95 100644 --- a/readme.md +++ b/readme.md @@ -2481,6 +2481,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2588 | [Count the Number of Beautiful Subarrays](https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/) | [无] | [C++](2001-2500/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/) | | | | 2589 | [Minimum Time to Complete All Tasks](https://leetcode.com/problems/minimum-time-to-complete-all-tasks/) | [无] | [C++](2001-2500/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/) | | | | 2590 | [Design a Todo List](https://leetcode.com/problems/design-a-todo-list/description/) | [无] | [C++](2001-2500/2590-Design-a-Todo-List/cpp-2590/) | | | +| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children/) | [无] | [C++](2001-2500/2591-Distribute-Money-to-Maximum-Children/cpp-2591/) | | | +| 2592 | [Maximize Greatness of an Array](https://leetcode.com/problems/maximize-greatness-of-an-array/) | [无] | [C++](2001-2500/2592-Maximize-Greatness-of-an-Array/cpp-2592/) | | | +| 2593 | [Find Score of an Array After Marking All Elements](https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/) | [无] | [C++](2001-2500/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/) | | | +| 2594 | [Minimum Time to Repair Cars](https://leetcode.com/problems/minimum-time-to-repair-cars/) | [无] | [C++](2001-2500/2594-Minimum-Time-to-Repair-Cars/cpp-2594/) | | | | | | | | | | | 2599 | [Make the Prefix Sum Non-negative](https://leetcode.com/problems/make-the-prefix-sum-non-negative/description/) | [无] | [C++](2001-2500/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/) | | | | | | | | | | From 12ba88760896f64446a0e08c97e0a003dbf85251 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 26 Mar 2023 21:00:40 -0700 Subject: [PATCH 320/390] 2600-2603 solved. --- .../cpp-2600/CMakeLists.txt | 6 + .../cpp-2600/main.cpp | 37 ++++++ .../cpp-2601/CMakeLists.txt | 6 + .../cpp-2601/main.cpp | 66 ++++++++++ .../cpp-2602/CMakeLists.txt | 6 + .../cpp-2602/main.cpp | 48 ++++++++ .../cpp-2603/CMakeLists.txt | 6 + .../cpp-2603/main.cpp | 114 ++++++++++++++++++ readme.md | 4 + 9 files changed, 293 insertions(+) create mode 100644 2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/CMakeLists.txt create mode 100644 2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/main.cpp create mode 100644 2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/CMakeLists.txt create mode 100644 2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/main.cpp create mode 100644 2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/CMakeLists.txt create mode 100644 2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/main.cpp create mode 100644 2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/CMakeLists.txt create mode 100644 2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/main.cpp diff --git a/2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/CMakeLists.txt b/2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/CMakeLists.txt new file mode 100644 index 00000000..2b9df6f0 --- /dev/null +++ b/2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(A) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(A main.cpp) diff --git a/2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/main.cpp b/2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/main.cpp new file mode 100644 index 00000000..fe485d87 --- /dev/null +++ b/2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/ +/// Author : liuyubobobo +/// Time : 2023-03-26 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) { + + int res = 0; + + int t = min(numOnes, k); + res += t, k -= t; + + t = min(numZeros, k); + k -= t; + + t = min(numNegOnes, k); + k -= t, res -= t; + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/CMakeLists.txt b/2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/CMakeLists.txt new file mode 100644 index 00000000..f9dab828 --- /dev/null +++ b/2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(B) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(B main.cpp) diff --git a/2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/main.cpp b/2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/main.cpp new file mode 100644 index 00000000..47ca6df5 --- /dev/null +++ b/2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/prime-subtraction-operation/ +/// Author : liuyubobobo +/// Time : 2023-03-26 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(max_num * log(max_num) + n * log(max_num)) +/// Space Complexity: O(max_num) +class Solution { +public: + bool primeSubOperation(vector& nums) { + + vector primes = sieve(*max_element(nums.begin(), nums.end())); + sort(primes.begin(), primes.end(), greater()); + for(int prime: primes){ + if(prime < nums[0]){ + nums[0] -= prime; + break; + } + } + + for(int i = 1; i < nums.size(); i ++){ + for(int prime: primes){ + if(prime < nums[i] && nums[i] - prime > nums[i - 1]){ + nums[i] -= prime; + break; + } + } + if(nums[i] <= nums[i - 1]) return false; + } + return true; + } + +private: + vector sieve(int n){ + + vector table(n + 1, 0); + vector primes; + table[0] = table[1] = 1; + for(int i = 2; i <= n; i ++){ + if(table[i] == 0) { + primes.push_back(i); + table[i] = i; + } + for(int j = 0; j < primes.size() && primes[j] <= table[i] && i * primes[j] <= n; j ++) + table[i * primes[j]] = primes[j]; + } + return primes; + } +}; + + +int main() { + + vector nums1 = {998, 2}; + cout << Solution().primeSubOperation(nums1) << '\n'; + // 1 + + return 0; +} diff --git a/2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/CMakeLists.txt b/2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/CMakeLists.txt new file mode 100644 index 00000000..8b7526c5 --- /dev/null +++ b/2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(C) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(C main.cpp) diff --git a/2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/main.cpp b/2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/main.cpp new file mode 100644 index 00000000..cf814d21 --- /dev/null +++ b/2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/ +/// Author : liuyubobobo +/// Time : 2023-03-26 + +#include +#include +#include + +using namespace std; + + +/// Presum + Binary Search +/// Time Complexity: O(nlogn + qlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector minOperations(vector& nums, vector& queries) { + + sort(nums.begin(), nums.end()); + + int n = nums.size(); + vector presum(n + 1, 0); + for(int i = 1; i <= n; i++) + presum[i] = presum[i - 1] + nums[i - 1]; + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i++){ + int q = queries[i]; + + auto iter = lower_bound(nums.begin(), nums.end(), q); + int index = iter - nums.begin(); + + // [0... index - 1] < q; [index ... n - 1] >= q; + + long long tres = 0; + tres += (long long)q * index - presum[index]; + tres += presum[n] - presum[index] - (long long)q * (n - index); + res[i] = tres; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/CMakeLists.txt b/2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/CMakeLists.txt new file mode 100644 index 00000000..d2d08329 --- /dev/null +++ b/2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(D) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(D main.cpp) diff --git a/2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/main.cpp b/2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/main.cpp new file mode 100644 index 00000000..675d51da --- /dev/null +++ b/2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/main.cpp @@ -0,0 +1,114 @@ +/// Source : https://leetcode.com/problems/collect-coins-in-a-tree/description/ +/// Author : liuyubobobo +/// Time : 2023-03-26 + +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int collectTheCoins(vector& coins, vector>& edges) { + + int n = coins.size(); + vector> tree(n); + + for(const vector& edge : edges){ + int u = edge[0], v = edge[1]; + tree[u].push_back(v), tree[v].push_back(u); + } + + vector mask = prune(n, tree, coins); + + vector d(n, 0); + for(const vector& edge : edges){ + int u = edge[0], v = edge[1]; + if(mask[u] && mask[v]) d[u] ++, d[v] ++; + } + + queue q; + vector visited(n, false); + for(int i = 0; i < n; i++) + if(d[i] == 1) q.push(i), visited[i] = true; + + vector dis(n, 0); + while(!q.empty()){ + int u = q.front(); q.pop(); + for(int v: tree[u]){ + if(visited[v] || !mask[v]) continue; + if(coins[u] || dis[u]) dis[v] = max(dis[v], dis[u] + 1); + d[v] --; + if(d[v] == 1) q.push(v), visited[v] = true; + } + } + + int root = -1; + for(int i = 0; i < n; i++) + if(dis[i] >= 2){root = i; break;} + + return root == -1 ? 0 : dfs(tree, root, -1, dis); + } + +private: + int dfs(const vector>& tree, int u, int p, const vector& value){ + + int res = 0; + for(int v: tree[u]){ + if(v == p || value[v] < 2) continue; + res += 2 + dfs(tree, v, u, value); + } + + return res; + } + + vector prune(int n, vector>& tree, const vector& coins){ + + vector d(n, 0); + for(int i = 0; i < n; i++) d[i] = tree[i].size(); + + queue q; + for(int i = 0; i < n; i++) + if(d[i] == 1 && coins[i] == 0) q.push(i); + + vector mask(n, true); + while(!q.empty()){ + int u = q.front(); q.pop(); + mask[u] = false; + + for(int v: tree[u]){ + if(!mask[v] || coins[v]) continue; + d[v] --; + if(d[v] == 1) q.push(v); + } + } + return mask; + } +}; + + +int main() { + + vectorcoins1 = {1,0,0,0,0,1}; + vector> edges1 = {{0,1},{1,2},{2,3},{3,4},{4,5}}; + cout << Solution().collectTheCoins(coins1, edges1) << endl; + // 2 + + vector coins2 = {0,0,0,1,1,0,0,1}; + vector> edges2 = {{0,1},{0,2},{1,3},{1,4},{2,5},{5,6},{5,7}}; + cout << Solution().collectTheCoins(coins2, edges2) << endl; + // 2 + + vector coins3 = {1,0,0,1,0,0,1,0,0}; + vector> edges3 = {{0,1},{0,2},{0,3},{1,4},{2,5},{4,6},{2,7},{5,8}}; + cout << Solution().collectTheCoins(coins3, edges3) << endl; + // 0 + + return 0; +} diff --git a/readme.md b/readme.md index d151ac95..03bfa685 100644 --- a/readme.md +++ b/readme.md @@ -2487,6 +2487,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2594 | [Minimum Time to Repair Cars](https://leetcode.com/problems/minimum-time-to-repair-cars/) | [无] | [C++](2001-2500/2594-Minimum-Time-to-Repair-Cars/cpp-2594/) | | | | | | | | | | | 2599 | [Make the Prefix Sum Non-negative](https://leetcode.com/problems/make-the-prefix-sum-non-negative/description/) | [无] | [C++](2001-2500/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/) | | | +| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum/) | [无] | [C++](2001-2500/2600-K-Items-With-the-Maximum-Sum/cpp-2600/) | | | +| 2601 | [Prime Subtraction Operation](https://leetcode.com/problems/prime-subtraction-operation/) | [无] | [C++](2001-2500/2601-Prime-Subtraction-Operation/cpp-2601/) | | | +| 2602 | [Minimum Operations to Make All Array Elements Equal](https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/) | [无] | [C++](2001-2500/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/) | | | +| 2603 | [Collect Coins in a Tree](https://leetcode.com/problems/collect-coins-in-a-tree/description/) | [无] | [C++](2001-2500/2603-Collect-Coins-in-a-Tree/cpp-2603/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 18bf42c96d8431c99a47cc835bba4fd49a47c7fc Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 27 Mar 2023 00:18:05 -0700 Subject: [PATCH 321/390] 2595-2598 solved. --- .../cpp-2595/CMakeLists.txt | 6 ++ .../cpp-2595/main.cpp | 32 +++++++++++ .../cpp-2596/CMakeLists.txt | 6 ++ .../cpp-2596/main.cpp | 44 +++++++++++++++ .../cpp-2597/CMakeLists.txt | 6 ++ .../cpp-2597/main.cpp | 55 +++++++++++++++++++ .../cpp-2598/CMakeLists.txt | 6 ++ .../cpp-2598/main.cpp | 50 +++++++++++++++++ readme.md | 5 +- 9 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/CMakeLists.txt create mode 100644 2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/main.cpp create mode 100644 2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/CMakeLists.txt create mode 100644 2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/main.cpp create mode 100644 2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/CMakeLists.txt create mode 100644 2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/main.cpp create mode 100644 2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/CMakeLists.txt create mode 100644 2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/main.cpp diff --git a/2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/CMakeLists.txt b/2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/CMakeLists.txt new file mode 100644 index 00000000..ba302f4d --- /dev/null +++ b/2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2595) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2595 main.cpp) diff --git a/2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/main.cpp b/2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/main.cpp new file mode 100644 index 00000000..0f4ee993 --- /dev/null +++ b/2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/number-of-even-and-odd-bits/description/ +/// Author : liuyubobobo +/// Time : 2023-03-25 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(log(n)) +/// Space Complexity: O(1) +class Solution { +public: + vector evenOddBit(int n) { + + int even = 0, odd = 0; + for(int p = 0; p < 10; p ++){ + if((n >> p) & 1){ + if(p & 1) odd ++; else even ++; + } + } + return {even, odd}; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/CMakeLists.txt b/2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/CMakeLists.txt new file mode 100644 index 00000000..28665a8b --- /dev/null +++ b/2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2596) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2596 main.cpp) diff --git a/2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/main.cpp b/2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/main.cpp new file mode 100644 index 00000000..ffe425f4 --- /dev/null +++ b/2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/check-knight-tour-configuration/description/ +/// Author : liuyubobobo +/// Time : 2023-03-25 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { +public: + bool checkValidGrid(vector>& grid) { + + int n = grid.size(); + + vector x(n * n, -1), y(n * n, -1); + for(int i = 0; i < n; i ++) + for(int j = 0; j < n; j ++) x[grid[i][j]] = i, y[grid[i][j]] = j; + + if(x.back() == -1) return false; + if(x[0] || y[0]) return false; + + for(int i = 1; i < n * n; i ++) + if(!check(x[i - 1], y[i - 1], x[i], y[i])) return false; + return true; + } + +private: + bool check(int x1, int y1, int x2, int y2){ + int dx = abs(x1 - x2), dy = abs(y1 - y2); + return (dx == 1 && dy == 2) || (dx == 2 && dy == 1); + } +}; + + +int main() { + + + return 0; +} diff --git a/2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/CMakeLists.txt b/2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/CMakeLists.txt new file mode 100644 index 00000000..b148dea0 --- /dev/null +++ b/2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2597) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2597 main.cpp) diff --git a/2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/main.cpp b/2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/main.cpp new file mode 100644 index 00000000..d142307d --- /dev/null +++ b/2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/the-number-of-beautiful-subsets/description/ +/// Author : liuyubobobo +/// Time : 2023-03-26 + +#include +#include +#include +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(nlogn + n^2 + 2^n * n) +/// Space Complexity: O(n^2) +class Solution { +public: + int beautifulSubsets(vector& nums, int k) { + + int n = nums.size(); + sort(nums.begin(), nums.end()); + + vector> check(n); + for(int i = 0; i < n; i ++){ + for(int j = i + 1; j < n; j ++){ + if(nums[i] + k == nums[j]){ + check[i].push_back(j); + } + } + } + + int res = 0; + for(int state = 1; state < (1 << n); state ++){ + bool ok = true; + for(int i = 0; i < n && ok; i ++){ + if(((state >> i) & 1) && !check[i].empty()){ + for(int j: check[i]){ + if((state >> j) & 1){ + ok = false; + break; + } + } + } + } + res += ok; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/CMakeLists.txt b/2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/CMakeLists.txt new file mode 100644 index 00000000..38379239 --- /dev/null +++ b/2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2598) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2598 main.cpp) diff --git a/2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/main.cpp b/2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/main.cpp new file mode 100644 index 00000000..fff1fc14 --- /dev/null +++ b/2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/description/ +/// Author : liuyubobobo +/// Time : 2023-03-27 + +#include +#include +#include + +using namespace std; + + +/// Using Hash Table +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int findSmallestInteger(vector& nums, int value) { + + vector f(2e5, 0); + for(int e: nums){ + if(e >= 0){ + int k = e / value; f[e - k * value] ++; + } + else{ + int k = (-e) / value + !!((-e) % value); f[e + k * value] ++; + } + } + + for(int res = 0;; res ++){ + int k = res / value, t = res - k * value; + if(f[t]) f[t] --; + else return res; + } + return -1; + } +}; + + +int main() { + + vector nums1 = {1, -10, 7, 13, 6, 8}; + cout << Solution().findSmallestInteger(nums1, 5) << '\n'; + // 4 + + vector nums2 = {1, -10, 7, 13, 6, 8}; + cout << Solution().findSmallestInteger(nums2, 7) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 03bfa685..32fc33f8 100644 --- a/readme.md +++ b/readme.md @@ -2485,7 +2485,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2592 | [Maximize Greatness of an Array](https://leetcode.com/problems/maximize-greatness-of-an-array/) | [无] | [C++](2001-2500/2592-Maximize-Greatness-of-an-Array/cpp-2592/) | | | | 2593 | [Find Score of an Array After Marking All Elements](https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/) | [无] | [C++](2001-2500/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/) | | | | 2594 | [Minimum Time to Repair Cars](https://leetcode.com/problems/minimum-time-to-repair-cars/) | [无] | [C++](2001-2500/2594-Minimum-Time-to-Repair-Cars/cpp-2594/) | | | -| | | | | | | +| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits/) | [无] | [C++](2001-2500/2595-Number-of-Even-and-Odd-Bits/cpp-2595/) | | | +| 2596 | [Check Knight Tour Configuration](https://leetcode.com/problems/check-knight-tour-configuration/) | [无] | [C++](2001-2500/2596-Check-Knight-Tour-Configuration/cpp-2596/) | | | +| 2597 | [The Number of Beautiful Subsets](https://leetcode.com/problems/the-number-of-beautiful-subsets/) | [无] | [C++](2001-2500/2597-The-Number-of-Beautiful-Subsets/cpp-2597/) | | | +| 2598 | [Smallest Missing Non-negative Integer After Operations](https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/) | [无] | [C++](2001-2500/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/) | | | | 2599 | [Make the Prefix Sum Non-negative](https://leetcode.com/problems/make-the-prefix-sum-non-negative/description/) | [无] | [C++](2001-2500/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/) | | | | 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum/) | [无] | [C++](2001-2500/2600-K-Items-With-the-Maximum-Sum/cpp-2600/) | | | | 2601 | [Prime Subtraction Operation](https://leetcode.com/problems/prime-subtraction-operation/) | [无] | [C++](2001-2500/2601-Prime-Subtraction-Operation/cpp-2601/) | | | From 77d0775f4a0677745a9e72ea4678f1e93d2c65ba Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 31 Mar 2023 18:45:24 -0700 Subject: [PATCH 322/390] 0831 solved. --- .../cpp-0831/CMakeLists.txt | 6 +++ .../cpp-0831/main.cpp | 48 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0831-Masking-Personal-Information/cpp-0831/CMakeLists.txt create mode 100644 0501-1000/0831-Masking-Personal-Information/cpp-0831/main.cpp diff --git a/0501-1000/0831-Masking-Personal-Information/cpp-0831/CMakeLists.txt b/0501-1000/0831-Masking-Personal-Information/cpp-0831/CMakeLists.txt new file mode 100644 index 00000000..faae20b0 --- /dev/null +++ b/0501-1000/0831-Masking-Personal-Information/cpp-0831/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_0831) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0831 main.cpp) diff --git a/0501-1000/0831-Masking-Personal-Information/cpp-0831/main.cpp b/0501-1000/0831-Masking-Personal-Information/cpp-0831/main.cpp new file mode 100644 index 00000000..cb3af39b --- /dev/null +++ b/0501-1000/0831-Masking-Personal-Information/cpp-0831/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/masking-personal-information/ +/// Author : liuyubobobo +/// Time : 2023-03-31 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|s|) +/// Space Complexity: O(|s|) +class Solution { +public: + string maskPII(string s) { + + if(s.find('@') != string::npos) return solve_email(s); + return solve_phone(s); + } + +private: + string solve_phone(string& s){ + + string number; + for(char c: s) if(isdigit(c)) number += c; + + int n = number.size(); + + if(n == 10) return "***-***-" + number.substr(n - 4); + return "+" + string(n - 10, '*') + "-***-***-" + number.substr(n - 4); + } + + string solve_email(string& s){ + + for(char& c: s) if(isupper(c)) c = tolower(c); + + int pos = s.find('@'); + string name = s.substr(0, pos), domain = s.substr(pos + 1); + + return string(1, name[0]) + "*****" + string(1, name.back()) + "@" + domain; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 32fc33f8..188b9c89 100644 --- a/readme.md +++ b/readme.md @@ -834,7 +834,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 828 | [Count Unique Characters of All Substrings of a Given String](https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [无] | [C++](0501-1000/0828-Count-Unique-Characters-of-All-Substrings-of-a-Given-String/cpp-0828/) | | | | 829 | [Consecutive Numbers Sum](https://leetcode.com/problems/consecutive-numbers-sum/) | [无] | [C++](0501-1000/0829-Consecutive-Numbers-Sum/cpp-0829/) | | | | 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [solution](https://leetcode.com/problems/positions-of-large-groups/solution/) | [C++](0501-1000/0830-Positions-of-Large-Groups/cpp-0830/) | | | -| | | | | | | +| 831 | [Masking Personal Information](https://leetcode.com/problems/masking-personal-information/) | [无] | [C++](0501-1000/0831-Masking-Personal-Information/cpp-0831/) | | | | 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [solution](https://leetcode.com/problems/flipping-an-image/solution/) | [C++](0501-1000/0832-Flipping-an-Image/cpp-0832/) | | | | | | | | | | | 834 | [Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/) | [solution](https://leetcode.com/problems/sum-of-distances-in-tree/solution/) | [C++](0501-1000/0834-Sum-of-Distances-in-Tree/cpp-0834/) | | | From 38eb0d9aee2df5ae8143f599f5cbd47fa2062d5f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Apr 2023 21:45:41 -0700 Subject: [PATCH 323/390] 1053 solved. --- .../cpp-1053/CMakeLists.txt | 6 +++ .../cpp-1053/main.cpp | 42 +++++++++++++++++++ readme.md | 1 + 3 files changed, 49 insertions(+) create mode 100644 1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/CMakeLists.txt create mode 100644 1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/main.cpp diff --git a/1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/CMakeLists.txt b/1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/CMakeLists.txt new file mode 100644 index 00000000..41d3cd9c --- /dev/null +++ b/1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_1053) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1053 main.cpp) diff --git a/1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/main.cpp b/1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/main.cpp new file mode 100644 index 00000000..867973c2 --- /dev/null +++ b/1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/previous-permutation-with-one-swap/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector prevPermOpt1(vector& arr) { + + if(is_sorted(arr.begin(), arr.end())) return arr; + + int n = arr.size(); + for(int i = n - 1; i > 0; i --) + if(arr[i - 1] > arr[i]){ + auto iter = lower_bound(arr.begin() + i, arr.end(), arr[i - 1]); + iter --; + + int index = iter - arr.begin(); + int t = *iter; + while(arr[index - 1] == t) index --; + + swap(arr[i - 1], arr[index]); + break; + } + return arr; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 188b9c89..a99ef44b 100644 --- a/readme.md +++ b/readme.md @@ -1054,6 +1054,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1050 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 1051 | [Height Checker](https://leetcode.com/problems/height-checker/) | [无] | [C++](1001-1500/1051-Height-Checker/cpp-1051/) | | | | 1052 | [Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner/) | [无] | [C++](1001-1500/1052-Grumpy-Bookstore-Owner/cpp-1052/) | | | +| 1053 | [Previous Permutation With One Swap](https://leetcode.com/problems/previous-permutation-with-one-swap/description/) | [无] | [C++](1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/) | | | | | | | | | | | 1055 | [Shortest Way to Form String](https://leetcode.com/problems/shortest-way-to-form-string/description/) | [无] | [C++](1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/) | | | | 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number/description/) | [无] | [C++](1001-1500/1056-Confusing-Number/cpp-1056/) | | | From 39a94140eba73c68363665beb925f985d18ae83d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Apr 2023 22:14:09 -0700 Subject: [PATCH 324/390] 2609-2612 solved. --- .../cpp-2609/CMakeLists.txt | 6 ++ .../cpp-2609/main.cpp | 40 +++++++++++ .../cpp-2610/CMakeLists.txt | 6 ++ .../cpp-2610/main.cpp | 39 +++++++++++ .../cpp-2611/CMakeLists.txt | 6 ++ .../2611-Mice-and-Cheese/cpp-2611/main.cpp | 40 +++++++++++ .../cpp-2612/CMakeLists.txt | 6 ++ .../cpp-2612/main.cpp | 69 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 217 insertions(+) create mode 100644 2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/CMakeLists.txt create mode 100644 2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/main.cpp create mode 100644 2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/CMakeLists.txt create mode 100644 2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/main.cpp create mode 100644 2501-3000/2611-Mice-and-Cheese/cpp-2611/CMakeLists.txt create mode 100644 2501-3000/2611-Mice-and-Cheese/cpp-2611/main.cpp create mode 100644 2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/CMakeLists.txt create mode 100644 2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/main.cpp diff --git a/2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/CMakeLists.txt b/2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/CMakeLists.txt new file mode 100644 index 00000000..233d8d7b --- /dev/null +++ b/2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2609) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2609 main.cpp) diff --git a/2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/main.cpp b/2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/main.cpp new file mode 100644 index 00000000..d6bed278 --- /dev/null +++ b/2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include + +using namespace std; + + +/// String split +/// Time Complexity: O(n) +/// Space Compelxity: O(n) +class Solution { +public: + int findTheLongestBalancedSubstring(string s) { + + int n = s.size(); + + vector> segs; + for(int start = 0, i = 1; i <= n; i ++){ + if(i == n || s[i] != s[start]){ + segs.push_back({s[start] - '0', i - start}); + start = i; + } + } + + int res = 0; + for(int i = 0; i + 1 < (int)segs.size(); i ++) + if(segs[i].first == 0 && segs[i + 1].first == 1) + res = max(res, 2 * min(segs[i].second, segs[i + 1].second)); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/CMakeLists.txt b/2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/CMakeLists.txt new file mode 100644 index 00000000..621f8ed3 --- /dev/null +++ b/2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2610) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2610 main.cpp) diff --git a/2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/main.cpp b/2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/main.cpp new file mode 100644 index 00000000..76f3660a --- /dev/null +++ b/2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(max_num + max_f) +class Solution { +public: + vector> findMatrix(vector& nums) { + + int max_num = *max_element(nums.begin(), nums.end()); + + vector f(max_num + 1, 0); + for(int e: nums) f[e] ++; + + int maxf = *max_element(f.begin(), f.end()); + + vector> res(maxf); + for(int e = 0; e <= max_num; e ++){ + if(!f[e]) continue; + for(int i = 0; i < f[e]; i ++) res[i].push_back(e); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2611-Mice-and-Cheese/cpp-2611/CMakeLists.txt b/2501-3000/2611-Mice-and-Cheese/cpp-2611/CMakeLists.txt new file mode 100644 index 00000000..eb6c2351 --- /dev/null +++ b/2501-3000/2611-Mice-and-Cheese/cpp-2611/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2611) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2611 main.cpp) diff --git a/2501-3000/2611-Mice-and-Cheese/cpp-2611/main.cpp b/2501-3000/2611-Mice-and-Cheese/cpp-2611/main.cpp new file mode 100644 index 00000000..e27355b5 --- /dev/null +++ b/2501-3000/2611-Mice-and-Cheese/cpp-2611/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/mice-and-cheese/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(nlogn + klogn) +/// Space Complexity: O(n) +class Solution { +public: + int miceAndCheese(vector& reward1, vector& reward2, int k) { + + int res = accumulate(reward2.begin(), reward2.end(), 0); + + int n = reward1.size(); + priority_queue pq; + for(int i = 0; i < n; i++) + pq.push(reward1[i] - reward2[i]); + + for(int i = 0; i < k; i++) { + int t = pq.top(); pq.pop(); + res += t; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/CMakeLists.txt b/2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/CMakeLists.txt new file mode 100644 index 00000000..4cb2739b --- /dev/null +++ b/2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2612) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2612 main.cpp) diff --git a/2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/main.cpp b/2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/main.cpp new file mode 100644 index 00000000..9a982231 --- /dev/null +++ b/2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/main.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.com/problems/minimum-reverse-operations/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector minReverseOperations(int n, int p, const vector& banned, int k) { + + vector ok(n, true); + for(int ban: banned) ok[ban] = false; + + set available[2]; + for(int i = 0; i < n; ++i) if(ok[i]) available[i & 1].insert(i); + vector res(n, -1); + + queue> q; + q.push({p, 0}); + res[p] = 0; + available[p & 1].erase(p); + while(!q.empty()){ + int pos = q.front().first, step = q.front().second; + q.pop(); + + int start = max(pos - k + 1, k - pos - 1); + int end = min(pos + k - 1, 2 * n - k -pos - 1); + set& s = available[start & 1]; + for(auto iter = s.lower_bound(start); iter != s.end() && *iter <= end; iter = s.erase(iter)){ + res[*iter] = step + 1; + q.push({*iter, step + 1}); + } + } + + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; + cout << '\n'; +} + +int main() { + + vector banned1 = {}; + print_vec(Solution().minReverseOperations(4, 2, banned1, 4)); + // -1 1 0 1 + + vector banned2 = {}; + print_vec(Solution().minReverseOperations(5, 0, banned2, 4)); + // 0 3 2 1 4 + + vector banned3 = {}; + print_vec(Solution().minReverseOperations(3, 2, banned3, 1)); + // -1 -1 0 + + return 0; +} diff --git a/readme.md b/readme.md index a99ef44b..e827456e 100644 --- a/readme.md +++ b/readme.md @@ -2496,6 +2496,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2602 | [Minimum Operations to Make All Array Elements Equal](https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/) | [无] | [C++](2001-2500/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/) | | | | 2603 | [Collect Coins in a Tree](https://leetcode.com/problems/collect-coins-in-a-tree/description/) | [无] | [C++](2001-2500/2603-Collect-Coins-in-a-Tree/cpp-2603/) | | | | | | | | | | +| 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/) | [无] | [C++](2001-2500/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/) | | | +| 2610 | [Convert an Array Into a 2D Array With Conditions](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/) | [无] | [C++](2001-2500/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/) | | | +| 2611 | [Mice and Cheese](https://leetcode.com/problems/mice-and-cheese/) | [无] | [C++](2001-2500/2611-Mice-and-Cheese/cpp-2611/) | | | +| 2612 | [Minimum Reverse Operations](https://leetcode.com/problems/minimum-reverse-operations/) | [无] | [C++](2001-2500/2612-Minimum-Reverse-Operations/cpp-2612/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From a5a62eebf7656cb83785d86a38141df85458a67f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Apr 2023 23:26:53 -0700 Subject: [PATCH 325/390] 2605-2608 solved. --- .../cpp-2605/CMakeLists.txt | 6 ++ .../cpp-2605/main.cpp | 39 +++++++++++ .../cpp-2606/CMakeLists.txt | 6 ++ .../cpp-2606/main.cpp | 37 +++++++++++ .../cpp-2607/CMakeLists.txt | 6 ++ .../cpp-2607/main.cpp | 43 ++++++++++++ .../cpp-2608/CMakeLists.txt | 6 ++ .../cpp-2608/main.cpp | 65 +++++++++++++++++++ .../cpp-2608/main2.cpp | 64 ++++++++++++++++++ readme.md | 4 ++ 10 files changed, 276 insertions(+) create mode 100644 2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/CMakeLists.txt create mode 100644 2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/main.cpp create mode 100644 2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/CMakeLists.txt create mode 100644 2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/main.cpp create mode 100644 2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/CMakeLists.txt create mode 100644 2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/main.cpp create mode 100644 2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt create mode 100644 2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp create mode 100644 2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp diff --git a/2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/CMakeLists.txt b/2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/CMakeLists.txt new file mode 100644 index 00000000..9c829faa --- /dev/null +++ b/2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2605) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2605 main.cpp) diff --git a/2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/main.cpp b/2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/main.cpp new file mode 100644 index 00000000..e11a4950 --- /dev/null +++ b/2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(100) +/// Space Complexity: O(1) +class Solution { +public: + int minNumber(vector& nums1, vector& nums2) { + + for(int i = 1; i < 100; i ++){ + int x = i, state = 0; + while(x){ + int d = x % 10; + if(find(nums1.begin(), nums1.end(),d) != nums1.end()) + state |= 1; + if(find(nums2.begin(), nums2.end(),d) != nums2.end()) + state |= 2; + x /= 10; + } + if(state == 3) + return i; + } + return -1; + } +}; + +int main() { + + return 0; +} diff --git a/2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/CMakeLists.txt b/2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/CMakeLists.txt new file mode 100644 index 00000000..6b8391c1 --- /dev/null +++ b/2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2606) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2606 main.cpp) diff --git a/2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/main.cpp b/2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/main.cpp new file mode 100644 index 00000000..409d3d48 --- /dev/null +++ b/2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/find-the-substring-with-maximum-cost/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int maximumCostSubstring(string s, string chars, vector& vals) { + + vector costs(26, 0); + for (int i = 0; i < 26; i ++) costs[i] = i + 1; + for(int i = 0; i < chars.size(); i ++) costs[chars[i] - 'a'] = vals[i]; + + int res = 0, min_pre = 0, pre = 0; + for(int i = 0; i < s.size(); i ++) { + pre += costs[s[i] - 'a']; + min_pre = min(min_pre, pre); + res = max(res, pre - min_pre); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/CMakeLists.txt b/2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/CMakeLists.txt new file mode 100644 index 00000000..7f1340cc --- /dev/null +++ b/2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2607) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2607 main.cpp) diff --git a/2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/main.cpp b/2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/main.cpp new file mode 100644 index 00000000..d06aac51 --- /dev/null +++ b/2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/make-k-subarray-sums-equal/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long makeSubKSumEqual(vector& arr, int k) { + + int n = arr.size(); + + vector visited(n, false); + long long res = 0; + for(int i = 0; i < n; i ++){ + if(visited[i]) continue; + + vector v; + for(int j = i; !visited[j]; j = (j + k) % n) + v.push_back(arr[j]), visited[j] = true; + + sort(v.begin(), v.end()); + int t = v[v.size() / 2]; + for(int j = 0; j < v.size(); j ++) + res += abs(v[j] - t); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt new file mode 100644 index 00000000..77f1de78 --- /dev/null +++ b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2608) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2608 main2.cpp) diff --git a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp new file mode 100644 index 00000000..712caa0b --- /dev/null +++ b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp @@ -0,0 +1,65 @@ +#include +#include +#include + +using namespace std; + + +class Solution { +public: + int findShortestCycle(int n, vector>& edges) { + + vector> g(n); + for(const vector& e : edges){ + int u = e[0], v = e[1]; + g[u].push_back(v), g[v].push_back(u); + } + + int res = INT_MAX; + vector in_cycle(n, false); + for(int i = 0; i < n; i ++){ + if(in_cycle[i]) continue; + + vector path; + vector visited(n, false); + res = min(res, dfs(g, i, -1, path, visited, in_cycle)); + } + return res == INT_MAX ? -1 : res; + } + +private: + int dfs(const vector>& g, int u, int p, + vector& path, vector& visited, vector& in_cycle){ + + visited[u] = true; + path.push_back(u); + + int res = INT_MAX; + for(int v : g[u]){ + if(!visited[v]) + res = min(res, dfs(g, v, u, path, visited, in_cycle)); + else if(v != p){ + int len = 0; + for(int i = path.size() - 1; i >= 0; i --){ + len ++; + in_cycle[path[i]] = true; + if(path[i] == v) break; + } + res = min(res, len); + } + } + visited[u] = false; + path.pop_back(); + return res; + } +}; + + +int main() { + + vector> edges1 = {{2,1},{0,1},{4,1},{3,0},{1,3}}; + cout << Solution().findShortestCycle(5, edges1) << '\n'; + // 3 + + return 0; +} diff --git a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp new file mode 100644 index 00000000..8321eedb --- /dev/null +++ b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/shortest-cycle-in-a-graph/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int findShortestCycle(int n, vector>& edges) { + + vector> g(n); + for(const vector& e : edges){ + int u = e[0], v = e[1]; + g[u].push_back(v), g[v].push_back(u); + } + + int res = INT_MAX; + for(const vector& e: edges){ + int u = e[0], v = e[1]; + g[u].remove(v), g[v].remove(u); + + vector dis = bfs(n, g, u); + if(dis[v] != INT_MAX) res = min(res, dis[v] + 1); + } + return res == INT_MAX ? -1 : res; + } + +private: + vector bfs(int n, const vector>& g, int s){ + + vector dis(n, INT_MAX); + queue q; + q.push(s), dis[s] = 0; + + while(!q.empty()){ + int u = q.front(), d = dis[u]; q.pop(); + for(int v: g[u]){ + if(dis[v] != INT_MAX) continue; + dis[v] = dis[u] + 1; + q.push(v); + } + } + return dis; + } +}; + + +int main() { + + vector> edges1 = {{2,1},{0,1},{4,1},{3,0},{1,3}}; + cout << Solution().findShortestCycle(5, edges1) << '\n'; + // 3 + + return 0; +} diff --git a/readme.md b/readme.md index e827456e..141ffdb6 100644 --- a/readme.md +++ b/readme.md @@ -2496,6 +2496,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2602 | [Minimum Operations to Make All Array Elements Equal](https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/) | [无] | [C++](2001-2500/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/) | | | | 2603 | [Collect Coins in a Tree](https://leetcode.com/problems/collect-coins-in-a-tree/description/) | [无] | [C++](2001-2500/2603-Collect-Coins-in-a-Tree/cpp-2603/) | | | | | | | | | | +| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays/) | [无] | [C++](2001-2500/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/) | | | +| 2606 | [Find the Substring With Maximum Cost](https://leetcode.com/problems/find-the-substring-with-maximum-cost/) | [无] | [C++](2001-2500/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/) | | | +| 2607 | [Make K-Subarray Sums Equal](https://leetcode.com/problems/make-k-subarray-sums-equal/) | [无] | [C++](2001-2500/2607-Make-K-Subarray-Sums-Equal/cpp-2607/) | | | +| 2608 | [Shortest Cycle in a Graph](https://leetcode.com/problems/shortest-cycle-in-a-graph/) | [无] | [C++](2001-2500/2608-Shortest-Cycle-in-a-Graph/cpp-2608/) | | | | 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/) | [无] | [C++](2001-2500/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/) | | | | 2610 | [Convert an Array Into a 2D Array With Conditions](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/) | [无] | [C++](2001-2500/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/) | | | | 2611 | [Mice and Cheese](https://leetcode.com/problems/mice-and-cheese/) | [无] | [C++](2001-2500/2611-Mice-and-Cheese/cpp-2611/) | | | From e5176a3c23eb3a3646394855190cbe20893ac1e6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Apr 2023 23:27:39 -0700 Subject: [PATCH 326/390] 2608 code updated. --- .../cpp-2608/CMakeLists.txt | 2 +- .../cpp-2608/main.cpp | 49 +++++++------- .../cpp-2608/main2.cpp | 64 ------------------- 3 files changed, 25 insertions(+), 90 deletions(-) delete mode 100644 2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp diff --git a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt index 77f1de78..ec49765a 100644 --- a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt +++ b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_2608) set(CMAKE_CXX_STANDARD 17) -add_executable(cpp_2608 main2.cpp) +add_executable(cpp_2608 main.cpp) diff --git a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp index 712caa0b..8321eedb 100644 --- a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp +++ b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp @@ -1,10 +1,18 @@ +/// Source : https://leetcode.com/problems/shortest-cycle-in-a-graph/description/ +/// Author : liuyubobobo +/// Time : 2023-04-02 + #include #include #include +#include using namespace std; +/// BFS +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) class Solution { public: int findShortestCycle(int n, vector>& edges) { @@ -16,41 +24,32 @@ class Solution { } int res = INT_MAX; - vector in_cycle(n, false); - for(int i = 0; i < n; i ++){ - if(in_cycle[i]) continue; + for(const vector& e: edges){ + int u = e[0], v = e[1]; + g[u].remove(v), g[v].remove(u); - vector path; - vector visited(n, false); - res = min(res, dfs(g, i, -1, path, visited, in_cycle)); + vector dis = bfs(n, g, u); + if(dis[v] != INT_MAX) res = min(res, dis[v] + 1); } return res == INT_MAX ? -1 : res; } private: - int dfs(const vector>& g, int u, int p, - vector& path, vector& visited, vector& in_cycle){ + vector bfs(int n, const vector>& g, int s){ - visited[u] = true; - path.push_back(u); + vector dis(n, INT_MAX); + queue q; + q.push(s), dis[s] = 0; - int res = INT_MAX; - for(int v : g[u]){ - if(!visited[v]) - res = min(res, dfs(g, v, u, path, visited, in_cycle)); - else if(v != p){ - int len = 0; - for(int i = path.size() - 1; i >= 0; i --){ - len ++; - in_cycle[path[i]] = true; - if(path[i] == v) break; - } - res = min(res, len); + while(!q.empty()){ + int u = q.front(), d = dis[u]; q.pop(); + for(int v: g[u]){ + if(dis[v] != INT_MAX) continue; + dis[v] = dis[u] + 1; + q.push(v); } } - visited[u] = false; - path.pop_back(); - return res; + return dis; } }; diff --git a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp deleted file mode 100644 index 8321eedb..00000000 --- a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main2.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/// Source : https://leetcode.com/problems/shortest-cycle-in-a-graph/description/ -/// Author : liuyubobobo -/// Time : 2023-04-02 - -#include -#include -#include -#include - -using namespace std; - - -/// BFS -/// Time Complexity: O(n^2) -/// Space Complexity: O(n) -class Solution { -public: - int findShortestCycle(int n, vector>& edges) { - - vector> g(n); - for(const vector& e : edges){ - int u = e[0], v = e[1]; - g[u].push_back(v), g[v].push_back(u); - } - - int res = INT_MAX; - for(const vector& e: edges){ - int u = e[0], v = e[1]; - g[u].remove(v), g[v].remove(u); - - vector dis = bfs(n, g, u); - if(dis[v] != INT_MAX) res = min(res, dis[v] + 1); - } - return res == INT_MAX ? -1 : res; - } - -private: - vector bfs(int n, const vector>& g, int s){ - - vector dis(n, INT_MAX); - queue q; - q.push(s), dis[s] = 0; - - while(!q.empty()){ - int u = q.front(), d = dis[u]; q.pop(); - for(int v: g[u]){ - if(dis[v] != INT_MAX) continue; - dis[v] = dis[u] + 1; - q.push(v); - } - } - return dis; - } -}; - - -int main() { - - vector> edges1 = {{2,1},{0,1},{4,1},{3,0},{1,3}}; - cout << Solution().findShortestCycle(5, edges1) << '\n'; - // 3 - - return 0; -} From f3c30110621a32849e0026e324fc1c02c3e880db Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 2 Apr 2023 23:31:10 -0700 Subject: [PATCH 327/390] 2608 codes updated. --- 2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp index 8321eedb..6506d0b0 100644 --- a/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp +++ b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp @@ -30,6 +30,8 @@ class Solution { vector dis = bfs(n, g, u); if(dis[v] != INT_MAX) res = min(res, dis[v] + 1); + + g[u].push_back(v), g[v].push_back(u); } return res == INT_MAX ? -1 : res; } From 33c134aa1e6127f8b70189d5fde65b20c839ceab Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 3 Apr 2023 00:19:20 -0700 Subject: [PATCH 328/390] 2604 solved. --- .../cpp-2604/CMakeLists.txt | 6 ++ .../cpp-2604/main.cpp | 67 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/CMakeLists.txt create mode 100644 2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/main.cpp diff --git a/2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/CMakeLists.txt b/2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/CMakeLists.txt new file mode 100644 index 00000000..f327652d --- /dev/null +++ b/2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2604) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2604 main.cpp) diff --git a/2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/main.cpp b/2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/main.cpp new file mode 100644 index 00000000..082c78de --- /dev/null +++ b/2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-eat-all-grains/description/ +/// Author : liuyubobobo +/// Time : 2023-04-03 + +#include +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogmlog(INT_MAX)) +/// Space Complexity: O(n + m) +class Solution { +public: + int minimumTime(vector& hens, vector& grains) { + + sort(hens.begin(), hens.end()); + sort(grains.begin(), grains.end()); + + int l = 0, r = INT_MAX; + while(l < r){ + int mid = l + (r - l) / 2; + if(check(hens, grains, mid)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool check(const vector& hens, const vector& grains, int k){ + + int gi = 0; + for(int hen: hens){ + + if(grains[gi] < hen){ + if(hen - grains[gi] > k) return false; + gi = get_max_gi(grains, gi, hen, k); + } + else{ + gi = upper_bound(grains.begin(), grains.end(), hen + k) - grains.begin(); + } + + if(gi >= grains.size()) break; + } + return gi == grains.size(); + } + + int get_max_gi(const vector& grains, int gi, int hen, int k){ + + assert(hen > grains[gi]); + int gi1 = upper_bound(grains.begin(), grains.end(), hen + (k - 2 * (hen - grains[gi]))) - grains.begin(); + int gi2 = upper_bound(grains.begin(), grains.end(), hen + (k - (hen - grains[gi])) / 2) - grains.begin(); + return max(gi1, gi2); + } +}; + + +int main() { + + vector hen1 = {0}, grain1 = {1000000000}; + cout << Solution().minimumTime(hen1, grain1) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index 141ffdb6..8a993e01 100644 --- a/readme.md +++ b/readme.md @@ -2495,7 +2495,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2601 | [Prime Subtraction Operation](https://leetcode.com/problems/prime-subtraction-operation/) | [无] | [C++](2001-2500/2601-Prime-Subtraction-Operation/cpp-2601/) | | | | 2602 | [Minimum Operations to Make All Array Elements Equal](https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/) | [无] | [C++](2001-2500/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/) | | | | 2603 | [Collect Coins in a Tree](https://leetcode.com/problems/collect-coins-in-a-tree/description/) | [无] | [C++](2001-2500/2603-Collect-Coins-in-a-Tree/cpp-2603/) | | | -| | | | | | | +| 2604 | [Minimum Time to Eat All Grains](https://leetcode.com/problems/minimum-time-to-eat-all-grains/description/) | [无] | [C++](2001-2500/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/) | | | | 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays/) | [无] | [C++](2001-2500/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/) | | | | 2606 | [Find the Substring With Maximum Cost](https://leetcode.com/problems/find-the-substring-with-maximum-cost/) | [无] | [C++](2001-2500/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/) | | | | 2607 | [Make K-Subarray Sums Equal](https://leetcode.com/problems/make-k-subarray-sums-equal/) | [无] | [C++](2001-2500/2607-Make-K-Subarray-Sums-Equal/cpp-2607/) | | | From 552e6a82319ff5b30439256197446bc1be632607 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 3 Apr 2023 00:42:28 -0700 Subject: [PATCH 329/390] 2574-2577 solved. --- .../cpp-2574/CMakeLists.txt | 6 ++ .../cpp-2574/main.cpp | 36 +++++++++ .../cpp-2575/CMakeLists.txt | 6 ++ .../cpp-2575/main.cpp | 33 ++++++++ .../cpp-2576/CMakeLists.txt | 6 ++ .../cpp-2576/main.cpp | 37 +++++++++ .../cpp-2577/CMakeLists.txt | 6 ++ .../cpp-2577/main.cpp | 78 +++++++++++++++++++ readme.md | 5 +- 9 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/CMakeLists.txt create mode 100644 2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/main.cpp create mode 100644 2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/CMakeLists.txt create mode 100644 2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/main.cpp create mode 100644 2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/CMakeLists.txt create mode 100644 2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/main.cpp create mode 100644 2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/CMakeLists.txt create mode 100644 2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/main.cpp diff --git a/2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/CMakeLists.txt b/2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/CMakeLists.txt new file mode 100644 index 00000000..feee6947 --- /dev/null +++ b/2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2574) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2574 main.cpp) diff --git a/2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/main.cpp b/2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/main.cpp new file mode 100644 index 00000000..525c41cc --- /dev/null +++ b/2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/left-and-right-sum-differences/description/ +/// Author : liuyubobobo +/// Time : 2023-02-27 + +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector leftRigthDifference(vector& nums) { + + int n = nums.size(); + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + nums[i]; + + vector res(n, 0); + for(int i = 0; i < n; i ++){ + int left = presum[i]; + int right = presum[n] - presum[i + 1]; + res[i] = abs(right - left); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/CMakeLists.txt b/2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/CMakeLists.txt new file mode 100644 index 00000000..5fa22d76 --- /dev/null +++ b/2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2575) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2575 main.cpp) diff --git a/2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/main.cpp b/2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/main.cpp new file mode 100644 index 00000000..1e1d99fb --- /dev/null +++ b/2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/find-the-divisibility-array-of-a-string/description/ +/// Author : liuyubobobo +/// Time : 2023-02-27 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector divisibilityArray(string word, long long m) { + + int n = word.size(); + vector res(n, 0); + long long cur = 0ll; + for(int i = 0; i < n; i++){ + cur = (cur * 10 + word[i] - '0') % m; + res[i] = cur == 0; + } + return res; + } +}; + + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/CMakeLists.txt b/2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/CMakeLists.txt new file mode 100644 index 00000000..5e5be77e --- /dev/null +++ b/2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2576) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2576 main.cpp) diff --git a/2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/main.cpp b/2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/main.cpp new file mode 100644 index 00000000..7c2864cb --- /dev/null +++ b/2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/find-the-maximum-number-of-marked-indices/description/ +/// Author : liuyubobobo +/// Time : 2023-02-27 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int maxNumOfMarkedIndices(vector& nums) { + + sort(nums.begin(), nums.end()); + int n = nums.size(), res = 0; + for(int i = 0, j = n / 2; i < n / 2 && j < n; i ++){ + while(j < n && nums[i] * 2 > nums[j]) j ++; + if(j < n) res += 2, j ++; + } + return res; + } +}; + + +int main() { + + vector nums1 = {42,83,48,10,24,55,9,100,10,17,17,99,51,32,16,98,99,31,28,68,71,14,64,29,15,40}; + cout << Solution().maxNumOfMarkedIndices(nums1) << '\n'; + // 26 + + return 0; +} diff --git a/2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/CMakeLists.txt b/2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/CMakeLists.txt new file mode 100644 index 00000000..cd668e00 --- /dev/null +++ b/2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.24) +project(cpp_2577) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2577 main.cpp) diff --git a/2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/main.cpp b/2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/main.cpp new file mode 100644 index 00000000..a4677608 --- /dev/null +++ b/2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/main.cpp @@ -0,0 +1,78 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/description/ +/// Author : liuyubobobo +/// Time : 2023-04-03 + +#include +#include +#include +#include + +using namespace std; + + +/// Dij +/// Time Complexity: O(R * C * log(R * C)) +/// Space Complexity: O(R * C) +class Solution { + +private: + const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + const int INF = INT_MAX / 2; + int R, C; + +public: + int minimumTime(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + + bool can_loop = false; + if(grid[0][1] <= 1 || grid[1][0] <= 1) can_loop = true; + + vector> dis(R, vector(C, INF)); + vector> visited(R, vector(C, false)); + + priority_queue, vector>, greater>> pq; + dis[0][0] = 0; + pq.push({0, 0}); + while(!pq.empty()){ + int d = pq.top().first, cx = pq.top().second / C, cy = pq.top().second % C; + pq.pop(); + + if(visited[cx][cy]) continue; + visited[cx][cy] = true; + + for(int i = 0; i < 4; i ++){ + int nx = cx + dirs[i][0], ny = cy + dirs[i][1]; + if(in_area(nx, ny) && !visited[nx][ny]){ + int nd = d + 1; + if(can_loop && nd < grid[nx][ny]) nd = grid[nx][ny] + (grid[nx][ny] % 2 != nd % 2); + if(nd < grid[nx][ny]) continue; + if(nd < dis[nx][ny]){ + dis[nx][ny] = nd; + pq.push({nd, nx * C + ny}); + } + } + } + } + return dis[R - 1][C - 1] == INF ? -1 : dis[R - 1][C - 1]; + } + +private: + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> grid1 = { + {0, 1, 3, 2}, + {5, 1, 2, 5}, + {4, 3, 8, 6} + }; + cout << Solution().minimumTime(grid1) << '\n'; + // 7 + + return 0; +} diff --git a/readme.md b/readme.md index 8a993e01..bc6fe250 100644 --- a/readme.md +++ b/readme.md @@ -2468,7 +2468,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2571 | [Minimum Operations to Reduce an Integer to 0](https://leetcode.com/problems/minimum-operations-to-reduce-an-integer-to-0/) | [无] | [C++](2001-2500/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/) | | | | 2572 | [Count the Number of Square-Free Subsets](https://leetcode.com/problems/count-the-number-of-square-free-subsets/) | [无] | [C++](2001-2500/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/) | | | | 2573 | [Find the String with LCP](https://leetcode.com/problems/find-the-string-with-lcp/) | [无] | [C++](2001-2500/2573-Find-the-String-with-LCP/cpp-2573/) | | | -| | | | | | | +| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences/) | [无] | [C++](2001-2500/2574-Left-and-Right-Sum-Differences/cpp-2574/) | | | +| 2575 | [Find the Divisibility Array of a String](https://leetcode.com/problems/find-the-divisibility-array-of-a-string/) | [无] | [C++](2001-2500/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/) | | | +| 2576 | [Find the Maximum Number of Marked Indices](https://leetcode.com/problems/find-the-maximum-number-of-marked-indices/) | [无] | [C++](2001-2500/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/) | | | +| 2577 | [Minimum Time to Visit a Cell In a Grid](https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/) | [无] | [C++](2001-2500/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/) | | | | 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum/) | [无] | [C++](2001-2500/2578-Split-With-Minimum-Sum/cpp-2578/) | | | | 2579 | [Count Total Number of Colored Cells](https://leetcode.com/problems/count-total-number-of-colored-cells/) | [无] | [C++](2001-2500/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/) | | | | 2580 | [Count Ways to Group Overlapping Ranges](https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/) | [无] | [C++](2001-2500/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/) | | | From 68bd801865e27b15d9309240411f2ea448e2d499 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 6 Apr 2023 14:58:01 -0700 Subject: [PATCH 330/390] 1254 solved. --- .../cpp-1254/CMakeLists.txt | 6 ++ .../cpp-1254/main.cpp | 59 +++++++++++++++++++ readme.md | 1 + 3 files changed, 66 insertions(+) create mode 100644 1001-1500/1254-Number-of-Closed-Islands/cpp-1254/CMakeLists.txt create mode 100644 1001-1500/1254-Number-of-Closed-Islands/cpp-1254/main.cpp diff --git a/1001-1500/1254-Number-of-Closed-Islands/cpp-1254/CMakeLists.txt b/1001-1500/1254-Number-of-Closed-Islands/cpp-1254/CMakeLists.txt new file mode 100644 index 00000000..6eaa78e4 --- /dev/null +++ b/1001-1500/1254-Number-of-Closed-Islands/cpp-1254/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1254) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1254 main.cpp) diff --git a/1001-1500/1254-Number-of-Closed-Islands/cpp-1254/main.cpp b/1001-1500/1254-Number-of-Closed-Islands/cpp-1254/main.cpp new file mode 100644 index 00000000..0e465ba6 --- /dev/null +++ b/1001-1500/1254-Number-of-Closed-Islands/cpp-1254/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/number-of-closed-islands/description/ +/// Author : liuyubobobo +/// Time : 2023-04-06 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(m * n) +/// Space Complexity: O(m * n) +class Solution { + +private: + int R, C; + const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + +public: + int closedIsland(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + + vector> visited(R, vector(C, false)); + int res = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(visited[i][j] || grid[i][j] == 1) continue; + res += !dfs(grid, i, j, visited); + } + return res; + } + +private: + bool dfs(const vector>& grid, int x, int y, vector>& visited){ + + bool ret = false; + visited[x][y] = true; + + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny) && !visited[nx][ny] && grid[nx][ny] == 0) + ret |= dfs(grid, nx, ny, visited); + else if(!in_area(nx, ny)) + ret = true; + } + return ret; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index bc6fe250..6b812e2d 100644 --- a/readme.md +++ b/readme.md @@ -1229,6 +1229,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1251 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [无] | [C++](1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/) | | | | | | | | | | +| 1254 | [Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands/description/) | [无] | [C++](1001-1500/1254-Number-of-Closed-Islands/cpp-1254/) | | | | 1255 | [Maximum Score Words Formed by Letters](https://leetcode.com/problems/maximum-score-words-formed-by-letters/description/) | [无] | [C++](1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/) | | | | | | | | | | | 1259 | [Handshakes That Don't Cross](https://leetcode.com/problems/handshakes-that-dont-cross/description/) | [无] | [C++](1001-1500/1259-Handshakes-That-Dont-Cross/cpp-1259/) | | | From 3c9af4dbee4d2b71f277fa9e3158413bf895af58 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 9 Apr 2023 13:27:45 -0700 Subject: [PATCH 331/390] 2613-1616 solved. --- .../cpp-2613/CMakeLists.txt | 6 +++ .../2613-Beautiful-Pairs/cpp-2613/main.cpp | 25 ++++++++++ .../cpp-2614/CMakeLists.txt | 6 +++ .../2614-Prime-In-Diagonal/cpp-2614/main.cpp | 43 +++++++++++++++++ .../cpp-2615/CMakeLists.txt | 6 +++ .../2615-Sum-of-Distances/cpp-2615/main.cpp | 46 ++++++++++++++++++ .../cpp-2616/CMakeLists.txt | 6 +++ .../cpp-2616/main.cpp | 47 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 190 insertions(+) create mode 100644 2501-3000/2613-Beautiful-Pairs/cpp-2613/CMakeLists.txt create mode 100644 2501-3000/2613-Beautiful-Pairs/cpp-2613/main.cpp create mode 100644 2501-3000/2614-Prime-In-Diagonal/cpp-2614/CMakeLists.txt create mode 100644 2501-3000/2614-Prime-In-Diagonal/cpp-2614/main.cpp create mode 100644 2501-3000/2615-Sum-of-Distances/cpp-2615/CMakeLists.txt create mode 100644 2501-3000/2615-Sum-of-Distances/cpp-2615/main.cpp create mode 100644 2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/CMakeLists.txt create mode 100644 2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/main.cpp diff --git a/2501-3000/2613-Beautiful-Pairs/cpp-2613/CMakeLists.txt b/2501-3000/2613-Beautiful-Pairs/cpp-2613/CMakeLists.txt new file mode 100644 index 00000000..762e3614 --- /dev/null +++ b/2501-3000/2613-Beautiful-Pairs/cpp-2613/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2613) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2613 main.cpp) diff --git a/2501-3000/2613-Beautiful-Pairs/cpp-2613/main.cpp b/2501-3000/2613-Beautiful-Pairs/cpp-2613/main.cpp new file mode 100644 index 00000000..f96cc72f --- /dev/null +++ b/2501-3000/2613-Beautiful-Pairs/cpp-2613/main.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + + +class Solution { +public: + vector beautifulPair(vector& nums1, vector& nums2) { + + int n = nums1.size(); + + vector> points(n); + for(int i = 0; i < n; i ++) + points[i] = {nums1[i] + nums2[i], nums1[i] - nums2[i], i}; + + + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2614-Prime-In-Diagonal/cpp-2614/CMakeLists.txt b/2501-3000/2614-Prime-In-Diagonal/cpp-2614/CMakeLists.txt new file mode 100644 index 00000000..727628de --- /dev/null +++ b/2501-3000/2614-Prime-In-Diagonal/cpp-2614/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2614) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2614 main.cpp) diff --git a/2501-3000/2614-Prime-In-Diagonal/cpp-2614/main.cpp b/2501-3000/2614-Prime-In-Diagonal/cpp-2614/main.cpp new file mode 100644 index 00000000..b15e7e00 --- /dev/null +++ b/2501-3000/2614-Prime-In-Diagonal/cpp-2614/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/prime-in-diagonal/description/ +/// Author : liuyubobobo +/// Time : 2023-04-09 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlogn + n * sqrt(max_num)) +/// Space Complexity: O(n) +class Solution { +public: + int diagonalPrime(vector>& nums) { + + int n = nums.size(); + + vector v; + for(int i = 0; i < n; i ++) + v.push_back(nums[i][i]), v.push_back(nums[i][n - i - 1]); + sort(v.begin(), v.end(), greater<>()); + + for(int e: v) if(is_prime(e)) return e; + return 0; + } + +private: + bool is_prime(int x){ + if(x < 2) return false; + for(int i = 2; i * i <= x; i ++) + if(x % i == 0) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2615-Sum-of-Distances/cpp-2615/CMakeLists.txt b/2501-3000/2615-Sum-of-Distances/cpp-2615/CMakeLists.txt new file mode 100644 index 00000000..1079ec86 --- /dev/null +++ b/2501-3000/2615-Sum-of-Distances/cpp-2615/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2615) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2615 main.cpp) diff --git a/2501-3000/2615-Sum-of-Distances/cpp-2615/main.cpp b/2501-3000/2615-Sum-of-Distances/cpp-2615/main.cpp new file mode 100644 index 00000000..370d8937 --- /dev/null +++ b/2501-3000/2615-Sum-of-Distances/cpp-2615/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/sum-of-distances/description/ +/// Author : liuyubobobo +/// Time : 2023-04-09 + +#include +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector distance(vector& nums) { + + map> m; + for (int i = 0; i < nums.size(); ++i) m[nums[i]].push_back(i); + + int n = nums.size(); + vector res(n); + for(const pair>& p: m){ + const vector& index_v = p.second; + + long long left_sum = 0, right_sum = 0, left_cnt = 0, right_cnt = index_v.size() - 1; + for(int i = 1; i < (int)index_v.size(); i ++) right_sum += index_v[i] - index_v[0]; + res[index_v[0]] = left_sum + right_sum; + + for(int i = 1; i < (int)index_v.size(); i ++){ + long long d = index_v[i] - index_v[i - 1]; + left_sum += ++ left_cnt * d; + right_sum -= right_cnt -- * d; + res[index_v[i]] = left_sum + right_sum; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/CMakeLists.txt b/2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/CMakeLists.txt new file mode 100644 index 00000000..ef2f7cec --- /dev/null +++ b/2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2616) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2616 main.cpp) diff --git a/2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/main.cpp b/2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/main.cpp new file mode 100644 index 00000000..82efbaee --- /dev/null +++ b/2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/description/ +/// Author : liuyubobobo +/// Time : 2023-04-09 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int minimizeMax(vector& nums, int p) { + + int n = nums.size(); + sort(nums.begin(), nums.end()); + + int l = 0, r = nums.back() - nums.front(); + while(l < r){ + int mid = (l + r) / 2; + if(ok(n, nums, p, mid)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(int n, const vector& nums, int p, int d){ + + int i = 0, cnt = 0; + while(i + 1 < n){ + if(nums[i + 1] - nums[i] <= d) cnt ++, i += 2; + else i ++; + } + return cnt >= p; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 6b812e2d..698a4b37 100644 --- a/readme.md +++ b/readme.md @@ -2509,6 +2509,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2611 | [Mice and Cheese](https://leetcode.com/problems/mice-and-cheese/) | [无] | [C++](2001-2500/2611-Mice-and-Cheese/cpp-2611/) | | | | 2612 | [Minimum Reverse Operations](https://leetcode.com/problems/minimum-reverse-operations/) | [无] | [C++](2001-2500/2612-Minimum-Reverse-Operations/cpp-2612/) | | | | | | | | | | +| 2614 | [Prime In Diagonal](https://leetcode.com/problems/prime-in-diagonal/) | [无] | [C++](2001-2500/2614-Prime-In-Diagonal/cpp-2614/) | | | +| 2615 | [Sum of Distances](https://leetcode.com/problems/sum-of-distances/) | [无] | [C++](2001-2500/2615-Sum-of-Distances/cpp-2615/) | | | +| 2616 | [Minimize the Maximum Difference of Pairs](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/) | [无] | [C++](2001-2500/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/) | | | +| 2617 | [Minimum Number of Visited Cells in a Grid](https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/) | [无] | [C++](2001-2500/2617-Minimum-Number-of-Visited-Cells-in-a-Grid/cpp-2617/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 62e6b678a183ef7c431d1c414a83339aace55076 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 15 Apr 2023 19:18:19 -0700 Subject: [PATCH 332/390] 2639-2642 solved. --- .../cpp-2639/CMakeLists.txt | 6 ++ .../cpp-2639/main.cpp | 35 ++++++++++ .../cpp-2640/CMakeLists.txt | 6 ++ .../cpp-2640/main.cpp | 36 ++++++++++ .../cpp-2641/CMakeLists.txt | 6 ++ .../cpp-2641/main.cpp | 65 +++++++++++++++++++ .../cpp-2642/CMakeLists.txt | 6 ++ .../cpp-2642/main.cpp | 62 ++++++++++++++++++ readme.md | 5 ++ 9 files changed, 227 insertions(+) create mode 100644 2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/CMakeLists.txt create mode 100644 2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/main.cpp create mode 100644 2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/CMakeLists.txt create mode 100644 2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/main.cpp create mode 100644 2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/CMakeLists.txt create mode 100644 2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/main.cpp create mode 100644 2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/CMakeLists.txt create mode 100644 2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/main.cpp diff --git a/2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/CMakeLists.txt b/2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/CMakeLists.txt new file mode 100644 index 00000000..f5ef912d --- /dev/null +++ b/2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2639) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2639 main.cpp) diff --git a/2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/main.cpp b/2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/main.cpp new file mode 100644 index 00000000..5125548b --- /dev/null +++ b/2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(R * C * log(max(grid[i][j]))) +/// Space Complexity: O(C) +class Solution { +public: + vector findColumnWidth(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + vector res(C, 0); + for(int j = 0; j < C; j ++){ + for(int i = 0; i < R; i ++){ + string s = to_string(grid[i][j]); + res[j] = max(res[j], (int)s.size()); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/CMakeLists.txt b/2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/CMakeLists.txt new file mode 100644 index 00000000..6550ad14 --- /dev/null +++ b/2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2640) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2640 main.cpp) diff --git a/2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/main.cpp b/2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/main.cpp new file mode 100644 index 00000000..cb97380e --- /dev/null +++ b/2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector findPrefixScore(vector& nums) { + + int n = nums.size(); + + vector pre_max(n, nums[0]); + for(int i = 1; i < n; i ++) pre_max[i] = max(pre_max[i - 1], 0ll + nums[i]); + + vector conver(n); + for(int i = 0; i < n; i ++) conver[i] = nums[i] + pre_max[i]; + + vector res(n, conver[0]); + for(int i = 1; i < n; i ++) res[i] = res[i - 1] + conver[i]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/CMakeLists.txt b/2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/CMakeLists.txt new file mode 100644 index 00000000..28c05001 --- /dev/null +++ b/2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2641) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2641 main.cpp) diff --git a/2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/main.cpp b/2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/main.cpp new file mode 100644 index 00000000..589f0b83 --- /dev/null +++ b/2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.com/problems/cousins-in-binary-tree-ii/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + TreeNode* replaceValueInTree(TreeNode* root) { + + vector depth_sum; + dfs(root, nullptr, 0, depth_sum); + + return dfs_replace(root, nullptr, 0, depth_sum); + } + +private: + TreeNode* dfs_replace(TreeNode* node, TreeNode* parent, int depth, const vector& depth_sum){ + + if(node == nullptr) return nullptr; + + TreeNode* left = dfs_replace(node->left, node, depth + 1, depth_sum); + TreeNode* right = dfs_replace(node->right, node, depth + 1, depth_sum); + TreeNode* ret = new TreeNode(0, left, right); + if(parent != nullptr) + ret->val = depth_sum[depth] - (parent->left ? parent->left->val : 0) - (parent->right ? parent->right->val : 0); + return ret; + } + + void dfs(TreeNode* node, TreeNode* parent, int depth, vector& depth_sum){ + + if (node == nullptr) return; + + while(depth >= depth_sum.size()) depth_sum.push_back(0); + depth_sum[depth] += node->val; + + dfs(node->left, node, depth + 1, depth_sum); + dfs(node->right, node, depth + 1, depth_sum); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/CMakeLists.txt b/2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/CMakeLists.txt new file mode 100644 index 00000000..f1455aa5 --- /dev/null +++ b/2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2642) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2642 main.cpp) diff --git a/2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/main.cpp b/2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/main.cpp new file mode 100644 index 00000000..141e7d45 --- /dev/null +++ b/2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/the-most-similar-path-in-a-graph/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// dij +/// Time Complexity: init: O(|edges|) +/// addEdge: O(1) +/// query: O(|E|log|E|) +/// Space Complexity: O(|V| + |E|) +class Graph { + +private: + const int INF = INT_MAX / 2; + int n; + vector>> g; + +public: + Graph(int n, vector>& edges) : n(n), g(n) { + for(const vector& e: edges){ + int u = e[0], v = e[1], w = e[2]; + g[u].push_back({v, w}); + } + } + + void addEdge(vector edge) { + int u = edge[0], v = edge[1], w = edge[2]; + g[u].push_back({v, w}); + } + + int shortestPath(int s, int t) { + + vector visited(n, false); + vector dis(n, INF); + priority_queue, vector>, greater>> pq; + pq.push({0, s}); + while(!pq.empty()){ + int d = pq.top().first, u = pq.top().second; pq.pop(); + + if(visited[u]) continue; + visited[u] = true; + if(u == t) return d; + + for(const auto& [v, w]: g[u]) + if(d + w < dis[v]) dis[v] = d + w, pq.push({dis[v], v}); + } + return -1; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 698a4b37..d7f737b5 100644 --- a/readme.md +++ b/readme.md @@ -2514,6 +2514,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2616 | [Minimize the Maximum Difference of Pairs](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/) | [无] | [C++](2001-2500/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/) | | | | 2617 | [Minimum Number of Visited Cells in a Grid](https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/) | [无] | [C++](2001-2500/2617-Minimum-Number-of-Visited-Cells-in-a-Grid/cpp-2617/) | | | | | | | | | | +| 2639 | [Find the Width of Columns of a Grid](https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/) | [无] | [C++](2001-2500/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/) | | | +| 2640 | [Find the Score of All Prefixes of an Array](https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/) | [无] | [C++](2001-2500/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/) | | | +| 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [无] | [C++](2001-2500/2641-Cousins-in-Binary-Tree-II/cpp-2641/) | | | +| 2642 | [Design Graph With Shortest Path Calculator](https://leetcode.com/problems/design-graph-with-shortest-path-calculator/) | [无] | [C++](2001-2500/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 44cd4b395ad80a7093e8d959b1d1f2ad770cfd7e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 15 Apr 2023 19:42:37 -0700 Subject: [PATCH 333/390] 2638 solved. --- .../cpp-2638/CMakeLists.txt | 6 ++ .../cpp-2638/main.cpp | 63 +++++++++++++++++++ readme.md | 1 + 3 files changed, 70 insertions(+) create mode 100644 2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/CMakeLists.txt create mode 100644 2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/main.cpp diff --git a/2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/CMakeLists.txt b/2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/CMakeLists.txt new file mode 100644 index 00000000..37e9dd45 --- /dev/null +++ b/2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2638) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2638 main.cpp) diff --git a/2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/main.cpp b/2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/main.cpp new file mode 100644 index 00000000..bbeb403a --- /dev/null +++ b/2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-k-free-subsets/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long countTheNumOfKFreeSubsets(vector& nums, int k) { + + int n = nums.size(); + vector> dp(2, vector(n + 1, 0)); + dp[0][1] = dp[1][1] = 1; + for(int len = 2; len <= n; len ++){ + dp[0][len] = dp[0][len - 1] + dp[1][len - 1]; + dp[1][len] = dp[0][len - 1]; + } + + sort(nums.begin(), nums.end()); + + long long res = 1; + vector visited(n, false); + + for(int i = 0; i < n; i ++){ + if(visited[i]) continue; + + int chain_len = get_chain_len(nums, i, k, visited); + res *= (dp[0][chain_len] + dp[1][chain_len]); + } + return res; + } + +private: + int get_chain_len(const vector& nums, int s, int k, vector& visited){ + + int cur = nums[s], res = 1; + while(true){ + auto iter = lower_bound(nums.begin(), nums.end(), cur + k); + if(iter != nums.end() && *iter == cur + k){ + int index = iter - nums.begin(); + visited[index] = true; + cur = nums[index]; + res ++; + } + else break; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d7f737b5..eb41e550 100644 --- a/readme.md +++ b/readme.md @@ -2514,6 +2514,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2616 | [Minimize the Maximum Difference of Pairs](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/) | [无] | [C++](2001-2500/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/) | | | | 2617 | [Minimum Number of Visited Cells in a Grid](https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/) | [无] | [C++](2001-2500/2617-Minimum-Number-of-Visited-Cells-in-a-Grid/cpp-2617/) | | | | | | | | | | +| 2638 | [Count the Number of K-Free Subsets](https://leetcode.com/problems/count-the-number-of-k-free-subsets/description/) | [无] | [C++](2001-2500/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/) | | | | 2639 | [Find the Width of Columns of a Grid](https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/) | [无] | [C++](2001-2500/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/) | | | | 2640 | [Find the Score of All Prefixes of an Array](https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/) | [无] | [C++](2001-2500/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/) | | | | 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [无] | [C++](2001-2500/2641-Cousins-in-Binary-Tree-II/cpp-2641/) | | | From 8c22dfe9340377af3ba3efd350d59fd51a3ebf4f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 15 Apr 2023 22:37:55 -0700 Subject: [PATCH 334/390] 2643-2646 solved. --- .../cpp-2643/CMakeLists.txt | 6 + .../cpp-2643/main.cpp | 32 ++++++ .../cpp-2644/CMakeLists.txt | 6 + .../cpp-2644/main.cpp | 35 ++++++ .../cpp-2645/CMakeLists.txt | 6 + .../cpp-2645/main.cpp | 34 ++++++ .../cpp-2646/CMakeLists.txt | 6 + .../cpp-2646/main.cpp | 103 ++++++++++++++++++ readme.md | 4 + 9 files changed, 232 insertions(+) create mode 100644 2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/CMakeLists.txt create mode 100644 2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/main.cpp create mode 100644 2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/CMakeLists.txt create mode 100644 2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/main.cpp create mode 100644 2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/CMakeLists.txt create mode 100644 2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/main.cpp create mode 100644 2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/CMakeLists.txt create mode 100644 2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/main.cpp diff --git a/2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/CMakeLists.txt b/2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/CMakeLists.txt new file mode 100644 index 00000000..7ac1f0d1 --- /dev/null +++ b/2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2643) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2643 main.cpp) diff --git a/2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/main.cpp b/2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/main.cpp new file mode 100644 index 00000000..c07642b4 --- /dev/null +++ b/2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/row-with-maximum-ones/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + vector rowAndMaximumOnes(vector>& mat) { + + int max_ones = -1, res = -1; + for(int i = 0; i < mat.size(); i ++){ + int ones = count(mat[i].begin(), mat[i].end(), 1); + if(ones > max_ones) max_ones = ones, res = i; + } + return {res, max_ones}; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/CMakeLists.txt b/2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/CMakeLists.txt new file mode 100644 index 00000000..71e97fa2 --- /dev/null +++ b/2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2644) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2644 main.cpp) diff --git a/2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/main.cpp b/2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/main.cpp new file mode 100644 index 00000000..680b9e52 --- /dev/null +++ b/2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/find-the-maximum-divisibility-score/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(|divisors| * log|divisors| + |nums| * |divisors|) +/// Space Complexity: O(1) +class Solution { +public: + int maxDivScore(vector& nums, vector& divisors) { + + sort(divisors.begin(), divisors.end()); + int best = -1, res = -1; + + for(int d: divisors){ + int cnt = 0; + for(int e: nums) cnt += (e % d == 0); + if(cnt > best) best = cnt, res = d; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/CMakeLists.txt b/2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/CMakeLists.txt new file mode 100644 index 00000000..4668e873 --- /dev/null +++ b/2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2645) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2645 main.cpp) diff --git a/2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/main.cpp b/2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/main.cpp new file mode 100644 index 00000000..be505075 --- /dev/null +++ b/2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/minimum-additions-to-make-valid-string/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int addMinimum(string word) { + + int n = word.size(), next = 0, index = 0, res = 0; + while(index < n) { + int cur = word[index] - 'a'; + if(next == cur) + index ++; + else res ++; + next = (next + 1) % 3; + } + + while(next != 0) res ++, next = (next + 1) % 3; + return res; + } +}; + +int main() { + + return 0; +} diff --git a/2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/CMakeLists.txt b/2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/CMakeLists.txt new file mode 100644 index 00000000..b1d70828 --- /dev/null +++ b/2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2646) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2646 main.cpp) diff --git a/2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/main.cpp b/2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/main.cpp new file mode 100644 index 00000000..8a424921 --- /dev/null +++ b/2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/main.cpp @@ -0,0 +1,103 @@ +/// Source : https://leetcode.com/problems/minimize-the-total-price-of-the-trips/description/ +/// Author : liuyubobobo +/// Time : 2023-04-15 + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// Tree DP +/// Time Complexity: O(|trip| * n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumTotalPrice(int n, vector>& edges, vector& price, vector>& trips) { + + vector> g(n); + for(const vector& e: edges) { + int u = e[0], v = e[1]; + g[u].push_back(v), g[v].push_back(u); + } + + vector f(n, 0); + for(const vector& trip: trips) { + int s = trip[0], t = trip[1]; + + vector path = get_path(n, g, s, t); + assert(path[0] == s && path.back() == t); + for(int e: path) f[e] ++; + } + + vector> dp(n, vector(2, -1)); + return dfs(g, 0, -1, true, f, price, dp); + } + +private: + int dfs(const vector>& g, int u, int p, bool can_halve, + const vector& f, const vector& price, vector>& dp) { + + if(dp[u][can_halve] != -1) return dp[u][can_halve]; + + int res = INT_MAX; + if(can_halve){ + int tres = price[u] / 2 * f[u]; + for(int v: g[u]) { + if(v == p) continue; + tres += dfs(g, v, u, false, f, price, dp); + } + res = min(res, tres); + } + + int tres = price[u] * f[u]; + for(int v: g[u]) { + if(v == p) continue; + tres += dfs(g, v, u, true, f, price, dp); + } + res = min(res, tres); + + return dp[u][can_halve] = res; + } + + vector get_path(int n, const vector>& g, int s, int t){ + + vector pre(n, -1); + vector visited(n, false); + queue q; + q.push(s); + visited[s] = true; + while(!q.empty()) { + int u = q.front(); q.pop(); + for(int v: g[u]) { + if(visited[v]) continue; + pre[v] = u; + q.push(v); + visited[v] = true; + } + } + + vector path = {t}; + while(path.back() != s) + path.push_back(pre[path.back()]); + reverse(path.begin(), path.end()); + return path; + } +}; + + +int main() { + + vector> edges1 = {{0,1},{1,2},{1,3}}; + vector price1 = {2,2,10,6}; + vector> trips1 = {{0,3},{2,1},{2,3}}; + cout << Solution().minimumTotalPrice(4, edges1, price1, trips1) << '\n'; + // 23 + + return 0; +} diff --git a/readme.md b/readme.md index eb41e550..82ed2f82 100644 --- a/readme.md +++ b/readme.md @@ -2519,6 +2519,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2640 | [Find the Score of All Prefixes of an Array](https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/) | [无] | [C++](2001-2500/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/) | | | | 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [无] | [C++](2001-2500/2641-Cousins-in-Binary-Tree-II/cpp-2641/) | | | | 2642 | [Design Graph With Shortest Path Calculator](https://leetcode.com/problems/design-graph-with-shortest-path-calculator/) | [无] | [C++](2001-2500/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/) | | | +| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones/) | [无] | [C++](2001-2500/2643-Row-With-Maximum-Ones/cpp-2643/) | | | +| 2644 | [Find the Maximum Divisibility Score](https://leetcode.com/problems/find-the-maximum-divisibility-score/) | [无] | [C++](2001-2500/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/) | | | +| 2645 | [Minimum Additions to Make Valid String](https://leetcode.com/problems/minimum-additions-to-make-valid-string/) | [无] | [C++](2001-2500/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/) | | | +| 2646 | [Minimize the Total Price of the Trips](https://leetcode.com/problems/minimize-the-total-price-of-the-trips/) | [无] | [C++](2001-2500/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From d788cf3a0bd35cca88e954f0e0406d81ec3b8912 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 21 Apr 2023 12:14:31 -0700 Subject: [PATCH 335/390] 1027 codes updated. --- .../cpp-1027/CMakeLists.txt | 2 +- .../cpp-1027/main.cpp | 40 +++++++---------- .../cpp-1027/main2.cpp | 45 ------------------- 3 files changed, 17 insertions(+), 70 deletions(-) delete mode 100644 1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main2.cpp diff --git a/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/CMakeLists.txt b/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/CMakeLists.txt index e47439c9..2235a661 100644 --- a/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/CMakeLists.txt +++ b/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/CMakeLists.txt @@ -3,4 +3,4 @@ project(C) set(CMAKE_CXX_STANDARD 14) -add_executable(C main2.cpp) \ No newline at end of file +add_executable(C main.cpp) \ No newline at end of file diff --git a/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main.cpp b/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main.cpp index f454d438..a896c371 100644 --- a/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main.cpp +++ b/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main.cpp @@ -1,45 +1,37 @@ /// Source : https://leetcode.com/problems/longest-arithmetic-sequence/ /// Author : liuyubobobo /// Time : 2019-04-13 +/// Updated: 2023-04-13 #include #include -#include +#include using namespace std; -/// HashMap + Bianry Search -/// Time Complexity: O(n^2*logn) +/// DP +/// Time Complexity: O(n^2) /// Space Complexity: O(n^2) class Solution { +private: + const int offset = 500; + public: int longestArithSeqLength(vector& A) { - unordered_map> map; - for(int i = 0; i < A.size(); i ++) - map[A[i]].push_back(i); - - int res = 2; - for(int i = 0; i < A.size(); i ++) - for(int j = i + 1; j < A.size(); j ++) - res = max(res, length(j, A[j], A[j] - A[i], map)); - return res; - } - -private: - int length(int pos, int cur, int diff, unordered_map>& map){ + int n = A.size(); + vector> dp(n, vector(1001, 1)); - int res = 2; - while(true){ - cur += diff; - vector::iterator iter = upper_bound(map[cur].begin(), map[cur].end(), pos); - if(iter == map[cur].end()) break; + int res = 1; + for(int i = 1; i < n; i ++) + for(int j = i - 1; j >= 0; j --){ + int d = A[i] - A[j] + offset; + dp[i][d] = max(dp[i][d], dp[j][d] + 1); + res = max(res, dp[i][d]); + } - res ++; - pos = *iter; - } return res; } }; diff --git a/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main2.cpp b/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main2.cpp deleted file mode 100644 index daa80322..00000000 --- a/1001-1500/1027-Longest-Arithmetic-Sequence/cpp-1027/main2.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/longest-arithmetic-sequence/ -/// Author : liuyubobobo -/// Time : 2019-04-13 - -#include -#include -#include - -using namespace std; - - -/// Dynamic Programming -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) -class Solution { - -public: - int longestArithSeqLength(vector& A) { - - unordered_map> dp; - - int res = 2; - for(int i = 0; i < A.size(); i ++) - for(int j = i + 1; j < A.size(); j ++){ - dp[A[j] - A[i]][j] = max(dp[A[j] - A[i]][j], dp[A[j] - A[i]][i] + 1); - res = max(res, dp[A[j] - A[i]][j]); - } - return res; - } -}; - - -int main() { - - vector A1 = {3, 6, 9, 12}; - cout << Solution().longestArithSeqLength(A1) << endl; // 4 - - vector A2 = {9, 4, 7, 2, 10}; - cout << Solution().longestArithSeqLength(A2) << endl; // 3 - - vector A3 = {20,1,15,3,10,5,8}; - cout << Solution().longestArithSeqLength(A3) << endl; // 4 - - return 0; -} \ No newline at end of file From 1555fe4ef4ef91c2159186b8372b8b177ef2b3c8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 22 Apr 2023 22:29:10 -0700 Subject: [PATCH 336/390] 0727 codes updated. --- .../cpp-0727/CMakeLists.txt | 2 +- .../cpp-0727/main.cpp | 95 +++++++++++-------- .../cpp-0727/main2.cpp | 84 ---------------- .../cpp-0727/main3.cpp | 89 ----------------- 4 files changed, 56 insertions(+), 214 deletions(-) delete mode 100644 0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main2.cpp delete mode 100644 0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main3.cpp diff --git a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/CMakeLists.txt b/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/CMakeLists.txt index d58a861e..1c3e7bb6 100644 --- a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/CMakeLists.txt +++ b/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/CMakeLists.txt @@ -3,5 +3,5 @@ project(D) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main3.cpp) +set(SOURCE_FILES main.cpp) add_executable(D ${SOURCE_FILES}) \ No newline at end of file diff --git a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main.cpp b/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main.cpp index 1ac865ab..d41f3ca2 100644 --- a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main.cpp +++ b/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/contest/weekly-contest-58/problems/minimum-window-subsequence/ /// Author : liuyubobobo -/// Time : 2017-11-11 +/// Time : 2017-11-12 +/// Updated: 2023-04-22 #include #include @@ -9,13 +10,13 @@ using namespace std; -/// Memory Search -/// -/// !!! Memory Limit Exceed !!! + +/// Dynamic Programming with Rolling 1-D Array +/// dp[i][j] - the minimum length W for subsequence in S[i...end) to satify T[j...end) +/// the result is the minimum value for all dp[i][0] where len(S[i...end)) > len(T) /// /// Time Complexity: O(len(S)*len(T)) -/// Space Complexity: O(len(S)*len(T)) - +/// Space Complexity: O(len(T)) class Solution { private: @@ -24,55 +25,69 @@ class Solution { public: string minWindow(string S, string T) { - vector> mem(20001, vector(101, -1)); + vector> dp(2, vector(T.size(), MY_MAX_INT)); int min_length = MY_MAX_INT; int start = -1; - for(int i = 0 ; i < S.size() ; i ++){ - search(mem, S, i, T, 0); - assert(mem[i][0] != -1); - //cout << mem[i][0] << endl; - if(mem[i][0] < min_length){ - min_length = mem[i][0]; - start = i; - } - } - for(int i = 0 ; i < S.size() ; i ++){ - for(int j = 0 ; j < T.size() ; j ++) - cout << mem[i][j] << "\t"; - cout << endl; + dp[(S.size()-1)%2][T.size()-1] = (S.back() == T.back() ? 1 : MY_MAX_INT); + if(dp[(S.size()-1)%2][0] == 1 && T.size() == 1){ + min_length = 1; + start = S.size()-1; } - //cout << start << " " << min_length << endl; - return start == -1 ? "" : S.substr(start, min_length); - return ""; - } - -private: - int search(vector>& mem, - const string& S, int i1, const string& T, int i2){ - - if(i2 == T.size()) - return 0; - - if(i1 == S.size()) - return MY_MAX_INT; + else + for(int j = T.size()-2 ; j >= 0 ; j --) + dp[(S.size()-1)%2][j] = MY_MAX_INT; + + for(int i = S.size()-2 ; i >= 0 ; i --){ + dp[i%2][T.size()-1] = dp[(i+1)%2][T.size()-1] + 1; + if(S[i] == T.back()) + dp[i%2][T.size()-1] = 1; + + for(int j = T.size() - 2 ; j >= 0 ; j --){ + dp[i%2][j] = min(MY_MAX_INT, 1 + dp[(i+1)%2][j]); + if(S[i] == T[j]) + dp[i%2][j] = min(dp[i%2][j], 1 + dp[(i+1)%2][j+1]); + } - if(mem[i1][i2] != -1) - return mem[i1][i2]; + if(i + T.size() <= S.size() && dp[i%2][0] <= min_length){ + min_length = dp[i%2][0]; + start = i; + } + } - int res = 1 + search(mem, S, i1+1, T, i2); - if(S[i1] == T[i2]) - res = min(res, 1 + search(mem, S, i1+1, T, i2+1)); - return mem[i1][i2] = res; + return (start != -1 && min_length < MY_MAX_INT) ? + S.substr(start, min_length) : ""; } }; + int main() { string S1 = "abcdebdde"; string T1 = "bde"; cout << Solution().minWindow(S1, T1) << endl; + // bcde + + string S2 = "ab"; + string T2 = "b"; + cout << Solution().minWindow(S2, T2) << endl; + // b + + string S3 = "cnhczmccqouqadqtmjjzl"; + string T3 = "mm"; + cout << Solution().minWindow(S3, T3) << endl; + // mccqouqadqtm + + string S4 = "clgkckxqhqojiroohcudeyhlylicvafvpbubcjictifyoshucybzswblioaflxaoxdjbjejvzgqiuedmzgmqbhpkjlwxvobrcgqhzzelxppwdkwqlplflnldxpkwobqyqhqbfcxolrmrllmzpgjemzhscagqxhyuqquopquyyxwcuetxnxebbrgsbiwtkqdpqmvsprrnyficfxagfsssvppwqdsqesz"; + string T4 = "cihfrleqav"; + cout << Solution().minWindow(S4, T4) << endl; + // cybzswblioaflxaoxdjbjejvzgqiuedmzgmqbhpkjlwxvobrcgqhzzelxppwdkwqlplflnldxpkwobqyqhqbfcxolrmrllmzpgjemzhscagqxhyuqquopquyyxwcuetxnxebbrgsbiwtkqdpqmvsprrnyficfxagfsssv + + string S5 = "aaa"; + string T5 = "aaaaaaaa"; + cout << Solution().minWindow(S5, T5) << endl; + // mccqouqadqtm return 0; } \ No newline at end of file diff --git a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main2.cpp b/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main2.cpp deleted file mode 100644 index 2ce133ff..00000000 --- a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main2.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/// Source : https://leetcode.com/contest/weekly-contest-58/problems/minimum-window-subsequence/ -/// Author : liuyubobobo -/// Time : 2017-11-12 - -#include -#include -#include -#include - -/// Dynamic Programming with Rolling 1-D Array -/// dp[i][j] - the minimum length W for subsequence in S[i...end) to satify T[j...end) -/// the result is the minimum value for all dp[i][0] where len(S[i...end)) > len(T) -/// -/// Time Complexity: O(len(S)*len(T)) -/// Space Complexity: O(len(T)) -using namespace std; - -class Solution { - -private: - int MY_MAX_INT = 20001; - -public: - string minWindow(string S, string T) { - - vector> dp(2, vector(T.size(), MY_MAX_INT)); - - int min_length = MY_MAX_INT; - int start = -1; - - dp[(S.size()-1)%2][T.size()-1] = (S.back() == T.back() ? 1 : MY_MAX_INT); - if(dp[(S.size()-1)%2][0] == 1 && T.size() == 1){ - min_length = 1; - start = S.size()-1; - } - else - for(int j = T.size()-2 ; j >= 0 ; j --) - dp[(S.size()-1)%2][j] = MY_MAX_INT; - - for(int i = S.size()-2 ; i >= 0 ; i --){ - dp[i%2][T.size()-1] = dp[(i+1)%2][T.size()-1] + 1; - if(S[i] == T.back()) - dp[i%2][T.size()-1] = 1; - - for(int j = T.size() - 2 ; j >= 0 ; j --){ - dp[i%2][j] = min(MY_MAX_INT, 1 + dp[(i+1)%2][j]); - if(S[i] == T[j]) - dp[i%2][j] = min(dp[i%2][j], 1 + dp[(i+1)%2][j+1]); - } - - if(i + T.size() <= S.size() && dp[i%2][0] <= min_length){ - min_length = dp[i%2][0]; - start = i; - } - } - - return (start != -1 && min_length < MY_MAX_INT) ? - S.substr(start, min_length) : ""; - } -}; - -int main() { - - string S1 = "abcdebdde"; - string T1 = "bde"; - cout << Solution().minWindow(S1, T1) << endl; - // bcde - - // --- - - string S2 = "ab"; - string T2 = "b"; - cout << Solution().minWindow(S2, T2) << endl; - // b - - // --- - - string S3 = "cnhczmccqouqadqtmjjzl"; - string T3 = "mm"; - cout << Solution().minWindow(S3, T3) << endl; - // mccqouqadqtm - - return 0; -} \ No newline at end of file diff --git a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main3.cpp b/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main3.cpp deleted file mode 100644 index 3b51228d..00000000 --- a/0501-1000/0727-Minimum-Window-Subsequence/cpp-0727/main3.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/// Source : https://leetcode.com/contest/weekly-contest-58/problems/minimum-window-subsequence/ -/// Author : liuyubobobo -/// Time : 2017-11-12 - -#include -#include -#include -#include - -/// Dynamic Programming with Rolling 1-D Array -/// dp[i][j] - the largest start index s for S[s...i] to satify T[0...j] -/// -/// Time Complexity: O(len(S)*len(T)) -/// Space Complexity: O(len(S)) -using namespace std; - -class Solution { - -private: - int MY_MAX_INT = 20001; - -public: - string minWindow(string S, string T) { - - if(T.size() == 1 && S.find(T[0]) != string::npos) - return T; - - vector> dp(2, vector(S.size(), -1)); - - dp[0][0] = (S[0] == T[0]) ? 0 : -1; - for(int j = 1 ; j < S.size() ; j ++) - dp[0][j] = (S[j] == T[0]) ? j : dp[0][j-1]; - - for(int i = 1 ; i < T.size() ; i ++){ - for(int j = 0 ; j < i ; j ++) - dp[i%2][j] = -1; - for(int j = i ; j < S.size() ; j ++){ - dp[i%2][j] = dp[i%2][j-1]; - if(T[i] == S[j]) - dp[i%2][j] = max(dp[(i-1)%2][j-1], dp[i%2][j]); - } - } - - int min_length = MY_MAX_INT; - int start = -1; - for(int j = T.size() - 1 ; j < S.size() ; j ++) - if(dp[(T.size()-1)%2][j] != -1){ - int length = j - dp[(T.size()-1)%2][j] + 1; - assert(length > 0); - if(length < min_length){ - min_length = length; - start = dp[(T.size()-1)%2][j]; - } - } - return (start != -1 && min_length < MY_MAX_INT) ? - S.substr(start, min_length) : ""; - } -}; - -int main() { - - string S1 = "abcdebdde"; - string T1 = "bde"; - cout << Solution().minWindow(S1, T1) << endl; - // bcde - - // --- - - string S2 = "ab"; - string T2 = "b"; - cout << Solution().minWindow(S2, T2) << endl; - // b - - // --- - - string S3 = "cnhczmccqouqadqtmjjzl"; - string T3 = "mm"; - cout << Solution().minWindow(S3, T3) << endl; - // mccqouqadqtm - - // --- - - string S4 = "clgkckxqhqojiroohcudeyhlylicvafvpbubcjictifyoshucybzswblioaflxaoxdjbjejvzgqiuedmzgmqbhpkjlwxvobrcgqhzzelxppwdkwqlplflnldxpkwobqyqhqbfcxolrmrllmzpgjemzhscagqxhyuqquopquyyxwcuetxnxebbrgsbiwtkqdpqmvsprrnyficfxagfsssvppwqdsqesz"; - string T4 = "cihfrleqav"; - cout << Solution().minWindow(S4, T4) << endl; - // cybzswblioaflxaoxdjbjejvzgqiuedmzgmqbhpkjlwxvobrcgqhzzelxppwdkwqlplflnldxpkwobqyqhqbfcxolrmrllmzpgjemzhscagqxhyuqquopquyyxwcuetxnxebbrgsbiwtkqdpqmvsprrnyficfxagfsssv - - return 0; -} \ No newline at end of file From 0753526383a32bc8b889bd30fa3a4b839447d93a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 23 Apr 2023 00:17:02 -0700 Subject: [PATCH 337/390] CLP 072-076 solved. --- LC/LCP072/cpp-LCP072/CMakeLists.txt | 6 ++ LC/LCP072/cpp-LCP072/main.cpp | 48 ++++++++++ LC/LCP073/cpp-LCP073/CMakeLists.txt | 6 ++ LC/LCP073/cpp-LCP073/main.cpp | 60 ++++++++++++ LC/LCP074/cpp-LCP074/CMakeLists.txt | 6 ++ LC/LCP074/cpp-LCP074/main.cpp | 104 +++++++++++++++++++++ LC/LCP075/cpp-LCP075/CMakeLists.txt | 6 ++ LC/LCP075/cpp-LCP075/main.cpp | 107 ++++++++++++++++++++++ LC/LCP076/cpp-LCP076/CMakeLists.txt | 6 ++ LC/LCP076/cpp-LCP076/main.cpp | 137 ++++++++++++++++++++++++++++ LC/readme.md | 6 ++ 11 files changed, 492 insertions(+) create mode 100644 LC/LCP072/cpp-LCP072/CMakeLists.txt create mode 100644 LC/LCP072/cpp-LCP072/main.cpp create mode 100644 LC/LCP073/cpp-LCP073/CMakeLists.txt create mode 100644 LC/LCP073/cpp-LCP073/main.cpp create mode 100644 LC/LCP074/cpp-LCP074/CMakeLists.txt create mode 100644 LC/LCP074/cpp-LCP074/main.cpp create mode 100644 LC/LCP075/cpp-LCP075/CMakeLists.txt create mode 100644 LC/LCP075/cpp-LCP075/main.cpp create mode 100644 LC/LCP076/cpp-LCP076/CMakeLists.txt create mode 100644 LC/LCP076/cpp-LCP076/main.cpp diff --git a/LC/LCP072/cpp-LCP072/CMakeLists.txt b/LC/LCP072/cpp-LCP072/CMakeLists.txt new file mode 100644 index 00000000..8a306afe --- /dev/null +++ b/LC/LCP072/cpp-LCP072/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(A) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(A main.cpp) diff --git a/LC/LCP072/cpp-LCP072/main.cpp b/LC/LCP072/cpp-LCP072/main.cpp new file mode 100644 index 00000000..7001e549 --- /dev/null +++ b/LC/LCP072/cpp-LCP072/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.cn/problems/hqCnmP/ +/// Author : liuyubobobo +/// Time : 2023-04-22 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + vector supplyWagon(vector& supplies) { + + int n = supplies.size(), k = n - n / 2; + while(k --) + supplies = merge(supplies); + return supplies; + } + +private: + vector merge(vector& supplies) { + + int n = supplies.size(); + int min_sum = supplies[0] + supplies[1], pos = 1; + for(int i = 2; i < n; i ++){ + if(supplies[i - 1] + supplies[i] < min_sum) + min_sum = supplies[i - 1] + supplies[i], pos = i; + } + + vector res; + for(int i = 0; i < n; ) + if(i == pos - 1) + res.push_back(min_sum), i += 2; + else + res.push_back(supplies[i ++]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP073/cpp-LCP073/CMakeLists.txt b/LC/LCP073/cpp-LCP073/CMakeLists.txt new file mode 100644 index 00000000..3e27b177 --- /dev/null +++ b/LC/LCP073/cpp-LCP073/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(B) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(B main.cpp) diff --git a/LC/LCP073/cpp-LCP073/main.cpp b/LC/LCP073/cpp-LCP073/main.cpp new file mode 100644 index 00000000..43f1a2f8 --- /dev/null +++ b/LC/LCP073/cpp-LCP073/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.cn/problems/0Zeoeg/ +/// Author : liuyubobobo +/// Time : 2023-04-22 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|total_characters|) +/// Space Complexity: O(|total_characters|) +class Solution { +public: + int adventureCamp(vector& expeditions) { + + set init_set = get_set(expeditions[0]); + + int best = 0, res = -1; + for(int i = 1; i < expeditions.size(); i ++){ + set cur_set = get_set(expeditions[i]); + int cur = 0; + for(const string& s: cur_set) + cur += !init_set.count(s); + + if(cur > best) best = cur, res = i; + + for(const string& s: cur_set) + init_set.insert(s); + } + return res; + } + +private: + set get_set(const string& s){ + + set res; + int pos = 0; + while(pos < s.size()){ + int arrow_pos = s.find("->", pos); + if(arrow_pos == string::npos){ + res.insert(s.substr(pos)); + break; + } + res.insert(s.substr(pos, arrow_pos - pos)); + pos = arrow_pos + 2; + } + return res; + } +}; + + +int main() { + + // ["xO->xO->xO","xO->BKbWDH","xO->BKbWDH","BKbWDH->mWXW","LSAYWW->LSAYWW","oAibBvPdJ","LSAYWW->u","LSAYWW->LSAYWW"] + // 1 + return 0; +} diff --git a/LC/LCP074/cpp-LCP074/CMakeLists.txt b/LC/LCP074/cpp-LCP074/CMakeLists.txt new file mode 100644 index 00000000..6d269de2 --- /dev/null +++ b/LC/LCP074/cpp-LCP074/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(C) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(C main.cpp) diff --git a/LC/LCP074/cpp-LCP074/main.cpp b/LC/LCP074/cpp-LCP074/main.cpp new file mode 100644 index 00000000..8e5af497 --- /dev/null +++ b/LC/LCP074/cpp-LCP074/main.cpp @@ -0,0 +1,104 @@ +/// Source : https://leetcode.cn/problems/xepqZ5/ +/// Author : liuyubobobo +/// Time : 2023-04-22 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { +public: + int fieldOfGreatestBlessing(vector>& forceField) { + + int n = forceField.size(); + vector> llforceField(n, vector(3)); + for(int i = 0; i < n; i ++) + for(int j = 0; j < 3; j ++) llforceField[i][j] = forceField[i][j] * 2ll; + + set> points; + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + get_points(points, llforceField[i], llforceField[j]); + + int res = 1; + for(const pair& p: points) + res = max(res, in_rec(p.first, p.second, llforceField)); + return res; + } + +private: + int in_rec(long long x, long long y, const vector>& f){ + int res = 0; + for(const vector& ff: f) res += in_rec(x, y, ff); + return res; + } + + int in_rec(long long x, long long y, const vector& f){ + long long x0 = f[0] - f[2] / 2, y0 = f[1] - f[2] / 2, len = f[2]; + return x0 <= x && x <= x0 + len && y0 <= y && y <= y0 + len; + } + + void get_points(set>& points, const vector& f1, const vector& f2) { + + long long x1 = f1[0] - f1[2] / 2, y1 = f1[1] - f1[2] / 2, len1 = f1[2]; + long long x2 = f2[0] - f2[2] / 2, y2 = f2[1] - f2[2] / 2, len2 = f2[2]; + + get_points(x1, y1, x1 + len1, y1, x2, y2, len2, points); + get_points(x1 + len1, y1, x1 + len1, y1 + len1, x2, y2, len2, points); + get_points(x1 + len1, y1 + len1, x1, y1 + len1, x2, y2, len2, points); + get_points(x1, y1 + len1, x1, y1, x2, y2, len2, points); + +// get_points(x2, y2, x2 + len2, y2, x1, y1, len1, points); +// get_points(x2 + len2, y2, x2 + len2, y2 + len2, x1, y1, len1, points); +// get_points(x2 + len2, y2 + len2, x2, y2 + len2, x1, y1, len1, points); +// get_points(x2, y2 + len2, x2, y2, x1, y1, len1, points); + } + + void get_points(long long x0, long long y0, long long x1, long long y1, + long long x, long long y, long long len, + set>& points) { + + if(x0 == x1) { + if(y0 > y1) swap(y0, y1); // y0 <= y1 + if(x <= x0 && x0 <= x + len) { + if(y0 <= y){ + if(y1 >= y) points.insert({x0, y}); + if(y1 <= y + len) points.insert({x0, y1}); + else if(y1 >= y + len) points.insert({x0, y + len}); + } + else if(y0 <= y + len) { + points.insert({x0, y0}); + if(y1 <= y + len) points.insert({x0, y1}); + else if(y1 >= y + len) points.insert({x0, y + len}); + } + } + } + if(y0 == y1){ + if(x0 > x1) swap(x0, x1); // x0 <= x1 + if(y <= y0 && y0 <= y + len) { + if(x0 <= x){ + if(x1 >= x) points.insert({x, y0}); + if(x1 <= x + len) points.insert({x1, y0}); + else if(x1 >= x + len) points.insert({x + len, y0}); + } + else if(x0 <= x + len) { + points.insert({x0, y0}); + if(x1 <= x + len) points.insert({x1, y0}); + else if(x1 >= x + len) points.insert({x + len, y0}); + } + } + } + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP075/cpp-LCP075/CMakeLists.txt b/LC/LCP075/cpp-LCP075/CMakeLists.txt new file mode 100644 index 00000000..6a6bdfdb --- /dev/null +++ b/LC/LCP075/cpp-LCP075/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(D) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(D main.cpp) diff --git a/LC/LCP075/cpp-LCP075/main.cpp b/LC/LCP075/cpp-LCP075/main.cpp new file mode 100644 index 00000000..9b759ab1 --- /dev/null +++ b/LC/LCP075/cpp-LCP075/main.cpp @@ -0,0 +1,107 @@ +/// Source : https://leetcode.cn/problems/rdmXM7/ +/// Author : liuyubobobo +/// Time : 2023-04-22 + +#include +#include +#include + +using namespace std; + + +/// BFS + Dijkstra +/// Time Complexity: O(n^2 * log(n^2)) +/// Space Complexity: O(n^2) +class Solution { + +private: + int R, C; + const int INF = INT_MAX / 2; + const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + +public: + int challengeOfTheKeeper(vector& maze) { + + R = maze.size(), C = maze[0].size(); + + int sx = -1, sy = -1, tx = -1, ty = -1; + for(int i = 0; i < R && (sx == -1 || tx == -1); i ++) + for(int j = 0; j < C && (sx == -1 || tx == -1); j ++) + if(maze[i][j] == 'S') sx = i, sy = j; + else if(maze[i][j] == 'T') tx = i, ty = j; + + vector> tdis = bfs(maze, tx, ty); + if(tdis[sx][sy] == INF) return -1; + + vector> w(R, vector(C, INF)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(maze[i][j] == 'S' || maze[i][j] == 'T'){ + w[i][j] = 0; continue; + } + if(maze[i][j] == '#') continue; + + int res = 0; + if(maze[R - 1 - i][j] != '#') res = max(res, tdis[R - 1 - i][j]); + if(maze[i][C - 1 - j] != '#') res = max(res, tdis[i][C - 1 - j]); + w[i][j] = res; + } + + vector> res = dij(maze, w, sx, sy); + return res[tx][ty] == INF ? -1 : res[tx][ty]; + } + +private: + vector> dij(const vector& maze, const vector>& w, int sx, int sy){ + + vector> dis(R, vector(C, INF)); + dis[sx][sy] = 0; + vector> visited(R, vector(C, false)); + priority_queue, vector>, greater<>> pq; + pq.push({0, sx * C + sy}); + while(!pq.empty()){ + int cd = pq.top().first, cx = pq.top().second / C, cy = pq.top().second % C; pq.pop(); + + if(visited[cx][cy]) continue; + visited[cx][cy] = true; + + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && maze[nx][ny] != '#' && dis[nx][ny] > max(cd, w[nx][ny])){ + dis[nx][ny] = max(cd, w[nx][ny]); + pq.push({dis[nx][ny], nx * C + ny}); + } + } + } + return dis; + } + + vector> bfs(const vector& maze, int sx, int sy){ + + vector> dis(R, vector(C, INF)); + dis[sx][sy] = 0; + queue> q; + q.push({sx, sy}); + while(!q.empty()){ + int cx = q.front().first, cy = q.front().second; q.pop(); + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && maze[nx][ny] != '#' && dis[nx][ny] == INF){ + dis[nx][ny] = dis[cx][cy] + 1; + q.push({nx, ny}); + } + } + } + return dis; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP076/cpp-LCP076/CMakeLists.txt b/LC/LCP076/cpp-LCP076/CMakeLists.txt new file mode 100644 index 00000000..69cec56c --- /dev/null +++ b/LC/LCP076/cpp-LCP076/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(E) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(E main.cpp) diff --git a/LC/LCP076/cpp-LCP076/main.cpp b/LC/LCP076/cpp-LCP076/main.cpp new file mode 100644 index 00000000..d41bd34d --- /dev/null +++ b/LC/LCP076/cpp-LCP076/main.cpp @@ -0,0 +1,137 @@ +/// Source : https://leetcode.cn/problems/1ybDKD/ +/// Author : liuyubobobo +/// Time : 2023-04-23 + +#include +#include +#include +#include + +using namespace std; + + +/// State Compression DP +/// Time Complexity: O(3^(min(R, C))^2 * max(R, C)) +/// Space Complexity: O(3^(min(R, C))^3) + +int pow3[6]; +int new_states[243][243][243][2]; +long long dp[30][243][243]; + +class Solution { + +private: + int R, C; + + +public: + long long getSchemeCount(int n, int m, vector& chessboard) { + + pow3[0] = 1; + for(int i = 1; i <= 5; i ++) pow3[i] = pow3[i - 1] * 3ll; + + memset(new_states, -1, sizeof(new_states)); + + R = chessboard.size(), C = chessboard[0].size(); + if(C > R) chessboard = rotate(chessboard), swap(R, C); + + memset(dp, -1, sizeof(dp)); + return dfs(chessboard, 0, 0, 0); + } + +private: + long long dfs(const vector& chessboard, int index, int state0, int state1){ + + if(index == R) return 1; + if(dp[index][state0][state1] != -1) return dp[index][state0][state1]; + + vector ok_states; + get_ok_states(chessboard[index], state0, state1, 0, 0, 0, ok_states); + + long long res = 0; + for(int state: ok_states){ + + if(new_states[state0][state1][state][0] == -1){ + vector states = {state0, state1, state}; + get_new_state(states, new_states[state0][state1][state]); + } + + res += dfs(chessboard, index + 1, new_states[state0][state1][state][0], new_states[state0][state1][state][1]); + } + return dp[index][state0][state1] = res; + } + + void get_new_state(const vector& states, int res[2]){ + + res[0] = res[1] = 0; + for(int j = 0; j < 5; j ++){ + int write_pos = 1; + for(int i = 2; i >= 0 && write_pos >= 0; i --){ + int val = states[i] / pow3[5 - 1 - j] % 3; + if(val) res[write_pos --] += val * pow3[5 - 1 - j]; + } + } + } + + void get_ok_states(const string& s, int state0, int state1, int index, int state, + int pre, vector& ok_states){ + + if(index == C){ + ok_states.push_back(state); + return; + } + + int l, r; + if(s[index] != '?') l = r = (s[index] == '.' ? 0 : (s[index] == 'R' ? 1 : 2)); + else l = 0, r = 2; + + for(int val = l; val <= r; val ++){ + int val0 = state0 / pow3[C - 1 - index] % 3, val1 = state1 / pow3[C - 1 - index] % 3; + if(val0 && val1 && val && val != val0) continue; + if(pre / 3 % 3 && val && pre / 3 % 3 != val) continue; + + if(val) pre = pre * 3 + val; + get_ok_states(s, state0, state1, index + 1, state * 3 + val, pre, ok_states); + if(val) pre /= 3; + } + return; + } + + vector rotate(const vector& A){ + + int r = A.size(), c = A[0].size(); + + vector B(c, string(r, ' ')); + for(int i = 0; i < r; i ++) + for(int j = 0; j < c; j ++) + B[j][R - 1 - i] = A[i][j]; + return B; + } +}; + + +int main() { + + vector chessboard1 = {"..R","..B","?R?"}; + cout << Solution().getSchemeCount(3, 3, chessboard1) << '\n'; + // 5 + + vector chessboard2 = {"?R?","B?B","?R?"}; + cout << Solution().getSchemeCount(3, 3, chessboard2) << '\n'; + // 105 + + vector chessboard3 = {"?B?B","R?.?"}; + cout << Solution().getSchemeCount(2, 4, chessboard3) << '\n'; + // 42 + + vector chessboard4 = {"????","???.","????","??R?"}; + cout << Solution().getSchemeCount(4, 4, chessboard4) << '\n'; + // 322765 + + vector chessboard5 = { + "??.B??","??????","??B???","??????","???.?." + }; + cout << Solution().getSchemeCount(5, 6, chessboard5) << '\n'; + + return 0; +} diff --git a/LC/readme.md b/LC/readme.md index 3766ed8e..176f3e0d 100644 --- a/LC/readme.md +++ b/LC/readme.md @@ -3,6 +3,12 @@ | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | +| LCP076 | [魔法棋盘](https://leetcode.cn/problems/1ybDKD/) | [无] | [C++](LCP0076/cpp-LCP076/) | | | +| LCP075 | [传送卷轴](https://leetcode.cn/problems/rdmXM7/) | [无] | [C++](LCP0075/cpp-LCP075/) | | | +| LCP074 | [最强祝福力场](https://leetcode.cn/problems/xepqZ5/) | [无] | [C++](LCP0074/cpp-LCP074/) | | | +| LCP073 | [探险营地](https://leetcode.cn/problems/0Zeoeg/) | [无] | [C++](LCP0073/cpp-LCP073/) | | | +| LCP072 | [补给马车](https://leetcode.cn/problems/hqCnmP/) | [无] | [C++](LCP0072/cpp-LCP072/) | | | +| | | | | | | | LCP070 | [沙地治理](https://leetcode.cn/problems/XxZZjK/) | [无] | [C++](LCP0070/cpp-LCP070/) | | | | LCP069 | [Hello LeetCode!](https://leetcode.cn/problems/rMeRt2/) | [无] | [C++](LCP0069/cpp-LCP069/) | | | | LCP068 | [美观的花束](https://leetcode.cn/problems/1GxJYY/) | [无] | [C++](LCP0068/cpp-LCP068/) | | | From 6310fc74ad64eb79bfd52d0b33fb36ef85a1e79a Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Apr 2023 21:09:29 -0700 Subject: [PATCH 338/390] 2656-2659 solved. --- .../cpp-2656/CMakeLists.txt | 6 + .../cpp-2656/main.cpp | 28 +++++ .../cpp-2657/CMakeLists.txt | 6 + .../cpp-2657/main.cpp | 36 ++++++ .../cpp-2658/CMakeLists.txt | 6 + .../cpp-2658/main.cpp | 56 +++++++++ .../cpp-2659/CMakeLists.txt | 6 + .../2659-Make-Array-Empty/cpp-2659/main.cpp | 118 ++++++++++++++++++ readme.md | 5 + 9 files changed, 267 insertions(+) create mode 100644 2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/CMakeLists.txt create mode 100644 2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/main.cpp create mode 100644 2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/CMakeLists.txt create mode 100644 2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/main.cpp create mode 100644 2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/CMakeLists.txt create mode 100644 2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/main.cpp create mode 100644 2501-3000/2659-Make-Array-Empty/cpp-2659/CMakeLists.txt create mode 100644 2501-3000/2659-Make-Array-Empty/cpp-2659/main.cpp diff --git a/2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/CMakeLists.txt b/2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/CMakeLists.txt new file mode 100644 index 00000000..7386ba30 --- /dev/null +++ b/2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2656) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2656 main.cpp) diff --git a/2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/main.cpp b/2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/main.cpp new file mode 100644 index 00000000..5c937436 --- /dev/null +++ b/2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/main.cpp @@ -0,0 +1,28 @@ +/// Source : https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int maximizeSum(vector& nums, int k) { + + int start = *max_element(nums.begin(), nums.end()); + return (start + start + k - 1) * k / 2; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/CMakeLists.txt b/2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/CMakeLists.txt new file mode 100644 index 00000000..56486179 --- /dev/null +++ b/2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2657) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2657 main.cpp) diff --git a/2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/main.cpp b/2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/main.cpp new file mode 100644 index 00000000..8a919aa0 --- /dev/null +++ b/2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + vector findThePrefixCommonArray(vector& A, vector& B) { + + set Aset; + vector res(A.size()); + for (int i = 0; i < A.size(); i++) { + Aset.insert(A[i]); + int cnt = 0; + for(int j = 0; j <= i; j ++) + cnt += Aset.count(B[j]); + res[i] = cnt; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/CMakeLists.txt b/2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/CMakeLists.txt new file mode 100644 index 00000000..ab98fcd4 --- /dev/null +++ b/2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2658) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2658 main.cpp) diff --git a/2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/main.cpp b/2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/main.cpp new file mode 100644 index 00000000..68f25292 --- /dev/null +++ b/2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { + +private: + const int dirs[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + int R, C; + +public: + int findMaxFish(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + vector> visited(R, vector(C, false)); + int res = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(grid[i][j] == 0 || visited[i][j]) continue; + res = max(res, dfs(grid, i, j, visited)); + } + return res; + } + +private: + int dfs(const vector>& grid, int cx, int cy, vector>& visited){ + + int res = grid[cx][cy]; + visited[cx][cy] = true; + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && grid[nx][ny] && !visited[nx][ny]) + res += dfs(grid, nx, ny, visited); + } + return res; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2659-Make-Array-Empty/cpp-2659/CMakeLists.txt b/2501-3000/2659-Make-Array-Empty/cpp-2659/CMakeLists.txt new file mode 100644 index 00000000..ba6a80a8 --- /dev/null +++ b/2501-3000/2659-Make-Array-Empty/cpp-2659/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2659) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2659 main.cpp) diff --git a/2501-3000/2659-Make-Array-Empty/cpp-2659/main.cpp b/2501-3000/2659-Make-Array-Empty/cpp-2659/main.cpp new file mode 100644 index 00000000..10f4bd1d --- /dev/null +++ b/2501-3000/2659-Make-Array-Empty/cpp-2659/main.cpp @@ -0,0 +1,118 @@ +/// Source : https://leetcode.com/problems/make-array-empty/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Using BIT +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class BIT{ + +private: + int n; + vector data; + vector tree; + +public: + BIT(const vector& arr) : n(arr.size()), data(arr.begin(), arr.end()), tree(n + 1){ + + for(int i = 0; i < n; i ++) + tree[i + 1]= data[i]; + + for(int i = 1; i <= n; i ++){ + int parent = i + (i & -i); + if(parent <= n) tree[parent] += tree[i]; + } + } + + BIT(int n): n(n), data(n, 0), tree(n + 1, 0){} + + void add(int index, T v){ + assert(0 <= index && index < n); + + data[index] += v; + + index ++; + for(;index <= n; index += (index & -index)) + tree[index] += v; + } + + T query(int l, int r){ + if(!(0 <= l && l < n) || !(0 <= r && r < n)) return 0; + if(l > r) return 0; + return prefix_sum(r + 1) - prefix_sum(l); + } + + void set(int index, T v){ + assert(0 <= index && index < n); + add(index, v - data[index]); + } + +private: + // r is 1-based + T prefix_sum(int r){ + + T res = 0; + for(; r; r -= (r & -r)) + res += tree[r]; + return res; + } +}; + +class Solution { +public: + long long countOperationsToEmptyArray(vector& nums) { + + int n = nums.size(); + vector> order(n); + for(int i = 0; i < n; i ++) order[i] = {nums[i], i}; + sort(order.begin(), order.end()); + + BIT bit(vector(n, 1)); + + int front = 0; + long long res = 0; + for(const pair& p: order){ + int index = p.second; + if(index > front){ + res += bit.query(front, index - 1); + bit.set(index, 0); + res ++; + front = (index + 1) % n; + } + else if(index < front){ + res += bit.query(front, n - 1); + res += bit.query(0, index - 1); + bit.set(index, 0); + res ++; + front = (index + 1) % n; + } + else{ + bit.set(index, 0); + res ++; + front = (index + 1) % n; + } + } + return res; + + } +}; + + +int main() { + + vector nums1 = {-15, -19, 5}; + cout << Solution().countOperationsToEmptyArray(nums1) << endl; + // 5 + + return 0; +} diff --git a/readme.md b/readme.md index 82ed2f82..5b22d537 100644 --- a/readme.md +++ b/readme.md @@ -2524,6 +2524,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2645 | [Minimum Additions to Make Valid String](https://leetcode.com/problems/minimum-additions-to-make-valid-string/) | [无] | [C++](2001-2500/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/) | | | | 2646 | [Minimize the Total Price of the Trips](https://leetcode.com/problems/minimize-the-total-price-of-the-trips/) | [无] | [C++](2001-2500/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/) | | | | | | | | | | +| 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/) | [无] | [C++](2001-2500/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/) | | | +| 2657 | [Find the Prefix Common Array of Two Arrays](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [无] | [C++](2001-2500/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/) | | | +| 2658 | [Maximum Number of Fish in a Grid](https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/) | [无] | [C++](2001-2500/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/) | | | +| 2659 | [Make Array Empty](https://leetcode.com/problems/make-array-empty/) | [无] | [C++](2001-2500/2659-Make-Array-Empty/cpp-2659/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 25d56c6e161f79459b4d5d4987a1304f97252686 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Apr 2023 22:18:38 -0700 Subject: [PATCH 339/390] 2660-2663 solved. --- .../cpp-2660/CMakeLists.txt | 6 ++ .../cpp-2660/main.cpp | 39 ++++++++ .../cpp-2661/CMakeLists.txt | 6 ++ .../cpp-2661/main.cpp | 41 +++++++++ .../cpp-2662/CMakeLists.txt | 6 ++ .../cpp-2662/main.cpp | 91 +++++++++++++++++++ .../cpp-2663/CMakeLists.txt | 6 ++ .../cpp-2663/main.cpp | 69 ++++++++++++++ readme.md | 4 + 9 files changed, 268 insertions(+) create mode 100644 2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/CMakeLists.txt create mode 100644 2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/main.cpp create mode 100644 2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/CMakeLists.txt create mode 100644 2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/main.cpp create mode 100644 2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/CMakeLists.txt create mode 100644 2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/main.cpp create mode 100644 2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/CMakeLists.txt create mode 100644 2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/main.cpp diff --git a/2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/CMakeLists.txt b/2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/CMakeLists.txt new file mode 100644 index 00000000..b69c4cdd --- /dev/null +++ b/2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2660) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2660 main.cpp) diff --git a/2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/main.cpp b/2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/main.cpp new file mode 100644 index 00000000..fb1c9282 --- /dev/null +++ b/2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/determine-the-winner-of-a-bowling-game/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int isWinner(vector& player1, vector& player2) { + + int a = get_score(player1), b = get_score(player2); + return (a == b ? 0 : (a > b ? 1 : 2)); + } + +private: + int get_score(const vector& v){ + int res = 0; + for(int i = 0; i < v.size(); i ++){ + bool double_score = false; + if(i - 1 >= 0 && v[i - 1] == 10) double_score = true; + if(i - 2 >= 0 && v[i - 2] == 10) double_score = true; + res += (double_score ? 2 : 1) * v[i]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/CMakeLists.txt b/2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/CMakeLists.txt new file mode 100644 index 00000000..29110081 --- /dev/null +++ b/2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2661) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2661 main.cpp) diff --git a/2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/main.cpp b/2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/main.cpp new file mode 100644 index 00000000..0a19ac9d --- /dev/null +++ b/2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/first-completely-painted-row-or-column/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { +public: + int firstCompleteIndex(vector& arr, vector>& mat) { + + int R = mat.size(), C = mat[0].size(); + vector> pos(R * C + 1); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + pos[mat[i][j]] = {i, j}; + + vector rows(R, C), cols(C, R); + for(int i = 0; i < arr.size(); i ++){ + int r = pos[arr[i]].first, c = pos[arr[i]].second; + rows[r] --, cols[c] --; + if(rows[r] == 0 || cols[c] == 0) return i; + } + return -1; + } +}; + + +int main() { + + + + + return 0; +} diff --git a/2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/CMakeLists.txt b/2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/CMakeLists.txt new file mode 100644 index 00000000..31ec1a99 --- /dev/null +++ b/2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2662) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2662 main.cpp) diff --git a/2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/main.cpp b/2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/main.cpp new file mode 100644 index 00000000..5905d18b --- /dev/null +++ b/2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/main.cpp @@ -0,0 +1,91 @@ +/// Source : https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// Dijkstra +/// Time Complexity: O(n^2 * logn) +/// Space Complexity: O(n^2) +class Solution { +public: + int minimumCost(vector& start, vector& target, vector>& specialRoads) { + + set> vertexs; + vertexs.insert({start[0], start[1]}); + vertexs.insert({target[0], target[1]}); + for(const vector& v: specialRoads) { + vertexs.insert({v[0], v[1]}); + vertexs.insert({v[2], v[3]}); + } + + vector> index2v(vertexs.begin(), vertexs.end()); + map, int> v2index; + for(int i = 0; i < index2v.size(); i++) v2index[index2v[i]] = i; + + int n = index2v.size(); + int s = v2index[{start[0], start[1]}], t = v2index[{target[0], target[1]}]; + + vector> g(n, vector(n, INT_MAX / 2)); + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + g[i][j] = g[j][i] = get_dis(index2v[i], index2v[j]); + + for(const vector& v: specialRoads) { + int a = v2index[{v[0], v[1]}], b = v2index[{v[2], v[3]}], w = v[4]; + if(w < g[a][b]) g[a][b] = w; + } + + + return dij(n, g, s, t); + } + +private: + int dij(int n, const vector>& g, int s, int t){ + + vector visited(n, false); + vector dis(n, INT_MAX / 2); + priority_queue, vector>, greater<>> pq; + pq.push({0, s}); + dis[s] = 0; + while(!pq.empty()){ + auto [d, u] = pq.top(); pq.pop(); + + if(visited[u]) continue; + visited[u] = true; + + dis[u] = d; + for(int v = 0; v < n; v ++){ + int w = g[u][v]; + if(!visited[v] && dis[v] > dis[u] + w){ + dis[v] = dis[u] + w; + pq.push({dis[v], v}); + } + } + } + return dis[t]; + } + + int get_dis(const pair& p1, const pair& p2){ + return abs(p1.first - p2.first) + abs(p1.second - p2.second); + } +}; + + +int main() { + + vector start1 = {1, 1}, target1 = {4, 5}; + vector> specialRoads1 = {{1, 2, 3, 3, 2}, {3, 4, 4, 5, 1}}; + cout << Solution().minimumCost(start1, target1, specialRoads1) << endl; + // 5 + + return 0; +} diff --git a/2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/CMakeLists.txt b/2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/CMakeLists.txt new file mode 100644 index 00000000..a20c2964 --- /dev/null +++ b/2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2663) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2663 main.cpp) diff --git a/2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/main.cpp b/2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/main.cpp new file mode 100644 index 00000000..333acab6 --- /dev/null +++ b/2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/main.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.com/problems/lexicographically-smallest-beautiful-string/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string smallestBeautifulString(string s, int k) { + + int n = s.size(), pos = n - 1; + while(add1(n, s, k, pos)){ + int pre = pos; + pos = check(pos, s); + if(pos == -1){ + for(int i = pre + 1; i < n; i ++){ + for(char c = 'a'; c < 'a' + k; c ++){ + if(i - 1 >= 0 && s[i - 1] == c) continue; + if(i - 2 >= 0 && s[i - 2] == c) continue; + s[i] = c; + break; + } + } + return s; + } + } + return ""; + } + +private: + int check(int pos, const string& s){ + + for(int i = pos; i > 0; i --){ + if(s[i] == s[i - 1]) return i; + if(i - 2 >= 0 && s[i] == s[i - 2]) return i; + } + return -1; + } + + bool add1(int n, string& s, int k, int pos){ + s[pos] += 1; + for(int i = pos; i > 0; i --) + if(s[i] == 'a' + k){ + s[i - 1] += 1; + s[i] = 'a'; + } + else break; + if(s[0] == 'a' + k) return false; + + return true; + } +}; + +int main() { + +// cout << Solution().smallestBeautifulString("abcz", 26) << endl; +// // abda + + cout << Solution().smallestBeautifulString("ponponp", 16) << endl; + + return 0; +} diff --git a/readme.md b/readme.md index 5b22d537..9722f779 100644 --- a/readme.md +++ b/readme.md @@ -2528,6 +2528,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2657 | [Find the Prefix Common Array of Two Arrays](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [无] | [C++](2001-2500/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/) | | | | 2658 | [Maximum Number of Fish in a Grid](https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/) | [无] | [C++](2001-2500/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/) | | | | 2659 | [Make Array Empty](https://leetcode.com/problems/make-array-empty/) | [无] | [C++](2001-2500/2659-Make-Array-Empty/cpp-2659/) | | | +| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game/) | [无] | [C++](2001-2500/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/) | | | +| 2661 | [First Completely Painted Row or Column](https://leetcode.com/problems/first-completely-painted-row-or-column/) | [无] | [C++](2001-2500/2661-First-Completely-Painted-Row-or-Column/cpp-2661/) | | | +| 2662 | [Minimum Cost of a Path With Special Roads](https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/) | [无] | [C++](2001-2500/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/) | | | +| 2663 | [Lexicographically Smallest Beautiful String](https://leetcode.com/problems/lexicographically-smallest-beautiful-string/) | [无] | [C++](2001-2500/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 10aa24d4d40ee0b60a56488281a4c35b35cdadce Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Apr 2023 22:36:47 -0700 Subject: [PATCH 340/390] 2655 solved. --- .../cpp-2655/CMakeLists.txt | 6 +++ .../cpp-2655/main.cpp | 45 +++++++++++++++++++ readme.md | 1 + 3 files changed, 52 insertions(+) create mode 100644 2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/CMakeLists.txt create mode 100644 2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/main.cpp diff --git a/2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/CMakeLists.txt b/2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/CMakeLists.txt new file mode 100644 index 00000000..20493d62 --- /dev/null +++ b/2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2655) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2655 main.cpp) diff --git a/2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/main.cpp b/2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/main.cpp new file mode 100644 index 00000000..652bd8e3 --- /dev/null +++ b/2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/find-maximal-uncovered-ranges/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector> findMaximalUncoveredRanges(int n, vector>& ranges) { + + sort(ranges.begin(), ranges.end()); + + vector> segs; + for(const vector& range: ranges){ + int a = range[0], b = range[1]; + if(segs.empty() || segs.back().second + 1 < a) + segs.push_back({a, b}); + else segs.back().second = max(segs.back().second, b); + } + + if(segs.empty()) return vector>(1, {0, n - 1}); + + vector> res; + if(segs[0].first) res.push_back({0, segs[0].first - 1}); + for(int i = 1; i < segs.size(); i++) + res.push_back({segs[i - 1].second + 1, segs[i].first - 1}); + if(segs.back().second + 1 < n) + res.push_back({segs.back().second + 1, n - 1}); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9722f779..0aacd6be 100644 --- a/readme.md +++ b/readme.md @@ -2524,6 +2524,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2645 | [Minimum Additions to Make Valid String](https://leetcode.com/problems/minimum-additions-to-make-valid-string/) | [无] | [C++](2001-2500/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/) | | | | 2646 | [Minimize the Total Price of the Trips](https://leetcode.com/problems/minimize-the-total-price-of-the-trips/) | [无] | [C++](2001-2500/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/) | | | | | | | | | | +| 2655 | [Find Maximal Uncovered Ranges](https://leetcode.com/problems/find-maximal-uncovered-ranges/description/) | [无] | [C++](2001-2500/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/) | | | | 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/) | [无] | [C++](2001-2500/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/) | | | | 2657 | [Find the Prefix Common Array of Two Arrays](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [无] | [C++](2001-2500/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/) | | | | 2658 | [Maximum Number of Fish in a Grid](https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/) | [无] | [C++](2001-2500/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/) | | | From da7fa74a00b7813a52b4f301562887d4990330da Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Apr 2023 22:47:42 -0700 Subject: [PATCH 341/390] 2651-2654 solved. --- .../cpp-2651/CMakeLists.txt | 6 ++ .../cpp-2651/main.cpp | 24 +++++++ .../cpp-2652/CMakeLists.txt | 6 ++ .../2652-Sum-Multiples/cpp-2652/main.cpp | 30 ++++++++ .../cpp-2653/CMakeLists.txt | 6 ++ .../cpp-2653/main.cpp | 51 +++++++++++++ .../cpp-2654/CMakeLists.txt | 6 ++ .../cpp-2654/main.cpp | 71 +++++++++++++++++++ readme.md | 4 ++ 9 files changed, 204 insertions(+) create mode 100644 2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/CMakeLists.txt create mode 100644 2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/main.cpp create mode 100644 2501-3000/2652-Sum-Multiples/cpp-2652/CMakeLists.txt create mode 100644 2501-3000/2652-Sum-Multiples/cpp-2652/main.cpp create mode 100644 2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/CMakeLists.txt create mode 100644 2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/main.cpp create mode 100644 2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/CMakeLists.txt create mode 100644 2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/main.cpp diff --git a/2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/CMakeLists.txt b/2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/CMakeLists.txt new file mode 100644 index 00000000..6b632806 --- /dev/null +++ b/2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2651) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2651 main.cpp) diff --git a/2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/main.cpp b/2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/main.cpp new file mode 100644 index 00000000..c4cb9790 --- /dev/null +++ b/2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/main.cpp @@ -0,0 +1,24 @@ +/// Source : https://leetcode.com/problems/calculate-delayed-arrival-time/description/ +/// Author : liuyubobobo +/// Time : 2023-04-23 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int findDelayedArrivalTime(int arrivalTime, int delayedTime) { + return (arrivalTime + delayedTime) % 24; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2652-Sum-Multiples/cpp-2652/CMakeLists.txt b/2501-3000/2652-Sum-Multiples/cpp-2652/CMakeLists.txt new file mode 100644 index 00000000..cbe90ee9 --- /dev/null +++ b/2501-3000/2652-Sum-Multiples/cpp-2652/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2652) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2652 main.cpp) diff --git a/2501-3000/2652-Sum-Multiples/cpp-2652/main.cpp b/2501-3000/2652-Sum-Multiples/cpp-2652/main.cpp new file mode 100644 index 00000000..c91b1f5d --- /dev/null +++ b/2501-3000/2652-Sum-Multiples/cpp-2652/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/sum-multiples/description/ +/// Author : liuyubobobo +/// Time : 2023-04-23 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int sumOfMultiples(int n) { + + int sum = 0; + for(int i = 1; i <= n; i ++) + if(i % 3 == 0 || i % 5 == 0 || i % 7 == 0) + sum += i; + return sum; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/CMakeLists.txt b/2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/CMakeLists.txt new file mode 100644 index 00000000..38d019a2 --- /dev/null +++ b/2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2653) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2653 main.cpp) diff --git a/2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/main.cpp b/2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/main.cpp new file mode 100644 index 00000000..a2ad8248 --- /dev/null +++ b/2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/sliding-subarray-beauty/description/ +/// Author : liuyubobobo +/// Time : 2023-04-23 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n * |max_num - min_num|) +/// Space Complexity: O(|max_num - min_num|) +class Solution { + +private: + const int OFFSET = 50; + +public: + vector getSubarrayBeauty(vector& nums, int k, int x) { + + vector f(101, 0); + + for(int i = 0; i < k - 1; i ++) f[nums[i] + OFFSET] ++; + + vector res; + for(int i = k - 1; i < nums.size(); i ++){ + f[nums[i] + OFFSET] ++; + + res.push_back(min(0, get_kth(f, x))); + f[nums[i - (k - 1)] + OFFSET] --; + } + return res; + } + +private: + int get_kth(vector& f, int k){ + int sum = 0; + for(int i = 0; i <= 100; i ++){ + sum += f[i]; + if(sum >= k) return i - OFFSET; + } + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/CMakeLists.txt b/2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/CMakeLists.txt new file mode 100644 index 00000000..1ae9fdf6 --- /dev/null +++ b/2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2654) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2654 main.cpp) diff --git a/2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/main.cpp b/2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/main.cpp new file mode 100644 index 00000000..9ce97ccb --- /dev/null +++ b/2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/main.cpp @@ -0,0 +1,71 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n^3) +/// Space Complexity: O(1) +class Solution { +public: + int minOperations(vector& nums) { + + int n = nums.size(), g = gcd(nums, 0, n - 1); + if(g > 1) return -1; + + int one_cnt = count(nums.begin(), nums.end(), 1); + if(one_cnt) return n - one_cnt; + + int min_len = n; + for(int s = 0; s < n; s ++) + for(int t = s + 1; t < n; t ++){ + int g = gcd(nums, s, t); + if(g == 1) min_len = min(min_len, t - s + 1); + } + return min_len - 1 + n - 1; + } + +private: + int gcd(vector& nums, int s, int t){ + int g = nums[s]; + for(int i = s + 1; i <= t; i ++) g = gcd(g, nums[i]); + return g; + } + + template + T gcd(T a, T b){ + if(a < b) swap(a, b); + while(b){ + int t = a % b; + a = b; + b = t; + } + return a; + } +}; + + +int main() { + + vector nums1 = {2, 6, 3, 4}; + cout << Solution().minOperations(nums1) << '\n'; + // 4 + + vector nums2 = {2, 10, 6, 14}; + cout << Solution().minOperations(nums2) << '\n'; + // -1 + + vector nums3 = {6, 10, 15}; + cout << Solution().minOperations(nums3) << '\n'; + // 4 + + return 0; +} diff --git a/readme.md b/readme.md index 0aacd6be..41b59aba 100644 --- a/readme.md +++ b/readme.md @@ -2524,6 +2524,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2645 | [Minimum Additions to Make Valid String](https://leetcode.com/problems/minimum-additions-to-make-valid-string/) | [无] | [C++](2001-2500/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/) | | | | 2646 | [Minimize the Total Price of the Trips](https://leetcode.com/problems/minimize-the-total-price-of-the-trips/) | [无] | [C++](2001-2500/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/) | | | | | | | | | | +| 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time/) | [无] | [C++](2001-2500/2651-Calculate-Delayed-Arrival-Time/cpp-2651/) | | | +| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples/) | [无] | [C++](2001-2500/2652-Sum-Multiples/cpp-2652/) | | | +| 2653 | [Sliding Subarray Beauty](https://leetcode.com/problems/sliding-subarray-beauty/) | [无] | [C++](2001-2500/2653-Sliding-Subarray-Beauty/cpp-2653/) | | | +| 2654 | [Minimum Number of Operations to Make All Array Elements Equal to 1](https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/) | [无] | [C++](2001-2500/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/) | | | | 2655 | [Find Maximal Uncovered Ranges](https://leetcode.com/problems/find-maximal-uncovered-ranges/description/) | [无] | [C++](2001-2500/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/) | | | | 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/) | [无] | [C++](2001-2500/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/) | | | | 2657 | [Find the Prefix Common Array of Two Arrays](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [无] | [C++](2001-2500/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/) | | | From 100a5fcd565a80c9b1328e952cab170623762f49 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 30 Apr 2023 22:56:31 -0700 Subject: [PATCH 342/390] 1065 solved. --- .../cpp-1065/CMakeLists.txt | 6 +++ .../cpp-1065/main.cpp | 40 +++++++++++++++++++ readme.md | 1 + 3 files changed, 47 insertions(+) create mode 100644 1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/CMakeLists.txt create mode 100644 1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/main.cpp diff --git a/1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/CMakeLists.txt b/1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/CMakeLists.txt new file mode 100644 index 00000000..34246b31 --- /dev/null +++ b/1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1065) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1065 main.cpp) diff --git a/1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/main.cpp b/1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/main.cpp new file mode 100644 index 00000000..b45b56e4 --- /dev/null +++ b/1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/index-pairs-of-a-string/description/ +/// Author : liuyubobobo +/// Time : 2023-04-30 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(|words|^2 * |text|) +/// Space Complexity: O(1) +class Solution { +public: + vector> indexPairs(string text, vector& words) { + + vector> res; + for(const string& word: words) { + int pos = 0; + while(true){ + pos = text.find(word, pos); + if(pos != string::npos) { + res.push_back({pos, pos + (int) word.size() - 1}); + pos = pos + 1; + } + else break; + } + } + sort(res.begin(), res.end()); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 41b59aba..5497ee0c 100644 --- a/readme.md +++ b/readme.md @@ -1064,6 +1064,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1061 | [Lexicographically Smallest Equivalent String](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/description/) | [无] | [C++](1001-1500/1061-Lexicographically-Smallest-Equivalent-String/cpp-1061/) | | | | 1062 | [Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/) | [无] | [C++](1001-1500/1062-Longest-Repeating-Substring/cpp-1062/) | | | | | | | | | | +| 1065 | [Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/description/) | [无] | [C++](1001-1500/1065-Index-Pairs-of-a-String/cpp-1065/) | | | | 1066 | [Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/) | [solution](https://leetcode.com/problems/campus-bikes-ii/solutions/1451959/campus-bikes-ii/) | [C++](1001-1500/1066-Campus-Bikes-II/cpp-1066/) | | | | | | | | | | | 1072 | [Flip Columns For Maximum Number of Equal Rows](https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/) | [无] | [C++](1001-1500/1072-Flip-Columns-For-Maximum-Number-of-Equal-Rows/cpp-1072/) | | | From 2b5b2f3d462ac7d21047aaf0cbb42148e8214ba3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 2 May 2023 22:06:12 -0700 Subject: [PATCH 343/390] 2647 solved. --- .../cpp-2647/CMakeLists.txt | 6 +++ .../cpp-2647/main.cpp | 39 +++++++++++++++++++ readme.md | 26 ++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 2501-3000/2647-Color-the-Triangle-Red/cpp-2647/CMakeLists.txt create mode 100644 2501-3000/2647-Color-the-Triangle-Red/cpp-2647/main.cpp diff --git a/2501-3000/2647-Color-the-Triangle-Red/cpp-2647/CMakeLists.txt b/2501-3000/2647-Color-the-Triangle-Red/cpp-2647/CMakeLists.txt new file mode 100644 index 00000000..82688815 --- /dev/null +++ b/2501-3000/2647-Color-the-Triangle-Red/cpp-2647/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2647) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2647 main.cpp) diff --git a/2501-3000/2647-Color-the-Triangle-Red/cpp-2647/main.cpp b/2501-3000/2647-Color-the-Triangle-Red/cpp-2647/main.cpp new file mode 100644 index 00000000..5c752e2f --- /dev/null +++ b/2501-3000/2647-Color-the-Triangle-Red/cpp-2647/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/color-the-triangle-red/description/ +/// Author : liuyubobobo +/// Time : 2023-05-02 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + vector> colorRed(int n) { + + vector> res; + for(int i = n; i >= 1; i -= 4){ + if(i < 4){ + res.push_back({1, 1}); + for(int j = 2; j <= i; j ++) res.push_back({j, 1}), res.push_back({j, 2 * j - 1}); + } + else{ + for(int j = 1; j <= 2 * i - 1; j += 2) res.push_back({i, j}); + res.push_back({i - 1, 2}); + for(int j = 3; j <= 2 * (i - 2) - 1; j += 2) res.push_back({i - 2, j}); + res.push_back({i - 3, 1}); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 5497ee0c..d5925ea0 100644 --- a/readme.md +++ b/readme.md @@ -2514,7 +2514,26 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2615 | [Sum of Distances](https://leetcode.com/problems/sum-of-distances/) | [无] | [C++](2001-2500/2615-Sum-of-Distances/cpp-2615/) | | | | 2616 | [Minimize the Maximum Difference of Pairs](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/) | [无] | [C++](2001-2500/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/) | | | | 2617 | [Minimum Number of Visited Cells in a Grid](https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/) | [无] | [C++](2001-2500/2617-Minimum-Number-of-Visited-Cells-in-a-Grid/cpp-2617/) | | | -| | | | | | | +| 2618 | JavaScript Problem | - | - | - | - | +| 2619 | JavaScript Problem | - | - | - | - | +| 2620 | JavaScript Problem | - | - | - | - | +| 2621 | JavaScript Problem | - | - | - | - | +| 2622 | JavaScript Problem | - | - | - | - | +| 2623 | JavaScript Problem | - | - | - | - | +| 2624 | JavaScript Problem | - | - | - | - | +| 2625 | JavaScript Problem | - | - | - | - | +| 2626 | JavaScript Problem | - | - | - | - | +| 2627 | JavaScript Problem | - | - | - | - | +| 2628 | JavaScript Problem | - | - | - | - | +| 2629 | JavaScript Problem | - | - | - | - | +| 2630 | JavaScript Problem | - | - | - | - | +| 2631 | JavaScript Problem | - | - | - | - | +| 2632 | JavaScript Problem | - | - | - | - | +| 2633 | JavaScript Problem | - | - | - | - | +| 2634 | JavaScript Problem | - | - | - | - | +| 2635 | JavaScript Problem | - | - | - | - | +| 2636 | JavaScript Problem | - | - | - | - | +| 2637 | JavaScript Problem | - | - | - | - | | 2638 | [Count the Number of K-Free Subsets](https://leetcode.com/problems/count-the-number-of-k-free-subsets/description/) | [无] | [C++](2001-2500/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/) | | | | 2639 | [Find the Width of Columns of a Grid](https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/) | [无] | [C++](2001-2500/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/) | | | | 2640 | [Find the Score of All Prefixes of an Array](https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/) | [无] | [C++](2001-2500/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/) | | | @@ -2524,7 +2543,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2644 | [Find the Maximum Divisibility Score](https://leetcode.com/problems/find-the-maximum-divisibility-score/) | [无] | [C++](2001-2500/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/) | | | | 2645 | [Minimum Additions to Make Valid String](https://leetcode.com/problems/minimum-additions-to-make-valid-string/) | [无] | [C++](2001-2500/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/) | | | | 2646 | [Minimize the Total Price of the Trips](https://leetcode.com/problems/minimize-the-total-price-of-the-trips/) | [无] | [C++](2001-2500/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/) | | | -| | | | | | | +| 2647 | [Color the Triangle Red](https://leetcode.com/problems/color-the-triangle-red/description/) | [无] | [C++](2001-2500/2647-Color-the-Triangle-Red/cpp-2647/) | | | +| 2648 | JavaScript Problem | - | - | - | - | +| 2649 | JavaScript Problem | - | - | - | - | +| 2650 | JavaScript Problem | - | - | - | - | | 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time/) | [无] | [C++](2001-2500/2651-Calculate-Delayed-Arrival-Time/cpp-2651/) | | | | 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples/) | [无] | [C++](2001-2500/2652-Sum-Multiples/cpp-2652/) | | | | 2653 | [Sliding Subarray Beauty](https://leetcode.com/problems/sliding-subarray-beauty/) | [无] | [C++](2001-2500/2653-Sliding-Subarray-Beauty/cpp-2653/) | | | From 7ff34d11ffedfd33bcc7a9687977e3ee3e39ab6d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 5 May 2023 18:55:59 -0700 Subject: [PATCH 344/390] 1419 bug fixed. --- .../cpp-1419/main.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/1001-1500/1419-Minimum-Number-of-Frogs-Croaking/cpp-1419/main.cpp b/1001-1500/1419-Minimum-Number-of-Frogs-Croaking/cpp-1419/main.cpp index 0ae56062..4611f59c 100644 --- a/1001-1500/1419-Minimum-Number-of-Frogs-Croaking/cpp-1419/main.cpp +++ b/1001-1500/1419-Minimum-Number-of-Frogs-Croaking/cpp-1419/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/minimum-number-of-frogs-croaking/ /// Author : liuyubobobo /// Time : 2020-04-18 +/// Updated: 2023-05-05 #include #include @@ -25,19 +26,19 @@ class Solution { else if(c == 'k') c = 'e'; else return -1; - vector record(5, 0); + vector waitfor(5, 0); int res = 0; for(char c: s){ - for(char x = 'a'; x < c; x ++) - if(record[x - 'a'] <= record[c - 'a']) return -1; - - record[c -'a'] ++; - res = max(res, record[c -'a']); - if(c == 'e') - for(int& e: record) e --; + int v = c - 'a'; + if(v == 0 && waitfor[0] == 0) res ++, waitfor[1] ++; + else{ + if(waitfor[v] == 0) return -1; + waitfor[v] --; + waitfor[(v + 1) % 5] ++; + } } - return res; + return waitfor[0] == res ? res : -1; } }; From d03014a384ce8fe6909d16507f352488e49d76f9 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 6 May 2023 21:42:22 -0700 Subject: [PATCH 345/390] 2670-2673 solved. --- .../cpp-2670/CMakeLists.txt | 6 ++ .../cpp-2670/main.cpp | 44 ++++++++++++ .../cpp-2671/CMakeLists.txt | 6 ++ .../2671-Frequency-Tracker/cpp-2671/main.cpp | 69 +++++++++++++++++++ .../cpp-2672/CMakeLists.txt | 6 ++ .../cpp-2672/main.cpp | 47 +++++++++++++ .../cpp-2673/CMakeLists.txt | 6 ++ .../cpp-2673/main.cpp | 39 +++++++++++ readme.md | 5 ++ 9 files changed, 228 insertions(+) create mode 100644 2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/CMakeLists.txt create mode 100644 2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/main.cpp create mode 100644 2501-3000/2671-Frequency-Tracker/cpp-2671/CMakeLists.txt create mode 100644 2501-3000/2671-Frequency-Tracker/cpp-2671/main.cpp create mode 100644 2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/CMakeLists.txt create mode 100644 2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/main.cpp create mode 100644 2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/CMakeLists.txt create mode 100644 2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/main.cpp diff --git a/2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/CMakeLists.txt b/2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/CMakeLists.txt new file mode 100644 index 00000000..8a306afe --- /dev/null +++ b/2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(A) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(A main.cpp) diff --git a/2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/main.cpp b/2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/main.cpp new file mode 100644 index 00000000..1e6a8032 --- /dev/null +++ b/2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/find-the-distinct-difference-array/ +/// Author : liuyubobobo +/// Time : 2023-05-06 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector distinctDifferenceArray(vector& nums) { + + int n = nums.size(); + + vector pre(n), suf(n + 1, 0); + set s; + for(int i = 0; i < n; i ++){ + s.insert(nums[i]); + pre[i] = s.size(); + } + + s.clear(); + for(int i = n - 1; i >= 0; i --){ + s.insert(nums[i]); + suf[i] = s.size(); + } + + vector res(n); + for(int i = 0; i < n; i ++) res[i] = pre[i] - suf[i + 1]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2671-Frequency-Tracker/cpp-2671/CMakeLists.txt b/2501-3000/2671-Frequency-Tracker/cpp-2671/CMakeLists.txt new file mode 100644 index 00000000..3e27b177 --- /dev/null +++ b/2501-3000/2671-Frequency-Tracker/cpp-2671/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(B) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(B main.cpp) diff --git a/2501-3000/2671-Frequency-Tracker/cpp-2671/main.cpp b/2501-3000/2671-Frequency-Tracker/cpp-2671/main.cpp new file mode 100644 index 00000000..522da15e --- /dev/null +++ b/2501-3000/2671-Frequency-Tracker/cpp-2671/main.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.com/problems/frequency-tracker/ +/// Author : liuyubobobo +/// Time : 2023-05-06 + +#include +#include +#include + +using namespace std; + + +/// Using Map and Set +/// Time Complexity: O(logn) for each operation +/// Space Complexity: O(n) +class FrequencyTracker { + +private: + map numberToFrequency; + multiset f; + +public: + FrequencyTracker() { + + } + + void add(int number) { + numberToFrequency[number] ++; + int new_f = numberToFrequency[number]; + int old_f = new_f - 1; + if(old_f) f.erase(f.find(old_f)); + f.insert(new_f); + } + + void deleteOne(int number) { + if(numberToFrequency.find(number) == numberToFrequency.end()) return; + + numberToFrequency[number] --; + int new_f = numberToFrequency[number]; + int old_f = new_f + 1; + f.erase(f.find(old_f)); + if(new_f) f.insert(new_f); + + if(new_f == 0) numberToFrequency.erase(number); + } + + bool hasFrequency(int frequency) { + return f.find(frequency) != f.end(); + } +}; + + +int main() { + +// ["FrequencyTracker","deleteOne","hasFrequency","hasFrequency","deleteOne","hasFrequency","hasFrequency","add","deleteOne","deleteOne"] +// [[],[5],[1],[1],[3],[1],[1],[7],[7],[7]] + + FrequencyTracker ft; + ft.deleteOne(5); + cout << ft.hasFrequency(1) << endl; + cout << ft.hasFrequency(1) << endl; + ft.deleteOne(3); + cout << ft.hasFrequency(1) << endl; + cout << ft.hasFrequency(1) << endl; + ft.add(7); + ft.deleteOne(7); + ft.deleteOne(7); + + return 0; +} diff --git a/2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/CMakeLists.txt b/2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/CMakeLists.txt new file mode 100644 index 00000000..6d269de2 --- /dev/null +++ b/2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(C) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(C main.cpp) diff --git a/2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/main.cpp b/2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/main.cpp new file mode 100644 index 00000000..5924d873 --- /dev/null +++ b/2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/description/ +/// Author : liuyubobobo +/// Time : 2023-05-06 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(q) +/// Space Complexity: O(n) +class Solution { +public: + vector colorTheArray(int n, vector>& queries) { + + vector nums(n, 0); + int cur = 0; + vector res(queries.size()); + for(int i = 0; i < queries.size(); i++) { + int index = queries[i][0], c = queries[i][1]; + int old_color = nums[index]; + if(c == old_color){ + res[i] = cur; continue; + } + + nums[index] = c; + if(old_color){ + if(index - 1 >= 0 && nums[index - 1] == old_color) cur --; + if(index + 1 < n && nums[index + 1] == old_color) cur --; + } + + if(index - 1 >= 0 && nums[index - 1] == c) cur ++; + if(index + 1 < n && nums[index + 1] == c) cur ++; + + res[i] = cur; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/CMakeLists.txt b/2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/CMakeLists.txt new file mode 100644 index 00000000..6a6bdfdb --- /dev/null +++ b/2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(D) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(D main.cpp) diff --git a/2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/main.cpp b/2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/main.cpp new file mode 100644 index 00000000..987b996a --- /dev/null +++ b/2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/ +/// Author : liuyubobobo +/// Time : 2023-05-06 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(h) +class Solution { +public: + int minIncrements(int n, vector& cost) { + + int res = 0; + dfs(n, 0, cost, res); + return res; + } + +private: + int dfs(int n, int cur, const vector& cost, int& res){ + + if(2 * cur + 1 >= n) return cost[cur]; + + int l = dfs(n, 2 * cur + 1, cost, res); + int r = dfs(n, 2 * cur + 2, cost, res); + res += abs(l - r); + return max(l, r) + cost[cur]; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d5925ea0..3538a2ee 100644 --- a/readme.md +++ b/readme.md @@ -2561,6 +2561,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2662 | [Minimum Cost of a Path With Special Roads](https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/) | [无] | [C++](2001-2500/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/) | | | | 2663 | [Lexicographically Smallest Beautiful String](https://leetcode.com/problems/lexicographically-smallest-beautiful-string/) | [无] | [C++](2001-2500/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/) | | | | | | | | | | +| 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array/) | [无] | [C++](2001-2500/2670-Find-the-Distinct-Difference-Array/cpp-2670/) | | | +| 2671 | [Frequency Tracker](https://leetcode.com/problems/frequency-tracker/) | [无] | [C++](2001-2500/2671-Frequency-Tracker/cpp-2671/) | | | +| 2672 | [Number of Adjacent Elements With the Same Color](https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/) | [无] | [C++](2001-2500/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/) | | | +| 2673 | [Make Costs of Paths Equal in a Binary Tree](https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/) | [无] | [C++](2001-2500/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From c60b26c94e30a5c040fe8728651f88bf35e5414b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 6 May 2023 21:52:01 -0700 Subject: [PATCH 346/390] 2664 solved. --- .../cpp-2664/CMakeLists.txt | 6 ++ .../2664-The-Knights-Tour/cpp-2664/main.cpp | 56 +++++++++++++++++++ readme.md | 1 + 3 files changed, 63 insertions(+) create mode 100644 2501-3000/2664-The-Knights-Tour/cpp-2664/CMakeLists.txt create mode 100644 2501-3000/2664-The-Knights-Tour/cpp-2664/main.cpp diff --git a/2501-3000/2664-The-Knights-Tour/cpp-2664/CMakeLists.txt b/2501-3000/2664-The-Knights-Tour/cpp-2664/CMakeLists.txt new file mode 100644 index 00000000..ac8ea56b --- /dev/null +++ b/2501-3000/2664-The-Knights-Tour/cpp-2664/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2664) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2664 main.cpp) diff --git a/2501-3000/2664-The-Knights-Tour/cpp-2664/main.cpp b/2501-3000/2664-The-Knights-Tour/cpp-2664/main.cpp new file mode 100644 index 00000000..5493c5bc --- /dev/null +++ b/2501-3000/2664-The-Knights-Tour/cpp-2664/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/the-knights-tour/description/ +/// Author : liuyubobobo +/// Time : 2023-05-06 + +#include +#include + +using namespace std; + + +/// Backtrack +/// Time Complexity: O(exp) +/// Space Complexity: O(R * C) +class Solution { + +private: + const int dirs[8][2] = { + {-2, -1}, + {-2, 1}, + {-1, -2}, + {-1, 2}, + {1, -2}, + {1, 2}, + {2, -1}, + {2, 1} + }; + +public: + vector> tourOfKnight(int R, int C, int r, int c) { + + vector> res(R, vector(C, -1)); + dfs(R, C, r, c, 0, res); + return res; + } + + bool dfs(int R, int C, int x, int y, int step, vector>& res) { + + res[x][y] = step; + if(step == R * C - 1) return true; + for (int i = 0; i < 8; ++i) { + int nx = x + dirs[i][0]; + int ny = y + dirs[i][1]; + if (nx >= 0 && nx < R && ny >= 0 && ny < C && res[nx][ny] == -1) { + if(dfs(R, C, nx, ny, step + 1, res)) return true; + } + } + res[x][y] = -1; + return false; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3538a2ee..a8d1aad3 100644 --- a/readme.md +++ b/readme.md @@ -2560,6 +2560,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2661 | [First Completely Painted Row or Column](https://leetcode.com/problems/first-completely-painted-row-or-column/) | [无] | [C++](2001-2500/2661-First-Completely-Painted-Row-or-Column/cpp-2661/) | | | | 2662 | [Minimum Cost of a Path With Special Roads](https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/) | [无] | [C++](2001-2500/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/) | | | | 2663 | [Lexicographically Smallest Beautiful String](https://leetcode.com/problems/lexicographically-smallest-beautiful-string/) | [无] | [C++](2001-2500/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/) | | | +| 2664 | [The Knight’s Tour](https://leetcode.com/problems/the-knights-tour/) | [无] | [C++](2001-2500/2664-The-Knights-Tour/cpp-2664/) | | | | | | | | | | | 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array/) | [无] | [C++](2001-2500/2670-Find-the-Distinct-Difference-Array/cpp-2670/) | | | | 2671 | [Frequency Tracker](https://leetcode.com/problems/frequency-tracker/) | [无] | [C++](2001-2500/2671-Frequency-Tracker/cpp-2671/) | | | From 785b50821656a4e8beea518c5fba407ffbc095ea Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 6 May 2023 21:55:34 -0700 Subject: [PATCH 347/390] readme updated. --- readme.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index a8d1aad3..869fe897 100644 --- a/readme.md +++ b/readme.md @@ -2561,7 +2561,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2662 | [Minimum Cost of a Path With Special Roads](https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/) | [无] | [C++](2001-2500/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/) | | | | 2663 | [Lexicographically Smallest Beautiful String](https://leetcode.com/problems/lexicographically-smallest-beautiful-string/) | [无] | [C++](2001-2500/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/) | | | | 2664 | [The Knight’s Tour](https://leetcode.com/problems/the-knights-tour/) | [无] | [C++](2001-2500/2664-The-Knights-Tour/cpp-2664/) | | | -| | | | | | | +| 2665 | JavaScript Problem | - | - | - | - | +| 2666 | JavaScript Problem | - | - | - | - | +| 2667 | JavaScript Problem | - | - | - | - | +| 2668 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2669 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array/) | [无] | [C++](2001-2500/2670-Find-the-Distinct-Difference-Array/cpp-2670/) | | | | 2671 | [Frequency Tracker](https://leetcode.com/problems/frequency-tracker/) | [无] | [C++](2001-2500/2671-Frequency-Tracker/cpp-2671/) | | | | 2672 | [Number of Adjacent Elements With the Same Color](https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/) | [无] | [C++](2001-2500/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/) | | | From eab1be8899a3d5aae926d2444ae20e19c1318140 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 7 May 2023 17:28:57 -0700 Subject: [PATCH 348/390] 1572 solved. --- .../cpp-1572/CMakeLists.txt | 6 ++++ .../cpp-1572/main.cpp | 30 +++++++++++++++++++ readme.md | 1 + 3 files changed, 37 insertions(+) create mode 100644 1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/CMakeLists.txt create mode 100644 1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/main.cpp diff --git a/1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/CMakeLists.txt b/1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/CMakeLists.txt new file mode 100644 index 00000000..5828bf49 --- /dev/null +++ b/1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1572) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1572 main.cpp) diff --git a/1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/main.cpp b/1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/main.cpp new file mode 100644 index 00000000..db259d3e --- /dev/null +++ b/1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/matrix-diagonal-sum/ +/// Author : liuyubobobo +/// Time : 2023-05-07 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int diagonalSum(vector>& mat) { + + int n = mat.size(), sum = 0; + for(int i = 0; i < n; i ++) + sum += mat[i][i] + mat[i][n - 1 - i]; + if(n & 1) sum -= mat[n / 2][n / 2]; + return sum; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 869fe897..46eeb648 100644 --- a/readme.md +++ b/readme.md @@ -1469,6 +1469,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1570 | [Dot Product of Two Sparse Vectors](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/) | [solution](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/) | [C++](1501-2000/1570-Dot-Product-of-Two-Sparse-Vectors/cpp-1570/) | | | | 1571 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 1572 | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) | [无] | [C++](1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/) | | | | | | | | | | | 1574 | [Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/description/) | [无] | [C++](1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/) | | | | | | | | | | From 82d5ecdbbbfa4fa2ab26a971e9df9e79b0dbeeb3 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 7 May 2023 17:38:01 -0700 Subject: [PATCH 349/390] 0311 solved. --- .../cpp-0311/CMakeLists.txt | 6 ++++ .../cpp-0311/main.cpp | 33 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/CMakeLists.txt create mode 100644 0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/main.cpp diff --git a/0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/CMakeLists.txt b/0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/CMakeLists.txt new file mode 100644 index 00000000..bc6e2b41 --- /dev/null +++ b/0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_0311) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0311 main.cpp) diff --git a/0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/main.cpp b/0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/main.cpp new file mode 100644 index 00000000..e90eb92e --- /dev/null +++ b/0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/sparse-matrix-multiplication/description/ +/// Author : liuyubobobo +/// Time : 2023-05-07 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n * m * k) +/// Space Complexity: O(n * m) +class Solution { +public: + vector> multiply(vector>& mat1, vector>& mat2) { + + int n = mat1.size(), k = mat1[0].size(), m = mat2[0].size(); + + vector> res(n, vector(m)); + for(int i = 0; i < n; i ++) + for(int j = 0; j < m; j ++) + for(int p = 0; p < k; p ++) + res[i][j] += mat1[i][p] * mat2[p][j]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 46eeb648..4aab6b2d 100644 --- a/readme.md +++ b/readme.md @@ -347,7 +347,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/description/) | | [C++](0001-0500/0308-Range-Sum-Query-2D-Mutable/cpp-0308/) | | | | 309 | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) | [solution](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/solution/) | [C++](0001-0500/0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/) | | | | 310 | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [solution](https://leetcode.com/problems/minimum-height-trees/solution/) | [C++](0001-0500/0310-Minimum-Height-Trees/cpp-0310/) | | | -| | | | | | | +| 311 | [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) | [无] | [C++](0001-0500/0311-Sparse-Matrix-Multiplication/cpp-0311/) | | | | 312 | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [solution](https://leetcode.com/problems/burst-balloons/solution/) [题解](https://leetcode-cn.com/problems/burst-balloons/solution/chuo-qi-qiu-by-leetcode/) | [C++](0001-0500/0312-Burst-Balloons/cpp-0312/) | | | | 313 | [Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [无] | [C++](0001-0500/0313-Super-Ugly-Number/cpp-0313/) | | | | 314 | [Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/) | [无] | [C++](0001-0500/0314-Binary-Tree-Vertical-Order-Traversal/cpp-0314/) | | | From 8ff27538525e7061a902a47bc526cf67d001adfd Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 7 May 2023 23:00:17 -0700 Subject: [PATCH 350/390] LCP 077-081 solved. --- LC/LCP077/cpp-LCP077/CMakeLists.txt | 6 ++ LC/LCP077/cpp-LCP077/main.cpp | 36 ++++++++ LC/LCP078/cpp-LCP078/CMakeLists.txt | 6 ++ LC/LCP078/cpp-LCP078/main.cpp | 46 ++++++++++ LC/LCP079/cpp-LCP079/CMakeLists.txt | 6 ++ LC/LCP079/cpp-LCP079/main.cpp | 59 ++++++++++++ LC/LCP080/cpp-LCP080/CMakeLists.txt | 6 ++ LC/LCP080/cpp-LCP080/main.cpp | 71 +++++++++++++++ LC/LCP081/cpp-LCP081/CMakeLists.txt | 6 ++ LC/LCP081/cpp-LCP081/main.cpp | 133 ++++++++++++++++++++++++++++ LC/readme.md | 25 +++--- 11 files changed, 390 insertions(+), 10 deletions(-) create mode 100644 LC/LCP077/cpp-LCP077/CMakeLists.txt create mode 100644 LC/LCP077/cpp-LCP077/main.cpp create mode 100644 LC/LCP078/cpp-LCP078/CMakeLists.txt create mode 100644 LC/LCP078/cpp-LCP078/main.cpp create mode 100644 LC/LCP079/cpp-LCP079/CMakeLists.txt create mode 100644 LC/LCP079/cpp-LCP079/main.cpp create mode 100644 LC/LCP080/cpp-LCP080/CMakeLists.txt create mode 100644 LC/LCP080/cpp-LCP080/main.cpp create mode 100644 LC/LCP081/cpp-LCP081/CMakeLists.txt create mode 100644 LC/LCP081/cpp-LCP081/main.cpp diff --git a/LC/LCP077/cpp-LCP077/CMakeLists.txt b/LC/LCP077/cpp-LCP077/CMakeLists.txt new file mode 100644 index 00000000..8a306afe --- /dev/null +++ b/LC/LCP077/cpp-LCP077/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(A) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(A main.cpp) diff --git a/LC/LCP077/cpp-LCP077/main.cpp b/LC/LCP077/cpp-LCP077/main.cpp new file mode 100644 index 00000000..5c8d165b --- /dev/null +++ b/LC/LCP077/cpp-LCP077/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.cn/problems/W2ZX4X/ +/// Author : liuyubobobo +/// Time : 2023-05-07 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int runeReserve(vector& runes) { + + sort(runes.begin(), runes.end()); + + int n = runes.size(), start = 0, res = 0; + while(start < n){ + int i; + for(i = start + 1; i < n && runes[i] - runes[i - 1] <= 1; i ++); + res = max(res, i - start); + start = i; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP078/cpp-LCP078/CMakeLists.txt b/LC/LCP078/cpp-LCP078/CMakeLists.txt new file mode 100644 index 00000000..3e27b177 --- /dev/null +++ b/LC/LCP078/cpp-LCP078/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(B) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(B main.cpp) diff --git a/LC/LCP078/cpp-LCP078/main.cpp b/LC/LCP078/cpp-LCP078/main.cpp new file mode 100644 index 00000000..3a006403 --- /dev/null +++ b/LC/LCP078/cpp-LCP078/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.cn/problems/Nsibyl/ +/// Author : liuyubobobo +/// Time : 2023-05-07 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlog(max_rampart)) +/// Space Complexity: O(1) +class Solution { +public: + int rampartDefensiveLine(vector>& rampart) { + + int n = rampart.size(), l = 0, r = 1e8; + while(l < r){ + int mid = l + (r - l + 1) / 2; + if(ok(n, rampart, mid)) l = mid; + else r = mid - 1; + } + return l; + } + +private: + bool ok(int n, vector> segs, int k){ + int r = segs[0][1]; + for(int i = 1; i < n; i ++){ + int a = segs[i][0], b = segs[i][1]; + if(a < r) return false; + + int left = a - r; + if(left >= k) r = b; + else r = b + (k - left); + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP079/cpp-LCP079/CMakeLists.txt b/LC/LCP079/cpp-LCP079/CMakeLists.txt new file mode 100644 index 00000000..6d269de2 --- /dev/null +++ b/LC/LCP079/cpp-LCP079/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(C) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(C main.cpp) diff --git a/LC/LCP079/cpp-LCP079/main.cpp b/LC/LCP079/cpp-LCP079/main.cpp new file mode 100644 index 00000000..dea31a70 --- /dev/null +++ b/LC/LCP079/cpp-LCP079/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.cn/problems/kjpLFZ/ +/// Author : liuyubobobo +/// Time : 2023-05-07 + +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(R * C * k) +/// Space Complexity: O(R * C * k) +class Solution { + +private: + const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + +public: + int extractMantra(vector& matrix, string mantra) { + + int R = matrix.size(), C = matrix[0].size(), k = mantra.size(); + + vector>> dis(k + 1, vector>(R, vector(C, -1))); + queue> q; + q.push({0, 0, 0}); + dis[0][0][0] = 0; + while(!q.empty()){ + int index = get<0>(q.front()), cx = get<1>(q.front()), cy = get<2>(q.front()); + q.pop(); + + int cur_dis = dis[index][cx][cy]; + if(index == k) return cur_dis; + + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(0 <= nx && nx < R && 0 <= ny && ny < C && dis[index][nx][ny] == -1){ + dis[index][nx][ny] = cur_dis + 1; + q.push({index, nx, ny}); + } + } + + if(matrix[cx][cy] == mantra[index] && dis[index + 1][cx][cy] == -1){ + dis[index + 1][cx][cy] = cur_dis + 1; + q.push({index + 1, cx, cy}); + } + } + + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP080/cpp-LCP080/CMakeLists.txt b/LC/LCP080/cpp-LCP080/CMakeLists.txt new file mode 100644 index 00000000..6a6bdfdb --- /dev/null +++ b/LC/LCP080/cpp-LCP080/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(D) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(D main.cpp) diff --git a/LC/LCP080/cpp-LCP080/main.cpp b/LC/LCP080/cpp-LCP080/main.cpp new file mode 100644 index 00000000..8fc959e8 --- /dev/null +++ b/LC/LCP080/cpp-LCP080/main.cpp @@ -0,0 +1,71 @@ +/// Source : https://leetcode.cn/problems/qoQAMX/ +/// Author : liuyubobobo +/// Time : 2023-05-07 + +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// Tree Encoding +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + string evolutionaryRecord(vector& parents) { + + int n = parents.size(); + vector> tree(n); + int root = -1; + for(int i = 0; i < n; i ++){ + if(parents[i] != -1) tree[parents[i]].push_back(i); + else root = i; + } + assert(root != -1); + + map, int> table; + vector tree2str(n); + string res = tree2str[encode(tree, root, table, tree2str)]; + while(res.back() == '1') res.pop_back(); + return res; + } + +private: + int encode(const vector>& tree, int u, map, int>& table, vector& tree2str){ + + vector subtrees; + for(int v: tree[u]){ + subtrees.push_back(encode(tree, v, table, tree2str)); + } + sort(subtrees.begin(), subtrees.end()); + + if(!table.count(subtrees)){ + table[subtrees] = table.size(); + + vector v; + for(int subtree: subtrees){ + v.push_back("0" + tree2str[subtree] + "1"); + } + sort(v.begin(), v.end()); + string res = ""; + for(const string& s: v) res += s; + tree2str[table[subtrees]] = res; + } + return table[subtrees]; + } +}; + + +int main() { + + vector parents1 = {-1, 0, 0, 2}; + cout << Solution().evolutionaryRecord(parents1) << endl; + // 00110 + + return 0; +} diff --git a/LC/LCP081/cpp-LCP081/CMakeLists.txt b/LC/LCP081/cpp-LCP081/CMakeLists.txt new file mode 100644 index 00000000..69cec56c --- /dev/null +++ b/LC/LCP081/cpp-LCP081/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(E) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(E main.cpp) diff --git a/LC/LCP081/cpp-LCP081/main.cpp b/LC/LCP081/cpp-LCP081/main.cpp new file mode 100644 index 00000000..2730c221 --- /dev/null +++ b/LC/LCP081/cpp-LCP081/main.cpp @@ -0,0 +1,133 @@ +/// Source : https://leetcode.cn/problems/ryfUiz/ +/// Author : liuyubobobo +/// Time : 2023-05-07 + +#include +#include + +using namespace std; + + +/// BIT +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +template +class BIT{ + +private: + int n; + vector data; + vector tree; + +public: + BIT(const vector& arr) : n(arr.size()), data(arr.begin(), arr.end()), tree(n + 1){ + + for(int i = 0; i < n; i ++) + tree[i + 1]= data[i]; + + for(int i = 1; i <= n; i ++){ + int parent = i + (i & -i); + if(parent <= n) tree[parent] += tree[i]; + } + } + + BIT(int n): n(n), data(n, 0), tree(n + 1, 0){} + + void add(int index, T v){ + assert(0 <= index && index < n); + + data[index] += v; + + index ++; + for(;index <= n; index += (index & -index)) + tree[index] += v; + } + + T query(int l, int r){ + assert(0 <= l && l < n); + assert(0 <= r && r < n); + assert(l <= r); + return prefix_sum(r + 1) - prefix_sum(l); + } + + void set(int index, T v){ + assert(0 <= index && index < n); + add(index, v - data[index]); + } + +private: + // r is 1-based + T prefix_sum(int r){ + + T res = 0; + for(; r; r -= (r & -r)) + res += tree[r]; + return res; + } +}; + +class Solution { +public: + int getNandResult(int k, vector& arr, vector>& operations) { + + int n = arr.size(); + vector> bits(k, BIT(n)); + for(int i = 0; i < n; i ++){ + int e = arr[i]; + for(int p = 0; p < k; p ++) + bits[p].set(i, (e >> p) & 1); + } + + int res = 0; + for(const vector& op: operations){ + int type = op[0]; + if(type == 0){ + int index = op[1], x = op[2]; + for(int p = 0; p < k; p ++) + bits[p].set(index, (x >> p) & 1); + } + else{ + int cnt = op[1], num = op[2]; + + int tres = 0; + for(int p = 0; p < k; p ++){ + int len = get_len(n, bits[p]); + long long x; + if(len == 0) x= 1; + else if(len == n){ + x = 1ll * cnt * n; + if((num >> p) & 1) x ++; + } + else x = len + 1; + + if(x % 2) tres |= (1 << p); + } + + res ^= tres; + } + } + return res; + } + +private: + int get_len(int n, BIT& bit){ + int l = 0, r = n; + while(l < r){ + int mid = (l + r) / 2; + if(bit.query(mid, n - 1) == n - mid) r = mid; + else l = mid + 1; + } + return n - l; + } +}; + + +int main() { + + vector arr1 = {1, 2}; + vector> op1 = {{1, 2, 3}, {0, 0, 3}, {1, 2, 2}}; + cout << Solution().getNandResult(3, arr1, op1) << endl; + // 2 + + return 0; +} diff --git a/LC/readme.md b/LC/readme.md index 176f3e0d..bd507912 100644 --- a/LC/readme.md +++ b/LC/readme.md @@ -3,17 +3,22 @@ | ID | Problem | Official
Solution | C++ | Java | Python | | --- | --- | :---: | :---: | :---: | :---: | | | | | | | | -| LCP076 | [魔法棋盘](https://leetcode.cn/problems/1ybDKD/) | [无] | [C++](LCP0076/cpp-LCP076/) | | | -| LCP075 | [传送卷轴](https://leetcode.cn/problems/rdmXM7/) | [无] | [C++](LCP0075/cpp-LCP075/) | | | -| LCP074 | [最强祝福力场](https://leetcode.cn/problems/xepqZ5/) | [无] | [C++](LCP0074/cpp-LCP074/) | | | -| LCP073 | [探险营地](https://leetcode.cn/problems/0Zeoeg/) | [无] | [C++](LCP0073/cpp-LCP073/) | | | -| LCP072 | [补给马车](https://leetcode.cn/problems/hqCnmP/) | [无] | [C++](LCP0072/cpp-LCP072/) | | | +| LCP081 | [与非的谜题](https://leetcode.cn/problems/ryfUiz/) | [无] | [C++](LCP081/cpp-LCP081/) | | | +| LCP080 | [生物进化录](https://leetcode.cn/problems/qoQAMX/) | [无] | [C++](LCP080/cpp-LCP080/) | | | +| LCP079 | [提取咒文](https://leetcode.cn/problems/kjpLFZ/) | [无] | [C++](LCP079/cpp-LCP079/) | | | +| LCP078 | [城墙防线](https://leetcode.cn/problems/Nsibyl/) | [无] | [C++](LCP078/cpp-LCP078/) | | | +| LCP077 | [符文储备](https://leetcode.cn/problems/W2ZX4X/) | [无] | [C++](LCP077/cpp-LCP077/) | | | +| LCP076 | [魔法棋盘](https://leetcode.cn/problems/1ybDKD/) | [无] | [C++](LCP076/cpp-LCP076/) | | | +| LCP075 | [传送卷轴](https://leetcode.cn/problems/rdmXM7/) | [无] | [C++](LCP075/cpp-LCP075/) | | | +| LCP074 | [最强祝福力场](https://leetcode.cn/problems/xepqZ5/) | [无] | [C++](LCP074/cpp-LCP074/) | | | +| LCP073 | [探险营地](https://leetcode.cn/problems/0Zeoeg/) | [无] | [C++](LCP073/cpp-LCP073/) | | | +| LCP072 | [补给马车](https://leetcode.cn/problems/hqCnmP/) | [无] | [C++](LCP072/cpp-LCP072/) | | | | | | | | | | -| LCP070 | [沙地治理](https://leetcode.cn/problems/XxZZjK/) | [无] | [C++](LCP0070/cpp-LCP070/) | | | -| LCP069 | [Hello LeetCode!](https://leetcode.cn/problems/rMeRt2/) | [无] | [C++](LCP0069/cpp-LCP069/) | | | -| LCP068 | [美观的花束](https://leetcode.cn/problems/1GxJYY/) | [无] | [C++](LCP0068/cpp-LCP068/) | | | -| LCP067 | [装饰树](https://leetcode.cn/problems/KnLfVT/) | [无] | [C++](LCP0067/cpp-LCP067/) | | | -| LCP066 | [最小展台数量](https://leetcode.cn/problems/600YaG/) | [无] | [C++](LCP0066/cpp-LCP066/) | | | +| LCP070 | [沙地治理](https://leetcode.cn/problems/XxZZjK/) | [无] | [C++](LCP070/cpp-LCP070/) | | | +| LCP069 | [Hello LeetCode!](https://leetcode.cn/problems/rMeRt2/) | [无] | [C++](LCP069/cpp-LCP069/) | | | +| LCP068 | [美观的花束](https://leetcode.cn/problems/1GxJYY/) | [无] | [C++](LCP068/cpp-LCP068/) | | | +| LCP067 | [装饰树](https://leetcode.cn/problems/KnLfVT/) | [无] | [C++](LCP067/cpp-LCP067/) | | | +| LCP066 | [最小展台数量](https://leetcode.cn/problems/600YaG/) | [无] | [C++](LCP066/cpp-LCP066/) | | | | | | | | | | | LCP057 | [打地鼠](https://leetcode-cn.com/problems/ZbAuEH/) | [无] | [C++](LCP057/cpp-LCP057/) | | | | LCP056 | [信物传送](https://leetcode-cn.com/problems/6UEx57/) | [无] | [C++](LCP056/cpp-LCP056/) | | | From 54ff0ccd934d827282ca32a30f90eb47af142115 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 12 May 2023 13:24:02 -0700 Subject: [PATCH 351/390] 2674 solved. --- .../cpp-2674/CMakeLists.txt | 6 +++ .../cpp-2674/main.cpp | 47 +++++++++++++++++++ readme.md | 1 + 3 files changed, 54 insertions(+) create mode 100644 2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/CMakeLists.txt create mode 100644 2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/main.cpp diff --git a/2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/CMakeLists.txt b/2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/CMakeLists.txt new file mode 100644 index 00000000..8c6143e7 --- /dev/null +++ b/2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2674) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2674 main.cpp) diff --git a/2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/main.cpp b/2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/main.cpp new file mode 100644 index 00000000..acba402f --- /dev/null +++ b/2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/split-a-circular-linked-list/description/ +/// Author : liuyubobobo +/// Time : 2023-05-12 + +#include +#include + +using namespace std; + + +/// Slow and Fast Pointer +/// Time Complexity: O(n) +/// Space Complexity: O(1) + +/// Definition for singly-linked list. +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution { +public: + vector splitCircularLinkedList(ListNode* list) { + + ListNode *slow = list, *fast = list->next; + while(fast->next != list){ + slow = slow->next; + fast = fast->next; + if(fast->next == list) break; + else fast = fast->next; + } + + ListNode *first = list, *second = slow->next; + slow->next = first; + fast->next = second; + return {first, second}; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 4aab6b2d..6b996096 100644 --- a/readme.md +++ b/readme.md @@ -2571,6 +2571,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2671 | [Frequency Tracker](https://leetcode.com/problems/frequency-tracker/) | [无] | [C++](2001-2500/2671-Frequency-Tracker/cpp-2671/) | | | | 2672 | [Number of Adjacent Elements With the Same Color](https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/) | [无] | [C++](2001-2500/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/) | | | | 2673 | [Make Costs of Paths Equal in a Binary Tree](https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/) | [无] | [C++](2001-2500/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/) | | | +| 2674 | [Split a Circular Linked List](https://leetcode.com/problems/split-a-circular-linked-list/description/) | [无] | [C++](2001-2500/2674-Split-a-Circular-Linked-List/cpp-2674/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 6604a7adb6419bffcc4a036a46aa347699c94308 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 12 May 2023 13:46:04 -0700 Subject: [PATCH 352/390] 1330 solved. --- .../cpp-1330/CMakeLists.txt | 6 ++ .../cpp-1330/main.cpp | 60 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/CMakeLists.txt create mode 100644 1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/main.cpp diff --git a/1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/CMakeLists.txt b/1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/CMakeLists.txt new file mode 100644 index 00000000..d06b08c3 --- /dev/null +++ b/1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1330) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1330 main.cpp) diff --git a/1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/main.cpp b/1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/main.cpp new file mode 100644 index 00000000..775235e1 --- /dev/null +++ b/1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/reverse-subarray-to-maximize-array-value/description/ +/// Author : liuyubobobo +/// Time : 2023-05-12 + +#include +#include + +using namespace std; + + +/// Absolute Value Expression +/// |a - b| = max(a - b, b - a) +/// max(a, b) - c = max(a - c, b - c) +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int maxValueAfterReverse(vector& nums) { + + int n = nums.size(); + if(n == 1) return 0; + + long long base = 0; + for(int i = 1; i < n; i ++) base += abs(nums[i] - nums[i - 1]); + + long long res = base; + for(int i = 1; i < n; i ++) + res = max(res, base - abs(nums[i] - nums[i - 1]) + abs(nums[i] - nums[0])); + for(int i = n - 2; i >= 0; i --) + res = max(res, base - abs(nums[i] - nums[i + 1]) + abs(nums[i] - nums[n - 1])); + + vector dp(4); + dp[0] = nums[0] + nums[1] - abs(nums[0] - nums[1]); + dp[1] = nums[0] - nums[1] - abs(nums[0] - nums[1]); + dp[2] = - nums[0] + nums[1] - abs(nums[0] - nums[1]); + dp[3] = - nums[0] - nums[1] - abs(nums[0] - nums[1]); + + for(int i = 1; i + 1 < n; i ++){ + int d = 0, x= nums[i], y = nums[i + 1]; + d = max(d, dp[0] - (x + y) - abs(x - y)); + d = max(d, dp[1] - (x - y) - abs(x - y)); + d = max(d, dp[2] - (- x + y) - abs(x - y)); + d = max(d, dp[3] - (- x - y) - abs(x - y)); + + res = max(res, base + d); + + dp[0] = max(dp[0], x + y - abs(x - y)); + dp[1] = max(dp[1], x - y - abs(x - y)); + dp[2] = max(dp[2], - x + y - abs(x - y)); + dp[3] = max(dp[3], - x - y - abs(x - y)); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 6b996096..3f9b068c 100644 --- a/readme.md +++ b/readme.md @@ -1282,7 +1282,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1328 | [Break a Palindrome](https://leetcode.com/problems/break-a-palindrome/) | [solution](https://leetcode.com/problems/break-a-palindrome/solution/) | [C++](1001-1500/1328-Break-a-Palindrome/cpp-1328/) | | | | 1329 | [Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [solution](https://leetcode.com/problems/sort-the-matrix-diagonally/solution/) | [C++](1001-1500/1329-Sort-the-Matrix-Diagonally/cpp-1329/) | | | -| | | | | | | +| 1330 | [Reverse Subarray To Maximize Array Value](https://leetcode.com/problems/reverse-subarray-to-maximize-array-value/description/) | [无] | [C++](1001-1500/1330-Reverse-Subarray-To-Maximize-Array-Value/cpp-1330/) | | | | 1331 | [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/) | [无] | [C++](1001-1500/1331-Rank-Transform-of-an-Array/cpp-1331/) | | | | 1332 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [solution](https://leetcode.com/problems/remove-palindromic-subsequences/solution/) | [C++](1001-1500/1332-Remove-Palindromic-Subsequences/cpp-1332/) | | | | 1333 | [Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [无] | [C++](1001-1500/1333-Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/cpp-1333/) | | | From 860372f71be63d5a1ca310fc792b98a6e104972e Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 13 May 2023 16:59:45 -0700 Subject: [PATCH 353/390] 2678-2681 solved. --- .../cpp-2678/CMakeLists.txt | 6 +++ .../cpp-2678/main.cpp | 31 ++++++++++++++ .../cpp-2679/CMakeLists.txt | 6 +++ .../2679-Sum-in-a-Matrix/cpp-2679/main.cpp | 35 ++++++++++++++++ .../2680-Maximum-OR/cpp-2680/CMakeLists.txt | 6 +++ 2501-3000/2680-Maximum-OR/cpp-2680/main.cpp | 42 +++++++++++++++++++ .../cpp-2681/CMakeLists.txt | 6 +++ .../2681-Power-of-Heroes/cpp-2681/main.cpp | 41 ++++++++++++++++++ readme.md | 7 ++++ 9 files changed, 180 insertions(+) create mode 100644 2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/CMakeLists.txt create mode 100644 2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/main.cpp create mode 100644 2501-3000/2679-Sum-in-a-Matrix/cpp-2679/CMakeLists.txt create mode 100644 2501-3000/2679-Sum-in-a-Matrix/cpp-2679/main.cpp create mode 100644 2501-3000/2680-Maximum-OR/cpp-2680/CMakeLists.txt create mode 100644 2501-3000/2680-Maximum-OR/cpp-2680/main.cpp create mode 100644 2501-3000/2681-Power-of-Heroes/cpp-2681/CMakeLists.txt create mode 100644 2501-3000/2681-Power-of-Heroes/cpp-2681/main.cpp diff --git a/2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/CMakeLists.txt b/2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/CMakeLists.txt new file mode 100644 index 00000000..8e117464 --- /dev/null +++ b/2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2678) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2678 main.cpp) diff --git a/2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/main.cpp b/2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/main.cpp new file mode 100644 index 00000000..2a5d73e4 --- /dev/null +++ b/2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/number-of-senior-citizens/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int countSeniors(vector& details) { + + int res = 0; + for(const string& s: details){ + int age = stoi(s.substr(11, 2)); + res += age > 60; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2679-Sum-in-a-Matrix/cpp-2679/CMakeLists.txt b/2501-3000/2679-Sum-in-a-Matrix/cpp-2679/CMakeLists.txt new file mode 100644 index 00000000..5ba116ef --- /dev/null +++ b/2501-3000/2679-Sum-in-a-Matrix/cpp-2679/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2679) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2679 main.cpp) diff --git a/2501-3000/2679-Sum-in-a-Matrix/cpp-2679/main.cpp b/2501-3000/2679-Sum-in-a-Matrix/cpp-2679/main.cpp new file mode 100644 index 00000000..12b4cb2f --- /dev/null +++ b/2501-3000/2679-Sum-in-a-Matrix/cpp-2679/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/sum-in-a-matrix/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(R * ClogC) +/// Space Complexity: O(1) +class Solution { +public: + int matrixSum(vector>& nums) { + + for(vector& v: nums) sort(v.begin(), v.end()); + + int res = 0; + for(int c = 0; c < nums[0].size(); c++) { + int maxv = 0; + for(int r = 0; r < nums.size(); r++) maxv = max(maxv, nums[r][c]); + res += maxv; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2680-Maximum-OR/cpp-2680/CMakeLists.txt b/2501-3000/2680-Maximum-OR/cpp-2680/CMakeLists.txt new file mode 100644 index 00000000..b57b907e --- /dev/null +++ b/2501-3000/2680-Maximum-OR/cpp-2680/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2680) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2680 main.cpp) diff --git a/2501-3000/2680-Maximum-OR/cpp-2680/main.cpp b/2501-3000/2680-Maximum-OR/cpp-2680/main.cpp new file mode 100644 index 00000000..cb345c92 --- /dev/null +++ b/2501-3000/2680-Maximum-OR/cpp-2680/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/maximum-or/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long maximumOr(vector& nums, int k) { + + vector f(30, 0); + for(int e: nums){ + for(int p = 0; p < 30; p ++) if(e & (1 << p)) f[p] ++; + } + + long long res = 0; + for(int e: nums){ + for(int p = 0; p < 30; p ++) if(e & (1 << p)) f[p] --; + + long long tres = e; + tres <<= k; + for(int p = 0; p < 30; p ++) if(f[p]) tres |= (1ll << p); + res = max(res, tres); + + for(int p = 0; p < 30; p ++) if(e & (1 << p)) f[p] ++; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2681-Power-of-Heroes/cpp-2681/CMakeLists.txt b/2501-3000/2681-Power-of-Heroes/cpp-2681/CMakeLists.txt new file mode 100644 index 00000000..0cae5c55 --- /dev/null +++ b/2501-3000/2681-Power-of-Heroes/cpp-2681/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2681) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2681 main.cpp) diff --git a/2501-3000/2681-Power-of-Heroes/cpp-2681/main.cpp b/2501-3000/2681-Power-of-Heroes/cpp-2681/main.cpp new file mode 100644 index 00000000..62241147 --- /dev/null +++ b/2501-3000/2681-Power-of-Heroes/cpp-2681/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/power-of-heroes/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int sumOfPower(vector& nums) { + + sort(nums.begin(), nums.end()); + + long long min_part = 0, res = 0; + for(long long e: nums){ + res += e * e % MOD * min_part % MOD; + res += e * e % MOD * e % MOD; + res %= MOD; + + min_part = (min_part * 2 + e) % MOD; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 3f9b068c..e88ba957 100644 --- a/readme.md +++ b/readme.md @@ -2572,6 +2572,13 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2672 | [Number of Adjacent Elements With the Same Color](https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/) | [无] | [C++](2001-2500/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/) | | | | 2673 | [Make Costs of Paths Equal in a Binary Tree](https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/) | [无] | [C++](2001-2500/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/) | | | | 2674 | [Split a Circular Linked List](https://leetcode.com/problems/split-a-circular-linked-list/description/) | [无] | [C++](2001-2500/2674-Split-a-Circular-Linked-List/cpp-2674/) | | | +| 2675 | JavaScript Problem | - | - | - | - | +| 2676 | JavaScript Problem | - | - | - | - | +| 2677 | JavaScript Problem | - | - | - | - | +| 2678 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens/) | [无] | [C++](2001-2500/2678-Number-of-Senior-Citizens/cpp-2678/) | | | +| 2679 | [Sum in a Matrix](https://leetcode.com/problems/sum-in-a-matrix/) | [无] | [C++](2001-2500/2679-Sum-in-a-Matrix/cpp-2679/) | | | +| 2680 | [Maximum OR](https://leetcode.com/problems/maximum-or/) | [无] | [C++](2001-2500/2680-Maximum-OR/cpp-2680/) | | | +| 2681 | [Power of Heroes](https://leetcode.com/problems/power-of-heroes/) | [无] | [C++](2001-2500/2681-Power-of-Heroes/cpp-2681/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From d7d4953a322772c21cfea83d8bdb3e6344648785 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 13 May 2023 22:29:28 -0700 Subject: [PATCH 354/390] 1054 solved. --- .../cpp-1054/CMakeLists.txt | 6 +++ .../1054-Distant-Barcodes/cpp-1054/main.cpp | 50 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1054-Distant-Barcodes/cpp-1054/CMakeLists.txt create mode 100644 1001-1500/1054-Distant-Barcodes/cpp-1054/main.cpp diff --git a/1001-1500/1054-Distant-Barcodes/cpp-1054/CMakeLists.txt b/1001-1500/1054-Distant-Barcodes/cpp-1054/CMakeLists.txt new file mode 100644 index 00000000..3bb48eff --- /dev/null +++ b/1001-1500/1054-Distant-Barcodes/cpp-1054/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1054) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1054 main.cpp) diff --git a/1001-1500/1054-Distant-Barcodes/cpp-1054/main.cpp b/1001-1500/1054-Distant-Barcodes/cpp-1054/main.cpp new file mode 100644 index 00000000..39a11892 --- /dev/null +++ b/1001-1500/1054-Distant-Barcodes/cpp-1054/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/distant-barcodes/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector rearrangeBarcodes(vector& barcodes) { + + int n = barcodes.size(); + map f; + for(int barcode : barcodes) f[barcode]++; + + vector> v; + for(const pair& p: f) v.push_back(p); + + sort(v.begin(), v.end(), [](const pair& a, const pair& b) { + return a.second < b.second; + }); + + vector res(n); + for(int i = 0; i < n; i += 2){ + res[i] = v.back().first; + v.back().second --; + if(v.back().second == 0) v.pop_back(); + } + for(int i = 1; i < n; i += 2){ + res[i] = v.back().first; + v.back().second --; + if(v.back().second == 0) v.pop_back(); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index e88ba957..563109e2 100644 --- a/readme.md +++ b/readme.md @@ -1055,7 +1055,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1051 | [Height Checker](https://leetcode.com/problems/height-checker/) | [无] | [C++](1001-1500/1051-Height-Checker/cpp-1051/) | | | | 1052 | [Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner/) | [无] | [C++](1001-1500/1052-Grumpy-Bookstore-Owner/cpp-1052/) | | | | 1053 | [Previous Permutation With One Swap](https://leetcode.com/problems/previous-permutation-with-one-swap/description/) | [无] | [C++](1001-1500/1053-Previous-Permutation-With-One-Swap/cpp-1053/) | | | -| | | | | | | +| 1054 | [Distant Barcodes](https://leetcode.com/problems/distant-barcodes/description/) | [无] | [C++](1001-1500/1054-Distant-Barcodes/cpp-1054/) | | | | 1055 | [Shortest Way to Form String](https://leetcode.com/problems/shortest-way-to-form-string/description/) | [无] | [C++](1001-1500/1055-Shortest-Way-to-Form-String/cpp-1055/) | | | | 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number/description/) | [无] | [C++](1001-1500/1056-Confusing-Number/cpp-1056/) | | | | | | | | | | From 4c32a77591263635708965904f6882ab89943a54 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 13 May 2023 22:57:56 -0700 Subject: [PATCH 355/390] 2682-2685 solved. --- .../cpp-2682/CMakeLists.txt | 6 ++ .../cpp-2682/main.cpp | 35 +++++++++++ .../cpp-2683/CMakeLists.txt | 6 ++ .../cpp-2683/main.cpp | 35 +++++++++++ .../cpp-2684/CMakeLists.txt | 6 ++ .../cpp-2684/main.cpp | 45 ++++++++++++++ .../cpp-2685/CMakeLists.txt | 6 ++ .../cpp-2685/main.cpp | 59 +++++++++++++++++++ readme.md | 4 ++ 9 files changed, 202 insertions(+) create mode 100644 2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/CMakeLists.txt create mode 100644 2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/main.cpp create mode 100644 2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/CMakeLists.txt create mode 100644 2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/main.cpp create mode 100644 2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/CMakeLists.txt create mode 100644 2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/main.cpp create mode 100644 2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/CMakeLists.txt create mode 100644 2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/main.cpp diff --git a/2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/CMakeLists.txt b/2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/CMakeLists.txt new file mode 100644 index 00000000..debb2188 --- /dev/null +++ b/2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2682) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2682 main.cpp) diff --git a/2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/main.cpp b/2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/main.cpp new file mode 100644 index 00000000..010436bf --- /dev/null +++ b/2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/find-the-losers-of-the-circular-game/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + vector circularGameLosers(int n, int k) { + + vector visited(n, false); + for(int i = 1, cur = 0; !visited[cur];i ++){ + visited[cur] = true; + cur = (cur + i * k) % n; + } + + vector res; + for(int i = 0; i < n; i ++) if(!visited[i]) res.push_back(i + 1); + return res; + } +}; + + +int main() { + + + return 0; +} diff --git a/2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/CMakeLists.txt b/2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/CMakeLists.txt new file mode 100644 index 00000000..5b5e0a19 --- /dev/null +++ b/2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2683) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2683 main.cpp) diff --git a/2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/main.cpp b/2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/main.cpp new file mode 100644 index 00000000..e37f63dc --- /dev/null +++ b/2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/neighboring-bitwise-xor/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + bool doesValidArrayExist(vector& derived) { + + int n = derived.size(); + return check(n, 0, derived) || check(n, 1, derived); + } + +private: + bool check(int n, int first, const vector& derived){ + + vector v(n, first); + for(int i = 1; i < n; i ++) v[i] = v[i - 1] ^ derived[i - 1]; + return v.back() ^ v[0] == derived.back(); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/CMakeLists.txt b/2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/CMakeLists.txt new file mode 100644 index 00000000..2edd12ea --- /dev/null +++ b/2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2684) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2684 main.cpp) diff --git a/2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/main.cpp b/2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/main.cpp new file mode 100644 index 00000000..4aa9416b --- /dev/null +++ b/2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-moves-in-a-grid/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { +public: + int maxMoves(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + vector> dp(R, vector(C, -1)); + for(int i = 0; i < R; i ++) dp[i][0] = 0; + + for(int j = 0; j + 1 < C; j ++){ + for(int i = 0; i < R; i ++){ + if(dp[i][j] == -1) continue; + for(int d = -1; d <= 1; d ++){ + if(i + d < 0 || i + d >= R) continue; + if(grid[i + d][j + 1] <= grid[i][j]) continue; + dp[i + d][j + 1] = max(dp[i + d][j + 1], dp[i][j] + 1); + } + } + } + + int res = 0; + for(int i = 0; i < R; i ++) res = max(res, *max_element(dp[i].begin(), dp[i].end())); + return res; + } +}; + + +int main() { + + + return 0; +} diff --git a/2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/CMakeLists.txt b/2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/CMakeLists.txt new file mode 100644 index 00000000..281ba124 --- /dev/null +++ b/2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2685) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2685 main.cpp) diff --git a/2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/main.cpp b/2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/main.cpp new file mode 100644 index 00000000..3ab0bafd --- /dev/null +++ b/2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/count-the-number-of-complete-components/description/ +/// Author : liuyubobobo +/// Time : 2023-05-13 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n + m) +/// Space Complexity: O(n + m) +class Solution { +public: + int countCompleteComponents(int n, vector>& edges) { + + vector> g(n); + for(auto& e : edges) + g[e[0]].push_back(e[1]), g[e[1]].push_back(e[0]); + + vector visited(n, false); + int res = 0; + for(int i = 0; i < n; i++){ + if(visited[i]) continue; + + vector cc; + dfs(g, i, visited, cc); + res += ok(g, cc); + } + return res; + } + +private: + bool ok(const vector>& g, const vector& cc){ + + int d = cc.size() - 1; + for(int u: cc) + if(g[u].size() != d) + return false; + return true; + } + + void dfs(const vector>& g, int u, vector& visited, vector& cc){ + + visited[u] = true; + cc.push_back(u); + for(int v: g[u]) + if(!visited[v]) + dfs(g, v, visited, cc); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 563109e2..25b61613 100644 --- a/readme.md +++ b/readme.md @@ -2579,6 +2579,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2679 | [Sum in a Matrix](https://leetcode.com/problems/sum-in-a-matrix/) | [无] | [C++](2001-2500/2679-Sum-in-a-Matrix/cpp-2679/) | | | | 2680 | [Maximum OR](https://leetcode.com/problems/maximum-or/) | [无] | [C++](2001-2500/2680-Maximum-OR/cpp-2680/) | | | | 2681 | [Power of Heroes](https://leetcode.com/problems/power-of-heroes/) | [无] | [C++](2001-2500/2681-Power-of-Heroes/cpp-2681/) | | | +| 2682 | [Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game/) | [无] | [C++](2001-2500/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/) | | | +| 2683 | [Neighboring Bitwise XOR](https://leetcode.com/problems/neighboring-bitwise-xor/) | [无] | [C++](2001-2500/2683-Neighboring-Bitwise-XOR/cpp-2683/) | | | +| 2684 | [Maximum Number of Moves in a Grid](https://leetcode.com/problems/maximum-number-of-moves-in-a-grid/) | [无] | [C++](2001-2500/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/) | | | +| 2685 | [Count the Number of Complete Components](https://leetcode.com/problems/count-the-number-of-complete-components/) | [无] | [C++](2001-2500/2685-Count-the-Number-of-Complete-Components/cpp-2685/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 0ef2fcd076e260496b6f8fddbd7677d9dbc5ccc0 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 17 May 2023 14:26:42 -0700 Subject: [PATCH 356/390] 2689 solved. --- .../cpp-2689/CMakeLists.txt | 6 +++ .../cpp-2689/main.cpp | 49 +++++++++++++++++++ readme.md | 4 ++ 3 files changed, 59 insertions(+) create mode 100644 2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/CMakeLists.txt create mode 100644 2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/main.cpp diff --git a/2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/CMakeLists.txt b/2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/CMakeLists.txt new file mode 100644 index 00000000..e9e891ad --- /dev/null +++ b/2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2689) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2689 main.cpp) diff --git a/2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/main.cpp b/2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/main.cpp new file mode 100644 index 00000000..b9277536 --- /dev/null +++ b/2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/description/ +/// Author : liuyubobobo +/// Time : 2023-05-17 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for a rope tree node. +struct RopeTreeNode { + int len; + string val; + RopeTreeNode *left; + RopeTreeNode *right; + RopeTreeNode() : len(0), val(""), left(nullptr), right(nullptr) {} + RopeTreeNode(string s) : len(0), val(std::move(s)), left(nullptr), right(nullptr) {} + RopeTreeNode(int x) : len(x), val(""), left(nullptr), right(nullptr) {} + RopeTreeNode(int x, RopeTreeNode *left, RopeTreeNode *right) : len(x), val(""), left(left), right(right) {} +}; + +class Solution { +public: + char getKthCharacter(RopeTreeNode* root, int k) { + + return dfs(root, k); + } + +private: + char dfs(RopeTreeNode* node, int k) { + + if(node->len == 0) return node->val[k - 1]; + + int left_len = node->left ? (node->left->len ? node->left->len : node->left->val.size()) : 0; + if(k <= left_len) return dfs(node->left, k); + return dfs(node->right, k - left_len); + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 25b61613..199b8cb5 100644 --- a/readme.md +++ b/readme.md @@ -2583,6 +2583,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2683 | [Neighboring Bitwise XOR](https://leetcode.com/problems/neighboring-bitwise-xor/) | [无] | [C++](2001-2500/2683-Neighboring-Bitwise-XOR/cpp-2683/) | | | | 2684 | [Maximum Number of Moves in a Grid](https://leetcode.com/problems/maximum-number-of-moves-in-a-grid/) | [无] | [C++](2001-2500/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/) | | | | 2685 | [Count the Number of Complete Components](https://leetcode.com/problems/count-the-number-of-complete-components/) | [无] | [C++](2001-2500/2685-Count-the-Number-of-Complete-Components/cpp-2685/) | | | +| 2686 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2687 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2688 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2689 | [Extract Kth Character From The Rope Tree](https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/) | [无] | [C++](2001-2500/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From bfc1f152b670554863cabdc1cec75a288e9d7ec1 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 17 May 2023 22:58:34 -0700 Subject: [PATCH 357/390] 1557 solved. --- .../cpp-1557/CMakeLists.txt | 6 ++++ .../cpp-1557/main.cpp | 34 +++++++++++++++++++ readme.md | 2 ++ 3 files changed, 42 insertions(+) create mode 100644 1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/CMakeLists.txt create mode 100644 1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/main.cpp diff --git a/1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/CMakeLists.txt b/1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/CMakeLists.txt new file mode 100644 index 00000000..290c8ea2 --- /dev/null +++ b/1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1557) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1557 main.cpp) diff --git a/1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/main.cpp b/1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/main.cpp new file mode 100644 index 00000000..085230b2 --- /dev/null +++ b/1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/description/ +/// Author : liuyubobobo +/// Time : 2023-05-17 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(m) +/// Space Complexity: O(n) +class Solution { +public: + vector findSmallestSetOfVertices(int n, vector>& edges) { + + vector d(n, 0); + for(const vector& edge: edges) + d[edge[1]]++; + + vector res; + for(int i = 0; i < n; i++) + if(d[i] == 0) + res.push_back(i); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 199b8cb5..01fdb8ec 100644 --- a/readme.md +++ b/readme.md @@ -1463,6 +1463,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [solution](https://leetcode.com/problems/kth-missing-positive-number/solution/) | [C++](1501-2000/1539-Kth-Missing-Positive-Number/cpp-1539/) | | | | | | | | | | +| 1557 | [Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/description/) | [无] | [C++](1501-2000/1557-Minimum-Number-of-Vertices-to-Reach-All-Nodes/cpp-1557/) | | | +| | | | | | | | 1559 | [Detect Cycles in 2D Grid](https://leetcode.com/problems/detect-cycles-in-2d-grid/) | [无] | [C++](1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/) | | | | | | | | | | | 1564 | [Put Boxes Into the Warehouse I](https://leetcode.com/problems/put-boxes-into-the-warehouse-i/) | [无] | [C++](1501-2000/1564-Put-Boxes-Into-the-Warehouse-I/cpp-1564/) | | | From 0215df877b9568f909f9ebde36c65bff6aeab3bd Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 21 May 2023 22:03:09 -0700 Subject: [PATCH 358/390] 2696-2699 solved. --- .../cpp-2696/CMakeLists.txt | 6 ++ .../cpp-2696/main.cpp | 34 ++++++ .../cpp-2697/CMakeLists.txt | 6 ++ .../cpp-2697/main.cpp | 30 ++++++ .../cpp-2698/CMakeLists.txt | 6 ++ .../cpp-2698/main.cpp | 51 +++++++++ .../cpp-2699/CMakeLists.txt | 6 ++ .../cpp-2699/main.cpp | 102 ++++++++++++++++++ readme.md | 5 + 9 files changed, 246 insertions(+) create mode 100644 2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/CMakeLists.txt create mode 100644 2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/main.cpp create mode 100644 2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/CMakeLists.txt create mode 100644 2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/main.cpp create mode 100644 2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/CMakeLists.txt create mode 100644 2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/main.cpp create mode 100644 2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/CMakeLists.txt create mode 100644 2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/main.cpp diff --git a/2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/CMakeLists.txt b/2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/CMakeLists.txt new file mode 100644 index 00000000..c566a118 --- /dev/null +++ b/2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2696) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2696 main.cpp) diff --git a/2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/main.cpp b/2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/main.cpp new file mode 100644 index 00000000..9b44c506 --- /dev/null +++ b/2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/minimum-string-length-after-removing-substrings/description/ +/// Author : liuyubobobo +/// Time : 2023-05-21 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(|s|^2) +/// Space Complexity: O(|s|) +class Solution { +public: + int minLength(string s) { + + while(true){ + bool flag = false; + if(s.find("AB") != string::npos) + s.replace(s.find("AB"), 2, ""), flag = true; + if(s.find("CD") != string::npos) + s.replace(s.find("CD"), 2, ""), flag = true; + if(!flag) break; + } + return s.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/CMakeLists.txt b/2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/CMakeLists.txt new file mode 100644 index 00000000..0c627a93 --- /dev/null +++ b/2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2697) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2697 main.cpp) diff --git a/2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/main.cpp b/2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/main.cpp new file mode 100644 index 00000000..48f3ca61 --- /dev/null +++ b/2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/lexicographically-smallest-palindrome/description/ +/// Author : liuyubobobo +/// Time : 2023-05-21 + +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string makeSmallestPalindrome(string s) { + + int n = s.size(); + for(int i = 0, j = n - 1; i < j; i ++, j --){ + char min_char = min(s[i], s[j]); + s[i] = s[j] = min_char; + } + return s; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/CMakeLists.txt b/2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/CMakeLists.txt new file mode 100644 index 00000000..2b1f2172 --- /dev/null +++ b/2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2698) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2698 main.cpp) diff --git a/2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/main.cpp b/2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/main.cpp new file mode 100644 index 00000000..eeb0e32d --- /dev/null +++ b/2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/find-the-punishment-number-of-an-integer/description/ +/// Author : liuyubobobo +/// Time : 2023-05-21 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n * n * logn) +/// Space Complexity: O(nlogn) +class Solution { +public: + int punishmentNumber(int n) { + + int res = 0; + for(int i = 1; i <= n; i ++){ + int num = i * i; + if(check(num, i)) res += num; + } + return res; + } + +private: + bool check(int num, int sum){ + string num_str = to_string(num); + vector> dp(num_str.size(), vector(sum + 1, -1)); + return dfs(num_str, 0, sum, dp); + } + + int dfs(const string& s, int index, int sum, vector>& dp){ + + if(index == s.size()) return sum == 0; + if(sum < 0) return 0; + if(dp[index][sum] != -1) return dp[index][sum]; + + int cur = 0; + for(int i = index; i < s.size(); i ++){ + cur = cur * 10 + (s[i] - '0'); + if(dfs(s, i + 1, sum - cur, dp)) return dp[index][sum] = 1; + } + return dp[index][sum] = 0; + } +}; + +int main() { + + return 0; +} diff --git a/2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/CMakeLists.txt b/2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/CMakeLists.txt new file mode 100644 index 00000000..8efe1dc4 --- /dev/null +++ b/2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2699) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2699 main.cpp) diff --git a/2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/main.cpp b/2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/main.cpp new file mode 100644 index 00000000..8996841d --- /dev/null +++ b/2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/main.cpp @@ -0,0 +1,102 @@ +/// Source : https://leetcode.com/problems/modify-graph-edge-weights/description/ +/// Author : liuyubobobo +/// Time : 2023-05-21 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(n^2 * log(target) * n^2 * logn) +/// Space Complexity: O(n^2) +class Solution { + +private: + const long long INF = LONG_LONG_MAX / 2; + long long MAXW = 1000000000ll; + +public: + vector> modifiedGraphEdges(int n, vector>& edges, int source, int destination, int target) { + + MAXW = target; + + vector> g(n, vector(n, INF)); + for(const vector& edge: edges) { + int u = edge[0], v = edge[1], w = edge[2]; + if(w == -1) w = MAXW; + g[u][v] = g[v][u] = w; + } + + long long dis = dij(n, g, source, destination); + if(dis < target) return {}; + if(dis == target){ + for(vector& edge: edges) if(edge[2] == -1) edge[2] = MAXW; + return edges; + } + + for(vector& edge: edges){ + if(edge[2] == -1){ + int u = edge[0], v = edge[1]; + long long l = 1, r = MAXW; + while(l < r){ + long long mid = (l + r) / 2; + g[u][v] = g[v][u] = mid; + long long tdis = dij(n, g, source, destination); + if(tdis < target) l = mid + 1; + else r = mid; + } + g[u][v] = g[v][u] = l; + } + } + + if(dij(n, g, source, destination) != target) return {}; + for(vector& edge: edges) edge[2] = g[edge[0]][edge[1]]; + return edges; + } + +private: + long long dij(int n, const vector>& g, int s, int t){ + + vector dis(n, INF); + vector visited(n, false); + + priority_queue, vector>, greater<>> pq; + pq.push({0, s}); + dis[s] = 0; + while(!pq.empty()){ + long long d = pq.top().first; int u = pq.top().second; pq.pop(); + if(visited[u]) continue; + visited[u] = true; + + for(int v = 0; v < n; v ++){ + if(visited[v]) continue; + long long w = g[u][v]; + if(d + w < dis[v]){ + dis[v] = d + w; + pq.push({dis[v], v}); + } + } + } + return dis[t]; + } +}; + + +void print_vec(const vector>& v){ + for(const vector& e: v){ + for(int x: e) cout << x << ' '; cout << '\n'; + } +} + +int main() { + + vector> edges1 = {{4, 1, -1}, {2, 0, -1}, {0, 3, -1}, {4, 3, -1}}; + print_vec(Solution().modifiedGraphEdges(5, edges1, 0, 1, 5)); + + return 0; +} diff --git a/readme.md b/readme.md index 01fdb8ec..1cd39fad 100644 --- a/readme.md +++ b/readme.md @@ -2590,6 +2590,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2688 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2689 | [Extract Kth Character From The Rope Tree](https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/) | [无] | [C++](2001-2500/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/) | | | | | | | | | | +| 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings/) | [无] | [C++](2001-2500/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/) | | | +| 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome/) | [无] | [C++](2001-2500/2697-Lexicographically-Smallest-Palindrome/cpp-2697/) | | | +| 2698 | [Find the Punishment Number of an Integer](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | [无] | [C++](2001-2500/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/) | | | +| 2699 | [Modify Graph Edge Weights](https://leetcode.com/problems/modify-graph-edge-weights/) | [无] | [C++](2001-2500/2699-Modify-Graph-Edge-Weights/cpp-2699/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 606c22e52fb7ec54b49c7a5cbc431cd2093ca138 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 21 May 2023 22:17:43 -0700 Subject: [PATCH 359/390] readme updated. --- readme.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 1cd39fad..c249eeb2 100644 --- a/readme.md +++ b/readme.md @@ -2589,7 +2589,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2687 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2688 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2689 | [Extract Kth Character From The Rope Tree](https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/) | [无] | [C++](2001-2500/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/) | | | -| | | | | | | +| 2690 | JavaScript Problem | - | - | - | - | +| 2691 | JavaScript Problem | - | - | - | - | +| 2692 | JavaScript Problem | - | - | - | - | +| 2693 | JavaScript Problem | - | - | - | - | +| 2694 | JavaScript Problem | - | - | - | - | +| 2695 | JavaScript Problem | - | - | - | - | | 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings/) | [无] | [C++](2001-2500/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/) | | | | 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome/) | [无] | [C++](2001-2500/2697-Lexicographically-Smallest-Palindrome/cpp-2697/) | | | | 2698 | [Find the Punishment Number of an Integer](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | [无] | [C++](2001-2500/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/) | | | From 8124f97903a5f4cdb672a8852a43c59194afb4f5 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 25 May 2023 14:14:27 -0700 Subject: [PATCH 360/390] 0837 solved. --- .../0837-New-21-Game/cpp-0837/CMakeLists.txt | 6 +++ 0501-1000/0837-New-21-Game/cpp-0837/main.cpp | 43 +++++++++++++++++++ readme.md | 1 + 3 files changed, 50 insertions(+) create mode 100644 0501-1000/0837-New-21-Game/cpp-0837/CMakeLists.txt create mode 100644 0501-1000/0837-New-21-Game/cpp-0837/main.cpp diff --git a/0501-1000/0837-New-21-Game/cpp-0837/CMakeLists.txt b/0501-1000/0837-New-21-Game/cpp-0837/CMakeLists.txt new file mode 100644 index 00000000..c98a11cf --- /dev/null +++ b/0501-1000/0837-New-21-Game/cpp-0837/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_0837) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0837 main.cpp) diff --git a/0501-1000/0837-New-21-Game/cpp-0837/main.cpp b/0501-1000/0837-New-21-Game/cpp-0837/main.cpp new file mode 100644 index 00000000..1ab2bd11 --- /dev/null +++ b/0501-1000/0837-New-21-Game/cpp-0837/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/new-21-game/description/ +/// Author : liuyubobobo +/// Time : 2023-05-25 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(k + maxPts) +/// Space Complexity: O(k + maxPts) +class Solution { +public: + double new21Game(int n, int k, int maxPts) { + + if(k == 0) return 1.0; + + vector p(k + maxPts, 0), presum(k + maxPts + 1, 0); + p[0] = 1.0; + presum[1] = 1.0; + for(int i = 1; i < p.size(); i ++){ + int r = min(i - 1, k - 1), l = max(i - maxPts, 0); + long double sum = presum[r + 1] - presum[l]; + p[i] = sum / maxPts; + presum[i + 1] = presum[i] + p[i]; + } + + long double res = 0.0; + for(int i = k; i <= n && i < p.size(); i ++) res += p[i]; + return res; + } +}; + + +int main() { + + cout << Solution().new21Game(10, 1, 10) << '\n'; + // 1.0 + + return 0; +} diff --git a/readme.md b/readme.md index c249eeb2..0998ded9 100644 --- a/readme.md +++ b/readme.md @@ -840,6 +840,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 834 | [Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/) | [solution](https://leetcode.com/problems/sum-of-distances-in-tree/solution/) | [C++](0501-1000/0834-Sum-of-Distances-in-Tree/cpp-0834/) | | | | 835 | [Image Overlap](https://leetcode.com/problems/image-overlap/) | [无] | [C++](0501-1000/0835-Image-Overlap/cpp-0835/) | | | | | | | | | | +| 837 | [New 21 Game](https://leetcode.com/problems/new-21-game/) | [无] | [C++](0501-1000/0837-New-21-Game/cpp-0837/) | | | | 838 | [Push Dominoes](https://leetcode.com/problems/push-dominoes/) | [solution](https://leetcode.com/problems/push-dominoes/solution/) | [C++](0501-1000/0838-Push-Dominoes/cpp-0838/) | | | | 839 | [Similar String Groups](https://leetcode.com/problems/similar-string-groups/) | [solution](https://leetcode.com/problems/similar-string-groups/solution/) | [C++](0501-1000/0839-Similar-String-Groups/cpp-0839/) | | | From 0f25141af4f833504ee6922025b00e051639b4dd Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 26 May 2023 23:06:17 -0700 Subject: [PATCH 361/390] 2702 solved. --- .../cpp-2702/CMakeLists.txt | 6 ++ .../cpp-2702/main.cpp | 57 +++++++++++++++++++ readme.md | 6 ++ 3 files changed, 69 insertions(+) create mode 100644 2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/CMakeLists.txt create mode 100644 2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/main.cpp diff --git a/2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/CMakeLists.txt b/2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/CMakeLists.txt new file mode 100644 index 00000000..019579d7 --- /dev/null +++ b/2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2702) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2702 main.cpp) diff --git a/2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/main.cpp b/2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/main.cpp new file mode 100644 index 00000000..0180d347 --- /dev/null +++ b/2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-make-numbers-non-positive/description/ +/// Author : liuyubobobo +/// Time : 2023-05-26 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlog(max(nums))) +/// Space Complexity: O(1) +class Solution { +public: + int minOperations(vector& nums, int x, int y) { + + long long l = 0, r = *max_element(nums.begin(), nums.end()); + while(l < r){ + long long mid = (l + r) / 2; + if(ok(nums, x, y, mid)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(vector& nums, long long x, long long y, long long k) { + + long long cnt = 0; + for (long long e: nums) { + e -= k * y; + if (e <= 0) continue; + cnt += e / (x - y) + !!(e % (x - y)); + } + return cnt <= k; + } +}; + + +int main() { + + vector nums1 = {3, 4, 1, 7, 6}; + cout << Solution().minOperations(nums1, 4, 2) << '\n'; + // 3 + + vector nums2 = {1, 2, 1}; + cout << Solution().minOperations(nums2, 2, 1) << '\n'; + // 1 + + vector nums3 = {74, 92, 25, 65, 77, 1, 73}; + cout << Solution().minOperations(nums3, 10, 4) << '\n'; + // 16 + + return 0; +} diff --git a/readme.md b/readme.md index 0998ded9..73d122a5 100644 --- a/readme.md +++ b/readme.md @@ -2600,6 +2600,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome/) | [无] | [C++](2001-2500/2697-Lexicographically-Smallest-Palindrome/cpp-2697/) | | | | 2698 | [Find the Punishment Number of an Integer](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | [无] | [C++](2001-2500/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/) | | | | 2699 | [Modify Graph Edge Weights](https://leetcode.com/problems/modify-graph-edge-weights/) | [无] | [C++](2001-2500/2699-Modify-Graph-Edge-Weights/cpp-2699/) | | | +| 2700 | JavaScript Problem | - | - | - | - | +| 2701 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2702 | [Minimum Operations to Make Numbers Non-positive](https://leetcode.com/problems/minimum-operations-to-make-numbers-non-positive/description/) | [无] | [C++](2001-2500/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/) | | | +| 2703 | JavaScript Problem | - | - | - | - | +| 2704 | JavaScript Problem | - | - | - | - | +| 2705 | JavaScript Problem | - | - | - | - | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 2250cf478fb6f7ab96acd500c4b55e9b31722552 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 27 May 2023 21:07:49 -0700 Subject: [PATCH 362/390] 2706-2709 solved. --- .../cpp-2706/CMakeLists.txt | 6 ++ .../2706-Buy-Two-Chocolates/cpp-2706/main.cpp | 29 ++++++ .../cpp-2707/CMakeLists.txt | 6 ++ .../cpp-2707/main.cpp | 35 +++++++ .../cpp-2708/CMakeLists.txt | 6 ++ .../cpp-2708/main.cpp | 33 +++++++ .../cpp-2709/CMakeLists.txt | 6 ++ .../cpp-2709/main.cpp | 99 +++++++++++++++++++ readme.md | 4 + 9 files changed, 224 insertions(+) create mode 100644 2501-3000/2706-Buy-Two-Chocolates/cpp-2706/CMakeLists.txt create mode 100644 2501-3000/2706-Buy-Two-Chocolates/cpp-2706/main.cpp create mode 100644 2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/CMakeLists.txt create mode 100644 2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/main.cpp create mode 100644 2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/CMakeLists.txt create mode 100644 2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/main.cpp create mode 100644 2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/CMakeLists.txt create mode 100644 2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/main.cpp diff --git a/2501-3000/2706-Buy-Two-Chocolates/cpp-2706/CMakeLists.txt b/2501-3000/2706-Buy-Two-Chocolates/cpp-2706/CMakeLists.txt new file mode 100644 index 00000000..8a306afe --- /dev/null +++ b/2501-3000/2706-Buy-Two-Chocolates/cpp-2706/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(A) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(A main.cpp) diff --git a/2501-3000/2706-Buy-Two-Chocolates/cpp-2706/main.cpp b/2501-3000/2706-Buy-Two-Chocolates/cpp-2706/main.cpp new file mode 100644 index 00000000..f52cd2ce --- /dev/null +++ b/2501-3000/2706-Buy-Two-Chocolates/cpp-2706/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/buy-two-chocolates/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int buyChoco(vector& prices, int money) { + + sort(prices.begin(), prices.end()); + int left = money - prices[0] - prices[1]; + return left >= 0 ? left : money; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/CMakeLists.txt b/2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/CMakeLists.txt new file mode 100644 index 00000000..3e27b177 --- /dev/null +++ b/2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(B) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(B main.cpp) diff --git a/2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/main.cpp b/2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/main.cpp new file mode 100644 index 00000000..733abfe4 --- /dev/null +++ b/2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/extra-characters-in-a-string/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * all_word_length) +/// Space Complexity: O(n) +class Solution { +public: + int minExtraChar(string s, vector& dictionary) { + + int n = s.size(); + vector dp(n + 1, 0); + for(int i = n - 1; i >= 0; i --){ + dp[i] = dp[i + 1]; + for(const string& word: dictionary) + if(s.find(word, i) == i) + dp[i] = max(dp[i], (int)word.size() + dp[i + word.size()]); + } + + return n - *max_element(dp.begin(), dp.end()); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/CMakeLists.txt b/2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/CMakeLists.txt new file mode 100644 index 00000000..6d269de2 --- /dev/null +++ b/2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(C) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(C main.cpp) diff --git a/2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/main.cpp b/2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/main.cpp new file mode 100644 index 00000000..75eab927 --- /dev/null +++ b/2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/maximum-strength-of-a-group/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(2^n) +/// Space Complexity: O(2^n) +class Solution { +public: + long long maxStrength(vector& nums) { + + int n = nums.size(); + + vector dp(1 << n, 1); + for(int state = 1; state < (1 << n); state ++){ + int p = __builtin_ffs(state) - 1; + dp[state] = dp[state ^ (1 << p)] * nums[p]; + } + return *max_element(dp.begin() + 1, dp.end()); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/CMakeLists.txt b/2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/CMakeLists.txt new file mode 100644 index 00000000..6a6bdfdb --- /dev/null +++ b/2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(D) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(D main.cpp) diff --git a/2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/main.cpp b/2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/main.cpp new file mode 100644 index 00000000..150c92e2 --- /dev/null +++ b/2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/main.cpp @@ -0,0 +1,99 @@ +/// Source : https://leetcode.com/problems/greatest-common-divisor-traversal/description/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Sieve Table + UF +/// Time Complexity: O(n * log(max(nums[i]))) +/// Space Complexity: O(n * log(max(nums[i]))) +class UF{ + +private: + vector parent; + int total; + +public: + UF(int n) : parent(n), total(n){ + for(int i = 0 ; i < n ; i ++) + parent[i] = i; + } + + int find(int p){ + if(p != parent[p]) + parent[p] = find(parent[p]); + return parent[p]; + } + + bool is_connected(int p, int q){ + return find(p) == find(q); + } + + void union_elements(int p, int q){ + + int p_root = find(p), q_root = find(q); + + if(p_root == q_root) return; + + parent[p_root] = q_root; + total --; + } + + int get_total(){ + return total; + } +}; + +class Solution { +public: + bool canTraverseAllPairs(vector& nums) { + + vector sieve_table = sieve(*max_element(nums.begin(), nums.end())); + map> table; // d -> index list + for(int i = 0; i < nums.size(); i ++){ + int e = nums[i]; + while(e > 1){ + int p = sieve_table[e]; + table[p].push_back(i); + while(e % p == 0) e /= p; + } + } + + UF uf(nums.size()); + for(const pair>& row: table){ + for(int i = 1; i < row.second.size(); i ++) + uf.union_elements(row.second[i - 1], row.second[i]); + } + return uf.get_total() == 1; + } + +private: + vector sieve(int n){ + + vector table(n + 1, 0); + vector primes; + table[0] = table[1] = 1; + for(int i = 2; i <= n; i ++){ + if(table[i] == 0) { + primes.push_back(i); + table[i] = i; + } + for(int j = 0; j < primes.size() && primes[j] <= table[i] && i * primes[j] <= n; j ++) + table[i * primes[j]] = primes[j]; + } + return table; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 73d122a5..082cf877 100644 --- a/readme.md +++ b/readme.md @@ -2606,6 +2606,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2703 | JavaScript Problem | - | - | - | - | | 2704 | JavaScript Problem | - | - | - | - | | 2705 | JavaScript Problem | - | - | - | - | +| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates/) | [无] | [C++](2001-2500/2706-Buy-Two-Chocolates/cpp-2706/) | | | +| 2707 | [Extra Characters in a String](https://leetcode.com/problems/extra-characters-in-a-string/) | [无] | [C++](2001-2500/2707-Extra-Characters-in-a-String/cpp-2707/) | | | +| 2708 | [Maximum Strength of a Group](https://leetcode.com/problems/maximum-strength-of-a-group/) | [无] | [C++](2001-2500/2708-Maximum-Strength-of-a-Group/cpp-2708/) | | | +| 2709 | [Greatest Common Divisor Traversal](https://leetcode.com/problems/greatest-common-divisor-traversal/) | [无] | [C++](2001-2500/2709-Greatest-Common-Divisor-Traversal/cpp-2709/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 7bb5d52a6a8bc3bd15d8e1183c089700fa6d8a65 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 27 May 2023 21:23:39 -0700 Subject: [PATCH 363/390] 2710-2713 solved. --- .../cpp-2710/CMakeLists.txt | 6 ++ .../cpp-2710/main.cpp | 26 ++++++ .../cpp-2711/CMakeLists.txt | 6 ++ .../cpp-2711/main.cpp | 51 ++++++++++++ .../cpp-2712/CMakeLists.txt | 6 ++ .../cpp-2712/main.cpp | 56 +++++++++++++ .../cpp-2713/CMakeLists.txt | 6 ++ .../cpp-2713/main.cpp | 66 +++++++++++++++ .../cpp-2713/main2.cpp | 83 +++++++++++++++++++ readme.md | 4 + 10 files changed, 310 insertions(+) create mode 100644 2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/CMakeLists.txt create mode 100644 2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/main.cpp create mode 100644 2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/CMakeLists.txt create mode 100644 2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/main.cpp create mode 100644 2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/CMakeLists.txt create mode 100644 2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/main.cpp create mode 100644 2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/CMakeLists.txt create mode 100644 2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main.cpp create mode 100644 2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main2.cpp diff --git a/2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/CMakeLists.txt b/2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/CMakeLists.txt new file mode 100644 index 00000000..8a306afe --- /dev/null +++ b/2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(A) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(A main.cpp) diff --git a/2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/main.cpp b/2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/main.cpp new file mode 100644 index 00000000..bc8b7ac3 --- /dev/null +++ b/2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/main.cpp @@ -0,0 +1,26 @@ +/// Source : https://leetcode.com/problems/remove-trailing-zeros-from-a-string/description/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|num|) +/// Space Complexity: O(|num|) +class Solution { +public: + string removeTrailingZeros(string num) { + + while(!num.empty() && num.back() == '0') num.pop_back(); + return num; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/CMakeLists.txt b/2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/CMakeLists.txt new file mode 100644 index 00000000..3e27b177 --- /dev/null +++ b/2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(B) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(B main.cpp) diff --git a/2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/main.cpp b/2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/main.cpp new file mode 100644 index 00000000..486196bc --- /dev/null +++ b/2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(R * C * max(R, C)) +/// Space Complexity: O(1) +class Solution { +public: + vector> differenceOfDistinctValues(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + vector> res(R, vector(C)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + res[i][j] = solve(R, C, grid, i, j); + return res; + } + +private: + int solve(int R, int C, vector>& grid, int x, int y){ + + int cx = x, cy = y; + set s1; + while(-- cx >= 0 && -- cy >= 0) + s1.insert(grid[cx][cy]); + + cx = x, cy = y; + set s2; + while(++ cx < R && ++ cy < C) + s2.insert(grid[cx][cy]); + + int t = (int)s1.size() - (int)s2.size(); + return abs(t); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/CMakeLists.txt b/2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/CMakeLists.txt new file mode 100644 index 00000000..6d269de2 --- /dev/null +++ b/2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(C) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(C main.cpp) diff --git a/2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/main.cpp b/2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/main.cpp new file mode 100644 index 00000000..d10b39a9 --- /dev/null +++ b/2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/description/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long minimumCost(string s) { + + int n = s.size(); + deque> pos0, pos1; + for(int start = 0, i = 1; i <= n; i ++){ + if(i == n || s[i] != s[start]){ + int a = start, b = i - 1; + if(s[start] == '0') pos0.push_back({a, b}); + else pos1.push_back({a, b}); + start = i; + } + } + return min(solve(n, pos0), solve(n, pos1)); + } + +private: + long long solve(int n, deque>& pos){ + + long long res = 0; + while(!pos.empty()){ + int tres1 = pos.front().second + 1 + pos.front().first; + int tres2 = (n - pos.back().first) + (n - (pos.back().second + 1)); + + if(tres1 < tres2) res += tres1, pos.pop_front(); + else res += tres2, pos.pop_back(); + } + return res; + } +}; + + +int main() { + + cout << Solution().minimumCost("0011") << '\n'; + // 2 + + cout << Solution().minimumCost("010101") << '\n'; + // 9 + + return 0; +} diff --git a/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/CMakeLists.txt b/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/CMakeLists.txt new file mode 100644 index 00000000..5a9eabe1 --- /dev/null +++ b/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(D) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(D main2.cpp) diff --git a/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main.cpp b/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main.cpp new file mode 100644 index 00000000..e4fb785e --- /dev/null +++ b/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +using namespace std; + + +class Solution { +public: + int maxIncreasingCells(vector>& mat) { + + int R = mat.size(), C = mat[0].size(); + + priority_queue, vector>, greater<>> pq; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) pq.push({mat[i][j], i * C + j}); + + vector row(R, 0), col(C, 0); + vector rowv(R, INT_MIN), colv(C, INT_MIN); + vector> dp(R, vector(C, 0)); + int res = 0; + while(!pq.empty()){ + int v = pq.top().first, pos = pq.top().second; + pq.pop(); + int r = pos / C, c = pos % C; + + if(v > rowv[r]) dp[r][c] = max(dp[r][c], row[r] + 1); + else dp[r][c] = max(dp[r][c], row[r]); + if(v > colv[c]) dp[r][c] = max(dp[r][c], col[c] + 1); + else dp[r][c] = max(dp[r][c], col[c]); + + if(dp[r][c] > row[r]) row[r] = dp[r][c], rowv[r] = v; + if(dp[r][c] > col[c]) col[c] = dp[r][c], colv[c] = v; + + res = max(res, dp[r][c]); + } + return res; + } +}; + + +int main() { + +// vector> mat1 = {{3, 1}, {3, 4}}; +// cout << Solution().maxIncreasingCells(mat1) << '\n'; +// // 2 +// +// vector> mat2 = {{1, 1}, {1, 1}}; +// cout << Solution().maxIncreasingCells(mat2) << '\n'; +// // 1 +// +// vector> mat3 = {{3, 1, 6}, {-9, 5, 7}}; +// cout << Solution().maxIncreasingCells(mat3) << '\n'; +// // 4 +// +// vector> mat4 = {{7, 6, 3}, {-7, -5, 6}, {-7, 0, -4}, {6, 6, 0}, {-8, 6, 0}}; +// cout << Solution().maxIncreasingCells(mat4) << '\n'; +// // 7 + + vector> mat5 = {{3, 1}, {3, 4}}; + cout << Solution().maxIncreasingCells(mat5) << '\n'; + // 2 + + return 0; +} diff --git a/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main2.cpp b/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main2.cpp new file mode 100644 index 00000000..c3b083d7 --- /dev/null +++ b/2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/main2.cpp @@ -0,0 +1,83 @@ +/// Source : https://leetcode.com/problems/maximum-strictly-increasing-cells-in-a-matrix/description/ +/// Author : liuyubobobo +/// Time : 2023-05-27 + +#include +#include +#include +#include + +using namespace std; + + +/// DAG DP +/// Time Complexity: O(R * C * log(R * C)) +/// Space Complexity: O(R + C) +class Solution { +public: + int maxIncreasingCells(vector>& mat) { + + int R = mat.size(), C = mat[0].size(); + + priority_queue, vector>, greater<>> pq; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) pq.push({mat[i][j], i * C + j}); + + vector row1(R, 0), col1(C, 0), row2(R, 0), col2(C, 0); + vector rowv1(R, INT_MIN), colv1(C, INT_MIN), rowv2(R, INT_MIN), colv2(C, INT_MIN); + vector> dp(R, vector(C, 0)); + int res = 0; + while(!pq.empty()){ + int v = pq.top().first, pos = pq.top().second; + pq.pop(); + int r = pos / C, c = pos % C; + + if(v > rowv1[r]) dp[r][c] = max(dp[r][c], row1[r] + 1); + else dp[r][c] = max(dp[r][c], row2[r] + 1); + + if(v > colv1[c]) dp[r][c] = max(dp[r][c], col1[c] + 1); + else dp[r][c] = max(dp[r][c], col2[c] + 1); + + if(v > rowv1[r] && dp[r][c] > row1[r]){ + row2[r] = row1[r], rowv2[r] = rowv1[r]; + row1[r] = dp[r][c], rowv1[r] = v; + } + else if(v == rowv1[r] && dp[r][c] > row1[r]) row1[r] = dp[r][c]; + + if(v > colv1[c] && dp[r][c] > col1[c]){ + col2[c] = col1[c], colv2[c] = colv1[c]; + col1[c] = dp[r][c], colv1[c] = v; + } + else if(v == colv1[c] && dp[r][c] > col1[c]) col1[c] = dp[r][c]; + + res = max(res, dp[r][c]); + } + return res; + } +}; + + +int main() { + + vector> mat1 = {{3, 1}, {3, 4}}; + cout << Solution().maxIncreasingCells(mat1) << '\n'; + // 2 + + vector> mat2 = {{1, 1}, {1, 1}}; + cout << Solution().maxIncreasingCells(mat2) << '\n'; + // 1 + + vector> mat3 = {{3, 1, 6}, {-9, 5, 7}}; + cout << Solution().maxIncreasingCells(mat3) << '\n'; + // 4 + + vector> mat4 = {{7, 6, 3}, {-7, -5, 6}, {-7, 0, -4}, {6, 6, 0}, {-8, 6, 0}}; + cout << Solution().maxIncreasingCells(mat4) << '\n'; + // 7 + + vector> mat5 = {{3, 1}, {3, 4}}; + cout << Solution().maxIncreasingCells(mat5) << '\n'; + // 2 + + return 0; +} diff --git a/readme.md b/readme.md index 082cf877..27abd989 100644 --- a/readme.md +++ b/readme.md @@ -2610,6 +2610,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2707 | [Extra Characters in a String](https://leetcode.com/problems/extra-characters-in-a-string/) | [无] | [C++](2001-2500/2707-Extra-Characters-in-a-String/cpp-2707/) | | | | 2708 | [Maximum Strength of a Group](https://leetcode.com/problems/maximum-strength-of-a-group/) | [无] | [C++](2001-2500/2708-Maximum-Strength-of-a-Group/cpp-2708/) | | | | 2709 | [Greatest Common Divisor Traversal](https://leetcode.com/problems/greatest-common-divisor-traversal/) | [无] | [C++](2001-2500/2709-Greatest-Common-Divisor-Traversal/cpp-2709/) | | | +| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string/) | [无] | [C++](2001-2500/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/) | | | +| 2711 | [Difference of Number of Distinct Values on Diagonals](https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/) | [无] | [C++](2001-2500/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/) | | | +| 2712 | [Minimum Cost to Make All Characters Equal](https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/) | [无] | [C++](2001-2500/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/) | | | +| 2713 | [Maximum Strictly Increasing Cells in a Matrix](https://leetcode.com/problems/maximum-strictly-increasing-cells-in-a-matrix/) | [无] | [C++](2001-2500/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 35fceb0e0ddee169e52d206fb86dde958847419f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sat, 3 Jun 2023 11:47:39 -0700 Subject: [PATCH 364/390] 2714 solved. --- .../cpp-2714/CMakeLists.txt | 6 ++ .../cpp-2714/main.cpp | 73 +++++++++++++++++++ readme.md | 1 + 3 files changed, 80 insertions(+) create mode 100644 2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/CMakeLists.txt create mode 100644 2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/main.cpp diff --git a/2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/CMakeLists.txt b/2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/CMakeLists.txt new file mode 100644 index 00000000..e497d7db --- /dev/null +++ b/2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2714) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2714 main.cpp) diff --git a/2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/main.cpp b/2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/main.cpp new file mode 100644 index 00000000..089f4031 --- /dev/null +++ b/2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/main.cpp @@ -0,0 +1,73 @@ +/// Source : https://leetcode.com/problems/find-shortest-path-with-k-hops/description/ +/// Author : liuyubobobo +/// Time : 2023-06-03 + +#include +#include +#include +#include + +using namespace std; + + +/// Dijkstra +/// Time Complexity: O(nk * log(n * k)) +/// Space Complexity: O(n * k) +class Solution { + +private: + const int INF = INT_MAX / 2; + +public: + int shortestPathWithHops(int n, vector>& edges, int s, int t, int k) { + + vector>> g(n); + for(auto& e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].push_back({v, w}), g[v].push_back({u, w}); + } + + priority_queue>, vector>>, greater<>> pq; + vector> dis(n, vector(k + 1, INF)); + vector> visited(n, vector(k + 1, false)); + pq.push({0, {s, k}}); + dis[s][k] = 0; + + while(!pq.empty()) { + const pair> p = pq.top(); pq.pop(); + int d = p.first, u = p.second.first, h = p.second.second; + + if(visited[u][h]) continue; + visited[u][h] = true; +// cout << u << " " << h << " " << d << endl; + + if(h){ + for(const pair& e : g[u]) { + int v = e.first, w = e.second; + if(!visited[v][h - 1] && dis[v][h - 1] > d) { + dis[v][h - 1] = d; + pq.push({d, {v, h - 1}}); + } + } + } + + for(const pair& e : g[u]) { + int v = e.first, w = e.second; + if(!visited[v][h] && dis[v][h] > d + w) { + dis[v][h] = d + w; + pq.push({d + w, {v, h}}); + } + } + } + return *min_element(dis[t].begin(), dis[t].end()); + } +}; + + +int main() { + + vector> edges1 = {{0,1,4},{0,2,2},{2,3,6}}; + cout << Solution().shortestPathWithHops(4, edges1, 1, 3, 2) << endl; + + return 0; +} diff --git a/readme.md b/readme.md index 27abd989..bab9d210 100644 --- a/readme.md +++ b/readme.md @@ -2614,6 +2614,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2711 | [Difference of Number of Distinct Values on Diagonals](https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/) | [无] | [C++](2001-2500/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/) | | | | 2712 | [Minimum Cost to Make All Characters Equal](https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/) | [无] | [C++](2001-2500/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/) | | | | 2713 | [Maximum Strictly Increasing Cells in a Matrix](https://leetcode.com/problems/maximum-strictly-increasing-cells-in-a-matrix/) | [无] | [C++](2001-2500/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/) | | | +| 2714 | [Find Shortest Path with K Hops](https://leetcode.com/problems/find-shortest-path-with-k-hops/description/) | [无] | [C++](2001-2500/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 24ab95f49b5679e5f32e8f253174fc9c59cd451c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 4 Jun 2023 14:12:35 -0700 Subject: [PATCH 365/390] 2716-2719 solved. --- .../cpp-2716/CMakeLists.txt | 6 ++ .../cpp-2716/main.cpp | 29 ++++++++ .../cpp-2717/CMakeLists.txt | 6 ++ .../cpp-2717/main.cpp | 33 +++++++++ .../cpp-2718/CMakeLists.txt | 6 ++ .../cpp-2718/main.cpp | 50 +++++++++++++ .../cpp-2719/CMakeLists.txt | 6 ++ .../2719-Count-of-Integers/cpp-2719/main.cpp | 73 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 214 insertions(+) create mode 100644 2501-3000/2716-Minimize-String-Length/cpp-2716/CMakeLists.txt create mode 100644 2501-3000/2716-Minimize-String-Length/cpp-2716/main.cpp create mode 100644 2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/CMakeLists.txt create mode 100644 2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/main.cpp create mode 100644 2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/CMakeLists.txt create mode 100644 2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/main.cpp create mode 100644 2501-3000/2719-Count-of-Integers/cpp-2719/CMakeLists.txt create mode 100644 2501-3000/2719-Count-of-Integers/cpp-2719/main.cpp diff --git a/2501-3000/2716-Minimize-String-Length/cpp-2716/CMakeLists.txt b/2501-3000/2716-Minimize-String-Length/cpp-2716/CMakeLists.txt new file mode 100644 index 00000000..de76dea0 --- /dev/null +++ b/2501-3000/2716-Minimize-String-Length/cpp-2716/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2716) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2716 main.cpp) diff --git a/2501-3000/2716-Minimize-String-Length/cpp-2716/main.cpp b/2501-3000/2716-Minimize-String-Length/cpp-2716/main.cpp new file mode 100644 index 00000000..2625e792 --- /dev/null +++ b/2501-3000/2716-Minimize-String-Length/cpp-2716/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/minimize-string-length/description/ +/// Author : liuyubobobo +/// Time : 2023-06-04 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimizedStringLength(string s) { + + vector visited(26, false); + for(char c: s) visited[c - 'a'] = true; + return accumulate(visited.begin(), visited.end(), 0); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/CMakeLists.txt b/2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/CMakeLists.txt new file mode 100644 index 00000000..04397242 --- /dev/null +++ b/2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2717) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2717 main.cpp) diff --git a/2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/main.cpp b/2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/main.cpp new file mode 100644 index 00000000..5787ff57 --- /dev/null +++ b/2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/semi-ordered-permutation/description/ +/// Author : liuyubobobo +/// Time : 2023-06-04 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int semiOrderedPermutation(vector& nums) { + + int n = nums.size(); + + int pos_1 = find(nums.begin(), nums.end(), 1) - nums.begin(); + int pos_n = find(nums.begin(), nums.end(), n) - nums.begin(); + + int res = pos_1 + n - 1 - pos_n; + if(pos_1 > pos_n) res--; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/CMakeLists.txt b/2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/CMakeLists.txt new file mode 100644 index 00000000..72d0e064 --- /dev/null +++ b/2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2718) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2718 main.cpp) diff --git a/2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/main.cpp b/2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/main.cpp new file mode 100644 index 00000000..82dbc157 --- /dev/null +++ b/2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/sum-of-matrix-after-queries/description/ +/// Author : liuyubobobo +/// Time : 2023-06-04 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(|queries|) +/// Space Complexity: O(n) +class Solution { +public: + long long matrixSumQueries(int n, vector>& queries) { + + long long res = 0; + + vector row_visited(n, false), col_visited(n, false); + int row_left = n, col_left = n; + + for(int i = queries.size() - 1; i >= 0; i --){ + int type = queries[i][0], index = queries[i][1], val = queries[i][2]; + + if(type == 0){ + if(row_visited[index]) continue; + + res += (long long)val * col_left; + row_visited[index] = true; + row_left --; + } + else{ + if(col_visited[index]) continue; + + res += (long long)val * row_left; + col_visited[index] = true; + col_left --; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2719-Count-of-Integers/cpp-2719/CMakeLists.txt b/2501-3000/2719-Count-of-Integers/cpp-2719/CMakeLists.txt new file mode 100644 index 00000000..dec1b17c --- /dev/null +++ b/2501-3000/2719-Count-of-Integers/cpp-2719/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2719) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2719 main.cpp) diff --git a/2501-3000/2719-Count-of-Integers/cpp-2719/main.cpp b/2501-3000/2719-Count-of-Integers/cpp-2719/main.cpp new file mode 100644 index 00000000..0dcc4c99 --- /dev/null +++ b/2501-3000/2719-Count-of-Integers/cpp-2719/main.cpp @@ -0,0 +1,73 @@ +/// Source : https://leetcode.com/problems/count-of-integers/description/ +/// Author : liuyubobobo +/// Time : 2023-06-04 + +#include +#include + +using namespace std; + + +/// Digital DP +/// Time Complexity: O(len(num2) * max_sum) +/// Space Complexity: O(len(num2) * max_sum) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int count(string num1, string num2, int min_sum, int max_sum) { + num1 = sub1(num1); + long long res = solve(num2, min_sum, max_sum) - solve(num1, min_sum, max_sum); + return (res + MOD) % MOD; + } + +private: + long long solve(const string& s, int min_sum, int max_sum){ + + int len = s.size(); + vector> dp(len << 1, vector(max_sum + 1, -1)); + return dfs(len, s, 0, 0, 0, min_sum, max_sum, dp); + } + + long long dfs(int len, const string& s, int index, int can_over, int cur_sum, + const int min_sum, const int max_sum, vector>& dp){ + + if(index == len) return min_sum <= cur_sum && cur_sum <= max_sum; + if(cur_sum > max_sum) return 0; + if(dp[index * 2 + can_over][cur_sum] != -1) return dp[index * 2 + can_over][cur_sum]; + + long long res = 0; + for(int i = 0; i <= (can_over ? 9 : s[index] - '0'); i ++){ + res += dfs(len, s, index + 1, can_over || i < s[index] - '0', cur_sum + i, min_sum, max_sum, dp); + res %= MOD; + } + return dp[index * 2 + can_over][cur_sum] = res; + } + + string sub1(const string& s){ + string res = s; + for(int i = res.size() - 1; i >= 0; i --){ + if(res[i] == '0'){ + res[i] = '9'; + }else{ + res[i] -= 1; + break; + } + } + + reverse(res.begin(), res.end()); + while(res.back() == '0') res.pop_back(); + reverse(res.begin(), res.end()); + return res.size() == 0 ? "0" : res; + } +}; + + +int main() { + + cout << Solution().count("1", "12", 1, 8) << '\n'; + + return 0; +} diff --git a/readme.md b/readme.md index bab9d210..a7d5e3ce 100644 --- a/readme.md +++ b/readme.md @@ -2615,6 +2615,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2712 | [Minimum Cost to Make All Characters Equal](https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/) | [无] | [C++](2001-2500/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/) | | | | 2713 | [Maximum Strictly Increasing Cells in a Matrix](https://leetcode.com/problems/maximum-strictly-increasing-cells-in-a-matrix/) | [无] | [C++](2001-2500/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/) | | | | 2714 | [Find Shortest Path with K Hops](https://leetcode.com/problems/find-shortest-path-with-k-hops/description/) | [无] | [C++](2001-2500/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/) | | | +| 2715 | JavaScript Problem | - | - | - | - | +| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length/) | [无] | [C++](2001-2500/2716-Minimize-String-Length/cpp-2716/) | | | +| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation/) | [无] | [C++](2001-2500/2717-Semi-Ordered-Permutation/cpp-2717/) | | | +| 2718 | [Sum of Matrix After Queries](https://leetcode.com/problems/sum-of-matrix-after-queries/) | [无] | [C++](2001-2500/2718-Sum-of-Matrix-After-Queries/cpp-2718/) | | | +| 2719 | [Count of Integers](https://leetcode.com/problems/count-of-integers/) | [无] | [C++](2001-2500/2719-Count-of-Integers/cpp-2719/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 39b33d4f4635d907f86781f45d595141523a62fc Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 8 Jun 2023 15:49:37 -0700 Subject: [PATCH 366/390] 2728 solved. --- .../cpp-2728/CMakeLists.txt | 6 +++ .../cpp-2728/main.cpp | 49 +++++++++++++++++++ readme.md | 9 ++++ 3 files changed, 64 insertions(+) create mode 100644 2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/CMakeLists.txt create mode 100644 2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/main.cpp diff --git a/2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/CMakeLists.txt b/2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/CMakeLists.txt new file mode 100644 index 00000000..9ba5342d --- /dev/null +++ b/2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2728) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2728 main.cpp) diff --git a/2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/main.cpp b/2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/main.cpp new file mode 100644 index 00000000..cb78e219 --- /dev/null +++ b/2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/count-houses-in-a-circular-street/description/ +/// Author : liuyubobobo +/// Time : 2023-06-08 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(k) +/// Space Complexity: O(1) + +/// Definition for a street. +class Street { +public: + Street(vector doors); + void openDoor(); + void closeDoor(); + bool isDoorOpen(); + void moveRight(); + void moveLeft(); +}; + +class Solution { +public: + int houseCount(Street* street, int k) { + + for(int i = 0; i < k; i ++, street->moveRight()) + if(street->isDoorOpen()) + street->closeDoor(); + + street->openDoor(); + street->moveRight(); + + int res = 1; + while(!street->isDoorOpen()){ + res ++; + street->moveRight(); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index a7d5e3ce..ad799b84 100644 --- a/readme.md +++ b/readme.md @@ -2620,6 +2620,15 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation/) | [无] | [C++](2001-2500/2717-Semi-Ordered-Permutation/cpp-2717/) | | | | 2718 | [Sum of Matrix After Queries](https://leetcode.com/problems/sum-of-matrix-after-queries/) | [无] | [C++](2001-2500/2718-Sum-of-Matrix-After-Queries/cpp-2718/) | | | | 2719 | [Count of Integers](https://leetcode.com/problems/count-of-integers/) | [无] | [C++](2001-2500/2719-Count-of-Integers/cpp-2719/) | | | +| 2720 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2721 | JavaScript Problem | - | - | - | - | +| 2722 | JavaScript Problem | - | - | - | - | +| 2723 | JavaScript Problem | - | - | - | - | +| 2724 | JavaScript Problem | - | - | - | - | +| 2725 | JavaScript Problem | - | - | - | - | +| 2726 | JavaScript Problem | - | - | - | - | +| 2727 | JavaScript Problem | - | - | - | - | +| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/description/) | [无] | [C++](2001-2500/2728-Count-Houses-in-a-Circular-Street/cpp-2728/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From b43d8c87fc21ba3932556990c3de8c8f17010160 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 16 Jun 2023 12:40:20 -0700 Subject: [PATCH 367/390] 1161 codes updated. --- .../cpp-1161/main2.cpp | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/1001-1500/1161-Maximum-Level-Sum-of-a-Binary-Tree/cpp-1161/main2.cpp b/1001-1500/1161-Maximum-Level-Sum-of-a-Binary-Tree/cpp-1161/main2.cpp index 3b09ff21..22583fa1 100644 --- a/1001-1500/1161-Maximum-Level-Sum-of-a-Binary-Tree/cpp-1161/main2.cpp +++ b/1001-1500/1161-Maximum-Level-Sum-of-a-Binary-Tree/cpp-1161/main2.cpp @@ -1,9 +1,12 @@ /// Source : https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/ /// Author : liuyubobobo /// Time : 2019-08-18 +/// Updated: 2022-06-16 #include #include +#include +#include using namespace std; @@ -27,25 +30,27 @@ class Solution { if(!root) return 0; - queue> q; - q.push(make_pair(root, 1)); - int sum = INT_MIN, record_level = 0, best = INT_MIN, res = 0; + queue q; + int best = INT_MIN, res = 0, cur_level = 0; + q.push(root); while(!q.empty()){ - TreeNode* curNode = q.front().first; - int cur_level = q.front().second; - q.pop(); - - if(cur_level != record_level){ - if(sum > best) - best = sum, res = record_level; - sum = 0, record_level = cur_level; + + cur_level ++; + queue q2; + int sum = 0; + while(!q.empty()){ + TreeNode* cur = q.front(); q.pop(); assert(cur); + sum += cur->val; + if(cur->left) q2.push(cur->left); + if(cur->right) q2.push(cur->right); } - sum += curNode->val; - if(curNode->left) q.push(make_pair(curNode->left, cur_level + 1)); - if(curNode->right) q.push(make_pair(curNode->right, cur_level + 1)); + // cout << cur_level << ' ' << sum << '\n'; + if(sum > best) best = sum, res = cur_level; + + q = q2; } - return sum > best ? record_level : res; + return res; } }; From 4184b6484efab44ab40e900a1da40a79bd8449c6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 16 Jun 2023 12:52:59 -0700 Subject: [PATCH 368/390] 0163 codes updated. --- .../0163-Missing-Ranges/cpp-0163/main.cpp | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/0001-0500/0163-Missing-Ranges/cpp-0163/main.cpp b/0001-0500/0163-Missing-Ranges/cpp-0163/main.cpp index ce8a9815..4f87bdf7 100644 --- a/0001-0500/0163-Missing-Ranges/cpp-0163/main.cpp +++ b/0001-0500/0163-Missing-Ranges/cpp-0163/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/missing-ranges/ /// Author : liuyubobobo /// Time : 2020-12-08 +/// Updated: 2022-06-16 #include #include @@ -13,24 +14,33 @@ using namespace std; /// Space Complexity: O(1) class Solution { public: - vector findMissingRanges(vector& nums, int lower, int upper) { + vector> findMissingRanges(vector& nums, int lower, int upper) { - nums.insert(nums.begin(), lower - 1); nums.push_back(upper + 1); - vector res; - for(int i = 0; i + 1 < nums.size(); i ++) - if(nums[i] + 1 != nums[i + 1]){ - int l = nums[i] + 1, r = nums[i + 1] - 1; - if(l == r) res.push_back(to_string(l)); - else res.push_back(to_string(l) + "->" + to_string(r)); + vector> res; + int l = lower; + for(int i = 0; i < nums.size(); i ++){ + if(nums[i] == l) l ++; + else{ + res.push_back({l, nums[i] - 1}); + l = nums[i] + 1; } + } return res; } }; +void print_vec(const vector>& v){ + for(const vector& seg: v) cout << '(' << seg[0] << ',' << seg[1] << ')'; + cout << '\n'; +} + int main() { + vector nums1 = {0, 1, 3, 50, 75}; + print_vec(Solution().findMissingRanges(nums1, 0, 99)); + return 0; } From 746bb7e504a6205326484448496fd70f7ceaa633 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 16 Jun 2023 13:01:53 -0700 Subject: [PATCH 369/390] 2737 solved. --- .../cpp-2737/CMakeLists.txt | 6 ++ .../cpp-2737/main.cpp | 59 +++++++++++++++++++ readme.md | 2 + 3 files changed, 67 insertions(+) create mode 100644 2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/CMakeLists.txt create mode 100644 2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/main.cpp diff --git a/2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/CMakeLists.txt b/2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/CMakeLists.txt new file mode 100644 index 00000000..d0cc5d1f --- /dev/null +++ b/2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2737) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2737 main.cpp) diff --git a/2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/main.cpp b/2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/main.cpp new file mode 100644 index 00000000..2b5d73dc --- /dev/null +++ b/2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.cn/problems/find-the-closest-marked-node/ +/// Author : liuyubobobo +/// Time : 2023-06-16 + +#include +#include +#include +#include + +using namespace std; + + +/// Dijkstra +/// Time Complexity: O(ElogE) +/// Space Complexity: O(V) +class Solution { + +private: + const int INF = INT_MAX / 2; + +public: + int minimumDistance(int n, vector>& edges, int s, vector& marked) { + + vector>> g(n); + for(const vector& edge: edges){ + int u = edge[0], v = edge[1], w = edge[2]; + g[u].push_back({v, w}); + } + + vector dis(n, INF); + dis[s] = 0; + vector visited(n, false); + priority_queue, vector>, greater<>> pq; + pq.push({0, s}); + while(!pq.empty()){ + int d = pq.top().first, u = pq.top().second; pq.pop(); + if(visited[u]) continue; + visited[u] = true; + + for(const pair& p: g[u]){ + int v = p.first, w = p.second; + if(!visited[v] && dis[v] > dis[u] + w){ + dis[v] = dis[u] + w; + pq.push({dis[v], v}); + } + } + } + + int res = INF; + for(int u: marked) res = min(res, dis[u]); + return res == INF ? -1 : res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ad799b84..2dab2c25 100644 --- a/readme.md +++ b/readme.md @@ -2630,6 +2630,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2727 | JavaScript Problem | - | - | - | - | | 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/description/) | [无] | [C++](2001-2500/2728-Count-Houses-in-a-Circular-Street/cpp-2728/) | | | | | | | | | | +| 2737 | [Find the Closest Marked Node](https://leetcode.com/problems/find-the-closest-marked-node/) | [无] | [C++](2001-2500/2737-Find-the-Closest-Marked-Node/cpp-2737/) | | | +| | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 6ae267fab9e2181708aa221e6c6df22f15ccde32 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 18 Jun 2023 17:18:10 -0700 Subject: [PATCH 370/390] 2739-2742 solved. --- .../cpp-2739/CMakeLists.txt | 6 +++ .../cpp-2739/main.cpp | 32 +++++++++++++ .../cpp-2740/CMakeLists.txt | 6 +++ .../cpp-2740/main.cpp | 31 ++++++++++++ .../cpp-2741/CMakeLists.txt | 6 +++ .../cpp-2741/main.cpp | 48 +++++++++++++++++++ .../cpp-2742/CMakeLists.txt | 6 +++ .../2742-Painting-the-Walls/cpp-2742/main.cpp | 48 +++++++++++++++++++ readme.md | 5 ++ 9 files changed, 188 insertions(+) create mode 100644 2501-3000/2739-Total-Distance-Traveled/cpp-2739/CMakeLists.txt create mode 100644 2501-3000/2739-Total-Distance-Traveled/cpp-2739/main.cpp create mode 100644 2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/CMakeLists.txt create mode 100644 2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/main.cpp create mode 100644 2501-3000/2741-Special-Permutations/cpp-2741/CMakeLists.txt create mode 100644 2501-3000/2741-Special-Permutations/cpp-2741/main.cpp create mode 100644 2501-3000/2742-Painting-the-Walls/cpp-2742/CMakeLists.txt create mode 100644 2501-3000/2742-Painting-the-Walls/cpp-2742/main.cpp diff --git a/2501-3000/2739-Total-Distance-Traveled/cpp-2739/CMakeLists.txt b/2501-3000/2739-Total-Distance-Traveled/cpp-2739/CMakeLists.txt new file mode 100644 index 00000000..1813dd45 --- /dev/null +++ b/2501-3000/2739-Total-Distance-Traveled/cpp-2739/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2739) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2739 main.cpp) diff --git a/2501-3000/2739-Total-Distance-Traveled/cpp-2739/main.cpp b/2501-3000/2739-Total-Distance-Traveled/cpp-2739/main.cpp new file mode 100644 index 00000000..cd73cef2 --- /dev/null +++ b/2501-3000/2739-Total-Distance-Traveled/cpp-2739/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/total-distance-traveled/description/ +/// Author : liuyubobobo +/// Time : 2023-06-18 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(mainTank) +/// Space Complexity: O(1) +class Solution { +public: + int distanceTraveled(int mainTank, int additionalTank) { + + int res = 0; + while(mainTank >= 5 && additionalTank) { + res += 50; + + mainTank = mainTank - 5 + 1; + additionalTank --; + } + return res + mainTank * 10; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/CMakeLists.txt b/2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/CMakeLists.txt new file mode 100644 index 00000000..a4f995d0 --- /dev/null +++ b/2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2740) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2740 main.cpp) diff --git a/2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/main.cpp b/2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/main.cpp new file mode 100644 index 00000000..a7b5bf5e --- /dev/null +++ b/2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/find-the-value-of-the-partition/description/ +/// Author : liuyubobobo +/// Time : 2023-06-18 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int findValueOfPartition(vector& nums) { + + sort(nums.begin(), nums.end()); + + int res = INT_MAX; + for(int i = 1; i < nums.size(); i ++) + res = min(res, nums[i] - nums[i - 1]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2741-Special-Permutations/cpp-2741/CMakeLists.txt b/2501-3000/2741-Special-Permutations/cpp-2741/CMakeLists.txt new file mode 100644 index 00000000..95adda90 --- /dev/null +++ b/2501-3000/2741-Special-Permutations/cpp-2741/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2741) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2741 main.cpp) diff --git a/2501-3000/2741-Special-Permutations/cpp-2741/main.cpp b/2501-3000/2741-Special-Permutations/cpp-2741/main.cpp new file mode 100644 index 00000000..bdbb5099 --- /dev/null +++ b/2501-3000/2741-Special-Permutations/cpp-2741/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/special-permutations/description/ +/// Author : liuyubobobo +/// Time : 2023-06-18 + +#include +#include + +using namespace std; + + +/// Bitmask + Memoization +/// Time Complexity: O(n^2 * 2^n) +/// Space Complexity: O(n * 2^n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int specialPerm(vector& nums) { + + int n = nums.size(); + vector> dp(n + 1, vector(1 << n, -1)); + return dfs(nums, 0, (1 << n) - 1, dp); + } + +private: + long long dfs(const vector& nums, int last_index, int state, vector>& dp) { + + if(state == 0) return 1; + if(dp[last_index][state] != -1) return dp[last_index][state]; + + long long res = 0; + for(int i = 0; i < nums.size(); i ++) { + if((state & (1 << i)) == 0) continue; + if(last_index == 0) res += dfs(nums, i + 1, state ^ (1 << i), dp); + else if(nums[i] % nums[last_index - 1] == 0 || nums[last_index - 1] % nums[i] == 0) + res += dfs(nums, i + 1, state ^ (1 << i), dp); + } + return dp[last_index][state] = res % MOD; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2742-Painting-the-Walls/cpp-2742/CMakeLists.txt b/2501-3000/2742-Painting-the-Walls/cpp-2742/CMakeLists.txt new file mode 100644 index 00000000..e4c7628e --- /dev/null +++ b/2501-3000/2742-Painting-the-Walls/cpp-2742/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2742) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2742 main.cpp) diff --git a/2501-3000/2742-Painting-the-Walls/cpp-2742/main.cpp b/2501-3000/2742-Painting-the-Walls/cpp-2742/main.cpp new file mode 100644 index 00000000..9cf0788b --- /dev/null +++ b/2501-3000/2742-Painting-the-Walls/cpp-2742/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/painting-the-walls/description/ +/// Author : liuyubobobo +/// Time : 2023-06-18 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n * (total_time + n)) +/// Space Complexity: O(n * (total_time + n)) +class Solution { + +private: + const int INF = INT_MAX / 2; + +public: + int paintWalls(vector& cost, vector& time) { + + int n = cost.size(); + int total_time = accumulate(time.begin(), time.end(), 0); + + vector> dp(n, vector(total_time + n + 1, -1)); + return dfs(n, cost, time, 0, 0, n, dp); + } + +private: + int dfs(int n, const vector& cost, const vector& time, + int index, int can_be_free_time, int offset, vector>& dp) { + + if(index == n) return can_be_free_time >= 0 ? 0 : INF; + if(dp[index][can_be_free_time + offset] != -1) return dp[index][can_be_free_time + offset]; + + int res = INF; + res = min(res, cost[index] + dfs(n, cost, time, index + 1, can_be_free_time + time[index], offset, dp)); + res = min(res, dfs(n, cost, time, index + 1, can_be_free_time - 1, offset, dp)); + return dp[index][can_be_free_time + offset] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 2dab2c25..d9f7d8e0 100644 --- a/readme.md +++ b/readme.md @@ -2631,6 +2631,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/description/) | [无] | [C++](2001-2500/2728-Count-Houses-in-a-Circular-Street/cpp-2728/) | | | | | | | | | | | 2737 | [Find the Closest Marked Node](https://leetcode.com/problems/find-the-closest-marked-node/) | [无] | [C++](2001-2500/2737-Find-the-Closest-Marked-Node/cpp-2737/) | | | +| 2738 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled/) | [无] | [C++](2001-2500/2739-Total-Distance-Traveled/cpp-2739/) | | | +| 2740 | [Find the Value of the Partition](https://leetcode.com/problems/find-the-value-of-the-partition/) | [无] | [C++](2001-2500/2740-Find-the-Value-of-the-Partition/cpp-2740/) | | | +| 2741 | [Special Permutations](https://leetcode.com/problems/special-permutations/) | [无] | [C++](2001-2500/2741-Special-Permutations/cpp-2741/) | | | +| 2742 | [Painting the Walls](https://leetcode.com/problems/painting-the-walls/) | [无] | [C++](2001-2500/2742-Painting-the-Walls/cpp-2742/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From ca3de8db18fa3ed23f44d98c275b42c198e8f218 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 19 Jun 2023 14:40:14 -0700 Subject: [PATCH 371/390] 2729-2732 solved. --- .../cpp-2729/CMakeLists.txt | 6 +++ .../cpp-2729/main.cpp | 33 +++++++++++++ .../cpp-2730/CMakeLists.txt | 6 +++ .../cpp-2730/main.cpp | 37 ++++++++++++++ .../cpp-2731/CMakeLists.txt | 6 +++ .../2731-Movement-of-Robots/cpp-2731/main.cpp | 49 +++++++++++++++++++ .../cpp-2732/CMakeLists.txt | 6 +++ .../cpp-2732/main.cpp | 44 +++++++++++++++++ readme.md | 4 ++ 9 files changed, 191 insertions(+) create mode 100644 2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/CMakeLists.txt create mode 100644 2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/main.cpp create mode 100644 2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/CMakeLists.txt create mode 100644 2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/main.cpp create mode 100644 2501-3000/2731-Movement-of-Robots/cpp-2731/CMakeLists.txt create mode 100644 2501-3000/2731-Movement-of-Robots/cpp-2731/main.cpp create mode 100644 2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/CMakeLists.txt create mode 100644 2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/main.cpp diff --git a/2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/CMakeLists.txt b/2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/CMakeLists.txt new file mode 100644 index 00000000..91d92404 --- /dev/null +++ b/2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2729) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2729 main.cpp) diff --git a/2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/main.cpp b/2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/main.cpp new file mode 100644 index 00000000..c8934730 --- /dev/null +++ b/2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/check-if-the-number-is-fascinating/description/ +/// Author : liuyubobobo +/// Time : 2023-06-13 + +#include +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + bool isFascinating(int n) { + + int a = n, b = 2 * n, c = 3 * n; + string s = to_string(a) + to_string(b) + to_string(c); + + vector f(10, 0); + for(char c: s) f[c - '0'] ++; + return count_if(f.begin() + 1, f.end(), [](int e){return e == 1;}) == 9; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/CMakeLists.txt b/2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/CMakeLists.txt new file mode 100644 index 00000000..02d93698 --- /dev/null +++ b/2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2730) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2730 main.cpp) diff --git a/2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/main.cpp b/2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/main.cpp new file mode 100644 index 00000000..8159a67c --- /dev/null +++ b/2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/description/ +/// Author : liuyubobobo +/// Time : 2023-06-13 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int longestSemiRepetitiveSubstring(string s) { + + int res = 1; + for(int i = 0; i + 1 < s.size(); i ++) + res = max(res, solve(s, i, i + 1)); + return res; + } + +private: + int solve(const string& s, int a, int b){ + int res = 2; + for(int i = a - 1; i >= 0 && s[i] != s[i + 1]; i --) res ++; + for(int i = b + 1; i < s.size() && s[i] != s[i - 1]; i ++) res ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2731-Movement-of-Robots/cpp-2731/CMakeLists.txt b/2501-3000/2731-Movement-of-Robots/cpp-2731/CMakeLists.txt new file mode 100644 index 00000000..153e6c90 --- /dev/null +++ b/2501-3000/2731-Movement-of-Robots/cpp-2731/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2731) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2731 main.cpp) diff --git a/2501-3000/2731-Movement-of-Robots/cpp-2731/main.cpp b/2501-3000/2731-Movement-of-Robots/cpp-2731/main.cpp new file mode 100644 index 00000000..500402e1 --- /dev/null +++ b/2501-3000/2731-Movement-of-Robots/cpp-2731/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/movement-of-robots/description/ +/// Author : liuyubobobo +/// Time : 2023-06-13 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int sumDistance(vector& nums, string s, int d) { + + int n = nums.size(); + for(int i = 0; i < n; i ++) + if(s[i] == 'R') nums[i] += d; else nums[i] -= d; + sort(nums.begin(), nums.end()); + + long long right_dis = 0, right_cnt = n - 1; + for(int i = 1; i < n; i ++) right_dis += 0ll + nums[i] - nums[0]; + + long long res = right_dis % MOD; + for(int i = 1; i < n; i ++){ + long long cur = 0ll + nums[i] - nums[i - 1]; + + right_dis = (right_dis - right_cnt * cur % MOD + MOD) % MOD; + right_cnt --; + + res = (res + right_dis) % MOD; + res %= MOD; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/CMakeLists.txt b/2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/CMakeLists.txt new file mode 100644 index 00000000..bb21c839 --- /dev/null +++ b/2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2732) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2732 main.cpp) diff --git a/2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/main.cpp b/2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/main.cpp new file mode 100644 index 00000000..ac1f2f08 --- /dev/null +++ b/2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/find-a-good-subset-of-the-matrix/description/ +/// Author : liuyubobobo +/// Time : 2023-06-19 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n * 2^b) +/// Space Complexity: O(2^b) +class Solution { +public: + vector goodSubsetofBinaryMatrix(vector>& grid) { + + int n = grid.size(), b = grid[0].size(); + + map pattern2index; + for(int i = 0; i < n; i ++){ + + int value = 0; + for(int j = 0; j < b; j ++) + if(grid[i][j]) value += (1 << j); + + if(value == 0) return {{i}}; + + auto iter = pattern2index.find(value); + if(iter != pattern2index.end()) return {iter->second, i}; + + for(int s = 0; s < (1 << b); s ++) + if((s & value) == 0) pattern2index[s] = i; + } + return {}; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d9f7d8e0..ed05d2af 100644 --- a/readme.md +++ b/readme.md @@ -2629,6 +2629,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2726 | JavaScript Problem | - | - | - | - | | 2727 | JavaScript Problem | - | - | - | - | | 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/description/) | [无] | [C++](2001-2500/2728-Count-Houses-in-a-Circular-Street/cpp-2728/) | | | +| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating/) | [无] | [C++](2001-2500/2729-Check-if-The-Number-is-Fascinating/cpp-2729/) | | | +| 2730 | [Find the Longest Semi-Repetitive Substring](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/) | [无] | [C++](2001-2500/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/) | | | +| 2731 | [Movement of Robots](https://leetcode.com/problems/movement-of-robots/) | [无] | [C++](2001-2500/2731-Movement-of-Robots/cpp-2731/) | | | +| 2732 | [Find a Good Subset of the Matrix](https://leetcode.com/problems/find-a-good-subset-of-the-matrix/) | [无] | [C++](2001-2500/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/) | | | | | | | | | | | 2737 | [Find the Closest Marked Node](https://leetcode.com/problems/find-the-closest-marked-node/) | [无] | [C++](2001-2500/2737-Find-the-Closest-Marked-Node/cpp-2737/) | | | | 2738 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | From 1d6d7a92512842f6949aa7f52c83ae0bca5708a4 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 19 Jun 2023 15:48:52 -0700 Subject: [PATCH 372/390] 2733-2736 solved. --- .../cpp-2733/CMakeLists.txt | 6 + .../cpp-2733/main.cpp | 32 +++++ .../cpp-2734/CMakeLists.txt | 6 + .../cpp-2734/main.cpp | 37 +++++ .../cpp-2735/CMakeLists.txt | 6 + .../cpp-2735/main.cpp | 54 +++++++ .../cpp-2736/CMakeLists.txt | 6 + .../cpp-2736/main.cpp | 135 ++++++++++++++++++ readme.md | 5 +- 9 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/CMakeLists.txt create mode 100644 2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/main.cpp create mode 100644 2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/CMakeLists.txt create mode 100644 2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/main.cpp create mode 100644 2501-3000/2735-Collecting-Chocolates/cpp-2735/CMakeLists.txt create mode 100644 2501-3000/2735-Collecting-Chocolates/cpp-2735/main.cpp create mode 100644 2501-3000/2736-Maximum-Sum-Queries/cpp-2736/CMakeLists.txt create mode 100644 2501-3000/2736-Maximum-Sum-Queries/cpp-2736/main.cpp diff --git a/2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/CMakeLists.txt b/2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/CMakeLists.txt new file mode 100644 index 00000000..9498c170 --- /dev/null +++ b/2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2733) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2733 main.cpp) diff --git a/2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/main.cpp b/2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/main.cpp new file mode 100644 index 00000000..be466ab5 --- /dev/null +++ b/2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/neither-minimum-nor-maximum/description/ +/// Author : liuyubobobo +/// Time : 2023-06-19 + +#include +#include +#include + +using namespace std; + + +/// Set +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int findNonMinOrMax(vector& nums) { + + set s(nums.begin(), nums.end()); + if(s.size() <= 2) return -1; + + auto iter = s.begin(); + iter ++; + return *iter; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/CMakeLists.txt b/2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/CMakeLists.txt new file mode 100644 index 00000000..0de9cf14 --- /dev/null +++ b/2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2734) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2734 main.cpp) diff --git a/2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/main.cpp b/2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/main.cpp new file mode 100644 index 00000000..6164a2cc --- /dev/null +++ b/2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/lexicographically-smallest-string-after-substring-operation/description/ +/// Author : liuyubobobo +/// Time : 2023-06-19 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string smallestString(string s) { + + int start = 0; + for(; start < s.size(); start ++) if(s[start] != 'a') break; + if(start == s.size()) { + s[s.size() - 1] = 'z'; + return s; + } + + int end; + for(end = start; end < s.size(); end ++) if(s[end] == 'a') break; + + for(int i = start; i < end; i ++) s[i] --; + return s; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2735-Collecting-Chocolates/cpp-2735/CMakeLists.txt b/2501-3000/2735-Collecting-Chocolates/cpp-2735/CMakeLists.txt new file mode 100644 index 00000000..38970f9d --- /dev/null +++ b/2501-3000/2735-Collecting-Chocolates/cpp-2735/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2735) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2735 main.cpp) diff --git a/2501-3000/2735-Collecting-Chocolates/cpp-2735/main.cpp b/2501-3000/2735-Collecting-Chocolates/cpp-2735/main.cpp new file mode 100644 index 00000000..68c7e02b --- /dev/null +++ b/2501-3000/2735-Collecting-Chocolates/cpp-2735/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/collecting-chocolates/description/ +/// Author : liuyubobobo +/// Time : 2023-06-19 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { +public: + long long minCost(vector& nums, int x) { + + int n = nums.size(); + + vector> min_table(n, vector(n, LONG_LONG_MAX)); + for(int start = 0; start < n; start ++){ + min_table[start][start] = nums[start]; + for(int i= start + 1; i < n; i ++) + min_table[start][i] = min(min_table[start][i - 1], 0ll + nums[i]); + } + + long long res = LONG_LONG_MAX; + for(int offset = 0; offset < n; offset ++){ + res = min(res, solve(n, x, offset, min_table)); + } + return res; + } + +private: + long long solve(int n, long long x, int offset, + const vector>& min_table){ + + long long res = x * offset; + for(int start = 0; start < n; start ++){ + int end = (start + offset) % n; + + if(start <= end) res += min_table[start][end]; + else res += min(min_table[start][n - 1], min_table[0][end]); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2736-Maximum-Sum-Queries/cpp-2736/CMakeLists.txt b/2501-3000/2736-Maximum-Sum-Queries/cpp-2736/CMakeLists.txt new file mode 100644 index 00000000..b364367f --- /dev/null +++ b/2501-3000/2736-Maximum-Sum-Queries/cpp-2736/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2736) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2736 main.cpp) diff --git a/2501-3000/2736-Maximum-Sum-Queries/cpp-2736/main.cpp b/2501-3000/2736-Maximum-Sum-Queries/cpp-2736/main.cpp new file mode 100644 index 00000000..33ea6f8f --- /dev/null +++ b/2501-3000/2736-Maximum-Sum-Queries/cpp-2736/main.cpp @@ -0,0 +1,135 @@ +/// Source : https://leetcode.com/problems/maximum-sum-queries/description/ +/// Author : liuyubobobo +/// Time : 2023-06-19 + +#include +#include +#include +#include + +using namespace std; + + +/// Segment Tree + Offline Queries +/// Time Complexity: O(nlogn + qlogn) +/// Space Complexity: O(n) +template +class SegmentTree{ + +private: + int n; + vector data, tree; + T (*combine)(T a, T b); + +public: + + SegmentTree(int n, T (*combine)(T a, T b)): n(n), data(n, -1), tree(4 * n, -1){ + this->combine = combine; + } + + void update(int index, T value){ + if(data[index] == value) return; + data[index] = value; + update(0, 0, n - 1, index, value); + } + + T query(int index){ + assert(0 <= index && index < n); + return data[index]; + } + + T query(int l, int r){ + assert(l <= r); + assert(0 <= l && l < n); + assert(0 <= r && r < n); + return query(0, 0, n - 1, l, r); + } + +private: + void buildSegTree(int treeID, int l, int r){ + + if(l == r){ + tree[treeID] = data[l]; + return; + } + + int mid = (l + r) / 2; + buildSegTree(treeID * 2 + 1, l, mid); + buildSegTree(treeID * 2 + 2, mid + 1, r); + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + void update(int treeID, int l, int r, int index, T value){ + + if(l == r){ + assert(l == index); + tree[treeID] = value; + return; + } + + int mid = (l + r) / 2; + if(index <= mid) update(treeID * 2 + 1, l, mid, index, value); + else update(treeID * 2 + 2, mid + 1, r, index, value); + + tree[treeID] = combine(tree[treeID * 2 + 1], tree[treeID * 2 + 2]); + return; + } + + T query(int treeID, int l, int r, int ql, int qr){ + + if(ql == l && qr == r) + return tree[treeID]; + + int mid = (l + r) / 2; + if(qr <= mid) return query(treeID * 2 + 1, l, mid, ql, qr); + else if(ql > mid) return query(treeID * 2 + 2, mid + 1, r, ql, qr); + + T resl = query(treeID * 2 + 1, l, mid, ql, mid); + T resr = query(treeID * 2 + 2, mid + 1, r, mid + 1, qr); + return combine(resl, resr); + } +}; + +class Solution { +public: + vector maximumSumQueries(vector& nums1, vector& nums2, vector>& queries) { + + vector> v; + for(int i = 0; i < nums1.size(); i ++) v.push_back({nums1[i], nums2[i]}); + sort(v.begin(), v.end(), greater<>()); + + for(int i = 0; i < queries.size(); i ++) + queries[i].push_back(i); + sort(queries.begin(), queries.end(), greater<>()); + + map value2index; + for(const pair& p: v) value2index[p.second] = 0; + for(const vector& p: queries) value2index[p[1]] = 0; + + int sz = 0; + for(auto iter = value2index.begin(); iter != value2index.end(); iter ++) + iter->second = sz ++; + + vector res(queries.size()); + SegmentTree segTree(sz, [](int a, int b){return max(a, b);}); + for(int i = 0, j = 0; i < queries.size(); i ++){ + while(j < v.size() && v[j].first >= queries[i][0]){ + int index = value2index[v[j].second]; + segTree.update(index, max(segTree.query(index), v[j].first + v[j].second)); + j ++; + } + + int index = value2index[queries[i][1]]; + int tres = segTree.query(index, sz - 1); + res[queries[i][2]] = tres; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ed05d2af..d1c5d1fb 100644 --- a/readme.md +++ b/readme.md @@ -2633,7 +2633,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2730 | [Find the Longest Semi-Repetitive Substring](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/) | [无] | [C++](2001-2500/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/) | | | | 2731 | [Movement of Robots](https://leetcode.com/problems/movement-of-robots/) | [无] | [C++](2001-2500/2731-Movement-of-Robots/cpp-2731/) | | | | 2732 | [Find a Good Subset of the Matrix](https://leetcode.com/problems/find-a-good-subset-of-the-matrix/) | [无] | [C++](2001-2500/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/) | | | -| | | | | | | +| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum/) | [无] | [C++](2001-2500/2733-Neither-Minimum-nor-Maximum/cpp-2733/) | | | +| 2734 | [Lexicographically Smallest String After Substring Operation](https://leetcode.com/problems/lexicographically-smallest-string-after-substring-operation/) | [无] | [C++](2001-2500/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/) | | | +| 2735 | [Collecting Chocolates](https://leetcode.com/problems/collecting-chocolates/) | [无] | [C++](2001-2500/2735-Collecting-Chocolates/cpp-2735/) | | | +| 2736 | [Maximum Sum Queries](https://leetcode.com/problems/maximum-sum-queries/) | [无] | [C++](2001-2500/2736-Maximum-Sum-Queries/cpp-2736/) | | | | 2737 | [Find the Closest Marked Node](https://leetcode.com/problems/find-the-closest-marked-node/) | [无] | [C++](2001-2500/2737-Find-the-Closest-Marked-Node/cpp-2737/) | | | | 2738 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled/) | [无] | [C++](2001-2500/2739-Total-Distance-Traveled/cpp-2739/) | | | From 6999ad933f086cd6c996cb75c95e93654ce169dc Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 22 Jun 2023 15:45:01 -0700 Subject: [PATCH 373/390] Cracking The Coding Interview 16 19 solved. --- .../16-19/cpp-16-19/CMakeLists.txt | 6 ++ .../16-19/cpp-16-19/main.cpp | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Cracking-The-Coding-Interview/16-19/cpp-16-19/CMakeLists.txt create mode 100644 Cracking-The-Coding-Interview/16-19/cpp-16-19/main.cpp diff --git a/Cracking-The-Coding-Interview/16-19/cpp-16-19/CMakeLists.txt b/Cracking-The-Coding-Interview/16-19/cpp-16-19/CMakeLists.txt new file mode 100644 index 00000000..8312a6ab --- /dev/null +++ b/Cracking-The-Coding-Interview/16-19/cpp-16-19/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_16_19) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_16_19 main.cpp) diff --git a/Cracking-The-Coding-Interview/16-19/cpp-16-19/main.cpp b/Cracking-The-Coding-Interview/16-19/cpp-16-19/main.cpp new file mode 100644 index 00000000..8d22e9c4 --- /dev/null +++ b/Cracking-The-Coding-Interview/16-19/cpp-16-19/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.cn/problems/pond-sizes-lcci/ +/// Author : liuyubobobo +/// Time : 2023-06-22 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { + +private: + int R, C; + const int dirs[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, + {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; + +public: + vector pondSizes(vector>& land) { + + R = land.size(); C = land[0].size(); + vector> visited(R, vector(C, false)); + vector res; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(visited[i][j] || land[i][j]) continue; + res.push_back(dfs(land, i, j, visited)); + } + sort(res.begin(), res.end()); + return res; + } + +private: + int dfs(const vector>& land, int cx, int cy, vector>& visited){ + + visited[cx][cy] = true; + int res = 1; + for(int d = 0; d < 8; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && !visited[nx][ny] && !land[nx][ny]) + res += dfs(land, nx, ny, visited); + } + return res; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} From 439a6881ec03d3baa9dc625e0e82b40acd5ff48f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 22 Jun 2023 15:52:44 -0700 Subject: [PATCH 374/390] 2743 solved. --- .../cpp-2743/CMakeLists.txt | 6 ++++ .../cpp-2743/main.cpp | 36 +++++++++++++++++++ readme.md | 1 + 3 files changed, 43 insertions(+) create mode 100644 2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/CMakeLists.txt create mode 100644 2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/main.cpp diff --git a/2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/CMakeLists.txt b/2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/CMakeLists.txt new file mode 100644 index 00000000..31ac4a90 --- /dev/null +++ b/2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2743) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2743 main.cpp) diff --git a/2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/main.cpp b/2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/main.cpp new file mode 100644 index 00000000..3ba7521d --- /dev/null +++ b/2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/count-substrings-without-repeating-character/description/ +/// Author : liuyubobobo +/// Time : 2023-06-22 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int numberOfSpecialSubstrings(string s) { + + vector f(26, 0); + int l = 0, r = -1, res = 0; + while(l < s.size()){ + if(r + 1 < s.size() && f[s[r + 1] - 'a'] == 0){ + f[s[++ r] - 'a'] ++; + res += r - l + 1; + } + else{ + f[s[l ++] - 'a'] --; + } + } + return res; + } +}; + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d1c5d1fb..9fa79d02 100644 --- a/readme.md +++ b/readme.md @@ -2643,6 +2643,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2740 | [Find the Value of the Partition](https://leetcode.com/problems/find-the-value-of-the-partition/) | [无] | [C++](2001-2500/2740-Find-the-Value-of-the-Partition/cpp-2740/) | | | | 2741 | [Special Permutations](https://leetcode.com/problems/special-permutations/) | [无] | [C++](2001-2500/2741-Special-Permutations/cpp-2741/) | | | | 2742 | [Painting the Walls](https://leetcode.com/problems/painting-the-walls/) | [无] | [C++](2001-2500/2742-Painting-the-Walls/cpp-2742/) | | | +| 2743 | [Count Substrings Without Repeating Character](https://leetcode.com/problems/count-substrings-without-repeating-character/description/) | [无] | [C++](2001-2500/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 6e6aa9d861834954fcd66db85d7b51780c54c65b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 25 Jun 2023 09:58:36 -0700 Subject: [PATCH 375/390] 1575 solved. --- .../cpp-1575/CMakeLists.txt | 6 +++ .../cpp-1575/main.cpp | 48 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 1501-2000/1575-Count-All-Possible-Routes/cpp-1575/CMakeLists.txt create mode 100644 1501-2000/1575-Count-All-Possible-Routes/cpp-1575/main.cpp diff --git a/1501-2000/1575-Count-All-Possible-Routes/cpp-1575/CMakeLists.txt b/1501-2000/1575-Count-All-Possible-Routes/cpp-1575/CMakeLists.txt new file mode 100644 index 00000000..53ab7b11 --- /dev/null +++ b/1501-2000/1575-Count-All-Possible-Routes/cpp-1575/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1575) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1575 main.cpp) diff --git a/1501-2000/1575-Count-All-Possible-Routes/cpp-1575/main.cpp b/1501-2000/1575-Count-All-Possible-Routes/cpp-1575/main.cpp new file mode 100644 index 00000000..ddede98a --- /dev/null +++ b/1501-2000/1575-Count-All-Possible-Routes/cpp-1575/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/count-all-possible-routes/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n^2 * fuel) +/// Space Complexity: O(n * fuel) +class Solution { + +private: + long long MOD = 1e9 + 7; + +public: + int countRoutes(vector& locations, int start, int finish, int fuel) { + + int n = locations.size(); + vector> dp(n, vector(fuel + 1, -1)); + return dfs(n, locations, finish, start, fuel, dp); + } + +private: + long long dfs(int n, const vector& locations, int finish, int cur, int fuel, + vector>& dp){ + + if(fuel < 0) return 0; + if(dp[cur][fuel] != -1) return dp[cur][fuel]; + + long long res = 0; + if(cur == finish) res = 1; + for(int i = 0; i < n; i ++){ + if(i == cur) continue; + res += dfs(n, locations, finish, i, fuel - abs(locations[i] - locations[cur]), dp); + } + return dp[cur][fuel] = res % MOD; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 9fa79d02..96ad1462 100644 --- a/readme.md +++ b/readme.md @@ -1475,7 +1475,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1572 | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) | [无] | [C++](1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/) | | | | | | | | | | | 1574 | [Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/description/) | [无] | [C++](1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/) | | | -| | | | | | | +| 1575 | [Count All Possible Routes](https://leetcode.com/problems/count-all-possible-routes/description/) | [无] | [C++](1501-2000/1575-Count-All-Possible-Routes/cpp-1575/) | | | | 1576 | [Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [无] | [C++](1501-2000/1576-Replace-All-Question-Marks-to-Avoid-Consecutive-Repeating-Characters/cpp-1576/) | | | | 1577 | [Number of Ways Where Square of Number Is Equal to Product of Two Numbers](https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/) | [无] | [C++](1501-2000/1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp-1577/) | | | | 1578 | [Minimum Deletion Cost to Avoid Repeating Letters](https://leetcode.com/problems/minimum-deletion-cost-to-avoid-repeating-letters/) | [无] | [C++](1501-2000/1578-Minimum-Deletion-Cost-to-Avoid-Repeating-Letters/cpp-1578/) | | | From 6852f2d5cc8ed0d68d1d685d6f7fe8467e03c767 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 25 Jun 2023 20:37:24 -0700 Subject: [PATCH 376/390] 2744-2747 solved. --- .../cpp-2744/CMakeLists.txt | 6 ++ .../cpp-2744/main.cpp | 34 +++++++++ .../cpp-2745/CMakeLists.txt | 6 ++ .../cpp-2745/main.cpp | 38 ++++++++++ .../cpp-2746/CMakeLists.txt | 6 ++ .../cpp-2746/main.cpp | 51 ++++++++++++++ .../cpp-2747/CMakeLists.txt | 6 ++ .../cpp-2747/main.cpp | 70 +++++++++++++++++++ readme.md | 4 ++ 9 files changed, 221 insertions(+) create mode 100644 2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/CMakeLists.txt create mode 100644 2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/main.cpp create mode 100644 2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/CMakeLists.txt create mode 100644 2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/main.cpp create mode 100644 2501-3000/2746-Decremental-String-Concatenation/cpp-2746/CMakeLists.txt create mode 100644 2501-3000/2746-Decremental-String-Concatenation/cpp-2746/main.cpp create mode 100644 2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/CMakeLists.txt create mode 100644 2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/main.cpp diff --git a/2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/CMakeLists.txt b/2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/CMakeLists.txt new file mode 100644 index 00000000..d021c502 --- /dev/null +++ b/2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2744) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2744 main.cpp) diff --git a/2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/main.cpp b/2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/main.cpp new file mode 100644 index 00000000..78dfcadb --- /dev/null +++ b/2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/find-maximum-number-of-string-pairs/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include +#include + +using namespace std; + + +/// Using Hash Table +/// Time Complexity: O(n + 26 * 26) +/// Space Complexity: O(26 * 26) +class Solution { +public: + int maximumNumberOfStringPairs(vector& words) { + + vector> f(26, vector(26, 0)); + for(const string& word: words) { + f[word[0] - 'a'][word[1] - 'a'] ++; + } + + int res = 0; + for(int i = 0; i < 26; i ++) + for(int j = i + 1; j < 26; j ++) res += min(f[i][j], f[j][i]); + for(int i = 0; i < 26; i ++) res += f[i][i] / 2; + return res; + } +}; + +int main() { + + return 0; +} diff --git a/2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/CMakeLists.txt b/2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/CMakeLists.txt new file mode 100644 index 00000000..baab7f00 --- /dev/null +++ b/2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2745) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2745 main.cpp) diff --git a/2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/main.cpp b/2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/main.cpp new file mode 100644 index 00000000..84c8d84f --- /dev/null +++ b/2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/construct-the-longest-new-string/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(x * y * z) +/// Space Complexity: O(x * y * z) +class Solution { +public: + int longestString(int x, int y, int z) { + + vector>>> dp(3, vector>>(x + 1, vector>(y + 1, vector(z + 1, -1)))); + return dfs(0, x, y, z, dp); + } + +private: + int dfs(int pre, int x, int y, int z, vector>>>& dp){ + + if(dp[pre][x][y][z] != -1) return dp[pre][x][y][z]; + int res = 0; + if(x && pre != 1) res = max(res, 2 + dfs(1, x - 1, y, z, dp)); + if(y && pre != 2) res = max(res, 2 + dfs(2, x, y - 1, z, dp)); + if(z && pre != 1) res = max(res, 2 + dfs(2, x, y, z - 1, dp)); + return dp[pre][x][y][z] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2746-Decremental-String-Concatenation/cpp-2746/CMakeLists.txt b/2501-3000/2746-Decremental-String-Concatenation/cpp-2746/CMakeLists.txt new file mode 100644 index 00000000..80edf606 --- /dev/null +++ b/2501-3000/2746-Decremental-String-Concatenation/cpp-2746/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2746) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2746 main.cpp) diff --git a/2501-3000/2746-Decremental-String-Concatenation/cpp-2746/main.cpp b/2501-3000/2746-Decremental-String-Concatenation/cpp-2746/main.cpp new file mode 100644 index 00000000..8169cc90 --- /dev/null +++ b/2501-3000/2746-Decremental-String-Concatenation/cpp-2746/main.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/decremental-string-concatenation/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n * 26^2) +/// Space Complexity: O(n * 26^2) +class Solution { +public: + int minimizeConcatenatedLength(vector& words) { + + int n = words.size(); + + vector>> dp(n, vector>(26, vector(26, -1))); + return (int)words[0].size() + dfs(words, 1, words[0][0] - 'a', words[0].back() - 'a', dp); + } + +private: + int dfs(const vector& words, int index, int front, int tail, + vector>>& dp){ + + if(index == words.size()) return 0; + if(dp[index][front][tail] != -1) return dp[index][front][tail]; + + int res = INT_MAX; + if(words[index].back() - 'a' == front) + res = min(res, (int)words[index].size() - 1 + dfs(words, index + 1, words[index][0] - 'a', tail, dp)); + else + res = min(res, (int)words[index].size() + dfs(words, index + 1, words[index][0] - 'a', tail, dp)); + + if(words[index][0] - 'a' == tail) + res = min(res, (int)words[index].size() - 1 + dfs(words, index + 1, front, words[index].back() - 'a', dp)); + else + res = min(res, (int)words[index].size() + dfs(words, index + 1, front, words[index].back() - 'a', dp)); + + return dp[index][front][tail] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/CMakeLists.txt b/2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/CMakeLists.txt new file mode 100644 index 00000000..7affa925 --- /dev/null +++ b/2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2747) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2747 main.cpp) diff --git a/2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/main.cpp b/2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/main.cpp new file mode 100644 index 00000000..9da23b17 --- /dev/null +++ b/2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/main.cpp @@ -0,0 +1,70 @@ +/// Source : https://leetcode.com/problems/count-zero-request-servers/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include +#include +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(nlogn + qlogq) +/// Space Complexity: O(n) +class Solution { +public: + vector countServers(int n, vector>& logs, int x, vector& queries) { + + sort(logs.begin(), logs.end(), + [](const vector& a, const vector& b) { + return a[1] < b[1]; + }); + + vector> queries_pair; + for(int i = 0; i < queries.size(); i ++) queries_pair.push_back({queries[i], i}); + sort(queries_pair.begin(), queries_pair.end()); + + map f; + int l = 0, r = -1, index = 0; + vector res(queries.size(), n); + while(l < logs.size() && index < queries_pair.size()){ + if(r + 1 < logs.size() && queries_pair[index].first - x <= logs[r + 1][1] && logs[r + 1][1] <= queries_pair[index].first){ + r ++, f[logs[r][0]] ++; + } + else{ + if(queries_pair[index].first - x <= logs[l][1] && (l == 0 || queries_pair[index].first - x > logs[l - 1][1])){ + res[queries_pair[index].second] -= f.size(); + index ++; + continue; + } + + if(l <= r){ + f[logs[l][0]] --; + if(f[logs[l][0]] == 0) f.erase(logs[l][0]); + } + + l ++; + r = max(r, l - 1); + } + } + + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector> logs1 = {{2,30},{2,5},{3,9},{4,21}}; + vector queries1 = {11, 28, 16, 18}; + print_vec(Solution().countServers(4, logs1, 9, queries1)); + // 2 3 3 3 + + return 0; +} diff --git a/readme.md b/readme.md index 96ad1462..43903220 100644 --- a/readme.md +++ b/readme.md @@ -2644,6 +2644,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2741 | [Special Permutations](https://leetcode.com/problems/special-permutations/) | [无] | [C++](2001-2500/2741-Special-Permutations/cpp-2741/) | | | | 2742 | [Painting the Walls](https://leetcode.com/problems/painting-the-walls/) | [无] | [C++](2001-2500/2742-Painting-the-Walls/cpp-2742/) | | | | 2743 | [Count Substrings Without Repeating Character](https://leetcode.com/problems/count-substrings-without-repeating-character/description/) | [无] | [C++](2001-2500/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/) | | | +| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs/) | [无] | [C++](2001-2500/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/) | | | +| 2745 | [Construct the Longest New String](https://leetcode.com/problems/construct-the-longest-new-string/) | [无] | [C++](2001-2500/2745-Construct-the-Longest-New-String/cpp-2745/) | | | +| 2746 | [Decremental String Concatenation](https://leetcode.com/problems/decremental-string-concatenation/) | [无] | [C++](2001-2500/2746-Decremental-String-Concatenation/cpp-2746/) | | | +| 2747 | [Count Zero Request Servers](https://leetcode.com/problems/count-zero-request-servers/) | [无] | [C++](2001-2500/2747-Count-Zero-Request-Servers/cpp-2747/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From ce245019a7c0a7a1996860491c47ba0c7ac18f4d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 26 Jun 2023 14:53:14 -0700 Subject: [PATCH 377/390] 2748-2751 solved. --- .../cpp-2748/CMakeLists.txt | 6 + .../cpp-2748/main.cpp | 45 ++++++++ .../cpp-2749/CMakeLists.txt | 6 + .../cpp-2749/main.cpp | 30 +++++ .../cpp-2750/CMakeLists.txt | 6 + .../cpp-2750/main.cpp | 40 +++++++ .../cpp-2751/CMakeLists.txt | 6 + .../2751-Robot-Collisions/cpp-2751/main.cpp | 106 ++++++++++++++++++ readme.md | 4 + 9 files changed, 249 insertions(+) create mode 100644 2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/CMakeLists.txt create mode 100644 2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/main.cpp create mode 100644 2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/CMakeLists.txt create mode 100644 2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/main.cpp create mode 100644 2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/CMakeLists.txt create mode 100644 2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/main.cpp create mode 100644 2501-3000/2751-Robot-Collisions/cpp-2751/CMakeLists.txt create mode 100644 2501-3000/2751-Robot-Collisions/cpp-2751/main.cpp diff --git a/2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/CMakeLists.txt b/2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/CMakeLists.txt new file mode 100644 index 00000000..e5f23f16 --- /dev/null +++ b/2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2748) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2748 main.cpp) diff --git a/2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/main.cpp b/2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/main.cpp new file mode 100644 index 00000000..adae7380 --- /dev/null +++ b/2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/number-of-beautiful-pairs/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int countBeautifulPairs(vector& nums) { + + int n = nums.size(), res = 0; + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++){ + int a = to_string(nums[i])[0] - '0'; + int b = to_string(nums[j]).back() - '0'; + res += gcd(a, b) == 1; + } + return res; + } + +private: + template + T gcd(T a, T b){ + if(a < b) swap(a, b); + while(b){ + int t = a % b; + a = b; + b = t; + } + return a; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/CMakeLists.txt b/2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/CMakeLists.txt new file mode 100644 index 00000000..18f3da21 --- /dev/null +++ b/2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2749) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2749 main.cpp) diff --git a/2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/main.cpp b/2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/main.cpp new file mode 100644 index 00000000..2317dc4e --- /dev/null +++ b/2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + int makeTheIntegerZero(long long num1, long long num2) { + + for(int k = 1; k <= 60; k ++){ + long long x = num1 - k * num2; + if(x <= 0) break; + if(__builtin_popcountll(x) <= k && x >= k) return k; + } + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/CMakeLists.txt b/2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/CMakeLists.txt new file mode 100644 index 00000000..7ba102a7 --- /dev/null +++ b/2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2750) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2750 main.cpp) diff --git a/2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/main.cpp b/2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/main.cpp new file mode 100644 index 00000000..b2c8c41c --- /dev/null +++ b/2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/ways-to-split-array-into-good-subarrays/description/ +/// Author : liuyubobobo +/// Time : 2023-06-25 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +private: + long long MOD = 1e9 + 7; + +public: + int numberOfGoodSubarraySplits(vector& nums) { + + vector pos; + for(int i = 0; i < nums.size(); i ++) if(nums[i] == 1) pos.push_back(i); + + if(pos.empty()) return 0; + + long long res = 1; + for(int i = 0; i + 1 < pos.size(); i ++){ + long long t = pos[i + 1] - pos[i]; + res = res * t % MOD; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2751-Robot-Collisions/cpp-2751/CMakeLists.txt b/2501-3000/2751-Robot-Collisions/cpp-2751/CMakeLists.txt new file mode 100644 index 00000000..d0f532c7 --- /dev/null +++ b/2501-3000/2751-Robot-Collisions/cpp-2751/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2751) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2751 main.cpp) diff --git a/2501-3000/2751-Robot-Collisions/cpp-2751/main.cpp b/2501-3000/2751-Robot-Collisions/cpp-2751/main.cpp new file mode 100644 index 00000000..b23e89b4 --- /dev/null +++ b/2501-3000/2751-Robot-Collisions/cpp-2751/main.cpp @@ -0,0 +1,106 @@ +/// Source : https://leetcode.com/problems/robot-collisions/description/ +/// Author : liuyubobobo +/// Time : 2023-06-26 + +#include +#include +#include + +using namespace std; + + +/// Stack +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { + + int n = positions.size(); + + vector> robots; // pos, health, dir, index + for(int i = 0; i < n; i ++) robots.push_back({positions[i], healths[i], directions[i], i}); + + sort(robots.begin(), robots.end()); + + vector> res; // health, index + vector> stack; + for(int i = 0; i < n;) { + if(!stack.empty() && get<2>(stack.back()) == 'L'){ + res.push_back({get<1>(stack.back()), get<3>(stack.back())}); + stack.pop_back(); + } + else if(stack.empty() || get<2>(robots[i]) == get<2>(stack.back())){ + stack.push_back(robots[i]); i ++; + } + else{ + int& stack_health = get<1>(stack.back()); + int& cur_health = get<1>(robots[i]); + if(stack_health == cur_health){ + stack.pop_back(); i ++; + } + else if(stack_health < cur_health){ + stack.pop_back(); + cur_health --; + } + else{ + stack_health --; + i ++; + } + } + } + + while(!stack.empty()){ + res.push_back({get<1>(stack.back()), get<3>(stack.back())}); + stack.pop_back(); + } + + sort(res.begin(), res.end(), [](pair& a, pair& b){ + return a.second < b.second; + }); + + vector ret; + for(const auto& p: res) ret.push_back(p.first); + return ret; + } +}; + + +void print_vec(const vector& v){ + + if(v.empty()){ + cout << "empty\n"; + return; + } + + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector pos1 = {5, 4, 3, 2, 1}; + vector health1 = {2, 17, 9, 15, 10}; + string dir1 = "RRRRR"; + print_vec(Solution().survivedRobotsHealths(pos1, health1, dir1)); + // 2 17 9 15 10 + + vector pos2 = {3, 5, 2, 6}; + vector health2 = {10, 10, 15, 12}; + string dir2 = "RLRL"; + print_vec(Solution().survivedRobotsHealths(pos2, health2, dir2)); + // 14 + + vector pos3 = {1, 2, 5, 6}; + vector health3 = {10, 10, 11, 11}; + string dir3 = "RLRL"; + print_vec(Solution().survivedRobotsHealths(pos3, health3, dir3)); + // empty + + vector pos4 = {3, 40}; + vector health4 = {49, 11}; + string dir4 = "LL"; + print_vec(Solution().survivedRobotsHealths(pos4, health4, dir4)); + // 49 11 + + return 0; +} diff --git a/readme.md b/readme.md index 43903220..8e64a14e 100644 --- a/readme.md +++ b/readme.md @@ -2648,6 +2648,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2745 | [Construct the Longest New String](https://leetcode.com/problems/construct-the-longest-new-string/) | [无] | [C++](2001-2500/2745-Construct-the-Longest-New-String/cpp-2745/) | | | | 2746 | [Decremental String Concatenation](https://leetcode.com/problems/decremental-string-concatenation/) | [无] | [C++](2001-2500/2746-Decremental-String-Concatenation/cpp-2746/) | | | | 2747 | [Count Zero Request Servers](https://leetcode.com/problems/count-zero-request-servers/) | [无] | [C++](2001-2500/2747-Count-Zero-Request-Servers/cpp-2747/) | | | +| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs/) | [无] | [C++](2001-2500/2748-Number-of-Beautiful-Pairs/cpp-2748/) | | | +| 2749 | [Minimum Operations to Make the Integer Zero](https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero/) | [无] | [C++](2001-2500/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/) | | | +| 2750 | [Ways to Split Array Into Good Subarrays](https://leetcode.com/problems/ways-to-split-array-into-good-subarrays/) | [无] | [C++](2001-2500/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/) | | | +| 2751 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [无] | [C++](2001-2500/2751-Robot-Collisions/cpp-2751/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From e5adc6aa1fe7af036f8bcfeae7bc6da7f3b1b91b Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 29 Jun 2023 00:11:33 -0700 Subject: [PATCH 378/390] 1253 solved. --- .../cpp-1253/CMakeLists.txt | 6 ++ .../cpp-1253/main.cpp | 56 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/CMakeLists.txt create mode 100644 1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/main.cpp diff --git a/1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/CMakeLists.txt b/1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/CMakeLists.txt new file mode 100644 index 00000000..83e42998 --- /dev/null +++ b/1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1253) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1253 main.cpp) diff --git a/1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/main.cpp b/1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/main.cpp new file mode 100644 index 00000000..9511cf16 --- /dev/null +++ b/1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/description/ +/// Author : liuyubobobo +/// Time : 2023-06-29 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector> reconstructMatrix(int upper, int lower, vector& colsum) { + + int n = colsum.size(); + + vector> res(2, vector(n, 0)); + priority_queue> pq; + for(int i = 0; i < n; i ++) + if(colsum[i]) pq.push({colsum[i], i}); + + while(!pq.empty()){ + int sum = pq.top().first, index = pq.top().second; + pq.pop(); + + if(sum == 2){ + res[0][index] = res[1][index] = 1; + upper --, lower --; + if(upper < 0 || lower < 0) return vector>(0); + } + else{ + if(upper) + res[0][index] = 1, upper --; + else if(lower) + res[1][index] = 1, lower --; + else return vector>(0); + } + } + + if(upper || lower) return vector>(0); + return res; + } +}; + + +int main() { + + vector colsum1 = {2,1,2,0,1,0,1,2,0,1}; + Solution().reconstructMatrix(5, 5, colsum1); + + return 0; +} diff --git a/readme.md b/readme.md index 8e64a14e..22d6473e 100644 --- a/readme.md +++ b/readme.md @@ -1230,7 +1230,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1250 | [Check If It Is a Good Array](https://leetcode.com/problems/check-if-it-is-a-good-array/description/) | [无] | [C++](1001-1500/1250-Check-If-It-Is-a-Good-Array/cpp-1250/) | | | | 1251 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [无] | [C++](1001-1500/1252-Cells-with-Odd-Values-in-a-Matrix/cpp-1252/) | | | -| | | | | | | +| 1253 | [Reconstruct a 2-Row Binary Matrix](https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/description/) | [无] | [C++](1001-1500/1253-Reconstruct-a-2-Row-Binary-Matrix/cpp-1253/) | | | | 1254 | [Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands/description/) | [无] | [C++](1001-1500/1254-Number-of-Closed-Islands/cpp-1254/) | | | | 1255 | [Maximum Score Words Formed by Letters](https://leetcode.com/problems/maximum-score-words-formed-by-letters/description/) | [无] | [C++](1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/) | | | | | | | | | | From c4099755188a595d9d43777d1548639f94a4aa5d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 9 Jul 2023 16:47:22 -0700 Subject: [PATCH 379/390] 2272 solved. --- .../cpp-2272/CMakeLists.txt | 6 ++ .../cpp-2272/main.cpp | 73 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt create mode 100644 2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp diff --git a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt new file mode 100644 index 00000000..ff71f363 --- /dev/null +++ b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(cpp_2272) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2272 main.cpp) diff --git a/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp new file mode 100644 index 00000000..5f9ec06d --- /dev/null +++ b/2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/main.cpp @@ -0,0 +1,73 @@ +/// Source : https://leetcode.com/problems/substring-with-largest-variance/description/ +/// Author : liuyubobobo +/// Time : 2023-07-09 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n * 26 * 26) +/// Space Complexity: O(n) +class Solution { +public: + int largestVariance(string s) { + + int n = s.size(); + + int res = 0; + for(char max_c = 'a'; max_c <= 'z'; max_c ++) + for(char min_c = 'a'; min_c <= 'z'; min_c ++){ + if(max_c == min_c) continue; + res = max(res, solve(n, s, max_c, min_c)); + res = max(res, solve(n, s, max_c, min_c, true)); + } + return res; + } + +private: + int solve(int n, const string& s, char max_c, char min_c, bool reverse = false){ + + vector v(n); + if(!reverse){ + for(int i = 0; i < n; i ++) + v[i] = ((s[i] == max_c ? 1 : (s[i] == min_c ? -1 : 0))); + } + else{ + for(int i = n - 1; i >= 0; i --) + v[n - 1 - i] = (s[i] == max_c ? 1 : (s[i] == min_c ? -1 : 0)); + } + + int res = 0, cur = 0; + bool hit_neg = false; + for(int e: v){ + if(cur + e >= 0){ + cur += e; + if(e < 0) hit_neg = true; + if(hit_neg) res = max(res, cur); + } + else cur = 0, hit_neg = false; + } + return res; + } +}; + + +int main() { + + string s1 = "aababbb"; + cout << Solution().largestVariance(s1) << '\n'; + // 3 + + string s2 = "abcde"; + cout << Solution().largestVariance(s2) << '\n'; + // 0 + + string s3 = "lripaa"; + cout << Solution().largestVariance(s3) << '\n'; + // 1 + + return 0; +} diff --git a/readme.md b/readme.md index 22d6473e..dec93e01 100644 --- a/readme.md +++ b/readme.md @@ -2172,7 +2172,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2269 | [Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | [无] | [C++](2001-2500/2269-Find-the-K-Beauty-of-a-Number/cpp-2269/) | | | | 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [无] | [C++](2001-2500/2270-Number-of-Ways-to-Split-Array/cpp-2270/) | | | | 2271 | [Maximum White Tiles Covered by a Carpet](https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/) | [无] | [C++](2001-2500/2271-Maximum-White-Tiles-Covered-by-a-Carpet/cpp-2271/) | | | -| | | | | | | +| 2272 | | [Substring With Largest Variance](https://leetcode.com/problems/substring-with-largest-variance/description/) | [无] | [C++](2001-2500/2272-Substring-With-Largest-Variance/cpp-2272/) | | | 2273 | [Find Resultant Array After Removing Anagrams](https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/) | [无] | [C++](2001-2500/2273-Find-Resultant-Array-After-Removing-Anagrams/cpp-2273/) | | | | 2274 | [Maximum Consecutive Floors Without Special Floors](https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/) | [无] | [C++](2001-2500/2274-Maximum-Consecutive-Floors-Without-Special-Floors/cpp-2274/) | | | | 2275 | [Largest Combination With Bitwise AND Greater Than Zero](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [无] | [C++](2001-2500/2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero/cpp-2275/) | | | From 77d73f5bfc08f91768e9bd133cb58ac99e18cb97 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 13 Jul 2023 09:51:56 -0700 Subject: [PATCH 380/390] 0207 codes updated. --- .../0207-Course-Schedule/cpp-0207/main.cpp | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/0001-0500/0207-Course-Schedule/cpp-0207/main.cpp b/0001-0500/0207-Course-Schedule/cpp-0207/main.cpp index 23c1f130..057ab6ec 100644 --- a/0001-0500/0207-Course-Schedule/cpp-0207/main.cpp +++ b/0001-0500/0207-Course-Schedule/cpp-0207/main.cpp @@ -1,59 +1,47 @@ /// Source : https://leetcode.com/problems/course-schedule-ii/ /// Author : liuyubobobo /// Time : 2018-12-16 -/// Updated: 2021-05-03 +/// Updated: 2023-07-13 #include #include +#include using namespace std; -/// Check Directed Graph has circle +/// T /// Time Complexity: O(m + n) /// Space Complexity: O(m + n) class Solution { public: - bool canFinish(int numCourses, vector>& prerequisites) { + bool canFinish(int n, vector>& prerequisites) { - vector> g(numCourses); + vector> g(n); for(const vector& p: prerequisites){ int from = p[1]; int to = p[0]; g[from].push_back(to); } - return hasCircle(g); - } - -private: - bool hasCircle(const vector>& g){ - - vector visited(g.size(), false); - vector onPath(g.size(), false); - for(int i = 0; i < g.size(); i ++) - if(!visited[i]) - if(circleDFS(g, i, visited, onPath)) - return true; - return false; - } + vector indegrees(n, 0); + for(int u = 0; u < n; u ++) + for(int v: g[u]) indegrees[v] ++; - bool circleDFS(const vector>& g, int v, - vector& visited, vector& onPath){ + queue q; + int left = n; + for(int u = 0; u < n; u ++) + if(indegrees[u] == 0) q.push(u), left --; - visited[v] = true; - onPath[v] = true; - for(int next: g[v]) - if(!visited[next]){ - if(circleDFS(g, next, visited, onPath)) - return true; + while(!q.empty()){ + int u = q.front(); q.pop(); + for(int v: g[u]){ + indegrees[v] --; + if(indegrees[v] == 0) q.push(v), left --; } - else if(onPath[next]) - return true; - - onPath[v] = false; - return false; + } + return left == 0; } }; From 74f628c84bdbb86052105f6d7848a277d0e1fdd8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Fri, 14 Jul 2023 18:51:05 -0700 Subject: [PATCH 381/390] 0018 codes updated. --- 0001-0500/0018-4Sum/cpp-0018/main.cpp | 3 ++- 0001-0500/0018-4Sum/cpp-0018/main2.cpp | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/0001-0500/0018-4Sum/cpp-0018/main.cpp b/0001-0500/0018-4Sum/cpp-0018/main.cpp index 35bf33c8..450ed79e 100644 --- a/0001-0500/0018-4Sum/cpp-0018/main.cpp +++ b/0001-0500/0018-4Sum/cpp-0018/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/4sum/ /// Author : liuyubobobo /// Time : 2016-12-06 +/// Updated: 2023-07-14 #include #include @@ -31,7 +32,7 @@ class Solution { for(int i = 0 ; i <= n - 4 ; i = nextNumberIndex(nums, i)) for (int j = i + 1; j <= n - 3; j = nextNumberIndex(nums, j)) { - int t = target - nums[i] - nums[j]; + long long t = 0ll + target - nums[i] - nums[j]; if(nums[j+1] + nums[j+2] > t || nums[n-1] + nums[n-2] < t) continue; diff --git a/0001-0500/0018-4Sum/cpp-0018/main2.cpp b/0001-0500/0018-4Sum/cpp-0018/main2.cpp index db03c031..b509627c 100644 --- a/0001-0500/0018-4Sum/cpp-0018/main2.cpp +++ b/0001-0500/0018-4Sum/cpp-0018/main2.cpp @@ -1,6 +1,8 @@ /// Source : https://leetcode.com/problems/4sum/ /// Author : liuyubobobo /// Time : 2017-09-27 +/// Updated: 2023-07-14 + #include #include #include @@ -38,7 +40,7 @@ class Solution { for( int i = 0 ; i <= n - 4 ; i = nextNumberIndex(nums, i) ) for (int j = i + 1; j <= n - 3; j = nextNumberIndex(nums, j)) { - int t = target - nums[i] - nums[j]; + long long t = 0ll + target - nums[i] - nums[j]; if( nums[j+1] + nums[j+2] > t || nums[n-1] + nums[n-2] < t) continue; From fa73d327830465e86833317ebc925126bcfaf5dc Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 19 Jul 2023 14:36:58 -0700 Subject: [PATCH 382/390] 2760-2763 solved. --- .../cpp-2760/CMakeLists.txt | 6 ++ .../cpp-2760/main.cpp | 38 +++++++++++ .../cpp-2761/CMakeLists.txt | 6 ++ .../cpp-2761/main.cpp | 49 ++++++++++++++ .../cpp-2762/CMakeLists.txt | 6 ++ .../cpp-2762/main.cpp | 48 ++++++++++++++ .../cpp-2763/CMakeLists.txt | 6 ++ .../cpp-2763/main.cpp | 65 +++++++++++++++++++ readme.md | 12 ++++ 9 files changed, 236 insertions(+) create mode 100644 2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/CMakeLists.txt create mode 100644 2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/main.cpp create mode 100644 2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/CMakeLists.txt create mode 100644 2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/main.cpp create mode 100644 2501-3000/2762-Continuous-Subarrays/cpp-2762/CMakeLists.txt create mode 100644 2501-3000/2762-Continuous-Subarrays/cpp-2762/main.cpp create mode 100644 2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/CMakeLists.txt create mode 100644 2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/main.cpp diff --git a/2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/CMakeLists.txt b/2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/CMakeLists.txt new file mode 100644 index 00000000..09927f63 --- /dev/null +++ b/2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2760) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2760 main.cpp) diff --git a/2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/main.cpp b/2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/main.cpp new file mode 100644 index 00000000..1e036f39 --- /dev/null +++ b/2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/description/ +/// Author : liuyubobobo +/// Time : 2023-07-10 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^3) +/// Space Complexity: O(1) +class Solution { +public: + int longestAlternatingSubarray(vector& nums, int threshold) { + + int n = nums.size(), res = 0; + for(int l = 0; l < n; l ++){ + if(nums[l] % 2 || nums[l] > threshold) continue; + + bool ok = true; + for(int r = l; r < n && ok; r ++){ + for(int i = l + 1; i <= r && ok; i ++) + if(nums[i] % 2 == nums[i - 1] % 2 || nums[i] > threshold) + ok = false; + if(ok) res = max(res, r - l + 1); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/CMakeLists.txt b/2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/CMakeLists.txt new file mode 100644 index 00000000..68991160 --- /dev/null +++ b/2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2761) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2761 main.cpp) diff --git a/2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/main.cpp b/2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/main.cpp new file mode 100644 index 00000000..3566096d --- /dev/null +++ b/2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/prime-pairs-with-target-sum/description/ +/// Author : liuyubobobo +/// Time : 2023-07-10 + +#include +#include + +using namespace std; + + +/// Sieve +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector> findPrimePairs(int n) { + + vector sieve_table = sieve(n); + + vector> res; + for(int x = 2; x + x <= n; x ++) + if(sieve_table[x] == x && sieve_table[n - x] == n - x) + res.push_back({x, n - x}); + return res; + } + +private: + vector sieve(int n){ + + vector table(n + 1, 0); + vector primes; + table[0] = table[1] = 1; + for(int i = 2; i <= n; i ++){ + if(table[i] == 0) { + primes.push_back(i); + table[i] = i; + } + for(int j = 0; j < primes.size() && primes[j] <= table[i] && i * primes[j] <= n; j ++) + table[i * primes[j]] = primes[j]; + } + return table; + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2762-Continuous-Subarrays/cpp-2762/CMakeLists.txt b/2501-3000/2762-Continuous-Subarrays/cpp-2762/CMakeLists.txt new file mode 100644 index 00000000..920ababa --- /dev/null +++ b/2501-3000/2762-Continuous-Subarrays/cpp-2762/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2762) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2762 main.cpp) diff --git a/2501-3000/2762-Continuous-Subarrays/cpp-2762/main.cpp b/2501-3000/2762-Continuous-Subarrays/cpp-2762/main.cpp new file mode 100644 index 00000000..70e2d9d9 --- /dev/null +++ b/2501-3000/2762-Continuous-Subarrays/cpp-2762/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/continuous-subarrays/description/ +/// Author : liuyubobobo +/// Time : 2023-07-10 + +#include +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long continuousSubarrays(vector& nums) { + + int n = nums.size(); + int l = 0, r = -1; + map f; + long long res = 0; + while(l < n){ + if(r + 1 < n && (f.empty() || max_diff(nums[r + 1], f) <= 2)) + f[nums[++ r]] ++, res += (r - l + 1); + else{ + f[nums[l]] --; + if(f[nums[l]] == 0) + f.erase(nums[l]); + l ++; + } + } + return res; + } + +private: + int max_diff(int e, const map& f){ + int a = abs(e - f.begin()->first); + int b = abs(e - f.rbegin()->first); + return max(a, b); + } +}; + + +int main() { + + return 0; +} diff --git a/2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/CMakeLists.txt b/2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/CMakeLists.txt new file mode 100644 index 00000000..efbbff6d --- /dev/null +++ b/2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_2763) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2763 main.cpp) diff --git a/2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/main.cpp b/2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/main.cpp new file mode 100644 index 00000000..fcfbe8d9 --- /dev/null +++ b/2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/description/ +/// Author : liuyubobobo +/// Time : 2023-07-19 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n^2 * logn) +/// Space Complexity: O(n) +class Solution { +public: + int sumImbalanceNumbers(vector& nums) { + + + int n = nums.size(); + int res = 0; + for(int l = 0; l < n; l ++){ + map f; + int cnt = 0; + for(int r = l; r < n; r ++){ + f[nums[r]] ++; + if(r == l) continue; + + auto cur_iter = f.find(nums[r]); + if(cur_iter->second > 1){ + res += cnt; continue; + } + auto next_iter = cur_iter; next_iter ++; + + if(cur_iter == f.begin()){ + int next_value = next_iter->first; + if(next_value - nums[r] > 1) cnt ++; + } + else if(next_iter == f.end()){ + auto pre_iter = cur_iter; pre_iter --; + int pre_value = pre_iter->first; + if(nums[r] - pre_value > 1) cnt ++; + } + else{ + auto pre_iter = cur_iter; pre_iter --; + int pre_value = pre_iter->first, next_value = next_iter->first; + if(next_value - pre_value > 1) cnt --; + if(nums[r] - pre_value > 1) cnt ++; + if(next_value - nums[r] > 1) cnt ++; + } + res += cnt; + } + } + return res; + } +}; + +int main() { + + vector nums2 = {1, 3, 3, 3, 5}; + cout << Solution().sumImbalanceNumbers(nums2) << '\n'; + // 8 + + return 0; +} diff --git a/readme.md b/readme.md index dec93e01..9d95e176 100644 --- a/readme.md +++ b/readme.md @@ -2652,6 +2652,18 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2749 | [Minimum Operations to Make the Integer Zero](https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero/) | [无] | [C++](2001-2500/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/) | | | | 2750 | [Ways to Split Array Into Good Subarrays](https://leetcode.com/problems/ways-to-split-array-into-good-subarrays/) | [无] | [C++](2001-2500/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/) | | | | 2751 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [无] | [C++](2001-2500/2751-Robot-Collisions/cpp-2751/) | | | +| 2752 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| | | | | | | +| 2754 | JavaScript Problem | - | - | - | - | +| 2755 | JavaScript Problem | - | - | - | - | +| 2756 | JavaScript Problem | - | - | - | - | +| 2757 | JavaScript Problem | - | - | - | - | +| 2758 | JavaScript Problem | - | - | - | - | +| 2759 | JavaScript Problem | - | - | - | - | +| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/) | [无] | [C++](2001-2500/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/) | | | +| 2761 | [Prime Pairs With Target Sum](https://leetcode.com/problems/prime-pairs-with-target-sum/) | [无] | [C++](2001-2500/2761-Prime-Pairs-With-Target-Sum/cpp-2761/) | | | +| 2762 | [Continuous Subarrays](https://leetcode.com/problems/continuous-subarrays/) | [无] | [C++](2001-2500/2762-Continuous-Subarrays/cpp-2762/) | | | +| 2763 | [Sum of Imbalance Numbers of All Subarrays](https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/) | [无] | [C++](2001-2500/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/) From 1d3b6f9d96ff330d1f35c2a6b0a4c756af5a905f Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 1 Aug 2023 22:56:52 -0700 Subject: [PATCH 383/390] 0486 solved. --- .../cpp-0486/CMakeLists.txt | 6 ++++ .../0486-Predict-the-Winner/cpp-0486/main.cpp | 28 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0486-Predict-the-Winner/cpp-0486/CMakeLists.txt create mode 100644 0001-0500/0486-Predict-the-Winner/cpp-0486/main.cpp diff --git a/0001-0500/0486-Predict-the-Winner/cpp-0486/CMakeLists.txt b/0001-0500/0486-Predict-the-Winner/cpp-0486/CMakeLists.txt new file mode 100644 index 00000000..635bf6a5 --- /dev/null +++ b/0001-0500/0486-Predict-the-Winner/cpp-0486/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_0486) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0486 main.cpp) diff --git a/0001-0500/0486-Predict-the-Winner/cpp-0486/main.cpp b/0001-0500/0486-Predict-the-Winner/cpp-0486/main.cpp new file mode 100644 index 00000000..56a0a5d2 --- /dev/null +++ b/0001-0500/0486-Predict-the-Winner/cpp-0486/main.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +using namespace std; + + +class Solution { +public: + bool PredictTheWinner(vector& nums) { + + int n = nums.size(); + int p1 = 0, p2 = n - 1; + + vector> dp(n, vector(n, INT_MIN / 2)); + return dfs(nums, p1, p2, dp) >= 0; + } + +private: + int dfs(const vector& nums, int p1, int p2, vector>& dp) { + + if (p1 > p2) return 0; + if (dp[p1][p2] != INT_MIN / 2) return dp[p1][p2]; + int a = nums[p1] - dfs(nums, p1 + 1, p2, dp); + int b = nums[p2] - dfs(nums, p1, p2 - 1, dp); + return dp[p1][p2] = max(a, b); + } +}; diff --git a/readme.md b/readme.md index 9d95e176..ee824c35 100644 --- a/readme.md +++ b/readme.md @@ -519,7 +519,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 483 | [Smallest Good Base](https://leetcode.com/problems/smallest-good-base/) | [无] | [C++](0001-0500/0483-Smallest-Good-Base/cpp-0483/) | | | | 484 | [Find Permutation](https://leetcode.com/problems/find-permutation/) | [solution](https://leetcode.com/problems/find-permutation/solution/) | [C++](0001-0500/0484-Find-Permutation/cpp-0484/) | | | | 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/description/) | [无] | [C++](0001-0500/0485-Max-Consecutive-Ones/cpp-0485/) | | | -| | | | | | | +| 486 | [Predict the Winner](https://leetcode.com/problems/predict-the-winner/) | [无] | [C++](0001-0500/0486-Predict-the-Winner/cpp-0486/) | | | | 487 | [Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/) | [无] | [C++](0001-0500/0487-Max-Consecutive-Ones-II/cpp-0487/) | | | | 488 | [Zuma Game](https://leetcode.com/problems/zuma-game/) | [无] | [C++](0001-0500/0488-Zuma-Game/cpp-0488/) | | | | 489 | [Robot Room Cleaner](https://leetcode.com/problems/robot-room-cleaner/) | [solution](https://leetcode.com/problems/robot-room-cleaner/solution/) | [C++](0001-0500/0489-Robot-Room-Cleaner/cpp-0489/) | | | From 6fe5259f1f98159f7913f34fb4b1c59c101d366d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 1 Aug 2023 23:22:12 -0700 Subject: [PATCH 384/390] 0822 solved. --- .../cpp-0822/CMakeLists.txt | 6 +++ .../0822-Card-Flipping-Game/cpp-0822/main.cpp | 44 +++++++++++++++++++ readme.md | 1 + 3 files changed, 51 insertions(+) create mode 100644 0501-1000/0822-Card-Flipping-Game/cpp-0822/CMakeLists.txt create mode 100644 0501-1000/0822-Card-Flipping-Game/cpp-0822/main.cpp diff --git a/0501-1000/0822-Card-Flipping-Game/cpp-0822/CMakeLists.txt b/0501-1000/0822-Card-Flipping-Game/cpp-0822/CMakeLists.txt new file mode 100644 index 00000000..9df7dd2d --- /dev/null +++ b/0501-1000/0822-Card-Flipping-Game/cpp-0822/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.26) +project(cpp_0822) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0822 main.cpp) diff --git a/0501-1000/0822-Card-Flipping-Game/cpp-0822/main.cpp b/0501-1000/0822-Card-Flipping-Game/cpp-0822/main.cpp new file mode 100644 index 00000000..e0a44312 --- /dev/null +++ b/0501-1000/0822-Card-Flipping-Game/cpp-0822/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/card-flipping-game/description/ +/// Author : liuyubobobo +/// Time : 2023-08-01 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int flipgame(vector& fronts, vector& backs) { + + int n = fronts.size(), res = INT_MAX; + for(int i = 0; i < n; i ++){ + int x = fronts[i], y = backs[i]; + if(x == y) continue; + if(x < res && ok(x, n, fronts, backs, i)) res = min(res, x); + if(y < res && ok(y, n, fronts, backs, i)) res = min(res, y); + } + return res == INT_MAX ? 0 : res; + } + +private: + bool ok(int x, int n, const vector& fronts, const vector& backs, + int del_idx){ + + for(int i = 0; i < fronts.size(); i ++) + if(i != del_idx && fronts[i] == x && backs[i] == x) + return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index ee824c35..d9ef2286 100644 --- a/readme.md +++ b/readme.md @@ -826,6 +826,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/description/) | [solution](https://leetcode.com/problems/most-common-word/solution/) | [C++](0501-1000/0819-Most-Common-Word/cpp-0819/) | | | | | | | | | | +| 822 | [Card Flipping Game](https://leetcode.com/problems/card-flipping-game/description/) | [无] | [C++](0501-1000/0822-Card-Flipping-Game/cpp-0822/) | | | | 823 | [Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors/) | [solution](https://leetcode.com/problems/binary-trees-with-factors/solution/) | [C++](0501-1000/0823-Binary-Trees-With-Factors/cpp-0823/) | | | | 824 | [Goat Latin](https://leetcode.com/problems/goat-latin/) | [无] | [C++](0501-1000/0824-Goat-Latin/cpp-0824/) | | | | 825 | [Friends Of Appropriate Ages](https://leetcode.com/problems/friends-of-appropriate-ages/) | [无] | [C++](0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/) | | | From cf55187e3d9f8ee32583ff4f5ee21745e9d63ee8 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 8 Aug 2023 22:11:04 -0700 Subject: [PATCH 385/390] 1281 solved. --- .../cpp-1281/CMakeLists.txt | 6 +++ .../cpp-1281/main.cpp | 40 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/CMakeLists.txt create mode 100644 1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/main.cpp diff --git a/1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/CMakeLists.txt b/1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/CMakeLists.txt new file mode 100644 index 00000000..d0b72c2f --- /dev/null +++ b/1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1281) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1281 main.cpp) diff --git a/1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/main.cpp b/1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/main.cpp new file mode 100644 index 00000000..19514ed8 --- /dev/null +++ b/1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/description/ +/// Author : liuyubobobo +/// Time : 2023-08-08 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + int subtractProductAndSum(int n) { + return product(n) - sum(n); + } + +private: + int product(int x){ + int res = 1; + while(x) + res *= x % 10, x /= 10; + return res; + } + + int sum(int x){ + int res = 0; + while(x) + res += x % 10, x /= 10; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index d9ef2286..169a2c43 100644 --- a/readme.md +++ b/readme.md @@ -1250,7 +1250,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1278 | [Palindrome Partitioning III](https://leetcode.com/problems/palindrome-partitioning-iii/) | [无] | [C++](1001-1500/1278-Palindrome-Partitioning-III/cpp-1278/) | | | | | | | | | | | 1280 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| | | | | | | +| 1281 | [Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/) | [无] | [C++](1001-1500/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/cpp-1281/) | | | | 1282 | [Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/) | [无] | [C++](1001-1500/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/cpp-1282/) | | | | | | | | | | | 1286 | [Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/) | [solution](https://leetcode.com/problems/iterator-for-combination/solution/) | [C++](1001-1500/1286-Iterator-for-Combination/cpp-1286/) | | | From c27f16e5bbfc5ec62ac101dcdbdf6ebcdef1cefc Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Wed, 9 Aug 2023 22:15:13 -0700 Subject: [PATCH 386/390] 1289 solved. --- .../cpp-1289/CMakeLists.txt | 6 +++ .../cpp-1289/main.cpp | 43 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/CMakeLists.txt create mode 100644 1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/main.cpp diff --git a/1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/CMakeLists.txt b/1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/CMakeLists.txt new file mode 100644 index 00000000..601bd10a --- /dev/null +++ b/1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_1289) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_1289 main.cpp) diff --git a/1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/main.cpp b/1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/main.cpp new file mode 100644 index 00000000..fcba1da7 --- /dev/null +++ b/1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/minimum-falling-path-sum-ii/ +/// Author : liuyubobobo +/// Time : 2023-08-09 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { +public: + int minFallingPathSum(vector>& grid) { + + int n = grid.size(); + + vector> dp(n, vector(n + 1, -1)); + return dfs(n, grid, 0, n, dp); + } + +private: + int dfs(int n, const vector>& grid, int row, int last_c, vector>& dp) { + + if (row == n) return 0; + if (dp[row][last_c] != -1) return dp[row][last_c]; + + int res = INT_MAX; + for (int j = 0; j < n; j ++) { + if (j == last_c) continue; + res = min(res, grid[row][j] + dfs(n, grid, row + 1, j, dp)); + } + return dp[row][last_c] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 169a2c43..043ae72f 100644 --- a/readme.md +++ b/readme.md @@ -1256,7 +1256,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 1286 | [Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/) | [solution](https://leetcode.com/problems/iterator-for-combination/solution/) | [C++](1001-1500/1286-Iterator-for-Combination/cpp-1286/) | | | | | | | | | | | 1288 | [Remove Covered Intervals](https://leetcode.com/problems/remove-covered-intervals/) | [solution](https://leetcode.com/problems/remove-covered-intervals/solution/) | [C++](1001-1500/1288-Remove-Covered-Intervals/cpp-1288/) | | | -| | | | | | | +| 1289 | [Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/) | [无] | [C++](1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/) | | | | 1290 | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [solution](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/solution/) | [C++](1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/) | | | | 1291 | [Sequential Digits](https://leetcode.com/problems/sequential-digits/) | [solution](https://leetcode.com/problems/sequential-digits/solution/) | [C++](1001-1500/1291-Sequential-Digits/cpp-1291/) | | | | | | | | | | From 37e18d446c66beed637cb9c589d0bf5806389b91 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 15 Aug 2023 16:51:39 -0700 Subject: [PATCH 387/390] 0833 solved. --- .../cpp-0833/CMakeLists.txt | 6 +++ .../cpp-0833/main.cpp | 39 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 0501-1000/0833-Find-And-Replace-in-String/cpp-0833/CMakeLists.txt create mode 100644 0501-1000/0833-Find-And-Replace-in-String/cpp-0833/main.cpp diff --git a/0501-1000/0833-Find-And-Replace-in-String/cpp-0833/CMakeLists.txt b/0501-1000/0833-Find-And-Replace-in-String/cpp-0833/CMakeLists.txt new file mode 100644 index 00000000..331f9989 --- /dev/null +++ b/0501-1000/0833-Find-And-Replace-in-String/cpp-0833/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_0833) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0833 main.cpp) diff --git a/0501-1000/0833-Find-And-Replace-in-String/cpp-0833/main.cpp b/0501-1000/0833-Find-And-Replace-in-String/cpp-0833/main.cpp new file mode 100644 index 00000000..96bf96a6 --- /dev/null +++ b/0501-1000/0833-Find-And-Replace-in-String/cpp-0833/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/find-and-replace-in-string/ +/// Author : liuyubobobo +/// Time : 2023-08-14 + +#include +#include +#include +#include + +using namespace std; + + +class Solution { +public: + string findReplaceString(string s, vector& indices, vector& sources, vector& targets) { + + int n = indices.size(); + vector> v(n); + for(int i = 0; i < n; i ++) v[i] = make_tuple(indices[i], sources[i], targets[i]); + sort(v.begin(), v.end()); + + int offset = 0; + for(int i = 0; i < n; i ++){ + int index = get<0>(v[i]); + string a = get<1>(v[i]), b = get<2>(v[i]); + if(s.find(a, index + offset) == index + offset){ + s.replace(index + offset, a.size(), b); + offset += b.size() - a.size(); + } + } + return s; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 043ae72f..7d62437e 100644 --- a/readme.md +++ b/readme.md @@ -837,7 +837,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [solution](https://leetcode.com/problems/positions-of-large-groups/solution/) | [C++](0501-1000/0830-Positions-of-Large-Groups/cpp-0830/) | | | | 831 | [Masking Personal Information](https://leetcode.com/problems/masking-personal-information/) | [无] | [C++](0501-1000/0831-Masking-Personal-Information/cpp-0831/) | | | | 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [solution](https://leetcode.com/problems/flipping-an-image/solution/) | [C++](0501-1000/0832-Flipping-an-Image/cpp-0832/) | | | -| | | | | | | +| 833 | [Find And Replace in String](https://leetcode.com/problems/find-and-replace-in-string/) | [无] | [C++](https://leetcode.com/problems/find-and-replace-in-string/) | | | | 834 | [Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/) | [solution](https://leetcode.com/problems/sum-of-distances-in-tree/solution/) | [C++](0501-1000/0834-Sum-of-Distances-in-Tree/cpp-0834/) | | | | 835 | [Image Overlap](https://leetcode.com/problems/image-overlap/) | [无] | [C++](0501-1000/0835-Image-Overlap/cpp-0835/) | | | | | | | | | | From 9b243cd579d6e9d88d89e936fe8a7bef6914a519 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Mon, 21 Aug 2023 00:28:14 -0700 Subject: [PATCH 388/390] 0459 solved. --- .../cpp-0459/CMakeLists.txt | 6 +++ .../cpp-0459/main.cpp | 43 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/CMakeLists.txt create mode 100644 0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/main.cpp diff --git a/0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/CMakeLists.txt b/0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/CMakeLists.txt new file mode 100644 index 00000000..64f48c06 --- /dev/null +++ b/0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(cpp_0459) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_0459 main.cpp) diff --git a/0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/main.cpp b/0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/main.cpp new file mode 100644 index 00000000..565e9e81 --- /dev/null +++ b/0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/repeated-substring-pattern/description/ +/// Author : liuyubobobo +/// Time : 2023-08-21 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(sqrt(n) * n) +/// Space Complexity: O(n) +class Solution { +public: + bool repeatedSubstringPattern(string s) { + + int n = s.size(); + if(n == 1) return false; + for(int i = 1; i * i <= n; i ++){ + if(n % i) continue; + + if(ok(n, s, i)) return true; + + if(i * i == n || i == 1) continue; + if(ok(n, s, n / i)) return true; + } + return false; + } + +private: + bool ok(int n, const string& s, int len){ + string subs = s.substr(0, len); + for(int i = len; i <= n - len; i += len) + if(s.substr(i, len) != subs) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 7d62437e..1777f0cf 100644 --- a/readme.md +++ b/readme.md @@ -493,7 +493,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 456 | [132 Pattern](https://leetcode.com/problems/132-pattern/) | [solution](https://leetcode.com/problems/132-pattern/solution/)
[缺:O(n) 单调栈算法] | [C++](0001-0500/0456-132-Pattern/cpp-0456/) | | | | 457 | [Circular Array Loop](https://leetcode.com/problems/circular-array-loop/) | [无] | [C++](0001-0500/0457-Circular-Array-Loop/cpp-0457/) | | | | 458 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | [solution](https://leetcode.com/problems/poor-pigs/solution/) | [C++](0001-0500/0458-Poor-Pigs/) | | | -| | | | | | | +| 459 | [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/description/) | [无] | [C++](0001-0500/0459-Repeated-Substring-Pattern/cpp-0459/) | | | | 460 | [LFU Cache](https://leetcode.com/problems/lfu-cache/description/) | [solution](https://leetcode.com/problems/lfu-cache/solutions/2815229/lfu-cache/?orderBy=most_votes) | [C++](0001-0500/0460-LFU-Cache/) | | | | 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [solution](https://leetcode.com/problems/hamming-distance/solution/) | [C++](0001-0500/0461-Hamming-Distance/cpp-0461/) | | | | 462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [solution](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/solution/) | [C++](0001-0500/0462-Minimum-Moves-to-Equal-Array-Elements-II/cpp-0462/) | | | From 656bee6885561eaef0d8cc5a3610ff96a8604e5c Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Tue, 31 Oct 2023 18:16:56 -0700 Subject: [PATCH 389/390] 1361 codes updated. --- .../cpp-1361/main.cpp | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/1001-1500/1361-Validate-Binary-Tree-Nodes/cpp-1361/main.cpp b/1001-1500/1361-Validate-Binary-Tree-Nodes/cpp-1361/main.cpp index 34bda65d..9e52d2ed 100644 --- a/1001-1500/1361-Validate-Binary-Tree-Nodes/cpp-1361/main.cpp +++ b/1001-1500/1361-Validate-Binary-Tree-Nodes/cpp-1361/main.cpp @@ -1,15 +1,18 @@ /// Source : https://leetcode.com/problems/validate-binary-tree-nodes/ /// Author : liuyubobobo /// Time : 2020-02-22 +/// Updated: 2023-10-31 #include #include +#include #include +#include using namespace std; -/// Constructing Graph +/// topo sort /// Time Complexity: O(n) /// Space Complexity: O(n) class Solution { @@ -18,28 +21,57 @@ class Solution { vector> g(n); vector indegrees(n, 0); - for(int i = 0; i < n; i ++) + for(int i = 0; i < n; i ++){ if(leftChild[i] != -1){ - if(i == leftChild[i]) return false; g[i].insert(leftChild[i]); indegrees[leftChild[i]] ++; } - for(int i = 0; i < n; i ++) if(rightChild[i] != -1){ - if(i == rightChild[i] || g[i].count(rightChild[i]) || indegrees[rightChild[i]]) return false; + if(g[i].count(rightChild[i])) return false; g[i].insert(rightChild[i]); indegrees[rightChild[i]] ++; } + } + + return topo_sort(n, g, indegrees); + } + +private: + bool topo_sort(int n, const vector>& g, vector& indegrees){ + + for(int i = 0; i < n; i ++) + if(indegrees[i] > 1 || g[i].size() > 2) return false; + + queue q; + vector visited(n, false); - int root = 0; for(int i = 0; i < n; i ++) - root += (indegrees[i] == 0); - return root == 1; + if(indegrees[i] == 0){ + q.push(i); + visited[i] = true; + } + + if(q.size() != 1) return false; + + while(!q.empty()){ + int cur = q.front(); q.pop(); + for(int next: g[cur]){ + if(visited[next]) return false; + visited[next] = true; + q.push(next); + } + } + + return accumulate(visited.begin(), visited.end(), 0) == n; } }; int main() { + vector leftChild1 = {1, -1, 3, -1}, rightChild1 = {2, -1, -1, -1}; + cout << Solution().validateBinaryTreeNodes(4, leftChild1, rightChild1) << endl; + // 1 + return 0; } From 31d7c1e72acfaf434b972d46c93775f30127ee0d Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Thu, 9 Nov 2023 01:31:31 -0800 Subject: [PATCH 390/390] 2849 solved. --- .../cpp-2849/CMakeLists.txt | 6 + .../cpp-2849/main.cpp | 28 ++ readme.md | 400 +++++++++--------- 3 files changed, 235 insertions(+), 199 deletions(-) create mode 100644 2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/CMakeLists.txt create mode 100644 2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/main.cpp diff --git a/2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/CMakeLists.txt b/2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/CMakeLists.txt new file mode 100644 index 00000000..d510b2ba --- /dev/null +++ b/2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.26) +project(cpp_2849) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(cpp_2849 main.cpp) diff --git a/2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/main.cpp b/2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/main.cpp new file mode 100644 index 00000000..8ce52a2e --- /dev/null +++ b/2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/main.cpp @@ -0,0 +1,28 @@ +/// Source : https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/description/ +/// Author : liuyubobobo +/// Time : 2023-11-09 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) { + + int a = abs(sx - fx), b = abs(sy - fy); + int d = max(a, b); + if(d == 0) return t != 1; + return t >= d; + } +}; + + +int main() { + + return 0; +} diff --git a/readme.md b/readme.md index 1777f0cf..a9dd2126 100644 --- a/readme.md +++ b/readme.md @@ -2402,123 +2402,123 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2498 | [Frog Jump II](https://leetcode.com/problems/frog-jump-ii/) | [无] | [C++](2001-2500/2498-Frog-Jump-II/cpp-2498/) | | | | 2499 | [Minimum Total Cost to Make Arrays Unequal](https://leetcode.com/problems/minimum-total-cost-to-make-arrays-unequal/description/) | [无] | [C++](2001-2500/2499-Minimum-Total-Cost-to-Make-Arrays-Unequal/cpp-2499/) | | | | 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [无] | [C++](2001-2500/2500-Delete-Greatest-Value-in-Each-Row/cpp-2500/) | | | -| 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2001-2500/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | -| 2502 | [Design Memory Allocator](https://leetcode.com/problems/design-memory-allocator/) | [无] | [C++](2001-2500/2502-Design-Memory-Allocator/cpp-2502/) | | | -| 2503 | [Maximum Number of Points From Grid Queries](https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/) | [无] | [C++](2001-2500/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/) | | | +| 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/) | [无] | [C++](2501-3000/2501-Longest-Square-Streak-in-an-Array/cpp-2501/) | | | +| 2502 | [Design Memory Allocator](https://leetcode.com/problems/design-memory-allocator/) | [无] | [C++](2501-3000/2502-Design-Memory-Allocator/cpp-2502/) | | | +| 2503 | [Maximum Number of Points From Grid Queries](https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/) | [无] | [C++](2501-3000/2503-Maximum-Number-of-Points-From-Grid-Queries/cpp-2503/) | | | | 2504 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| 2505 | [Bitwise OR of All Subsequence Sums](https://leetcode.com/problems/bitwise-or-of-all-subsequence-sums/description/) | [无] | [C++](2001-2500/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/) | | | -| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [无] | [C++](2001-2500/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/) | | | -| 2507 | [Smallest Value After Replacing With Sum of Prime Factors](https://leetcode.com/problems/smallest-value-after-replacing-with-sum-of-prime-factors/) | [无] | [C++](2001-2500/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/) | | | -| 2508 | [Add Edges to Make Degrees of All Nodes Even](https://leetcode.com/problems/add-edges-to-make-degrees-of-all-nodes-even/) | [无] | [C++](2001-2500/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/) | | | -| 2509 | [Cycle Length Queries in a Tree](https://leetcode.com/problems/cycle-length-queries-in-a-tree/) | [无] | [C++](2001-2500/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/) | | | -| 2510 | [Check if There is a Path With Equal Number of 0's And 1's](https://leetcode.com/problems/check-if-there-is-a-path-with-equal-number-of-0s-and-1s/description/) | [无] | [C++](2001-2500/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/) | | | -| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/) | [无] | [C++](2001-2500/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/) | | | -| 2512 | [Reward Top K Students](https://leetcode.com/problems/reward-top-k-students/) | [无] | [C++](2001-2500/2512-Reward-Top-K-Students/cpp-2512/) | | | -| 2513 | [Minimize the Maximum of Two Arrays](https://leetcode.com/problems/minimize-the-maximum-of-two-arrays/) | [无] | [C++](2001-2500/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/) | | | -| 2514 | [Count Anagrams](https://leetcode.com/problems/count-anagrams/) | [无] | [C++](2001-2500/2514-Count-Anagrams/cpp-2514/) | | | -| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [无] | [C++](2001-2500/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/) | | | -| 2516 | [Take K of Each Character From Left and Right](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | [无] | [C++](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | | | -| 2517 | [Maximum Tastiness of Candy Basket](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | [无] | [C++](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | | | -| 2518 | [Number of Great Partitions](https://leetcode.com/problems/number-of-great-partitions/) | [无] | [C++](2001-2500/2518-Number-of-Great-Partitions/cpp-2518/) | | | -| 2519 | [Count the Number of K-Big Indices](https://leetcode.com/problems/count-the-number-of-k-big-indices/description/) | [无] | [C++](2001-2500/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/) | | | -| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number/) | [无] | [C++](2001-2500/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/) | | | -| 2521 | [Distinct Prime Factors of Product of Array](https://leetcode.com/problems/distinct-prime-factors-of-product-of-array/) | [无] | [C++](2001-2500/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/) | | | -| 2522 | [Partition String Into Substrings With Values at Most K](https://leetcode.com/problems/partition-string-into-substrings-with-values-at-most-k/) | [无] | [C++](2001-2500/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/) | | | -| 2523 | [Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/) | [无] | [C++](2001-2500/2523-Closest-Prime-Numbers-in-Range/cpp-2523/) | | | -| 2524 | [Maximum Frequency Score of a Subarray](https://leetcode.com/problems/maximum-frequency-score-of-a-subarray/description/) | [无] | [C++](2001-2500/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/) | | | -| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria/) | [无] | [C++](2001-2500/2525-Categorize-Box-According-to-Criteria/cpp-2525/) | | | -| 2526 | [Find Consecutive Integers from a Data Stream](https://leetcode.com/problems/find-consecutive-integers-from-a-data-stream/) | [无] | [C++](2001-2500/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/) | | | -| 2527 | [Find Xor-Beauty of Array](https://leetcode.com/problems/find-xor-beauty-of-array/) | [无] | [C++](2001-2500/2527-Find-Xor-Beauty-of-Array/cpp-2527/) | | | -| 2528 | [Maximize the Minimum Powered City](https://leetcode.com/problems/maximize-the-minimum-powered-city/) | [无] | [C++](2001-2500/2528-Maximize-the-Minimum-Powered-City/cpp-2528/) | | | -| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | [无] | [C++](2001-2500/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/) | | | -| 2530 | [Maximal Score After Applying K Operations](https://leetcode.com/problems/maximal-score-after-applying-k-operations/) | [无] | [C++](2001-2500/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/) | | | -| 2531 | [Make Number of Distinct Characters Equal](https://leetcode.com/problems/make-number-of-distinct-characters-equal/) | [无] | [C++](2001-2500/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/) | | | -| 2532 | [Time to Cross a Bridge](https://leetcode.com/problems/time-to-cross-a-bridge/description/) | [无] | [C++](2001-2500/2532-Time-to-Cross-a-Bridge/cpp-2532/) | | | -| 2533 | [Number of Good Binary Strings](https://leetcode.com/problems/number-of-good-binary-strings/) | [无] | [C++](2001-2500/2533-Number-of-Good-Binary-Strings/cpp-2533/) | | | -| 2534 | [Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door/description/) | [无] | [C++](2001-2500/2534-Time-Taken-to-Cross-the-Door/cpp-2534/) | | | -| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/) | [无] | [C++](2001-2500/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/) | | | -| 2536 | [Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/) | [无] | [C++](2001-2500/2536-Increment-Submatrices-by-One/cpp-2536/) | | | -| 2537 | [Count the Number of Good Subarrays](https://leetcode.com/problems/count-the-number-of-good-subarrays/) | [无] | [C++](2001-2500/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/) | | | -| | | | | | | -| 2539 | [Count the Number of Good Subsequences](https://leetcode.com/problems/count-the-number-of-good-subsequences/) | [无] | [C++](2001-2500/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/) | | | -| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value/) | [无] | [C++](2001-2500/2540-Minimum-Common-Value/cpp-2540/) | | | -| 2541 | [Minimum Operations to Make Array Equal II](https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/) | [无] | [C++](2001-2500/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/) | | | -| 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) | [无] | [C++](2001-2500/2542-Maximum-Subsequence-Score/cpp-2542/) | | | -| 2543 | [Check if Point Is Reachable](https://leetcode.com/problems/check-if-point-is-reachable/description/) | [无] | [C++](2001-2500/2543-Check-if-Point-Is-Reachable/cpp-2543/) | | | -| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/) | [无] | [C++](2001-2500/2544-Alternating-Digit-Sum/cpp-2544/) | | | -| 2545 | [Sort the Students by Their Kth Score](https://leetcode.com/problems/sort-the-students-by-their-kth-score/) | [无] | [C++](2001-2500/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/) | | | -| 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [无] | [C++](2001-2500/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | | -| 2547 | [Minimum Cost to Split an Array](https://leetcode.com/problems/minimum-cost-to-split-an-array/) | [无] | [C++](2001-2500/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/) | | | -| 2548 | [Maximum Price to Fill a Bag](https://leetcode.com/problems/maximum-price-to-fill-a-bag/) | [无] | [C++](2001-2500/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/) | | | -| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board/) | [无] | [C++](2001-2500/2549-Count-Distinct-Numbers-on-Board/cpp-2549/) | | | -| 2550 | [Count Collisions of Monkeys on a Polygon](https://leetcode.com/problems/count-collisions-of-monkeys-on-a-polygon/) | [无] | [C++](2001-2500/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/) | | | -| 2551 | [Put Marbles in Bags](https://leetcode.com/problems/put-marbles-in-bags/) | [无] | [C++](2001-2500/2551-Put-Marbles-in-Bags/cpp-2551/) | | | -| | | | | | | -| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array/) | [无] | [C++](2001-2500/2553-Separate-the-Digits-in-an-Array/cpp-2553/) | | | -| 2554 | [Maximum Number of Integers to Choose From a Range I](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/) | [无] | [C++](2001-2500/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/) | | | -| 2555 | [Maximize Win From Two Segments](https://leetcode.com/problems/maximize-win-from-two-segments/) | [无] | [C++](2001-2500/2555-Maximize-Win-From-Two-Segments/cpp-2555/) | | | -| 2556 | [Disconnect Path in a Binary Matrix by at Most One Flip](https://leetcode.com/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/) | [无] | [C++](2001-2500/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/) | | | -| 2557 | [Maximum Number of Integers to Choose From a Range II](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-ii/description/) | [无] | [C++](2001-2500/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/) | | | -| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | [无] | [C++](2001-2500/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/) | | | -| 2559 | [Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/) | [无] | [C++](2001-2500/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/) | | | -| 2560 | [House Robber IV](https://leetcode.com/problems/house-robber-iv/) | [无] | [C++](2001-2500/2560-House-Robber-IV/cpp-2560/) | | | -| 2561 | [Rearranging Fruits](https://leetcode.com/problems/rearranging-fruits/) | [无] | [C++](2001-2500/2561-Rearranging-Fruits/cpp-2561/) | | | -| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value/) | [无] | [C++](2001-2500/2562-Find-the-Array-Concatenation-Value/cpp-2562/) | | | -| 2563 | [Count the Number of Fair Pairs](https://leetcode.com/problems/count-the-number-of-fair-pairs/) | [无] | [C++](2001-2500/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/) | | | -| 2564 | [Substring XOR Queries](https://leetcode.com/problems/substring-xor-queries/) | [无] | [C++](2001-2500/2564-Substring-XOR-Queries/cpp-2564/) | | | -| 2565 | [Subsequence With the Minimum Score](https://leetcode.com/problems/subsequence-with-the-minimum-score/) | [无] | [C++](2001-2500/2565-Subsequence-With-the-Minimum-Score/cpp-2565/) | | | -| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/) | [无] | [C++](2001-2500/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/) | | | -| 2567 | [Minimum Score by Changing Two Elements](https://leetcode.com/problems/minimum-score-by-changing-two-elements/) | [无] | [C++](2001-2500/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/) | | | -| 2568 | [Minimum Impossible OR](https://leetcode.com/problems/minimum-impossible-or/) | [无] | [C++](2001-2500/2568-Minimum-Impossible-OR/cpp-2568/) | | | -| 2569 | [Handling Sum Queries After Update](https://leetcode.com/problems/handling-sum-queries-after-update/) | [无] | [C++](2001-2500/2569-Handling-Sum-Queries-After-Update/cpp-2569/) | | | -| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/) | [无] | [C++](2001-2500/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/) | | | -| 2571 | [Minimum Operations to Reduce an Integer to 0](https://leetcode.com/problems/minimum-operations-to-reduce-an-integer-to-0/) | [无] | [C++](2001-2500/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/) | | | -| 2572 | [Count the Number of Square-Free Subsets](https://leetcode.com/problems/count-the-number-of-square-free-subsets/) | [无] | [C++](2001-2500/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/) | | | -| 2573 | [Find the String with LCP](https://leetcode.com/problems/find-the-string-with-lcp/) | [无] | [C++](2001-2500/2573-Find-the-String-with-LCP/cpp-2573/) | | | -| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences/) | [无] | [C++](2001-2500/2574-Left-and-Right-Sum-Differences/cpp-2574/) | | | -| 2575 | [Find the Divisibility Array of a String](https://leetcode.com/problems/find-the-divisibility-array-of-a-string/) | [无] | [C++](2001-2500/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/) | | | -| 2576 | [Find the Maximum Number of Marked Indices](https://leetcode.com/problems/find-the-maximum-number-of-marked-indices/) | [无] | [C++](2001-2500/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/) | | | -| 2577 | [Minimum Time to Visit a Cell In a Grid](https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/) | [无] | [C++](2001-2500/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/) | | | -| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum/) | [无] | [C++](2001-2500/2578-Split-With-Minimum-Sum/cpp-2578/) | | | -| 2579 | [Count Total Number of Colored Cells](https://leetcode.com/problems/count-total-number-of-colored-cells/) | [无] | [C++](2001-2500/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/) | | | -| 2580 | [Count Ways to Group Overlapping Ranges](https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/) | [无] | [C++](2001-2500/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/) | | | -| 2581 | [Count Number of Possible Root Nodes](https://leetcode.com/problems/count-number-of-possible-root-nodes/) | [无] | [C++](2001-2500/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/) | | | -| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow/) | [无] | [C++](2001-2500/2582-Pass-the-Pillow/cpp-2582/) | | | -| 2583 | [Kth Largest Sum in a Binary Tree](https://leetcode.com/problems/kth-largest-sum-in-a-binary-tree/) | [无] | [C++](2001-2500/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/) | | | -| 2584 | [Split the Array to Make Coprime Products](https://leetcode.com/problems/split-the-array-to-make-coprime-products/) | [无] | [C++](2001-2500/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/) | | | -| 2585 | [Number of Ways to Earn Points](https://leetcode.com/problems/number-of-ways-to-earn-points/) | [无] | [C++](2001-2500/2585-Number-of-Ways-to-Earn-Points/cpp-2585/) | | | -| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/) | [无] | [C++](2001-2500/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/) | | | -| 2587 | [Rearrange Array to Maximize Prefix Score](https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/) | [无] | [C++](2001-2500/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/) | | | -| 2588 | [Count the Number of Beautiful Subarrays](https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/) | [无] | [C++](2001-2500/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/) | | | -| 2589 | [Minimum Time to Complete All Tasks](https://leetcode.com/problems/minimum-time-to-complete-all-tasks/) | [无] | [C++](2001-2500/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/) | | | -| 2590 | [Design a Todo List](https://leetcode.com/problems/design-a-todo-list/description/) | [无] | [C++](2001-2500/2590-Design-a-Todo-List/cpp-2590/) | | | -| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children/) | [无] | [C++](2001-2500/2591-Distribute-Money-to-Maximum-Children/cpp-2591/) | | | -| 2592 | [Maximize Greatness of an Array](https://leetcode.com/problems/maximize-greatness-of-an-array/) | [无] | [C++](2001-2500/2592-Maximize-Greatness-of-an-Array/cpp-2592/) | | | -| 2593 | [Find Score of an Array After Marking All Elements](https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/) | [无] | [C++](2001-2500/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/) | | | -| 2594 | [Minimum Time to Repair Cars](https://leetcode.com/problems/minimum-time-to-repair-cars/) | [无] | [C++](2001-2500/2594-Minimum-Time-to-Repair-Cars/cpp-2594/) | | | -| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits/) | [无] | [C++](2001-2500/2595-Number-of-Even-and-Odd-Bits/cpp-2595/) | | | -| 2596 | [Check Knight Tour Configuration](https://leetcode.com/problems/check-knight-tour-configuration/) | [无] | [C++](2001-2500/2596-Check-Knight-Tour-Configuration/cpp-2596/) | | | -| 2597 | [The Number of Beautiful Subsets](https://leetcode.com/problems/the-number-of-beautiful-subsets/) | [无] | [C++](2001-2500/2597-The-Number-of-Beautiful-Subsets/cpp-2597/) | | | -| 2598 | [Smallest Missing Non-negative Integer After Operations](https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/) | [无] | [C++](2001-2500/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/) | | | -| 2599 | [Make the Prefix Sum Non-negative](https://leetcode.com/problems/make-the-prefix-sum-non-negative/description/) | [无] | [C++](2001-2500/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/) | | | -| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum/) | [无] | [C++](2001-2500/2600-K-Items-With-the-Maximum-Sum/cpp-2600/) | | | -| 2601 | [Prime Subtraction Operation](https://leetcode.com/problems/prime-subtraction-operation/) | [无] | [C++](2001-2500/2601-Prime-Subtraction-Operation/cpp-2601/) | | | -| 2602 | [Minimum Operations to Make All Array Elements Equal](https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/) | [无] | [C++](2001-2500/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/) | | | -| 2603 | [Collect Coins in a Tree](https://leetcode.com/problems/collect-coins-in-a-tree/description/) | [无] | [C++](2001-2500/2603-Collect-Coins-in-a-Tree/cpp-2603/) | | | -| 2604 | [Minimum Time to Eat All Grains](https://leetcode.com/problems/minimum-time-to-eat-all-grains/description/) | [无] | [C++](2001-2500/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/) | | | -| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays/) | [无] | [C++](2001-2500/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/) | | | -| 2606 | [Find the Substring With Maximum Cost](https://leetcode.com/problems/find-the-substring-with-maximum-cost/) | [无] | [C++](2001-2500/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/) | | | -| 2607 | [Make K-Subarray Sums Equal](https://leetcode.com/problems/make-k-subarray-sums-equal/) | [无] | [C++](2001-2500/2607-Make-K-Subarray-Sums-Equal/cpp-2607/) | | | -| 2608 | [Shortest Cycle in a Graph](https://leetcode.com/problems/shortest-cycle-in-a-graph/) | [无] | [C++](2001-2500/2608-Shortest-Cycle-in-a-Graph/cpp-2608/) | | | -| 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/) | [无] | [C++](2001-2500/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/) | | | -| 2610 | [Convert an Array Into a 2D Array With Conditions](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/) | [无] | [C++](2001-2500/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/) | | | -| 2611 | [Mice and Cheese](https://leetcode.com/problems/mice-and-cheese/) | [无] | [C++](2001-2500/2611-Mice-and-Cheese/cpp-2611/) | | | -| 2612 | [Minimum Reverse Operations](https://leetcode.com/problems/minimum-reverse-operations/) | [无] | [C++](2001-2500/2612-Minimum-Reverse-Operations/cpp-2612/) | | | -| | | | | | | -| 2614 | [Prime In Diagonal](https://leetcode.com/problems/prime-in-diagonal/) | [无] | [C++](2001-2500/2614-Prime-In-Diagonal/cpp-2614/) | | | -| 2615 | [Sum of Distances](https://leetcode.com/problems/sum-of-distances/) | [无] | [C++](2001-2500/2615-Sum-of-Distances/cpp-2615/) | | | -| 2616 | [Minimize the Maximum Difference of Pairs](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/) | [无] | [C++](2001-2500/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/) | | | -| 2617 | [Minimum Number of Visited Cells in a Grid](https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/) | [无] | [C++](2001-2500/2617-Minimum-Number-of-Visited-Cells-in-a-Grid/cpp-2617/) | | | +| 2505 | [Bitwise OR of All Subsequence Sums](https://leetcode.com/problems/bitwise-or-of-all-subsequence-sums/description/) | [无] | [C++](2501-3000/2505-Bitwise-OR-of-All-Subsequence-Sums/cpp-2505/) | | | +| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [无] | [C++](2501-3000/2506-Count-Pairs-Of-Similar-Strings/cpp-2506/) | | | +| 2507 | [Smallest Value After Replacing With Sum of Prime Factors](https://leetcode.com/problems/smallest-value-after-replacing-with-sum-of-prime-factors/) | [无] | [C++](2501-3000/2507-Smallest-Value-After-Replacing-With-Sum-of-Prime-Factors/cpp-2507/) | | | +| 2508 | [Add Edges to Make Degrees of All Nodes Even](https://leetcode.com/problems/add-edges-to-make-degrees-of-all-nodes-even/) | [无] | [C++](2501-3000/2508-Add-Edges-to-Make-Degrees-of-All-Nodes-Even/cpp-2508/) | | | +| 2509 | [Cycle Length Queries in a Tree](https://leetcode.com/problems/cycle-length-queries-in-a-tree/) | [无] | [C++](2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/) | | | +| 2510 | [Check if There is a Path With Equal Number of 0's And 1's](https://leetcode.com/problems/check-if-there-is-a-path-with-equal-number-of-0s-and-1s/description/) | [无] | [C++](2501-3000/2510-Check-if-There-is-a-Path-With-Equal-Number-of-0s-And-1s/cpp-2510/) | | | +| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/) | [无] | [C++](2501-3000/2511-Maximum-Enemy-Forts-That-Can-Be-Captured/cpp-2511/) | | | +| 2512 | [Reward Top K Students](https://leetcode.com/problems/reward-top-k-students/) | [无] | [C++](2501-3000/2512-Reward-Top-K-Students/cpp-2512/) | | | +| 2513 | [Minimize the Maximum of Two Arrays](https://leetcode.com/problems/minimize-the-maximum-of-two-arrays/) | [无] | [C++](2501-3000/2513-Minimize-the-Maximum-of-Two-Arrays/cpp-2513/) | | | +| 2514 | [Count Anagrams](https://leetcode.com/problems/count-anagrams/) | [无] | [C++](2501-3000/2514-Count-Anagrams/cpp-2514/) | | | +| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [无] | [C++](2501-3000/2515-Shortest-Distance-to-Target-String-in-a-Circular-Array/cpp-2515/) | | | +| 2516 | [Take K of Each Character From Left and Right](2001-2500/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | [无] | [C++](2501-3000/2516-Take-K-of-Each-Character-From-Left-and-Right/cpp-2516/) | | | +| 2517 | [Maximum Tastiness of Candy Basket](2001-2500/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | [无] | [C++](2501-3000/2517-Maximum-Tastiness-of-Candy-Basket/cpp-2517/) | | | +| 2518 | [Number of Great Partitions](https://leetcode.com/problems/number-of-great-partitions/) | [无] | [C++](2501-3000/2518-Number-of-Great-Partitions/cpp-2518/) | | | +| 2519 | [Count the Number of K-Big Indices](https://leetcode.com/problems/count-the-number-of-k-big-indices/description/) | [无] | [C++](2501-3000/2519-Count-the-Number-of-K-Big-Indices/cpp-2519/) | | | +| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number/) | [无] | [C++](2501-3000/2520-Count-the-Digits-That-Divide-a-Number/cpp-2520/) | | | +| 2521 | [Distinct Prime Factors of Product of Array](https://leetcode.com/problems/distinct-prime-factors-of-product-of-array/) | [无] | [C++](2501-3000/2521-Distinct-Prime-Factors-of-Product-of-Array/cpp-2521/) | | | +| 2522 | [Partition String Into Substrings With Values at Most K](https://leetcode.com/problems/partition-string-into-substrings-with-values-at-most-k/) | [无] | [C++](2501-3000/2522-Partition-String-Into-Substrings-With-Values-at-Most-K/cpp-2522/) | | | +| 2523 | [Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/) | [无] | [C++](2501-3000/2523-Closest-Prime-Numbers-in-Range/cpp-2523/) | | | +| 2524 | [Maximum Frequency Score of a Subarray](https://leetcode.com/problems/maximum-frequency-score-of-a-subarray/description/) | [无] | [C++](2501-3000/2524-Maximum-Frequency-Score-of-a-Subarray/cpp-2524/) | | | +| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria/) | [无] | [C++](2501-3000/2525-Categorize-Box-According-to-Criteria/cpp-2525/) | | | +| 2526 | [Find Consecutive Integers from a Data Stream](https://leetcode.com/problems/find-consecutive-integers-from-a-data-stream/) | [无] | [C++](2501-3000/2526-Find-Consecutive-Integers-from-a-Data-Stream/cpp-2526/) | | | +| 2527 | [Find Xor-Beauty of Array](https://leetcode.com/problems/find-xor-beauty-of-array/) | [无] | [C++](2501-3000/2527-Find-Xor-Beauty-of-Array/cpp-2527/) | | | +| 2528 | [Maximize the Minimum Powered City](https://leetcode.com/problems/maximize-the-minimum-powered-city/) | [无] | [C++](2501-3000/2528-Maximize-the-Minimum-Powered-City/cpp-2528/) | | | +| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | [无] | [C++](2501-3000/2529-Maximum-Count-of-Positive-Integer-and-Negative-Integer/cpp-2529/) | | | +| 2530 | [Maximal Score After Applying K Operations](https://leetcode.com/problems/maximal-score-after-applying-k-operations/) | [无] | [C++](2501-3000/2530-Maximal-Score-After-Applying-K-Operations/cpp-2530/) | | | +| 2531 | [Make Number of Distinct Characters Equal](https://leetcode.com/problems/make-number-of-distinct-characters-equal/) | [无] | [C++](2501-3000/2531-Make-Number-of-Distinct-Characters-Equal/cpp-2531/) | | | +| 2532 | [Time to Cross a Bridge](https://leetcode.com/problems/time-to-cross-a-bridge/description/) | [无] | [C++](2501-3000/2532-Time-to-Cross-a-Bridge/cpp-2532/) | | | +| 2533 | [Number of Good Binary Strings](https://leetcode.com/problems/number-of-good-binary-strings/) | [无] | [C++](2501-3000/2533-Number-of-Good-Binary-Strings/cpp-2533/) | | | +| 2534 | [Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door/description/) | [无] | [C++](2501-3000/2534-Time-Taken-to-Cross-the-Door/cpp-2534/) | | | +| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/) | [无] | [C++](2501-3000/2535-Difference-Between-Element-Sum-and-Digit-Sum-of-an-Array/cpp-2535/) | | | +| 2536 | [Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/) | [无] | [C++](2501-3000/2536-Increment-Submatrices-by-One/cpp-2536/) | | | +| 2537 | [Count the Number of Good Subarrays](https://leetcode.com/problems/count-the-number-of-good-subarrays/) | [无] | [C++](2501-3000/2537-Count-the-Number-of-Good-Subarrays/cpp-2537/) | | | +| | | | | | | +| 2539 | [Count the Number of Good Subsequences](https://leetcode.com/problems/count-the-number-of-good-subsequences/) | [无] | [C++](2501-3000/2539-Count-the-Number-of-Good-Subsequences/cpp-2539/) | | | +| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value/) | [无] | [C++](2501-3000/2540-Minimum-Common-Value/cpp-2540/) | | | +| 2541 | [Minimum Operations to Make Array Equal II](https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/) | [无] | [C++](2501-3000/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/) | | | +| 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) | [无] | [C++](2501-3000/2542-Maximum-Subsequence-Score/cpp-2542/) | | | +| 2543 | [Check if Point Is Reachable](https://leetcode.com/problems/check-if-point-is-reachable/description/) | [无] | [C++](2501-3000/2543-Check-if-Point-Is-Reachable/cpp-2543/) | | | +| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/) | [无] | [C++](2501-3000/2544-Alternating-Digit-Sum/cpp-2544/) | | | +| 2545 | [Sort the Students by Their Kth Score](https://leetcode.com/problems/sort-the-students-by-their-kth-score/) | [无] | [C++](2501-3000/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/) | | | +| 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [无] | [C++](2501-3000/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | | +| 2547 | [Minimum Cost to Split an Array](https://leetcode.com/problems/minimum-cost-to-split-an-array/) | [无] | [C++](2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/) | | | +| 2548 | [Maximum Price to Fill a Bag](https://leetcode.com/problems/maximum-price-to-fill-a-bag/) | [无] | [C++](2501-3000/2548-Maximum-Price-to-Fill-a-Bag/cpp-2548/) | | | +| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board/) | [无] | [C++](2501-3000/2549-Count-Distinct-Numbers-on-Board/cpp-2549/) | | | +| 2550 | [Count Collisions of Monkeys on a Polygon](https://leetcode.com/problems/count-collisions-of-monkeys-on-a-polygon/) | [无] | [C++](2501-3000/2550-Count-Collisions-of-Monkeys-on-a-Polygon/cpp-2550/) | | | +| 2551 | [Put Marbles in Bags](https://leetcode.com/problems/put-marbles-in-bags/) | [无] | [C++](2501-3000/2551-Put-Marbles-in-Bags/cpp-2551/) | | | +| | | | | | | +| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array/) | [无] | [C++](2501-3000/2553-Separate-the-Digits-in-an-Array/cpp-2553/) | | | +| 2554 | [Maximum Number of Integers to Choose From a Range I](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/) | [无] | [C++](2501-3000/2554-Maximum-Number-of-Integers-to-Choose-From-a-Range-I/cpp-2554/) | | | +| 2555 | [Maximize Win From Two Segments](https://leetcode.com/problems/maximize-win-from-two-segments/) | [无] | [C++](2501-3000/2555-Maximize-Win-From-Two-Segments/cpp-2555/) | | | +| 2556 | [Disconnect Path in a Binary Matrix by at Most One Flip](https://leetcode.com/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/) | [无] | [C++](2501-3000/2556-Disconnect-Path-in-a-Binary-Matrix-by-at-Most-One-Flip/cpp-2556/) | | | +| 2557 | [Maximum Number of Integers to Choose From a Range II](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-ii/description/) | [无] | [C++](2501-3000/2557-Maximum-Number-of-Integers-to-Choose-From-a-Range-II/cpp-2557/) | | | +| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | [无] | [C++](2501-3000/2558-Take-Gifts-From-the-Richest-Pile/cpp-2558/) | | | +| 2559 | [Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/) | [无] | [C++](2501-3000/2559-Count-Vowel-Strings-in-Ranges/cpp-2559/) | | | +| 2560 | [House Robber IV](https://leetcode.com/problems/house-robber-iv/) | [无] | [C++](2501-3000/2560-House-Robber-IV/cpp-2560/) | | | +| 2561 | [Rearranging Fruits](https://leetcode.com/problems/rearranging-fruits/) | [无] | [C++](2501-3000/2561-Rearranging-Fruits/cpp-2561/) | | | +| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value/) | [无] | [C++](2501-3000/2562-Find-the-Array-Concatenation-Value/cpp-2562/) | | | +| 2563 | [Count the Number of Fair Pairs](https://leetcode.com/problems/count-the-number-of-fair-pairs/) | [无] | [C++](2501-3000/2563-Count-the-Number-of-Fair-Pairs/cpp-2563/) | | | +| 2564 | [Substring XOR Queries](https://leetcode.com/problems/substring-xor-queries/) | [无] | [C++](2501-3000/2564-Substring-XOR-Queries/cpp-2564/) | | | +| 2565 | [Subsequence With the Minimum Score](https://leetcode.com/problems/subsequence-with-the-minimum-score/) | [无] | [C++](2501-3000/2565-Subsequence-With-the-Minimum-Score/cpp-2565/) | | | +| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/) | [无] | [C++](2501-3000/2566-Maximum-Difference-by-Remapping-a-Digit/cpp-2566/) | | | +| 2567 | [Minimum Score by Changing Two Elements](https://leetcode.com/problems/minimum-score-by-changing-two-elements/) | [无] | [C++](2501-3000/2567-Minimum-Score-by-Changing-Two-Elements/cpp-2567/) | | | +| 2568 | [Minimum Impossible OR](https://leetcode.com/problems/minimum-impossible-or/) | [无] | [C++](2501-3000/2568-Minimum-Impossible-OR/cpp-2568/) | | | +| 2569 | [Handling Sum Queries After Update](https://leetcode.com/problems/handling-sum-queries-after-update/) | [无] | [C++](2501-3000/2569-Handling-Sum-Queries-After-Update/cpp-2569/) | | | +| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/) | [无] | [C++](2501-3000/2570-Merge-Two-2D-Arrays-by-Summing-Values/cpp-2570/) | | | +| 2571 | [Minimum Operations to Reduce an Integer to 0](https://leetcode.com/problems/minimum-operations-to-reduce-an-integer-to-0/) | [无] | [C++](2501-3000/2571-Minimum-Operations-to-Reduce-an-Integer-to-0/cpp-2571/) | | | +| 2572 | [Count the Number of Square-Free Subsets](https://leetcode.com/problems/count-the-number-of-square-free-subsets/) | [无] | [C++](2501-3000/2572-Count-the-Number-of-Square-Free-Subsets/cpp-2572/) | | | +| 2573 | [Find the String with LCP](https://leetcode.com/problems/find-the-string-with-lcp/) | [无] | [C++](2501-3000/2573-Find-the-String-with-LCP/cpp-2573/) | | | +| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences/) | [无] | [C++](2501-3000/2574-Left-and-Right-Sum-Differences/cpp-2574/) | | | +| 2575 | [Find the Divisibility Array of a String](https://leetcode.com/problems/find-the-divisibility-array-of-a-string/) | [无] | [C++](2501-3000/2575-Find-the-Divisibility-Array-of-a-String/cpp-2575/) | | | +| 2576 | [Find the Maximum Number of Marked Indices](https://leetcode.com/problems/find-the-maximum-number-of-marked-indices/) | [无] | [C++](2501-3000/2576-Find-the-Maximum-Number-of-Marked-Indices/cpp-2576/) | | | +| 2577 | [Minimum Time to Visit a Cell In a Grid](https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/) | [无] | [C++](2501-3000/2577-Minimum-Time-to-Visit-a-Cell-In-a-Grid/cpp-2577/) | | | +| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum/) | [无] | [C++](2501-3000/2578-Split-With-Minimum-Sum/cpp-2578/) | | | +| 2579 | [Count Total Number of Colored Cells](https://leetcode.com/problems/count-total-number-of-colored-cells/) | [无] | [C++](2501-3000/2579-Count-Total-Number-of-Colored-Cells/cpp-2579/) | | | +| 2580 | [Count Ways to Group Overlapping Ranges](https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/) | [无] | [C++](2501-3000/2580-Count-Ways-to-Group-Overlapping-Ranges/cpp-2580/) | | | +| 2581 | [Count Number of Possible Root Nodes](https://leetcode.com/problems/count-number-of-possible-root-nodes/) | [无] | [C++](2501-3000/2581-Count-Number-of-Possible-Root-Nodes/cpp-2581/) | | | +| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow/) | [无] | [C++](2501-3000/2582-Pass-the-Pillow/cpp-2582/) | | | +| 2583 | [Kth Largest Sum in a Binary Tree](https://leetcode.com/problems/kth-largest-sum-in-a-binary-tree/) | [无] | [C++](2501-3000/2583-Kth-Largest-Sum-in-a-Binary-Tree/cpp-2583/) | | | +| 2584 | [Split the Array to Make Coprime Products](https://leetcode.com/problems/split-the-array-to-make-coprime-products/) | [无] | [C++](2501-3000/2584-Split-the-Array-to-Make-Coprime-Products/cpp-2584/) | | | +| 2585 | [Number of Ways to Earn Points](https://leetcode.com/problems/number-of-ways-to-earn-points/) | [无] | [C++](2501-3000/2585-Number-of-Ways-to-Earn-Points/cpp-2585/) | | | +| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/) | [无] | [C++](2501-3000/2586-Count-the-Number-of-Vowel-Strings-in-Range/cpp-2586/) | | | +| 2587 | [Rearrange Array to Maximize Prefix Score](https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/) | [无] | [C++](2501-3000/2587-Rearrange-Array-to-Maximize-Prefix-Score/cpp-2587/) | | | +| 2588 | [Count the Number of Beautiful Subarrays](https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/) | [无] | [C++](2501-3000/2588-Count-the-Number-of-Beautiful-Subarrays/cpp-2588/) | | | +| 2589 | [Minimum Time to Complete All Tasks](https://leetcode.com/problems/minimum-time-to-complete-all-tasks/) | [无] | [C++](2501-3000/2589-Minimum-Time-to-Complete-All-Tasks/cpp-2589/) | | | +| 2590 | [Design a Todo List](https://leetcode.com/problems/design-a-todo-list/description/) | [无] | [C++](2501-3000/2590-Design-a-Todo-List/cpp-2590/) | | | +| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children/) | [无] | [C++](2501-3000/2591-Distribute-Money-to-Maximum-Children/cpp-2591/) | | | +| 2592 | [Maximize Greatness of an Array](https://leetcode.com/problems/maximize-greatness-of-an-array/) | [无] | [C++](2501-3000/2592-Maximize-Greatness-of-an-Array/cpp-2592/) | | | +| 2593 | [Find Score of an Array After Marking All Elements](https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/) | [无] | [C++](2501-3000/2593-Find-Score-of-an-Array-After-Marking-All-Elements/cpp-2593/) | | | +| 2594 | [Minimum Time to Repair Cars](https://leetcode.com/problems/minimum-time-to-repair-cars/) | [无] | [C++](2501-3000/2594-Minimum-Time-to-Repair-Cars/cpp-2594/) | | | +| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits/) | [无] | [C++](2501-3000/2595-Number-of-Even-and-Odd-Bits/cpp-2595/) | | | +| 2596 | [Check Knight Tour Configuration](https://leetcode.com/problems/check-knight-tour-configuration/) | [无] | [C++](2501-3000/2596-Check-Knight-Tour-Configuration/cpp-2596/) | | | +| 2597 | [The Number of Beautiful Subsets](https://leetcode.com/problems/the-number-of-beautiful-subsets/) | [无] | [C++](2501-3000/2597-The-Number-of-Beautiful-Subsets/cpp-2597/) | | | +| 2598 | [Smallest Missing Non-negative Integer After Operations](https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/) | [无] | [C++](2501-3000/2598-Smallest-Missing-Non-negative-Integer-After-Operations/cpp-2598/) | | | +| 2599 | [Make the Prefix Sum Non-negative](https://leetcode.com/problems/make-the-prefix-sum-non-negative/description/) | [无] | [C++](2501-3000/2599-Make-the-Prefix-Sum-Non-negative/cpp-2599/) | | | +| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum/) | [无] | [C++](2501-3000/2600-K-Items-With-the-Maximum-Sum/cpp-2600/) | | | +| 2601 | [Prime Subtraction Operation](https://leetcode.com/problems/prime-subtraction-operation/) | [无] | [C++](2501-3000/2601-Prime-Subtraction-Operation/cpp-2601/) | | | +| 2602 | [Minimum Operations to Make All Array Elements Equal](https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/) | [无] | [C++](2501-3000/2602-Minimum-Operations-to-Make-All-Array-Elements-Equal/cpp-2602/) | | | +| 2603 | [Collect Coins in a Tree](https://leetcode.com/problems/collect-coins-in-a-tree/description/) | [无] | [C++](2501-3000/2603-Collect-Coins-in-a-Tree/cpp-2603/) | | | +| 2604 | [Minimum Time to Eat All Grains](https://leetcode.com/problems/minimum-time-to-eat-all-grains/description/) | [无] | [C++](2501-3000/2604-Minimum-Time-to-Eat-All-Grains/cpp-2604/) | | | +| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays/) | [无] | [C++](2501-3000/2605-Form-Smallest-Number-From-Two-Digit-Arrays/cpp-2605/) | | | +| 2606 | [Find the Substring With Maximum Cost](https://leetcode.com/problems/find-the-substring-with-maximum-cost/) | [无] | [C++](2501-3000/2606-Find-the-Substring-With-Maximum-Cost/cpp-2606/) | | | +| 2607 | [Make K-Subarray Sums Equal](https://leetcode.com/problems/make-k-subarray-sums-equal/) | [无] | [C++](2501-3000/2607-Make-K-Subarray-Sums-Equal/cpp-2607/) | | | +| 2608 | [Shortest Cycle in a Graph](https://leetcode.com/problems/shortest-cycle-in-a-graph/) | [无] | [C++](2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/) | | | +| 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/) | [无] | [C++](2501-3000/2609-Find-the-Longest-Balanced-Substring-of-a-Binary-String/cpp-2609/) | | | +| 2610 | [Convert an Array Into a 2D Array With Conditions](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/) | [无] | [C++](2501-3000/2610-Convert-an-Array-Into-a-2D-Array-With-Conditions/cpp-2610/) | | | +| 2611 | [Mice and Cheese](https://leetcode.com/problems/mice-and-cheese/) | [无] | [C++](2501-3000/2611-Mice-and-Cheese/cpp-2611/) | | | +| 2612 | [Minimum Reverse Operations](https://leetcode.com/problems/minimum-reverse-operations/) | [无] | [C++](2501-3000/2612-Minimum-Reverse-Operations/cpp-2612/) | | | +| | | | | | | +| 2614 | [Prime In Diagonal](https://leetcode.com/problems/prime-in-diagonal/) | [无] | [C++](2501-3000/2614-Prime-In-Diagonal/cpp-2614/) | | | +| 2615 | [Sum of Distances](https://leetcode.com/problems/sum-of-distances/) | [无] | [C++](2501-3000/2615-Sum-of-Distances/cpp-2615/) | | | +| 2616 | [Minimize the Maximum Difference of Pairs](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/) | [无] | [C++](2501-3000/2616-Minimize-the-Maximum-Difference-of-Pairs/cpp-2616/) | | | +| 2617 | [Minimum Number of Visited Cells in a Grid](https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/) | [无] | [C++](2501-3000/2617-Minimum-Number-of-Visited-Cells-in-a-Grid/cpp-2617/) | | | | 2618 | JavaScript Problem | - | - | - | - | | 2619 | JavaScript Problem | - | - | - | - | | 2620 | JavaScript Problem | - | - | - | - | @@ -2539,88 +2539,88 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2635 | JavaScript Problem | - | - | - | - | | 2636 | JavaScript Problem | - | - | - | - | | 2637 | JavaScript Problem | - | - | - | - | -| 2638 | [Count the Number of K-Free Subsets](https://leetcode.com/problems/count-the-number-of-k-free-subsets/description/) | [无] | [C++](2001-2500/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/) | | | -| 2639 | [Find the Width of Columns of a Grid](https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/) | [无] | [C++](2001-2500/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/) | | | -| 2640 | [Find the Score of All Prefixes of an Array](https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/) | [无] | [C++](2001-2500/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/) | | | -| 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [无] | [C++](2001-2500/2641-Cousins-in-Binary-Tree-II/cpp-2641/) | | | -| 2642 | [Design Graph With Shortest Path Calculator](https://leetcode.com/problems/design-graph-with-shortest-path-calculator/) | [无] | [C++](2001-2500/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/) | | | -| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones/) | [无] | [C++](2001-2500/2643-Row-With-Maximum-Ones/cpp-2643/) | | | -| 2644 | [Find the Maximum Divisibility Score](https://leetcode.com/problems/find-the-maximum-divisibility-score/) | [无] | [C++](2001-2500/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/) | | | -| 2645 | [Minimum Additions to Make Valid String](https://leetcode.com/problems/minimum-additions-to-make-valid-string/) | [无] | [C++](2001-2500/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/) | | | -| 2646 | [Minimize the Total Price of the Trips](https://leetcode.com/problems/minimize-the-total-price-of-the-trips/) | [无] | [C++](2001-2500/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/) | | | -| 2647 | [Color the Triangle Red](https://leetcode.com/problems/color-the-triangle-red/description/) | [无] | [C++](2001-2500/2647-Color-the-Triangle-Red/cpp-2647/) | | | +| 2638 | [Count the Number of K-Free Subsets](https://leetcode.com/problems/count-the-number-of-k-free-subsets/description/) | [无] | [C++](2501-3000/2638-Count-the-Number-of-K-Free-Subsets/cpp-2638/) | | | +| 2639 | [Find the Width of Columns of a Grid](https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/) | [无] | [C++](2501-3000/2639-Find-the-Width-of-Columns-of-a-Grid/cpp-2639/) | | | +| 2640 | [Find the Score of All Prefixes of an Array](https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/) | [无] | [C++](2501-3000/2640-Find-the-Score-of-All-Prefixes-of-an-Array/cpp-2640/) | | | +| 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [无] | [C++](2501-3000/2641-Cousins-in-Binary-Tree-II/cpp-2641/) | | | +| 2642 | [Design Graph With Shortest Path Calculator](https://leetcode.com/problems/design-graph-with-shortest-path-calculator/) | [无] | [C++](2501-3000/2642-Design-Graph-With-Shortest-Path-Calculator/cpp-2642/) | | | +| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones/) | [无] | [C++](2501-3000/2643-Row-With-Maximum-Ones/cpp-2643/) | | | +| 2644 | [Find the Maximum Divisibility Score](https://leetcode.com/problems/find-the-maximum-divisibility-score/) | [无] | [C++](2501-3000/2644-Find-the-Maximum-Divisibility-Score/cpp-2644/) | | | +| 2645 | [Minimum Additions to Make Valid String](https://leetcode.com/problems/minimum-additions-to-make-valid-string/) | [无] | [C++](2501-3000/2645-Minimum-Additions-to-Make-Valid-String/cpp-2645/) | | | +| 2646 | [Minimize the Total Price of the Trips](https://leetcode.com/problems/minimize-the-total-price-of-the-trips/) | [无] | [C++](2501-3000/2646-Minimize-the-Total-Price-of-the-Trips/cpp-2646/) | | | +| 2647 | [Color the Triangle Red](https://leetcode.com/problems/color-the-triangle-red/description/) | [无] | [C++](2501-3000/2647-Color-the-Triangle-Red/cpp-2647/) | | | | 2648 | JavaScript Problem | - | - | - | - | | 2649 | JavaScript Problem | - | - | - | - | | 2650 | JavaScript Problem | - | - | - | - | -| 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time/) | [无] | [C++](2001-2500/2651-Calculate-Delayed-Arrival-Time/cpp-2651/) | | | -| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples/) | [无] | [C++](2001-2500/2652-Sum-Multiples/cpp-2652/) | | | -| 2653 | [Sliding Subarray Beauty](https://leetcode.com/problems/sliding-subarray-beauty/) | [无] | [C++](2001-2500/2653-Sliding-Subarray-Beauty/cpp-2653/) | | | -| 2654 | [Minimum Number of Operations to Make All Array Elements Equal to 1](https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/) | [无] | [C++](2001-2500/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/) | | | -| 2655 | [Find Maximal Uncovered Ranges](https://leetcode.com/problems/find-maximal-uncovered-ranges/description/) | [无] | [C++](2001-2500/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/) | | | -| 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/) | [无] | [C++](2001-2500/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/) | | | -| 2657 | [Find the Prefix Common Array of Two Arrays](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [无] | [C++](2001-2500/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/) | | | -| 2658 | [Maximum Number of Fish in a Grid](https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/) | [无] | [C++](2001-2500/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/) | | | -| 2659 | [Make Array Empty](https://leetcode.com/problems/make-array-empty/) | [无] | [C++](2001-2500/2659-Make-Array-Empty/cpp-2659/) | | | -| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game/) | [无] | [C++](2001-2500/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/) | | | -| 2661 | [First Completely Painted Row or Column](https://leetcode.com/problems/first-completely-painted-row-or-column/) | [无] | [C++](2001-2500/2661-First-Completely-Painted-Row-or-Column/cpp-2661/) | | | -| 2662 | [Minimum Cost of a Path With Special Roads](https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/) | [无] | [C++](2001-2500/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/) | | | -| 2663 | [Lexicographically Smallest Beautiful String](https://leetcode.com/problems/lexicographically-smallest-beautiful-string/) | [无] | [C++](2001-2500/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/) | | | -| 2664 | [The Knight’s Tour](https://leetcode.com/problems/the-knights-tour/) | [无] | [C++](2001-2500/2664-The-Knights-Tour/cpp-2664/) | | | +| 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time/) | [无] | [C++](2501-3000/2651-Calculate-Delayed-Arrival-Time/cpp-2651/) | | | +| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples/) | [无] | [C++](2501-3000/2652-Sum-Multiples/cpp-2652/) | | | +| 2653 | [Sliding Subarray Beauty](https://leetcode.com/problems/sliding-subarray-beauty/) | [无] | [C++](2501-3000/2653-Sliding-Subarray-Beauty/cpp-2653/) | | | +| 2654 | [Minimum Number of Operations to Make All Array Elements Equal to 1](https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/) | [无] | [C++](2501-3000/2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1/cpp-2654/) | | | +| 2655 | [Find Maximal Uncovered Ranges](https://leetcode.com/problems/find-maximal-uncovered-ranges/description/) | [无] | [C++](2501-3000/2655-Find-Maximal-Uncovered-Ranges/cpp-2655/) | | | +| 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/) | [无] | [C++](2501-3000/2656-Maximum-Sum-With-Exactly-K-Elements/cpp-2656/) | | | +| 2657 | [Find the Prefix Common Array of Two Arrays](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [无] | [C++](2501-3000/2657-Find-the-Prefix-Common-Array-of-Two-Arrays/cpp-2657/) | | | +| 2658 | [Maximum Number of Fish in a Grid](https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/) | [无] | [C++](2501-3000/2658-Maximum-Number-of-Fish-in-a-Grid/cpp-2658/) | | | +| 2659 | [Make Array Empty](https://leetcode.com/problems/make-array-empty/) | [无] | [C++](2501-3000/2659-Make-Array-Empty/cpp-2659/) | | | +| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game/) | [无] | [C++](2501-3000/2660-Determine-the-Winner-of-a-Bowling-Game/cpp-2660/) | | | +| 2661 | [First Completely Painted Row or Column](https://leetcode.com/problems/first-completely-painted-row-or-column/) | [无] | [C++](2501-3000/2661-First-Completely-Painted-Row-or-Column/cpp-2661/) | | | +| 2662 | [Minimum Cost of a Path With Special Roads](https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/) | [无] | [C++](2501-3000/2662-Minimum-Cost-of-a-Path-With-Special-Roads/cpp-2662/) | | | +| 2663 | [Lexicographically Smallest Beautiful String](https://leetcode.com/problems/lexicographically-smallest-beautiful-string/) | [无] | [C++](2501-3000/2663-Lexicographically-Smallest-Beautiful-String/cpp-2663/) | | | +| 2664 | [The Knight’s Tour](https://leetcode.com/problems/the-knights-tour/) | [无] | [C++](2501-3000/2664-The-Knights-Tour/cpp-2664/) | | | | 2665 | JavaScript Problem | - | - | - | - | | 2666 | JavaScript Problem | - | - | - | - | | 2667 | JavaScript Problem | - | - | - | - | | 2668 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2669 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array/) | [无] | [C++](2001-2500/2670-Find-the-Distinct-Difference-Array/cpp-2670/) | | | -| 2671 | [Frequency Tracker](https://leetcode.com/problems/frequency-tracker/) | [无] | [C++](2001-2500/2671-Frequency-Tracker/cpp-2671/) | | | -| 2672 | [Number of Adjacent Elements With the Same Color](https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/) | [无] | [C++](2001-2500/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/) | | | -| 2673 | [Make Costs of Paths Equal in a Binary Tree](https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/) | [无] | [C++](2001-2500/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/) | | | -| 2674 | [Split a Circular Linked List](https://leetcode.com/problems/split-a-circular-linked-list/description/) | [无] | [C++](2001-2500/2674-Split-a-Circular-Linked-List/cpp-2674/) | | | +| 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array/) | [无] | [C++](2501-3000/2670-Find-the-Distinct-Difference-Array/cpp-2670/) | | | +| 2671 | [Frequency Tracker](https://leetcode.com/problems/frequency-tracker/) | [无] | [C++](2501-3000/2671-Frequency-Tracker/cpp-2671/) | | | +| 2672 | [Number of Adjacent Elements With the Same Color](https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/) | [无] | [C++](2501-3000/2672-Number-of-Adjacent-Elements-With-the-Same-Color/cpp-2672/) | | | +| 2673 | [Make Costs of Paths Equal in a Binary Tree](https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/) | [无] | [C++](2501-3000/2673-Make-Costs-of-Paths-Equal-in-a-Binary-Tree/cpp-2673/) | | | +| 2674 | [Split a Circular Linked List](https://leetcode.com/problems/split-a-circular-linked-list/description/) | [无] | [C++](2501-3000/2674-Split-a-Circular-Linked-List/cpp-2674/) | | | | 2675 | JavaScript Problem | - | - | - | - | | 2676 | JavaScript Problem | - | - | - | - | | 2677 | JavaScript Problem | - | - | - | - | -| 2678 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens/) | [无] | [C++](2001-2500/2678-Number-of-Senior-Citizens/cpp-2678/) | | | -| 2679 | [Sum in a Matrix](https://leetcode.com/problems/sum-in-a-matrix/) | [无] | [C++](2001-2500/2679-Sum-in-a-Matrix/cpp-2679/) | | | -| 2680 | [Maximum OR](https://leetcode.com/problems/maximum-or/) | [无] | [C++](2001-2500/2680-Maximum-OR/cpp-2680/) | | | -| 2681 | [Power of Heroes](https://leetcode.com/problems/power-of-heroes/) | [无] | [C++](2001-2500/2681-Power-of-Heroes/cpp-2681/) | | | -| 2682 | [Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game/) | [无] | [C++](2001-2500/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/) | | | -| 2683 | [Neighboring Bitwise XOR](https://leetcode.com/problems/neighboring-bitwise-xor/) | [无] | [C++](2001-2500/2683-Neighboring-Bitwise-XOR/cpp-2683/) | | | -| 2684 | [Maximum Number of Moves in a Grid](https://leetcode.com/problems/maximum-number-of-moves-in-a-grid/) | [无] | [C++](2001-2500/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/) | | | -| 2685 | [Count the Number of Complete Components](https://leetcode.com/problems/count-the-number-of-complete-components/) | [无] | [C++](2001-2500/2685-Count-the-Number-of-Complete-Components/cpp-2685/) | | | +| 2678 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens/) | [无] | [C++](2501-3000/2678-Number-of-Senior-Citizens/cpp-2678/) | | | +| 2679 | [Sum in a Matrix](https://leetcode.com/problems/sum-in-a-matrix/) | [无] | [C++](2501-3000/2679-Sum-in-a-Matrix/cpp-2679/) | | | +| 2680 | [Maximum OR](https://leetcode.com/problems/maximum-or/) | [无] | [C++](2501-3000/2680-Maximum-OR/cpp-2680/) | | | +| 2681 | [Power of Heroes](https://leetcode.com/problems/power-of-heroes/) | [无] | [C++](2501-3000/2681-Power-of-Heroes/cpp-2681/) | | | +| 2682 | [Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game/) | [无] | [C++](2501-3000/2682-Find-the-Losers-of-the-Circular-Game/cpp-2682/) | | | +| 2683 | [Neighboring Bitwise XOR](https://leetcode.com/problems/neighboring-bitwise-xor/) | [无] | [C++](2501-3000/2683-Neighboring-Bitwise-XOR/cpp-2683/) | | | +| 2684 | [Maximum Number of Moves in a Grid](https://leetcode.com/problems/maximum-number-of-moves-in-a-grid/) | [无] | [C++](2501-3000/2684-Maximum-Number-of-Moves-in-a-Grid/cpp-2684/) | | | +| 2685 | [Count the Number of Complete Components](https://leetcode.com/problems/count-the-number-of-complete-components/) | [无] | [C++](2501-3000/2685-Count-the-Number-of-Complete-Components/cpp-2685/) | | | | 2686 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2687 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2688 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| 2689 | [Extract Kth Character From The Rope Tree](https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/) | [无] | [C++](2001-2500/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/) | | | +| 2689 | [Extract Kth Character From The Rope Tree](https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/) | [无] | [C++](2501-3000/2689-Extract-Kth-Character-From-The-Rope-Tree/cpp-2689/) | | | | 2690 | JavaScript Problem | - | - | - | - | | 2691 | JavaScript Problem | - | - | - | - | | 2692 | JavaScript Problem | - | - | - | - | | 2693 | JavaScript Problem | - | - | - | - | | 2694 | JavaScript Problem | - | - | - | - | | 2695 | JavaScript Problem | - | - | - | - | -| 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings/) | [无] | [C++](2001-2500/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/) | | | -| 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome/) | [无] | [C++](2001-2500/2697-Lexicographically-Smallest-Palindrome/cpp-2697/) | | | -| 2698 | [Find the Punishment Number of an Integer](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | [无] | [C++](2001-2500/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/) | | | -| 2699 | [Modify Graph Edge Weights](https://leetcode.com/problems/modify-graph-edge-weights/) | [无] | [C++](2001-2500/2699-Modify-Graph-Edge-Weights/cpp-2699/) | | | +| 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings/) | [无] | [C++](2501-3000/2696-Minimum-String-Length-After-Removing-Substrings/cpp-2696/) | | | +| 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome/) | [无] | [C++](2501-3000/2697-Lexicographically-Smallest-Palindrome/cpp-2697/) | | | +| 2698 | [Find the Punishment Number of an Integer](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | [无] | [C++](2501-3000/2698-Find-the-Punishment-Number-of-an-Integer/cpp-2698/) | | | +| 2699 | [Modify Graph Edge Weights](https://leetcode.com/problems/modify-graph-edge-weights/) | [无] | [C++](2501-3000/2699-Modify-Graph-Edge-Weights/cpp-2699/) | | | | 2700 | JavaScript Problem | - | - | - | - | | 2701 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| 2702 | [Minimum Operations to Make Numbers Non-positive](https://leetcode.com/problems/minimum-operations-to-make-numbers-non-positive/description/) | [无] | [C++](2001-2500/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/) | | | +| 2702 | [Minimum Operations to Make Numbers Non-positive](https://leetcode.com/problems/minimum-operations-to-make-numbers-non-positive/description/) | [无] | [C++](2501-3000/2702-Minimum-Operations-to-Make-Numbers-Non-positive/cpp-2702/) | | | | 2703 | JavaScript Problem | - | - | - | - | | 2704 | JavaScript Problem | - | - | - | - | | 2705 | JavaScript Problem | - | - | - | - | -| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates/) | [无] | [C++](2001-2500/2706-Buy-Two-Chocolates/cpp-2706/) | | | -| 2707 | [Extra Characters in a String](https://leetcode.com/problems/extra-characters-in-a-string/) | [无] | [C++](2001-2500/2707-Extra-Characters-in-a-String/cpp-2707/) | | | -| 2708 | [Maximum Strength of a Group](https://leetcode.com/problems/maximum-strength-of-a-group/) | [无] | [C++](2001-2500/2708-Maximum-Strength-of-a-Group/cpp-2708/) | | | -| 2709 | [Greatest Common Divisor Traversal](https://leetcode.com/problems/greatest-common-divisor-traversal/) | [无] | [C++](2001-2500/2709-Greatest-Common-Divisor-Traversal/cpp-2709/) | | | -| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string/) | [无] | [C++](2001-2500/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/) | | | -| 2711 | [Difference of Number of Distinct Values on Diagonals](https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/) | [无] | [C++](2001-2500/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/) | | | -| 2712 | [Minimum Cost to Make All Characters Equal](https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/) | [无] | [C++](2001-2500/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/) | | | -| 2713 | [Maximum Strictly Increasing Cells in a Matrix](https://leetcode.com/problems/maximum-strictly-increasing-cells-in-a-matrix/) | [无] | [C++](2001-2500/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/) | | | -| 2714 | [Find Shortest Path with K Hops](https://leetcode.com/problems/find-shortest-path-with-k-hops/description/) | [无] | [C++](2001-2500/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/) | | | +| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates/) | [无] | [C++](2501-3000/2706-Buy-Two-Chocolates/cpp-2706/) | | | +| 2707 | [Extra Characters in a String](https://leetcode.com/problems/extra-characters-in-a-string/) | [无] | [C++](2501-3000/2707-Extra-Characters-in-a-String/cpp-2707/) | | | +| 2708 | [Maximum Strength of a Group](https://leetcode.com/problems/maximum-strength-of-a-group/) | [无] | [C++](2501-3000/2708-Maximum-Strength-of-a-Group/cpp-2708/) | | | +| 2709 | [Greatest Common Divisor Traversal](https://leetcode.com/problems/greatest-common-divisor-traversal/) | [无] | [C++](2501-3000/2709-Greatest-Common-Divisor-Traversal/cpp-2709/) | | | +| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string/) | [无] | [C++](2501-3000/2710-Remove-Trailing-Zeros-From-a-String/cpp-2710/) | | | +| 2711 | [Difference of Number of Distinct Values on Diagonals](https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/) | [无] | [C++](2501-3000/2711-Difference-of-Number-of-Distinct-Values-on-Diagonals/cpp-2711/) | | | +| 2712 | [Minimum Cost to Make All Characters Equal](https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/) | [无] | [C++](2501-3000/2712-Minimum-Cost-to-Make-All-Characters-Equal/cpp-2712/) | | | +| 2713 | [Maximum Strictly Increasing Cells in a Matrix](https://leetcode.com/problems/maximum-strictly-increasing-cells-in-a-matrix/) | [无] | [C++](2501-3000/2713-Maximum-Strictly-Increasing-Cells-in-a-Matrix/cpp-2713/) | | | +| 2714 | [Find Shortest Path with K Hops](https://leetcode.com/problems/find-shortest-path-with-k-hops/description/) | [无] | [C++](2501-3000/2714-Find-Shortest-Path-with-K-Hops/cpp-2714/) | | | | 2715 | JavaScript Problem | - | - | - | - | -| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length/) | [无] | [C++](2001-2500/2716-Minimize-String-Length/cpp-2716/) | | | -| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation/) | [无] | [C++](2001-2500/2717-Semi-Ordered-Permutation/cpp-2717/) | | | -| 2718 | [Sum of Matrix After Queries](https://leetcode.com/problems/sum-of-matrix-after-queries/) | [无] | [C++](2001-2500/2718-Sum-of-Matrix-After-Queries/cpp-2718/) | | | -| 2719 | [Count of Integers](https://leetcode.com/problems/count-of-integers/) | [无] | [C++](2001-2500/2719-Count-of-Integers/cpp-2719/) | | | +| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length/) | [无] | [C++](2501-3000/2716-Minimize-String-Length/cpp-2716/) | | | +| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation/) | [无] | [C++](2501-3000/2717-Semi-Ordered-Permutation/cpp-2717/) | | | +| 2718 | [Sum of Matrix After Queries](https://leetcode.com/problems/sum-of-matrix-after-queries/) | [无] | [C++](2501-3000/2718-Sum-of-Matrix-After-Queries/cpp-2718/) | | | +| 2719 | [Count of Integers](https://leetcode.com/problems/count-of-integers/) | [无] | [C++](2501-3000/2719-Count-of-Integers/cpp-2719/) | | | | 2720 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | 2721 | JavaScript Problem | - | - | - | - | | 2722 | JavaScript Problem | - | - | - | - | @@ -2629,30 +2629,30 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2725 | JavaScript Problem | - | - | - | - | | 2726 | JavaScript Problem | - | - | - | - | | 2727 | JavaScript Problem | - | - | - | - | -| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/description/) | [无] | [C++](2001-2500/2728-Count-Houses-in-a-Circular-Street/cpp-2728/) | | | -| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating/) | [无] | [C++](2001-2500/2729-Check-if-The-Number-is-Fascinating/cpp-2729/) | | | -| 2730 | [Find the Longest Semi-Repetitive Substring](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/) | [无] | [C++](2001-2500/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/) | | | -| 2731 | [Movement of Robots](https://leetcode.com/problems/movement-of-robots/) | [无] | [C++](2001-2500/2731-Movement-of-Robots/cpp-2731/) | | | -| 2732 | [Find a Good Subset of the Matrix](https://leetcode.com/problems/find-a-good-subset-of-the-matrix/) | [无] | [C++](2001-2500/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/) | | | -| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum/) | [无] | [C++](2001-2500/2733-Neither-Minimum-nor-Maximum/cpp-2733/) | | | -| 2734 | [Lexicographically Smallest String After Substring Operation](https://leetcode.com/problems/lexicographically-smallest-string-after-substring-operation/) | [无] | [C++](2001-2500/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/) | | | -| 2735 | [Collecting Chocolates](https://leetcode.com/problems/collecting-chocolates/) | [无] | [C++](2001-2500/2735-Collecting-Chocolates/cpp-2735/) | | | -| 2736 | [Maximum Sum Queries](https://leetcode.com/problems/maximum-sum-queries/) | [无] | [C++](2001-2500/2736-Maximum-Sum-Queries/cpp-2736/) | | | -| 2737 | [Find the Closest Marked Node](https://leetcode.com/problems/find-the-closest-marked-node/) | [无] | [C++](2001-2500/2737-Find-the-Closest-Marked-Node/cpp-2737/) | | | +| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/description/) | [无] | [C++](2501-3000/2728-Count-Houses-in-a-Circular-Street/cpp-2728/) | | | +| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating/) | [无] | [C++](2501-3000/2729-Check-if-The-Number-is-Fascinating/cpp-2729/) | | | +| 2730 | [Find the Longest Semi-Repetitive Substring](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/) | [无] | [C++](2501-3000/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/) | | | +| 2731 | [Movement of Robots](https://leetcode.com/problems/movement-of-robots/) | [无] | [C++](2501-3000/2731-Movement-of-Robots/cpp-2731/) | | | +| 2732 | [Find a Good Subset of the Matrix](https://leetcode.com/problems/find-a-good-subset-of-the-matrix/) | [无] | [C++](2501-3000/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/) | | | +| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum/) | [无] | [C++](2501-3000/2733-Neither-Minimum-nor-Maximum/cpp-2733/) | | | +| 2734 | [Lexicographically Smallest String After Substring Operation](https://leetcode.com/problems/lexicographically-smallest-string-after-substring-operation/) | [无] | [C++](2501-3000/2734-Lexicographically-Smallest-String-After-Substring-Operation/cpp-2734/) | | | +| 2735 | [Collecting Chocolates](https://leetcode.com/problems/collecting-chocolates/) | [无] | [C++](2501-3000/2735-Collecting-Chocolates/cpp-2735/) | | | +| 2736 | [Maximum Sum Queries](https://leetcode.com/problems/maximum-sum-queries/) | [无] | [C++](2501-3000/2736-Maximum-Sum-Queries/cpp-2736/) | | | +| 2737 | [Find the Closest Marked Node](https://leetcode.com/problems/find-the-closest-marked-node/) | [无] | [C++](2501-3000/2737-Find-the-Closest-Marked-Node/cpp-2737/) | | | | 2738 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | -| 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled/) | [无] | [C++](2001-2500/2739-Total-Distance-Traveled/cpp-2739/) | | | -| 2740 | [Find the Value of the Partition](https://leetcode.com/problems/find-the-value-of-the-partition/) | [无] | [C++](2001-2500/2740-Find-the-Value-of-the-Partition/cpp-2740/) | | | -| 2741 | [Special Permutations](https://leetcode.com/problems/special-permutations/) | [无] | [C++](2001-2500/2741-Special-Permutations/cpp-2741/) | | | -| 2742 | [Painting the Walls](https://leetcode.com/problems/painting-the-walls/) | [无] | [C++](2001-2500/2742-Painting-the-Walls/cpp-2742/) | | | -| 2743 | [Count Substrings Without Repeating Character](https://leetcode.com/problems/count-substrings-without-repeating-character/description/) | [无] | [C++](2001-2500/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/) | | | -| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs/) | [无] | [C++](2001-2500/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/) | | | -| 2745 | [Construct the Longest New String](https://leetcode.com/problems/construct-the-longest-new-string/) | [无] | [C++](2001-2500/2745-Construct-the-Longest-New-String/cpp-2745/) | | | -| 2746 | [Decremental String Concatenation](https://leetcode.com/problems/decremental-string-concatenation/) | [无] | [C++](2001-2500/2746-Decremental-String-Concatenation/cpp-2746/) | | | -| 2747 | [Count Zero Request Servers](https://leetcode.com/problems/count-zero-request-servers/) | [无] | [C++](2001-2500/2747-Count-Zero-Request-Servers/cpp-2747/) | | | -| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs/) | [无] | [C++](2001-2500/2748-Number-of-Beautiful-Pairs/cpp-2748/) | | | -| 2749 | [Minimum Operations to Make the Integer Zero](https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero/) | [无] | [C++](2001-2500/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/) | | | -| 2750 | [Ways to Split Array Into Good Subarrays](https://leetcode.com/problems/ways-to-split-array-into-good-subarrays/) | [无] | [C++](2001-2500/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/) | | | -| 2751 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [无] | [C++](2001-2500/2751-Robot-Collisions/cpp-2751/) | | | +| 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled/) | [无] | [C++](2501-3000/2739-Total-Distance-Traveled/cpp-2739/) | | | +| 2740 | [Find the Value of the Partition](https://leetcode.com/problems/find-the-value-of-the-partition/) | [无] | [C++](2501-3000/2740-Find-the-Value-of-the-Partition/cpp-2740/) | | | +| 2741 | [Special Permutations](https://leetcode.com/problems/special-permutations/) | [无] | [C++](2501-3000/2741-Special-Permutations/cpp-2741/) | | | +| 2742 | [Painting the Walls](https://leetcode.com/problems/painting-the-walls/) | [无] | [C++](2501-3000/2742-Painting-the-Walls/cpp-2742/) | | | +| 2743 | [Count Substrings Without Repeating Character](https://leetcode.com/problems/count-substrings-without-repeating-character/description/) | [无] | [C++](2501-3000/2743-Count-Substrings-Without-Repeating-Character/cpp-2743/) | | | +| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs/) | [无] | [C++](2501-3000/2744-Find-Maximum-Number-of-String-Pairs/cpp-2744/) | | | +| 2745 | [Construct the Longest New String](https://leetcode.com/problems/construct-the-longest-new-string/) | [无] | [C++](2501-3000/2745-Construct-the-Longest-New-String/cpp-2745/) | | | +| 2746 | [Decremental String Concatenation](https://leetcode.com/problems/decremental-string-concatenation/) | [无] | [C++](2501-3000/2746-Decremental-String-Concatenation/cpp-2746/) | | | +| 2747 | [Count Zero Request Servers](https://leetcode.com/problems/count-zero-request-servers/) | [无] | [C++](2501-3000/2747-Count-Zero-Request-Servers/cpp-2747/) | | | +| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs/) | [无] | [C++](2501-3000/2748-Number-of-Beautiful-Pairs/cpp-2748/) | | | +| 2749 | [Minimum Operations to Make the Integer Zero](https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero/) | [无] | [C++](2501-3000/2749-Minimum-Operations-to-Make-the-Integer-Zero/cpp-2749/) | | | +| 2750 | [Ways to Split Array Into Good Subarrays](https://leetcode.com/problems/ways-to-split-array-into-good-subarrays/) | [无] | [C++](2501-3000/2750-Ways-to-Split-Array-Into-Good-Subarrays/cpp-2750/) | | | +| 2751 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [无] | [C++](2501-3000/2751-Robot-Collisions/cpp-2751/) | | | | 2752 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 2754 | JavaScript Problem | - | - | - | - | @@ -2661,10 +2661,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2757 | JavaScript Problem | - | - | - | - | | 2758 | JavaScript Problem | - | - | - | - | | 2759 | JavaScript Problem | - | - | - | - | -| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/) | [无] | [C++](2001-2500/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/) | | | -| 2761 | [Prime Pairs With Target Sum](https://leetcode.com/problems/prime-pairs-with-target-sum/) | [无] | [C++](2001-2500/2761-Prime-Pairs-With-Target-Sum/cpp-2761/) | | | -| 2762 | [Continuous Subarrays](https://leetcode.com/problems/continuous-subarrays/) | [无] | [C++](2001-2500/2762-Continuous-Subarrays/cpp-2762/) | | | -| 2763 | [Sum of Imbalance Numbers of All Subarrays](https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/) | [无] | [C++](2001-2500/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/) | | | +| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/) | [无] | [C++](2501-3000/2760-Longest-Even-Odd-Subarray-With-Threshold/cpp-2760/) | | | +| 2761 | [Prime Pairs With Target Sum](https://leetcode.com/problems/prime-pairs-with-target-sum/) | [无] | [C++](2501-3000/2761-Prime-Pairs-With-Target-Sum/cpp-2761/) | | | +| 2762 | [Continuous Subarrays](https://leetcode.com/problems/continuous-subarrays/) | [无] | [C++](2501-3000/2762-Continuous-Subarrays/cpp-2762/) | | | +| 2763 | [Sum of Imbalance Numbers of All Subarrays](https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/) | [无] | [C++](2501-3000/2763-Sum-of-Imbalance-Numbers-of-All-Subarrays/cpp-2763/) | | | +| | | | | | | +| 2849 | [Determine if a Cell Is Reachable at a Given Time](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/) | [无] | [C++](2501-3000/2849-Determine-if-a-Cell-Is-Reachable-at-a-Given-Time/cpp-2849/) | | | | | | | | | | ### 力扣中文站比赛 [传送门](LC/)