diff --git a/0001-0500/0006-Zigzag-Conversion/cpp-0006/CMakeLists.txt b/0001-0500/0006-Zigzag-Conversion/cpp-0006/CMakeLists.txt new file mode 100644 index 00000000..f39e1e37 --- /dev/null +++ b/0001-0500/0006-Zigzag-Conversion/cpp-0006/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0006) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0006 main.cpp) diff --git a/0001-0500/0006-Zigzag-Conversion/cpp-0006/main.cpp b/0001-0500/0006-Zigzag-Conversion/cpp-0006/main.cpp new file mode 100644 index 00000000..daf19aa8 --- /dev/null +++ b/0001-0500/0006-Zigzag-Conversion/cpp-0006/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/zigzag-conversion/ +/// Author : liuyubobobo +/// Time : 2022-02-28 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|s|) +/// Space Complexity: O(numRows * |s|) +class Solution { +public: + string convert(string s, int numRows) { + + if(numRows == 1) return s; + + int n = s.size(); + + vector res(numRows, string(n, '-')); + int index = 0, cury = 0; + while(index < n){ + for(int k = 0; k < numRows && index < n; k ++) + res[k][cury] = s[index ++]; + if(index >= n) break; + + for(int k = numRows - 2, y = cury + 1; k > 0 && index < n; k --, y ++) + res[k][y] = s[index ++]; + if(index >= n) break; + + cury += (numRows - 1); + } + + string ret = ""; + for(int i = 0; i < numRows; i ++) + for(char c: res[i]) if(c != '-') ret += c; + return ret; + } +}; + + +int main() { + + cout << Solution().convert("PAYPALISHIRING", 3) << endl; + // "PAHNAPLSIIGYIR" + + return 0; +} diff --git a/0001-0500/0008-String-to-Integer/cpp-0008/CMakeLists.txt b/0001-0500/0008-String-to-Integer/cpp-0008/CMakeLists.txt new file mode 100644 index 00000000..a50225c0 --- /dev/null +++ b/0001-0500/0008-String-to-Integer/cpp-0008/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0008) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0008 main.cpp) diff --git a/0001-0500/0008-String-to-Integer/cpp-0008/main.cpp b/0001-0500/0008-String-to-Integer/cpp-0008/main.cpp new file mode 100644 index 00000000..25943b94 --- /dev/null +++ b/0001-0500/0008-String-to-Integer/cpp-0008/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/string-to-integer-atoi/ +/// Author : liuyubobobo +/// Time : 2022-01-13 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int myAtoi(string s) { + + int i; + for(i = 0; i < s.size() && s[i] == ' '; i ++); + + if(i == s.size()) return 0; + + bool pos = true; + if(s[i] == '-') pos = false, i ++; + else if(s[i] == '+') i ++; + + int j = i; + for(j = i; j < s.size() && isdigit(s[j]); j ++); + + long long num = get_num(s.substr(i, j - i)); + if(pos) return min(num, (long long)INT_MAX); + return max(-num, (long long)INT_MIN); + } + +private: + long long get_num(const string& s){ + + long long res = 0; + for(char c: s){ + res = res * 10 + (c - '0'); + if(res > INT_MAX) return res; + } + return res; + } +}; + + +int main() { + + cout << Solution().myAtoi("42") << endl; + // 42 + + cout << Solution().myAtoi(" -42") << endl; + // -42 + + cout << Solution().myAtoi("4193 with words") << endl; + // 4193 + + cout << Solution().myAtoi("+1") << endl; + // 1 + + return 0; +} 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; 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/0001-0500/0043-Multiply-Strings/cpp-0043/CMakeLists.txt b/0001-0500/0043-Multiply-Strings/cpp-0043/CMakeLists.txt new file mode 100644 index 00000000..3f795255 --- /dev/null +++ b/0001-0500/0043-Multiply-Strings/cpp-0043/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0043) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0043 main.cpp) diff --git a/0001-0500/0043-Multiply-Strings/cpp-0043/main.cpp b/0001-0500/0043-Multiply-Strings/cpp-0043/main.cpp new file mode 100644 index 00000000..8ac8d3b2 --- /dev/null +++ b/0001-0500/0043-Multiply-Strings/cpp-0043/main.cpp @@ -0,0 +1,79 @@ +/// Source : https://leetcode.com/problems/multiply-strings/ +/// Author : liuyubobobo +/// Time : 2021-11-07 + +#include +#include + +using namespace std; + + +/// Simulation +class Solution { +public: + string multiply(string num1, string num2) { + + if(num1 == "0" || num2 == "0") return "0"; + + vector v1, v2; + for(int i = num1.size() - 1; i >= 0; i --) v1.push_back(num1[i] - '0'); + for(int i = num2.size() - 1; i >= 0; i --) v2.push_back(num2[i] - '0'); + + vector res = {0}; + for(int i = 0; i < v1.size(); i ++){ + vector tres = mul(v2, v1[i]); + reverse(tres.begin(), tres.end()); + for(int j = 0; j < i; j ++) tres.push_back(0); + reverse(tres.begin(), tres.end()); + + add(res, tres); + } + + while(res.back() == 0) res.pop_back(); + assert(res.size()); + + string s = ""; + for(int i = res.size() - 1; i >= 0; i --) s += string(1, '0' + res[i]); + return s; + } + +private: + void add(vector& res, vector& tres){ + + while(res.size() < tres.size()) res.push_back(0); + while(tres.size() < res.size()) tres.push_back(0); + + int carry = 0; + for(int i = 0; i < tres.size(); i ++){ + int t = res[i] + tres[i] + carry; + res[i] = t % 10; + carry = t >= 10; + } + if(carry) res.push_back(1); + } + + vector mul(const vector& v, int m){ + assert(0 <= m && m < 10); + + vector res; + int carry = 0; + for(int i = 0; i < v.size(); i ++){ + res.push_back((v[i] * m + carry) % 10); + carry = (v[i] * m + carry) / 10; + } + if(carry) res.push_back(carry); + return res; + } +}; + + +int main() { + + cout << Solution().multiply("2", "3") << endl; + // 6 + + cout << Solution().multiply("123", "456") << endl; + // 56088 + + return 0; +} 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/0001-0500/0069-Sqrt-x/cpp-0069/CMakeLists.txt b/0001-0500/0069-Sqrt-x/cpp-0069/CMakeLists.txt index 11bce44c..ea176d8e 100644 --- a/0001-0500/0069-Sqrt-x/cpp-0069/CMakeLists.txt +++ b/0001-0500/0069-Sqrt-x/cpp-0069/CMakeLists.txt @@ -3,5 +3,5 @@ project(cpp_0069) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main2.cpp) +set(SOURCE_FILES main3.cpp) add_executable(cpp_0069 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0001-0500/0069-Sqrt-x/cpp-0069/main3.cpp b/0001-0500/0069-Sqrt-x/cpp-0069/main3.cpp new file mode 100644 index 00000000..0f341e12 --- /dev/null +++ b/0001-0500/0069-Sqrt-x/cpp-0069/main3.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/sqrtx/description/ +/// Author : liuyubobobo +/// Time : 2022-03-16 + +#include + +using namespace std; + + +/// Taylor Series Expansion +/// Time Complexity: iterative count +/// Space Complexity: O(1) +class Solution { + +private: + double e = 1e-5; + +public: + int mySqrt(int x) { + + if(!x) return x; + + double a = x / 2.0; + while(true){ + double next_a = a - (a * a - (double)x) / (2.0 * a); + if(abs(next_a - a) < e) break; + a = next_a; + } + return (int)a; + } +}; + + +int main() { + + cout << Solution().mySqrt(4) << endl; + cout << Solution().mySqrt(8) << endl; + + return 0; +} \ No newline at end of file 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 diff --git a/0001-0500/0133-Clone-Graph/cpp-0133/main.cpp b/0001-0500/0133-Clone-Graph/cpp-0133/main.cpp index 10890562..fe1febcf 100644 --- a/0001-0500/0133-Clone-Graph/cpp-0133/main.cpp +++ b/0001-0500/0133-Clone-Graph/cpp-0133/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/clone-graph/description/ /// Author : liuyubobobo /// Time : 2018-08-30 +/// Updated: 2022-02-22 #include #include @@ -10,10 +11,22 @@ using namespace std; /// Definition for undirected graph. -struct UndirectedGraphNode { - int label; - vector neighbors; - UndirectedGraphNode(int x) : label(x) {}; +class Node { +public: + int val; + vector neighbors; + Node() { + val = 0; + neighbors = vector(); + } + Node(int _val) { + val = _val; + neighbors = vector(); + } + Node(int _val, vector _neighbors) { + val = _val; + neighbors = _neighbors; + } }; /// Using Two Stacks and HashMap from label to Node @@ -21,35 +34,35 @@ struct UndirectedGraphNode { /// Space Complexity: O(V) class Solution { public: - UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { + Node *cloneGraph(Node *node) { if(node == NULL) return NULL; - UndirectedGraphNode* ret = new UndirectedGraphNode(node->label); + Node* ret = new Node(node->val); - stack stack1; + stack stack1; stack1.push(node); - unordered_map visited; - visited[ret->label] = ret; + unordered_map visited; + visited[ret->val] = ret; - stack stack2; + stack stack2; stack2.push(ret); while(!stack1.empty()){ - UndirectedGraphNode* old_cur = stack1.top(); + Node* old_cur = stack1.top(); stack1.pop(); - UndirectedGraphNode* new_cur = stack2.top(); + Node* new_cur = stack2.top(); stack2.pop(); - for(UndirectedGraphNode *next: old_cur->neighbors) { - if (visited.find(next->label) == visited.end()) { - visited[next->label] = new UndirectedGraphNode(next->label); + for(Node *next: old_cur->neighbors) { + if (visited.find(next->val) == visited.end()) { + visited[next->val] = new Node(next->val); stack1.push(next); - stack2.push(visited[next->label]); + stack2.push(visited[next->val]); } - new_cur->neighbors.push_back(visited[next->label]); + new_cur->neighbors.push_back(visited[next->val]); } } return ret; diff --git a/0001-0500/0133-Clone-Graph/cpp-0133/main2.cpp b/0001-0500/0133-Clone-Graph/cpp-0133/main2.cpp index 7010b949..79e81058 100644 --- a/0001-0500/0133-Clone-Graph/cpp-0133/main2.cpp +++ b/0001-0500/0133-Clone-Graph/cpp-0133/main2.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/clone-graph/description/ /// Author : liuyubobobo /// Time : 2018-09-14 +/// Updated: 2022-02-22 #include #include @@ -10,10 +11,22 @@ using namespace std; /// Definition for undirected graph. -struct UndirectedGraphNode { - int label; - vector neighbors; - UndirectedGraphNode(int x) : label(x) {}; +class Node { +public: + int val; + vector neighbors; + Node() { + val = 0; + neighbors = vector(); + } + Node(int _val) { + val = _val; + neighbors = vector(); + } + Node(int _val, vector _neighbors) { + val = _val; + neighbors = _neighbors; + } }; /// Using Only One Stacks and HashMap from Node to Node @@ -21,22 +34,22 @@ struct UndirectedGraphNode { /// Space Complexity: O(V) class Solution { public: - UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { + Node *cloneGraph(Node *node) { if(node == NULL) return NULL; - UndirectedGraphNode* ret = new UndirectedGraphNode(node->label); - stack stack; + Node* ret = new Node(node->val); + stack stack; stack.push(node); - unordered_map nodeMap; + unordered_map nodeMap; nodeMap[node] = ret; while(!stack.empty()){ - UndirectedGraphNode* cur = stack.top(); + Node* cur = stack.top(); stack.pop(); - for(UndirectedGraphNode *next: cur->neighbors) { + for(Node *next: cur->neighbors) { if (!nodeMap.count(next)) { - nodeMap[next] = new UndirectedGraphNode(next->label); + nodeMap[next] = new Node(next->val); stack.push(next); } nodeMap[cur]->neighbors.push_back(nodeMap[next]); diff --git a/0001-0500/0133-Clone-Graph/cpp-0133/main3.cpp b/0001-0500/0133-Clone-Graph/cpp-0133/main3.cpp index 56f106c2..4b953d0c 100644 --- a/0001-0500/0133-Clone-Graph/cpp-0133/main3.cpp +++ b/0001-0500/0133-Clone-Graph/cpp-0133/main3.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/clone-graph/description/ /// Author : liuyubobobo /// Time : 2018-09-14 +/// Updated: 2022-02-22 #include #include @@ -9,10 +10,22 @@ using namespace std; /// Definition for undirected graph. -struct UndirectedGraphNode { - int label; - vector neighbors; - UndirectedGraphNode(int x) : label(x) {}; +class Node { +public: + int val; + vector neighbors; + Node() { + val = 0; + neighbors = vector(); + } + Node(int _val) { + val = _val; + neighbors = vector(); + } + Node(int _val, vector _neighbors) { + val = _val; + neighbors = _neighbors; + } }; /// DFS @@ -21,24 +34,23 @@ struct UndirectedGraphNode { class Solution { public: - UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { + Node *cloneGraph(Node *node) { if(node == NULL) return NULL; - unordered_map nodeMap; + unordered_map nodeMap; return dfs(node, nodeMap); } private: - UndirectedGraphNode *dfs(UndirectedGraphNode *node, - unordered_map& nodeMap){ + Node *dfs(Node *node, unordered_map& nodeMap){ if(nodeMap.count(node)) return nodeMap[node]; - nodeMap[node] = new UndirectedGraphNode(node->label); - for(UndirectedGraphNode* next: node->neighbors) + nodeMap[node] = new Node(node->val); + for(Node* next: node->neighbors) nodeMap[node]->neighbors.push_back(dfs(next, nodeMap)); return nodeMap[node]; } diff --git a/0001-0500/0146-LRU-Cache/java-0146/src/LRUCache.java b/0001-0500/0146-LRU-Cache/java-0146/src/LRUCache.java new file mode 100644 index 00000000..0da00d4a --- /dev/null +++ b/0001-0500/0146-LRU-Cache/java-0146/src/LRUCache.java @@ -0,0 +1,79 @@ +/// Source : https://leetcode.com/problems/lru-cache/ +/// Author : liuyubobobo +/// Time : 2021-11-24 + +import java.util.*; + + +/// Using Time Stamp +/// Time Complexity: init: O(1) +/// get: O(logn) +/// put: O(logn) +/// Space Complexity: O(n) +public class LRUCache { + + private int timestamp; + private int cap; + + private HashMap data; + private HashMap key2timestamp; + private TreeMap timestamp2key; + + public LRUCache(int capacity) { + + timestamp = 0; + cap = capacity; + + data = new HashMap<>(); + key2timestamp = new HashMap<>(); + timestamp2key = new TreeMap<>(); + } + + public int get(int key) { + + if(data.containsKey(key)){ + int oldTimestamp = key2timestamp.get(key); + timestamp2key.remove(oldTimestamp); + + key2timestamp.put(key, timestamp); + timestamp2key.put(timestamp, key); + timestamp ++; + + return data.get(key); + } + return -1; + } + + public void put(int key, int value) { + + if(data.containsKey(key)){ + int oldTimestamp = key2timestamp.get(key); + timestamp2key.remove(oldTimestamp); + } + + data.put(key, value); + + key2timestamp.put(key, timestamp); + timestamp2key.put(timestamp, key); + timestamp ++; + + if(data.size() > cap){ + int removeTimestamp = timestamp2key.firstKey(); + int removeKey = timestamp2key.get(removeTimestamp); + + data.remove(removeKey); + key2timestamp.remove(removeKey); + timestamp2key.remove(removeTimestamp); + } + } + + public static void main(String[] args) { + + LRUCache lru = new LRUCache(2); + lru.put(1, 1); + lru.put(2, 2); + System.out.println(lru.get(1)); // 1 + lru.put(3, 3); + System.out.println(lru.get(2)); // -1 + } +} 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; } diff --git a/0001-0500/0172-Factorial-Trailing-Zeroes/cpp-0172/CMakeLists.txt b/0001-0500/0172-Factorial-Trailing-Zeroes/cpp-0172/CMakeLists.txt new file mode 100644 index 00000000..9e142993 --- /dev/null +++ b/0001-0500/0172-Factorial-Trailing-Zeroes/cpp-0172/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0172) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0172 main.cpp) diff --git a/0001-0500/0172-Factorial-Trailing-Zeroes/cpp-0172/main.cpp b/0001-0500/0172-Factorial-Trailing-Zeroes/cpp-0172/main.cpp new file mode 100644 index 00000000..7c10ed51 --- /dev/null +++ b/0001-0500/0172-Factorial-Trailing-Zeroes/cpp-0172/main.cpp @@ -0,0 +1,26 @@ +/// Source : https://leetcode.com/problems/factorial-trailing-zeroes/ +/// Author : liuyubobobo +/// Time : 2022-03-24 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + int trailingZeroes(int n) { + int res = 0; + for(int p = 5; p <= n; p *= 5) res += n / p; + return res; + } +}; + + +int main() { + + return 0; +} 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; } }; 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 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..b97f26cc --- /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 + 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 +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) { + + Deque nodeStack = new LinkedList<>(); + + nodeStack.push(root); + while(!nodeStack.isEmpty()){ + Node node = nodeStack.poll(); + 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 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; 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 diff --git a/0001-0500/0229-Majority-Element-II/cpp-0229/CMakeLists.txt b/0001-0500/0229-Majority-Element-II/cpp-0229/CMakeLists.txt new file mode 100644 index 00000000..5a22d45d --- /dev/null +++ b/0001-0500/0229-Majority-Element-II/cpp-0229/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0229) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0229 main.cpp) diff --git a/0001-0500/0229-Majority-Element-II/cpp-0229/main.cpp b/0001-0500/0229-Majority-Element-II/cpp-0229/main.cpp new file mode 100644 index 00000000..f9d31e88 --- /dev/null +++ b/0001-0500/0229-Majority-Element-II/cpp-0229/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/majority-element-ii/ +/// Author : liuyubobobo +/// Time : 2021-10-21 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + vector majorityElement(vector& nums) { + + vector res; + + int n = nums.size(); + sort(nums.begin(), nums.end()); + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || nums[i] != nums[start]){ + if(i - start > n / 3) res.push_back(nums[start]); + start = i; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/CMakeLists.txt b/0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/CMakeLists.txt new file mode 100644 index 00000000..3efcc2cd --- /dev/null +++ b/0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0238) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0238 main.cpp) diff --git a/0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/main.cpp b/0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/main.cpp new file mode 100644 index 00000000..1311668e --- /dev/null +++ b/0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/product-of-array-except-self/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Pre + Suf +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector productExceptSelf(vector& nums) { + + int n = nums.size(); + + vector pre(n, nums[0]); + for(int i = 1; i < n; i ++) pre[i] = pre[i - 1] * nums[i]; + + vector suf(n, nums.back()); + for(int i = n - 2; i >= 0; i --) suf[i] = suf[i + 1] * nums[i]; + + vector res(n); + res[0] = suf[1]; res[n - 1] = pre[n - 2]; + for(int i = 1; i + 1 < n; i ++) + res[i] = pre[i - 1] * suf[i + 1]; + return res; + } +}; + + +int main() { + + return 0; +} 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/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..a5b84be3 --- /dev/null +++ b/0001-0500/0251-Flatten-2D-Vector/cpp-0251/main.cpp @@ -0,0 +1,41 @@ +/// 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: + 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/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 diff --git a/0001-0500/0258-Add-Digits/cpp-0258/CMakeLists.txt b/0001-0500/0258-Add-Digits/cpp-0258/CMakeLists.txt new file mode 100644 index 00000000..44ca2503 --- /dev/null +++ b/0001-0500/0258-Add-Digits/cpp-0258/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0258) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0258 main.cpp) diff --git a/0001-0500/0258-Add-Digits/cpp-0258/main.cpp b/0001-0500/0258-Add-Digits/cpp-0258/main.cpp new file mode 100644 index 00000000..0eb38b99 --- /dev/null +++ b/0001-0500/0258-Add-Digits/cpp-0258/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/add-digits/ +/// Author : liuyubobobo +/// Time : 2022-02-07 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O((logn)^2) +/// Space Complexity: O(1) +class Solution { +public: + int addDigits(int num) { + while(num >= 10) + num = digit_sum(num); + return num; + } + +private: + int digit_sum(int num){ + int res = 0; + while(num) res += num % 10, num /= 10; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/0001-0500/0260-Single-Number-III/cpp-0260/CMakeLists.txt b/0001-0500/0260-Single-Number-III/cpp-0260/CMakeLists.txt new file mode 100644 index 00000000..1a0decbe --- /dev/null +++ b/0001-0500/0260-Single-Number-III/cpp-0260/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0260) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0260 main.cpp) diff --git a/0001-0500/0260-Single-Number-III/cpp-0260/main.cpp b/0001-0500/0260-Single-Number-III/cpp-0260/main.cpp new file mode 100644 index 00000000..e14687c3 --- /dev/null +++ b/0001-0500/0260-Single-Number-III/cpp-0260/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/single-number-iii/ +/// Author : liuyubobobo +/// Time : 2021-10-29 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector singleNumber(vector& nums) { + + map f; + for(int e: nums) f[e] ++; + + vector res; + for(const pair& p: f) + if(p.second == 1) res.push_back(p.first); + return res; + } +}; + + +int main() { + + return 0; +} 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/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]; } diff --git a/0001-0500/0299-Bulls-and-Cows/cpp-0299/CMakeLists.txt b/0001-0500/0299-Bulls-and-Cows/cpp-0299/CMakeLists.txt new file mode 100644 index 00000000..5c7c1b58 --- /dev/null +++ b/0001-0500/0299-Bulls-and-Cows/cpp-0299/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0299) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0299 main.cpp) diff --git a/0001-0500/0299-Bulls-and-Cows/cpp-0299/main.cpp b/0001-0500/0299-Bulls-and-Cows/cpp-0299/main.cpp new file mode 100644 index 00000000..6679563e --- /dev/null +++ b/0001-0500/0299-Bulls-and-Cows/cpp-0299/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/bulls-and-cows/ +/// Author : liuyubobobo +/// Time : 2021-11-07 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string getHint(string secret, string guess) { + + int n = secret.size(); + + vector a(10, 0), b(10, 0); + int bull = 0; + for(int i = 0; i < n; i ++) + if(secret[i] == guess[i]) bull ++; + else a[secret[i] - '0'] ++, b[guess[i] - '0'] ++; + + int cow = 0; + for(int i = 0; i < 10; i ++) + cow += min(a[i], b[i]); + + return to_string(bull) + "A" + to_string(cow) + "B"; + } +}; + + +int main() { + + return 0; +} 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/0001-0500/0306-Additive-Number/cpp-0306/CMakeLists.txt b/0001-0500/0306-Additive-Number/cpp-0306/CMakeLists.txt new file mode 100644 index 00000000..ff45e2df --- /dev/null +++ b/0001-0500/0306-Additive-Number/cpp-0306/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0306) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0306 main.cpp) diff --git a/0001-0500/0306-Additive-Number/cpp-0306/main.cpp b/0001-0500/0306-Additive-Number/cpp-0306/main.cpp new file mode 100644 index 00000000..0b027cab --- /dev/null +++ b/0001-0500/0306-Additive-Number/cpp-0306/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/additive-number/ +/// Author : liuyubobobo +/// Time : 2022-01-09 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + bool isAdditiveNumber(string num) { + + int n = num.size(); + for(int len1 = 1; len1 <= 17 && len1 + 2 <= n; len1 ++) + for(int len2 = 1; len2 <= 17 && len1 + len2 + 1 <= n; len2 ++) + if(ok(n, num, len1, len2)) return true; + return false; + } + +private: + bool ok(int n, const string& num, int len1, int len2){ + + if(num[0] == '0' && len1 > 1) return false; + if(num[len1] == '0' && len2 > 1) return false; + + vector v; + v.push_back(atoll(num.substr(0, len1).c_str())); + v.push_back(atoll(num.substr(len1, len2).c_str())); + + int cur_index = len1 + len2; + while(cur_index < n){ + + long long next = v[v.size() - 2] + v[v.size() - 1]; + string next_s = to_string(next); + int next_s_len = next_s.size(); + if(num.substr(cur_index, next_s_len) == next_s) + cur_index += next_s_len; + else + return false; + v.push_back(next); + } + return true; + } +}; + + +int main() { + + cout << Solution().isAdditiveNumber("112358") << endl; + // 1 + + cout << Solution().isAdditiveNumber("199100199") << endl; + // 1 + + cout << Solution().isAdditiveNumber("111") << endl; + // 0 + + cout << Solution().isAdditiveNumber("101") << endl; + // 1 + + return 0; +} diff --git a/0001-0500/0310-Minimum-Height-Trees/cpp-0310/CMakeLists.txt b/0001-0500/0310-Minimum-Height-Trees/cpp-0310/CMakeLists.txt new file mode 100644 index 00000000..2bdd3dc6 --- /dev/null +++ b/0001-0500/0310-Minimum-Height-Trees/cpp-0310/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0310) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0310 main.cpp) diff --git a/0001-0500/0310-Minimum-Height-Trees/cpp-0310/main.cpp b/0001-0500/0310-Minimum-Height-Trees/cpp-0310/main.cpp new file mode 100644 index 00000000..d74edadd --- /dev/null +++ b/0001-0500/0310-Minimum-Height-Trees/cpp-0310/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/minimum-height-trees/ +/// Author : liuyubobobo +/// Time : 2021-12-15 + +#include +#include + +using namespace std; + + +/// Tree Centroids +/// Time Complexity: O(V + E) +/// Space Complexity: O(V + E) +class Solution { +public: + vector findMinHeightTrees(int n, vector>& edges) { + + vector> g(n); + for(const vector& e: edges) + g[e[0]].push_back(e[1]), g[e[1]].push_back(e[0]); + + return get_tree_centroid(n, g); + } + +private: + vector get_tree_centroid(int n, const vector>& g){ + + vector degrees(n, 0); + vector leaves; + for(int u = 0; u < n; u ++){ + degrees[u] = g[u].size(); + if(degrees[u] == 0 || degrees[u] == 1){ + leaves.push_back(u); + degrees[u] = 0; + } + } + + int count = leaves.size(); + while(count < n){ + vector new_leaves; + for(int u: leaves){ + for(int v: g[u]){ + degrees[v] --; + if(degrees[v] == 1) new_leaves.push_back(v); + } + degrees[u] = 0; + } + count += new_leaves.size(); + leaves = new_leaves; + } + assert(1 <= leaves.size() && leaves.size() <= 2); + return leaves; + } +}; + + +int main() { + + return 0; +} 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/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/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 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/0001-0500/0332-Reconstruct-Itinerary/cpp-0332/CMakeLists.txt b/0001-0500/0332-Reconstruct-Itinerary/cpp-0332/CMakeLists.txt new file mode 100644 index 00000000..67dee4eb --- /dev/null +++ b/0001-0500/0332-Reconstruct-Itinerary/cpp-0332/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0332) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0332 main.cpp) diff --git a/0001-0500/0332-Reconstruct-Itinerary/cpp-0332/main.cpp b/0001-0500/0332-Reconstruct-Itinerary/cpp-0332/main.cpp new file mode 100644 index 00000000..d55f50d9 --- /dev/null +++ b/0001-0500/0332-Reconstruct-Itinerary/cpp-0332/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/reconstruct-itinerary/ +/// Author : liuyubobobo +/// Time : 2022-01-27 + +#include +#include +#include +#include + +using namespace std; + + +/// Euler Path +/// Time Complexity: O(V + E) +/// Space Complexity: O(V + E) +class Solution { +public: + vector findItinerary(vector>& tickets) { + + map> g; + for(const vector& e: tickets) g[e[0]].push_back(e[1]); + for(const pair>& p: g) + sort(g[p.first].begin(), g[p.first].end(), greater()); + + vector res; + + stack stack; + stack.push("JFK"); + while(!stack.empty()){ + if(g[stack.top()].size()){ + string u = stack.top(), v = g[u].back(); + stack.push(v); + g[u].pop_back(); + } + else{ + res.push_back(stack.top()); + stack.pop(); + } + } + + reverse(res.begin(), res.end()); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/0001-0500/0335-Self-Crossing/cpp-0335/CMakeLists.txt b/0001-0500/0335-Self-Crossing/cpp-0335/CMakeLists.txt new file mode 100644 index 00000000..68d39435 --- /dev/null +++ b/0001-0500/0335-Self-Crossing/cpp-0335/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0335) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0335 main.cpp) diff --git a/0001-0500/0335-Self-Crossing/cpp-0335/main.cpp b/0001-0500/0335-Self-Crossing/cpp-0335/main.cpp new file mode 100644 index 00000000..626f5d61 --- /dev/null +++ b/0001-0500/0335-Self-Crossing/cpp-0335/main.cpp @@ -0,0 +1,91 @@ +/// Source : https://leetcode.com/problems/self-crossing/ +/// Author : liuyubobobo +/// Time : 2021-10-28 + +#include +#include + +using namespace std; + + +/// Geography +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { + +private: + typedef struct Point{ + long long x, y; + Point(long long x, long long y) : x(x), y(y){} + }Point; + + const int dirs[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; + +public: + bool isSelfCrossing(vector& dis) { + + int n = dis.size(); + int L = 5; + + vector> segs; + Point cur(0, 0); + for(int i = 0; i < n; i ++){ + Point next(cur.x + dirs[i % 4][0] * dis[i], cur.y + dirs[i % 4][1] * dis[i]); + segs.push_back({cur, next}); + + for(int j = i - 2; j >= max(0, i - L); j --) + if(intersect(segs[i].first, segs[i].second, segs[j].first, segs[j].second)) + return true; + + cur = next; + } + return false; + } + +private: + bool on_segment(Point p, Point q, Point r) + { + if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) && + q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y)) + return true; + return false; + } + + int orientation(Point p, Point q, Point r) + { + long long val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); + if (val == 0) return 0; // collinear + return (val > 0)? 1: 2; // clock or counterclock wise + } + + bool intersect(Point p1, Point q1, Point p2, Point q2){ + + int o1 = orientation(p1, q1, p2); + int o2 = orientation(p1, q1, q2); + int o3 = orientation(p2, q2, p1); + int o4 = orientation(p2, q2, q1); + + if (o1 != o2 && o3 != o4) + return true; + + if (o1 == 0 && on_segment(p1, p2, q1)) return true; + if (o2 == 0 && on_segment(p1, q2, q1)) return true; + if (o3 == 0 && on_segment(p2, p1, q2)) return true; + if (o4 == 0 && on_segment(p2, q1, q2)) return true; + return false; + } +}; + + +int main() { + + vector dis1 = {2, 1, 1, 2}; + cout << Solution().isSelfCrossing(dis1) << endl; + // 1 + + vector dis2 = {3, 3, 4, 2, 2}; + cout << Solution().isSelfCrossing(dis2) << endl; + // 0 + + return 0; +} 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; } diff --git a/0001-0500/0357-Count-Numbers-with-Unique-Digits/cpp-0357/CMakeLists.txt b/0001-0500/0357-Count-Numbers-with-Unique-Digits/cpp-0357/CMakeLists.txt new file mode 100644 index 00000000..8aa92127 --- /dev/null +++ b/0001-0500/0357-Count-Numbers-with-Unique-Digits/cpp-0357/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0357) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0357 main.cpp) diff --git a/0001-0500/0357-Count-Numbers-with-Unique-Digits/cpp-0357/main.cpp b/0001-0500/0357-Count-Numbers-with-Unique-Digits/cpp-0357/main.cpp new file mode 100644 index 00000000..0769d659 --- /dev/null +++ b/0001-0500/0357-Count-Numbers-with-Unique-Digits/cpp-0357/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/count-numbers-with-unique-digits/ +/// Author : liuyubobobo +/// Time : 2022-04-10 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O((logn)^2) +/// Space Complexity: O(1) +class Solution { +public: + int countNumbersWithUniqueDigits(int n) { + + int res = 1; + for(int w = 1; w <= n; w ++){ + int tres = 9, choice = 9; + for(int i = 1; i < w; i ++) tres *= choice, choice --; + res += tres; + } + return res; + } +}; + + +int main() { + + return 0; +} 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/0001-0500/0367-Valid-Perfect-Square/cpp-0367/CMakeLists.txt b/0001-0500/0367-Valid-Perfect-Square/cpp-0367/CMakeLists.txt new file mode 100644 index 00000000..f710ddd6 --- /dev/null +++ b/0001-0500/0367-Valid-Perfect-Square/cpp-0367/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0367) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0367 main.cpp) diff --git a/0001-0500/0367-Valid-Perfect-Square/cpp-0367/main.cpp b/0001-0500/0367-Valid-Perfect-Square/cpp-0367/main.cpp new file mode 100644 index 00000000..1948fab5 --- /dev/null +++ b/0001-0500/0367-Valid-Perfect-Square/cpp-0367/main.cpp @@ -0,0 +1,26 @@ +/// Source : https://leetcode.com/problems/valid-perfect-square/ +/// Author : liuyubobobo +/// Time : 2021-11-03 + +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + bool isPerfectSquare(int num) { + + int x = (int)(sqrt(num) + 1e-6); + return x * x == num; + } +}; + + +int main() { + + return 0; +} diff --git a/0001-0500/0372-Super-Pow/cpp-0372/CMakeLists.txt b/0001-0500/0372-Super-Pow/cpp-0372/CMakeLists.txt new file mode 100644 index 00000000..78bee50f --- /dev/null +++ b/0001-0500/0372-Super-Pow/cpp-0372/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0372) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0372 main.cpp) diff --git a/0001-0500/0372-Super-Pow/cpp-0372/main.cpp b/0001-0500/0372-Super-Pow/cpp-0372/main.cpp new file mode 100644 index 00000000..ebce1f4d --- /dev/null +++ b/0001-0500/0372-Super-Pow/cpp-0372/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/super-pow/ +/// Author : liuyubobobo +/// Time : 2021-12-04 + +#include +#include + +using namespace std; + + +/// Quick Pow +/// Time Complexity: O(|b|^2) +/// Space Complexity: O(|b|^2) +class Solution { + +private: + const int MOD = 1337; + +public: + int superPow(int a, vector& b) { + + a %= MOD; + + if(b.empty() || (b.size() == 1 && b[0] == 0)) + return 1; + if(b.size() == 1 && b[0] == 1) + return a; + + vector q; + int cur = 0; + for(int e: b){ + cur = cur * 10 + e; + if(!q.empty() || cur / 2) + q.push_back(cur / 2); + cur %= 2; + } + + int tres = superPow(a, q); + int res = tres * tres % MOD; + if(cur) res = res * a % MOD; + return res; + } +}; + + +int main() { + + vector b1 = {1, 0}; + cout << Solution().superPow(2, b1) << endl; + // 1024 + + vector b2 = {2, 0, 0}; + cout << Solution().superPow(2147483647, b2) << endl; + // 400 + + return 0; +} diff --git a/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/CMakeLists.txt b/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/cpp-0373/CMakeLists.txt similarity index 100% rename from 0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/CMakeLists.txt rename to 0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/cpp-0373/CMakeLists.txt diff --git a/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/main.cpp b/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/cpp-0373/main.cpp similarity index 85% rename from 0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/main.cpp rename to 0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/cpp-0373/main.cpp index 7b6a91d8..149caba6 100644 --- a/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/main.cpp +++ b/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/cpp-0373/main.cpp @@ -14,13 +14,12 @@ using namespace std; /// Space Complexity: O(len(nums1)) class Solution { public: - vector> kSmallestPairs( - vector& nums1, vector& nums2, int k) { + vector> kSmallestPairs( vector& nums1, vector& nums2, int k) { if(!nums1.size() || !nums2.size()) return {}; - vector> res; + vector> res; priority_queue>, vector>>, greater>>> pq; @@ -30,7 +29,7 @@ class Solution { while(k-- && !pq.empty()){ pair p = pq.top().second; - res.push_back(make_pair(nums1[p.first], nums2[p.second])); + res.push_back({nums1[p.first], nums2[p.second]}); pq.pop(); if(p.second + 1 < nums2.size()){ @@ -44,9 +43,9 @@ class Solution { }; -void print_vec(const vector>& vec){ - for(const pair& p: vec) - cout << "(" << p.first << "," << p.second << ") "; +void print_vec(const vector>& vec){ + for(const vector& p: vec) + cout << "(" << p[0] << "," << p[1] << ") "; cout << endl; } diff --git a/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/main2.cpp b/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/main2.cpp deleted file mode 100644 index 501b74f4..00000000 --- a/0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/main2.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/// Source : https://leetcode.com/problems/find-k-pairs-with-smallest-sums/ -/// Author : liuyubobobo -/// Time : 2018-11-10 - -#include -#include -#include - -using namespace std; - - -/// Using Binary Search -/// Time Complexity: O(nums1 * log(nums2) * log(maxnum)) -/// Space Complexity: O(1) -class Solution { -public: - vector> kSmallestPairs( - vector& nums1, vector& nums2, int k) { - - if(nums1.size() == 0 || nums2.size() == 0) - return {}; - - int low = nums1[0] + nums2[0], high = nums1.back() + nums2.back(); - while(low != high){ - int mid = low + high; - if(mid >= 0 || mid % 2 == 0) mid /= 2; - else mid = (mid - 1) / 2; // very tricky here! - // since C++ doesn't make floor division for minus number! - - int num = get_num(nums1, nums2, mid); - if(num >= k) - high = mid; - else - low = mid + 1; - } - - return get_res(nums1, nums2, low, k); - } - -private: - vector> get_res(const vector& nums1, const vector& nums2, int sum, int k){ - - vector> res; - for(int e1: nums1){ - for(int e2: nums2) - if(e1 + e2 < sum) - res.push_back(make_pair(e1, e2)); - else - break; - } - - sort(res.begin(), res.end(), - [nums1, nums2](const pair& p1, const pair& p2){ - - int sum1 = p1.first + p1.second; - int sum2 = p2.first + p2.second; - if(sum1 != sum2) - return sum1 < sum2; - return p1 < p2; - }); - - if(res.size() > k){ - while(res.size() > k) - res.pop_back(); - } - else - for(int e1: nums1){ - int l = lower_bound(nums2.begin(), nums2.end(), sum - e1) - nums2.begin(); - if(l >= 0 && l < nums2.size()) - while(l < nums2.size() && nums2[l] == sum - e1 && res.size() < k) - res.push_back(make_pair(e1, nums2[l++])); - if(res.size() == k) - break; - } - - return res; - } - - int get_num(const vector& nums1, const vector& nums2, int sum){ - - int num = 0; - for(int e: nums1) - if(e + nums2[0] <= sum){ - int index = lower_bound(nums2.begin(), nums2.end(), sum - e) - nums2.begin(); - if(nums2[index] > sum - e) - index --; - num += index + 1; - } - - return num; - } -}; - - -void print_vec(const vector>& vec){ - for(const pair& p: vec) - cout << "(" << p.first << "," << p.second << ") "; - cout << endl; -} - -int main() { - - vector A1 = {1, 7, 11}; - vector B1 = {2, 4, 6}; - print_vec(Solution().kSmallestPairs(A1, B1, 3)); - // (1, 2) (1, 4) (1, 6) - - vector A2 = {1, 1, 2}; - vector B2 = {1, 2, 3}; - print_vec(Solution().kSmallestPairs(A2, B2, 2)); - // (1, 1) (1, 1) - - vector A3 = {1, 2}; - vector B3 = {3}; - print_vec(Solution().kSmallestPairs(A3, B3, 3)); - // (1, 3) (2, 3) - - vector A4 = {1, 1, 2}; - vector B4 = {1, 2, 3}; - print_vec(Solution().kSmallestPairs(A4, B4, 10)); - // (1, 1) (1, 1) (2, 1) (1, 2) (1, 2) (2, 2) (1, 3) (1, 3) (2, 3) - - vector A5 = {1, 2, 4}; - vector B5 = {-1, 1, 2}; - print_vec(Solution().kSmallestPairs(A5, B5, 100)); - // (1, -1) (2, -1) (1, 1) (4, -1) (2, 1) (1, 2) (2, 2) (4, 1) (4, 2) - - vector A6 = {-10, -4, 0, 0, 6}; - vector B6 = {3, 5, 6, 7, 8, 100}; - print_vec(Solution().kSmallestPairs(A6, B6, 10)); - // (-10, 3) (-10, 5) (-10, 6) (-10, 7) (-10, 8) (-4, 3) (-4, 5) (-4, 6) (-4, 7) (0, 3) - - vector A7 = {1,2,4,5,6}; - vector B7 = {3,5,7,9}; - print_vec(Solution().kSmallestPairs(A7, B7, 10)); - - vector A8 = {3,22,35,56,76}; - vector B8 = {3,22,35,56,76}; - print_vec(Solution().kSmallestPairs(A8, B8, 25)); - - vector A9 = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; - vector B9 = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; - print_vec(Solution().kSmallestPairs(A9, B9, 1000)); - - return 0; -} \ No newline at end of file diff --git a/0001-0500/0375-Guess-Number-Higher-or-Lower-II/cpp-0375/CMakeLists.txt b/0001-0500/0375-Guess-Number-Higher-or-Lower-II/cpp-0375/CMakeLists.txt new file mode 100644 index 00000000..cd3b7308 --- /dev/null +++ b/0001-0500/0375-Guess-Number-Higher-or-Lower-II/cpp-0375/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0375) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0375 main.cpp) diff --git a/0001-0500/0375-Guess-Number-Higher-or-Lower-II/cpp-0375/main.cpp b/0001-0500/0375-Guess-Number-Higher-or-Lower-II/cpp-0375/main.cpp new file mode 100644 index 00000000..5d8c78f6 --- /dev/null +++ b/0001-0500/0375-Guess-Number-Higher-or-Lower-II/cpp-0375/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/guess-number-higher-or-lower-ii/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { +public: + int getMoneyAmount(int n) { + vector> dp(n + 1, vector(n + 1, -1)); + return dfs(1, n, dp); + } + +private: + int dfs(int l, int r, vector>& dp){ + + if(l == r) return 0; + if(l + 1 == r) return l; + if(l + 2 == r) return l + 1; + if(dp[l][r] != -1) return dp[l][r]; + + int res = INT_MAX; + for(int root = l + 1; root < r; root ++){ + res = min(res, root + max(dfs(l, root - 1, dp), dfs(root + 1, r, dp))); + } + return dp[l][r] = res; + } +}; + + +int main() { + + cout << Solution().getMoneyAmount(10) << endl; + // 16 + + return 0; +} 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 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/0001-0500/0394-Decode-String/cpp-0394/CMakeLists.txt b/0001-0500/0394-Decode-String/cpp-0394/CMakeLists.txt index 5d9e7520..e86c04ac 100644 --- a/0001-0500/0394-Decode-String/cpp-0394/CMakeLists.txt +++ b/0001-0500/0394-Decode-String/cpp-0394/CMakeLists.txt @@ -3,5 +3,5 @@ project(cpp_0394) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main2.cpp) +set(SOURCE_FILES main3.cpp) add_executable(cpp_0394 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0001-0500/0394-Decode-String/cpp-0394/main3.cpp b/0001-0500/0394-Decode-String/cpp-0394/main3.cpp new file mode 100644 index 00000000..369528f5 --- /dev/null +++ b/0001-0500/0394-Decode-String/cpp-0394/main3.cpp @@ -0,0 +1,76 @@ +/// Source : https://leetcode.com/problems/decode-string/description/ +/// Author : liuyubobobo +/// Time : 2021-12-19 + +#include +#include +#include +#include + +using namespace std; + + +/// DFS - recursion algorithm +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string decodeString(string s) { + + int n = s.size(); + + // ends[i] 表示如果 s[i] == '[', 其对应的 ']' 的位置 + vector ends(n, -1); + stack stack; + for(int i = 0; i < n; i ++) { + if (s[i] == '[') stack.push(i); + else if (s[i] == ']') ends[stack.top()] = i, stack.pop(); + } + + return dfs(s, 0, n - 1, ends); + } + +private: + string dfs(const string& s, int l, int r, const vector& ends){ + + if(l > r) return ""; + + // 如果 s[l] 是字符,则可以直接处理后面 + if(isalpha(s[l])) return string(1, s[l]) + dfs(s, l + 1, r, ends); + + // 否则 s[l] 一定是数字 + assert(isdigit(s[l])); + int num = 0, i; // num 是当前完整的数字(数字可能多位) + for(i = l; isdigit(s[i]); i ++) + num = num * 10 + (s[i] - '0'); + + // 数字最后一位的后一个字符一定是 '[' + assert(s[i] == '['); + string res = ""; + string t = dfs(s, i + 1, ends[i] - 1, ends); // 递归求出 [] 中的字符串 + while(num --) res += t; // 这个字符串重复 num 次 + + // 如果 ']' 后续还有未处理的字符,则继续递归处理 + if(ends[i] + 1 <= r) res += dfs(s, ends[i] + 1, r, ends); + + return res; + } +}; + + +int main() { + + string s1 = "3[a]2[bc]"; + cout << Solution().decodeString(s1) << endl; + // "aaabcbc" + + string s2 = "3[a2[c]]"; + cout << Solution().decodeString(s2) << endl; + // "accaccacc" + + string s3 = "2[abc]3[cd]ef"; + cout << Solution().decodeString(s3) << endl; + // "abcabccdcdcdef" + + return 0; +} \ No newline at end of file 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/0001-0500/0397-Integer-Replacement/cpp-0397/CMakeLists.txt b/0001-0500/0397-Integer-Replacement/cpp-0397/CMakeLists.txt new file mode 100644 index 00000000..97b4dd14 --- /dev/null +++ b/0001-0500/0397-Integer-Replacement/cpp-0397/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0397) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0397 main.cpp) diff --git a/0001-0500/0397-Integer-Replacement/cpp-0397/main.cpp b/0001-0500/0397-Integer-Replacement/cpp-0397/main.cpp new file mode 100644 index 00000000..26bc345a --- /dev/null +++ b/0001-0500/0397-Integer-Replacement/cpp-0397/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/integer-replacement/ +/// Author : liuyubobobo +/// Time : 2021-11-18 + +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + int integerReplacement(long long n) { + + int res = 0; + while(n != 1){ + if(n == 3) n = 1, res += 2; + else if(n % 2 == 0) res ++, n /= 2; + else if((n + 1) % 4 == 0) n ++, res ++; + else n --, res ++; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/0001-0500/0400-Nth-Digit/cpp-0400/CMakeLists.txt b/0001-0500/0400-Nth-Digit/cpp-0400/CMakeLists.txt new file mode 100644 index 00000000..5d157a9f --- /dev/null +++ b/0001-0500/0400-Nth-Digit/cpp-0400/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0400) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0400 main.cpp) diff --git a/0001-0500/0400-Nth-Digit/cpp-0400/main.cpp b/0001-0500/0400-Nth-Digit/cpp-0400/main.cpp new file mode 100644 index 00000000..1d2b6313 --- /dev/null +++ b/0001-0500/0400-Nth-Digit/cpp-0400/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/nth-digit/ +/// Author : liuyubobobo +/// Time : 2021-11-29 + +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + int findNthDigit(int n) { + + n --; + + int d = 1; + long long total = 9; + while(n >= total * d) + n -= total * d, d ++, total *= 10; + return to_string(total / 9 + n / d)[n % d] - '0'; + } +}; + + +int main() { + + cout << Solution().findNthDigit(3) << endl; + // 3 + + cout << Solution().findNthDigit(11) << endl; + // 0 + + cout << Solution().findNthDigit(100) << endl; + // 5 + + cout << Solution().findNthDigit(1000) << endl; + // 3 + + return 0; +} 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/0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/CMakeLists.txt b/0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/CMakeLists.txt new file mode 100644 index 00000000..9f0676bf --- /dev/null +++ b/0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0407) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0407 main.cpp) diff --git a/0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/main.cpp b/0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/main.cpp new file mode 100644 index 00000000..178cbead --- /dev/null +++ b/0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/main.cpp @@ -0,0 +1,77 @@ +/// Source : https://leetcode.com/problems/trapping-rain-water-ii/ +/// Author : liuyubobobo +/// Time : 2021-11-03 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// This solution is analysis is amazing: +/// https://leetcode.com/problems/trapping-rain-water-ii/discuss/89461/Java-solution-using-PriorityQueue +/// +/// Time Compelexity: 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}}; + int R, C; + +public: + int trapRainWater(vector>& heightMap) { + + R = heightMap.size(), C = heightMap[0].size(); + + priority_queue, vector>, greater>> pq; // height, pos + vector visited(R * C, false); + for(int i = 0; i < R; i ++){ + pq.push({heightMap[i][0], i * C + 0}); + pq.push({heightMap[i][C - 1], i * C + C - 1}); + visited[i * C + 0] = visited[i * C + C - 1] = true; + } + for(int j = 1; j + 1 < C; j ++){ + pq.push({heightMap[0][j], j}); + pq.push({heightMap[R - 1][j], (R - 1) * C + j}); + visited[j] = visited[(R - 1) * C + j] = true; + } + + int res = 0; + while(!pq.empty()){ + int h = pq.top().first, pos = pq.top().second; + pq.pop(); + + int x = pos / C, y = pos % C; + res += max(0, h - heightMap[x][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 * C + ny]){ + pq.push({max(h, heightMap[nx][ny]), nx * C + ny}); + visited[nx * C + ny] = true; + } + } + } + return res; + } + +private: + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> height1 = { + {12,13,1,12},{13,4,13,12},{13,8,10,12},{12,13,12,12},{13,13,13,13} + }; + cout << Solution().trapRainWater(height1) << endl; + // 14 + + return 0; +} 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..c5dc18c1 --- /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 main2.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/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; +} diff --git a/0001-0500/0419-Battleships-in-a-Board/cpp-0419/CMakeLists.txt b/0001-0500/0419-Battleships-in-a-Board/cpp-0419/CMakeLists.txt new file mode 100644 index 00000000..8ecb6a4d --- /dev/null +++ b/0001-0500/0419-Battleships-in-a-Board/cpp-0419/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0419) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0419 main.cpp) diff --git a/0001-0500/0419-Battleships-in-a-Board/cpp-0419/main.cpp b/0001-0500/0419-Battleships-in-a-Board/cpp-0419/main.cpp new file mode 100644 index 00000000..b603a970 --- /dev/null +++ b/0001-0500/0419-Battleships-in-a-Board/cpp-0419/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/battleships-in-a-board/ +/// Author : liuyubobobo +/// Time : 2021-12-17 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n^3) +/// Space Complexity: O(1) +class Solution { +public: + int countBattleships(vector>& board) { + + int R = board.size(), C = board[0].size(), res = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + if(board[i][j] == '.') continue; + + int tres = (i + 1 < R && board[i + 1][j] == 'X') + (j + 1 < C && board[i][j + 1] == 'X'); + if(tres == 0) tres = 1; + res += tres; + + for(int k = j + 1; k < C && board[i][k] == 'X'; k ++) board[i][k] = '.'; + for(int k = i + 1; k < R && board[k][j] == 'X'; k ++) board[k][j] = '.'; + board[i][j] = '.'; + } + return res; + } +}; + + +int main() { + + return 0; +} 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/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/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/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/0001-0500/0432-All-O-one-Data-Structure/cpp-0432/CMakeLists.txt b/0001-0500/0432-All-O-one-Data-Structure/cpp-0432/CMakeLists.txt new file mode 100644 index 00000000..75fd9397 --- /dev/null +++ b/0001-0500/0432-All-O-one-Data-Structure/cpp-0432/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0432) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0432 main.cpp) diff --git a/0001-0500/0432-All-O-one-Data-Structure/cpp-0432/main.cpp b/0001-0500/0432-All-O-one-Data-Structure/cpp-0432/main.cpp new file mode 100644 index 00000000..39988bfe --- /dev/null +++ b/0001-0500/0432-All-O-one-Data-Structure/cpp-0432/main.cpp @@ -0,0 +1,66 @@ +/// Source : https://leetcode.com/problems/all-oone-data-structure/ +/// Author : liuyubobobo +/// Time : 2022-03-15 + +#include +#include +#include + +using namespace std; + + +/// Map + Set +/// Time Complexity: O(logn) for all operations +/// Space Complexity: O(n) +class AllOne { + +private: + map f; + set> fs_set; + +public: + AllOne() {} + + void inc(string key) { + int old_f = f[key]; + f[key] ++; + + if(old_f) fs_set.erase({old_f, key}); + fs_set.insert({old_f + 1, key}); + } + + void dec(string key) { + int old_f = f[key]; + f[key] --; + if(old_f - 1 == 0) f.erase(key); + + fs_set.erase({old_f, key}); + if(old_f - 1) fs_set.insert({old_f - 1, key}); + } + + string getMaxKey() { + if(fs_set.empty()) return ""; + return fs_set.rbegin()->second; + } + + string getMinKey() { + if(fs_set.empty()) return ""; + return fs_set.begin()->second; + } +}; + + +int main() { + + AllOne obj; + obj.inc("hello"); + obj.inc("hello"); + cout << obj.getMaxKey() << '\n'; + cout << obj.getMinKey() << '\n'; + + obj.inc("leet"); + cout << obj.getMaxKey() << '\n'; + cout << obj.getMinKey() << '\n'; + + return 0; +} 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/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/0001-0500/0440-K-th-Smallest-in-Lexicographical-Order/cpp-0440/CMakeLists.txt b/0001-0500/0440-K-th-Smallest-in-Lexicographical-Order/cpp-0440/CMakeLists.txt new file mode 100644 index 00000000..23bd8c5b --- /dev/null +++ b/0001-0500/0440-K-th-Smallest-in-Lexicographical-Order/cpp-0440/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0440) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0440 main.cpp) diff --git a/0001-0500/0440-K-th-Smallest-in-Lexicographical-Order/cpp-0440/main.cpp b/0001-0500/0440-K-th-Smallest-in-Lexicographical-Order/cpp-0440/main.cpp new file mode 100644 index 00000000..45d0d7a3 --- /dev/null +++ b/0001-0500/0440-K-th-Smallest-in-Lexicographical-Order/cpp-0440/main.cpp @@ -0,0 +1,97 @@ +/// Source : https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/ +/// Author : liuyubobobo +/// Time : 2022-03-23 + +#include +#include +#include + +using namespace std; + + +/// Digital DP +/// Time Complexity: O(logn) +/// Space Complexity: O(logn) +class Solution { + +private: + vector pow10; + vector> dp; + +public: + int findKthNumber(int n, int k) { + + pow10.assign(15, 1); + for(int i = 1; i < 15; i ++) + pow10[i] = pow10[i - 1] * 10ll; + + dp = vector>(2, vector(20, -1)); + + string s = to_string(n); + string cur = ""; + assert(solve(s, cur, 0, k - 1)); + return cur == "" ? 1 : atoi(cur.c_str()); + } + +private: + bool solve(const string& s, string& cur, int index, int k){ + + if(k == 0) return true; + + int cnt_total = index == 0 ? 0 : 1; + for(int d = (index == 0 ? 1 : 0); d <= 9; d ++){ + int cnt = get_cnt(s, cur + (char)('0' + d), index + 1); + if(k < cnt_total + cnt){ + cur += (char)('0' + d); + solve(s, cur, index + 1, k - cnt_total); + return true; + } + cnt_total += cnt; + } + return false; + } + + int get_cnt(const string& s, const string& cur, int index){ + + int res = 0; + for(int len = index; len < s.size(); len ++) + res += pow10[len - index]; + + if(cur <= s.substr(0, cur.size())) + res += get_cnt_dfs(s, index, cur < s.substr(0, cur.size())); + return res; + } + + int get_cnt_dfs(const string& s, int index, bool can_over){ + + if(index == s.size()) return 1; + if(dp[can_over][index] != -1) return dp[can_over][index]; + + int res = 0; + for(int d = 0; d <= (can_over ? 9 : (s[index] - '0')); d ++){ + res += get_cnt_dfs(s, index + 1, can_over || d < s[index] - '0'); + } + return dp[can_over][index] = res; + } +}; + + +int main() { + + cout << Solution().findKthNumber(13, 2) << '\n'; + // 10 + + cout << Solution().findKthNumber(1, 1) << '\n'; + // 1 + + cout << Solution().findKthNumber(10, 3) << '\n'; + // 2 + + cout << Solution().findKthNumber(100, 10) << '\n'; + // 17 + + cout << Solution().findKthNumber(681692778, 351251360) << '\n'; + // 416126219 + + return 0; +} 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/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/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/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/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/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/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/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..66079cfe --- /dev/null +++ b/0001-0500/0468-Validate-IP-Address/cpp-0468/main.cpp @@ -0,0 +1,85 @@ +/// 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) { + + 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/0001-0500/0472-Concatenated-Words/cpp-0472/CMakeLists.txt b/0001-0500/0472-Concatenated-Words/cpp-0472/CMakeLists.txt new file mode 100644 index 00000000..913b14fb --- /dev/null +++ b/0001-0500/0472-Concatenated-Words/cpp-0472/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0472) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0472 main.cpp) diff --git a/0001-0500/0472-Concatenated-Words/cpp-0472/main.cpp b/0001-0500/0472-Concatenated-Words/cpp-0472/main.cpp new file mode 100644 index 00000000..678f9d7c --- /dev/null +++ b/0001-0500/0472-Concatenated-Words/cpp-0472/main.cpp @@ -0,0 +1,103 @@ +/// Source : https://leetcode.com/problems/concatenated-words/ +/// Author : liuyubobobo +/// Time : 2021-12-29 + +#include +#include +#include + +using namespace std; + + +/// Sorting + Trie +/// Time Complexity: O(n * |s|^2) +/// Space Complexity: O(n * |s|) +class Trie { + +private: + class Node{ + + public: + vector next; + bool is_word = false; + + Node() : next(26, nullptr) {}; + }; + + Node* root; + const int NEG_INF = -1000000; + +public: + Trie() : root(new Node()) {} + + void insert(const string& word) { + + Node* cur = root; + for(char c: word){ + if(cur->next[c - 'a'] == nullptr) + cur->next[c - 'a'] = new Node(); + cur = cur->next[c - 'a']; + } + cur->is_word = true; + } + + bool ok(const string& s){ + return query(root, s, 0) >= 2; + } + +private: + int query(Node* node, const string& s, int index) { + + if(index == s.size()) + return (node && node->is_word) ? 1 : NEG_INF; + + int res = NEG_INF; + if(node->next[s[index] - 'a']) + res = max(res, query(node->next[s[index] - 'a'], s, index + 1)); + + if(node->is_word) + res = max(res, 1 + query(root, s, index)); + + return res; + } +}; + +class Solution { +public: + vector findAllConcatenatedWordsInADict(vector& words) { + + sort(words.begin(), words.end(), [](const string& s1, const string& s2){ + return s1.size() < s2.size(); + }); + + Trie trie; + vector res; + for(const string& s: words) { + if(s == "") continue; + else if(trie.ok(s)) res.push_back(s); + else trie.insert(s); + } + return res; + } +}; + + +void print_vec(const vector& res){ + for(const string& e: res) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector words1 = {"cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"}; + print_vec(Solution().findAllConcatenatedWordsInADict(words1)); + // "catsdogcats","dogcatsdog","ratcatdogcat" + + vector words2 = {"cat","dog","catdog"}; + print_vec(Solution().findAllConcatenatedWordsInADict(words2)); + // "catdog" + + vector words3 = {"rfkqyuqfjkx","","vnrtysfrzrmzl","gfve","qfpd","lqdqrrcrwdnxeuo","q","klaitgdphcspij","hbsfyfv","adzpbfudkklrw","aozmixr","ife","feclhbvfuk","yeqfqojwtw","sileeztxwjl","ngbqqmbxqcqp","khhqr","dwfcayssyoqc","omwufbdfxu","zhift","kczvhsybloet","crfhpxprbsshsjxd","ilebxwbcto","yaxzfbjbkrxi","imqpzwmshlpj","ta","hbuxhwadlpto","eziwkmg","ovqzgdixrpddzp","c","wnqwqecyjyib","jy","mjfqwltvzk","tpvo","phckcyufdqml","lim","lfz","tgygdt","nhcvpf","fbrpzlk","shwywshtdgmb","bkkxcvg","monmwvytby","nuqhmfj","qtg","cwkuzyamnerp","fmwevhwlezo","ye","hbrcewjxvcezi","tiq","tfsrptug","iznorvonzjfea","gama","apwlmbzit","s","hzkosvn","nberblt","kggdgpljfisylt","mf","h","bljvkypcflsaqe","cijcyrgmqirz","iaxakholawoydvch","e","gttxwpuk","jf","xbrtspfttota","sngqvoijxuv","bztvaal","zxbshnrvbykjql","zz","mlvyoshiktodnsjj","qplci","lzqrxl","qxru","ygjtyzleizme","inx","lwhhjwsl","endjvxjyghrveu","phknqtsdtwxcktmw","wsdthzmlmbhjkm","u","pbqurqfxgqlojmws","mowsjvpvhznbsi","hdkbdxqg","ge","pzchrgef","ukmcowoe","nwhpiid","xdnnl","n","yjyssbsoc","cdzcuunkrf","uvouaghhcyvmlk","aajpfpyljt","jpyntsefxi","wjute","y","pbcnmhf","qmmidmvkn","xmywegmtuno","vuzygv","uxtrdsdfzfssmel","odjgdgzfmrazvnd","a","rdkugsbdpawxi","ivd","bbqeonycaegxfj","lrfkraoheucsvpi","eqrswgkaaaohxx","hqjtkqaqh","berbpmglbjipnuj","wogwczlkyrde","aqufowbig","snjniegvdvotu","ocedkt","bbufnxorixibbd","rzuqsyr","qghoy","evcuanuujszitaoa","wsx","glafbwzdd","znrvjqeyqi","npitruijvyllsi","objltu","ryp","nvybsfrxtlfmp","id","zoolzslgd","owijatklvjzscizr","upmsoxftumyxifyu","xucubv","fctkqlroq","zjv","wzi","ppvs","mflvioemycnphfjt","nwedtubynsb","repgcx","gsfomhvpmy","kdohe","tyycsibbeaxn","wjkfvabn","llkmagl","thkglauzgkeuly","paeurdvexqlw","akdt","ihmfrj","janxk","rqdll","cyhbsuxnlftmjc","yybwsjmajbwtuhkk","ovytgaufpjl","iwbnzhybsx","mumbh","jqmdabmyu","br","lwstjkoxbczkj","vhsgzvwiixxaob","fso","qnebmfl","ooetjiz","lq","msxphqdgz","mqhoggvrvjqrp","xbhkkfg","zxjegsyovdrmw","jav","mshoj","ax","biztkfomz","hujdmcyxdqteqja","gqgsomonv","reqqzzpw","lihdnvud","lznfhbaokxvce","fhxbldylqqewdnj","rlbskqgfvn","lfvobeyolyy","v","iwh","fpbuiujlolnjl","gvwxljbo","ypaotdzjxxrsc","mwrvel","umzpnoiei","ogwilaswn","yw","egdgye","hsrznlzrf","mwdgxaigmxpy","yaqgault","dtlg","cyvfiykmkllf","zxqyhvizqmamj","lvvgoifltzywueyp","abinmy","ppzaecvmx","qsmzc","iddymnl","uskihek","evxtehxtbthq","jvtfzddlgch","czohpyewf","ufzazyxtqxcu","brxpfymuvfvs","xrrcfuusicc","aqhlswbzievij","rv","udvmara","upityz","fecd","suxteeitxtg","dfuydrtbfypbn","cypqodxr","wikfuxwjht","jrliuaifpp","vkmxys","wvpfyfpkvgthq","rmajxis","jncxgviyu","av","nmhskodmidaj","lkfrimprrhen","uip","hstyopbvuiqc","p","vwduwmjpblqo","fnxwgqtvwztje","xwnbcuggl","iehimvoymyjasin","spsqiu","flhyfac","mqrbq","pstsxhplrrmbeddv","hnegtuxx","alsyxezjwtlwmxv","jtxytykkcku","bhhlovgcx","xhhivxnutkx","had","aysulvk","m","anhsyxli","jdkgfc","potn","lcibpxkidmwexp","gwoxjicdkv","tltienw","ngiutnuqbzi","o","tzlyb","vumnwehj","os","np","lhv","uzvgyeette","ipfvr","lpprjjalchhhcmh","k","pciulccqssaqgd","tp","dmzdzveslyjad","wtsbhgkd","eouxbldsxzm","vhtonlampljgzyve","xhnlcrldtfthul","xhflc","upgei","rlaks","yfqvnvtnqspyjbxr","phouoyhvls","voibuvbhhjcdflvl","rgorfbjrofokggaf","dqhqats","zchpicyuawpovm","yzwfor","koat","pybf","fhdzsbiyjld","gznfnqydisn","xz","po","tcjup","wygsnxk","kqlima","fgxnuohrnhg","publurhztntgmimc","zuufzphd","iucrmmmjqtcey","wnnbq","rghzyz","ukjqsjbmp","mdtrgv","vyeikgjdnml","kxwldnmi","apzuhsbssaxj","tkbkoljyodlipof","nkq","ktwtj","vgmkgjwle","t","agylw","vomtuy","jbtvitkqn","vtdxwrclpspcn","rdrls","yxfeoh","upj","myctacn","fdnor","ahqghzhoqprgkym","phiuvdv","jp","fdgpouzjwbq","hqoyefmugjvewhxu","qfzwuwe","fnsbijkeepyxry","oja","qthkcij","zpmqfbmnr","ybaibmzonzqlnmd","svo","gjftyfehik","jfrfgznuaytvaegm","aljhrx","odjq","ogwaxrssjxgvnka","zaqswwofedxj","lugpktauixp","dc","odknlbvxrs","jeobu","vqythyvzxbcgrlbg","hwc","erpbaxq","ujxcxck","rrklkb","wlrwyuy","zmg","yyhga","xwdbycdu","htedgvsrhchox","wr","suhesetv","jonqwhkwezjvjgg","sqqyrxtjkcalswq","hvyimhe","pjzdkmoue","zbphmgoxq","lbdlcumdgixjbcq","ztzdjqmadthtdmv","qcagsyqggcf","if","jpjxcjyi","chyicqibxdgkqtg","iwpdklhum","wljmg","micmun","npdbamofynykqv","ijsnfkpfy","lmq","oyjmeqvhcrvgm","mqopusqktdthpvz","fz","r","qbsqtipq","nxtsnason","xbpipyhh","topsuqomfjrd","islif","gbndakaq","bwnkxnwpzeoohlx","hrtbfnq","fguvomeepxoffg","mat","dzfpfnwbfuj","onlvy","cwcchvsasdylb","rxfcztzqopdi","ybrhodjn","oqkijy","ncvrjo","dphbfaal","xgtpdtkz","sebevsopjvciwljf","rcumyacqdapwczen","mabkapuoud","pbozezeygljfftvy","bvazmzbndl","vl","qiaixdtbhqvlzd","ffjfb","svthrfmkoxbho","cvet","ucgqyvopafyttrh","lbgihet","naiqyufxffdw","vruh","uz","ukffmudygjavem","dccamymhp","wofwgjkykm","fbuujzxhln","kmm","lzandlltowjpwsal","fapfvrmezbsjxs","wiw","sc","soqlh","hzaplclkwl","gcdqbcdwbwa","gadgt","pgowefka","juffuguqepwnfh","nbuinl","cpdxf","sox","fq","lfnrhgsxkhx","xrcorfygjxpi","mwtqjwbhgh","loc","fkglorkkvx","nlzdhucvayrz","azefobxutitrf","rlrstkcbtikklmh","ggk","sbphcejuylh","nraoenhd","zngyodiqlchxyycx","rrbhfwohfv","krzolrglgn","cpjesdzy","yoifoyg","hqqevqjugi","ahmv","xgaujnyclcjq","evhyfnlohavrj","byyvhgh","hyw","kedhvwy","ysljsqminajfipds","rglnpxfqwu","cibpynkxg","su","mbntqrlwyampdg","nig","ldhlhqdyjcfhu","jfymrbafmyoc","tyjmnhlfnrtz","dlazixtlxyvm","fbiguhsfuqo","rhymsno","rkbdlchs","ocbbwwd","astaiamnepwkya","mplirup","edkxjq","g","exlwulswtvot","tlnc","vnrrzerz","ygeraoozbtt","yyifkin","eo","ua","qgztvqdolf","rlzddjzcshvd","khxkdxflwxme","kk","zylbhoaac","cw","iizic","gcdxstpz","kjwdqeg","earjrncmmkdel","kbesuhquepj","nrzbllldgdmyrpgl","hllwnqozf","djpchowhwevbqvjj","zsmhylnjpktb","pxnktxkm","fxwiaqqb","qjwufmwresfsfaok","aa","d","iobioqm","svjgzk","khbzp","euexyudhrioi","yqsj","ngrwqpoh","rwuvd","eruffmlg","bxzovyew","faz","pmvfvyguqdi","jlxnoixsy","hyfrdngjf","ly","eibcapetpmeaid","tpnwwiif","pfgsp","kvhhwkzvtvlhhb","pjxurgqbtldims","rncplkeweoirje","akyprzzphew","wyvfpjyglzrmhfqp","ubheeqt","rmbxlcmn","taqakgim","apsbu","khwnykughmwrlk","vtdlzwpbhcsbvjno","tffmjggrmyil","schgwrrzt","mvndmua","nlwpw","glvbtkegzjs","piwllpgnlpcnezqs","xkelind","urtxsezrwz","zechoc","vfaimxrqnyiq","ybugjsblhzfravzn","btgcpqwovwp","zgxgodlhmix","sfzdknoxzassc","wgzvqkxuqrsqxs","dwneyqisozq","fg","vhfsf","uspujvqhydw","eadosqafyxbmzgr","tyff","blolplosqnfcwx","uwkl","puenodlvotb","iizudxqjvfnky","cjcywjkfvukvveq","jrxd","igwb","dftdyelydzyummmt","uvfmaicednym","oai","higfkfavgeemcgo","naefganqo","iqebfibigljbc","ulicojzjfrc","igxprunj","cymbrl","fqmwciqtynca","zjyagi","mzuejrttefhdwqc","zyiurxvf","wrjxffzbjexsh","wrxw","mhrbdxjwi","htknfa","wfrvxqdkhbwwef","vqsghhhutdget","cwupzrts","hbjnb","wpccoa","nx","howbzhaoscgyk","bilt","wqqatye","zceuuwg","jxzon","kkfj","bwsezd","ifdegsyjtswselk","xweimxlnzoh","tqthlftjblnpht","ww","ss","b","jmruuqscwjp","nxbk","wd","cqkrtbxgzg","xhppcjnq","cfq","tkkolzcfi","wblxki","ijeglxsvc","kcqjjwcwuhvzydm","gubqavlqffhrzz","hiwxrgftittd","caybc","ncsyjlzlxyyklc","poxcgnexmaajzuha","dhaccuualacyl","mtkewbprs","oncggqvr","sqqoffmwkplsgbrp","ioajuppvqluhbdet","dzwwzaelmo","afumtqugec","wglucmugwqi","zveswrjevfz","nxlbkak","pzcejvxzeoybb","fd","vewj","ivws","zjhudtpqsfc","zcmukotirrxx","zksmx","umofzhhowyftz","zbotrokaxaryxlk","ueolqk","dxmzhoq","zvu","cjl","esfmqgvxwfy","npbep","vbgjtbv","poeugoqynkbfiv","fewjjscjrei","yqssxzsydgllfzmo","urxkwcypctjkabi","wqtldwhjouas","tovdtkr","onzgeyddkqwuhnim","ffxviyvsktqrfa","qujhd","pvcz","hiyjlkxmeplnrvxg","hdykehkefp","vepcxhozpjxtreyn","liguhuxudbnh","f","ordxzm","klgohcmmbukz","yrmooliaobbnlap","dutnbetocxylcey","ywdsjegd","cr","blbxhjsgcuoxmqft","ngzdc","srfyjjumcbxole","dazwzwtdjoyuqeqj","xazjarqgfm","fxyfqbeoktcc","qrsjchxp","iltaqzawhgu","sgenjcfxr","yfikp","dvwhbyumthkiktb","walsx","jyajrkcvysicisab","brdeumb","tviihjwxdcz","dnrrgmem","ydgxlrjzucxyid","cdvdpvjlagwmg","ngnpxjkxims","gvyhnchlimsxc","w","jtizpezjl","qe","rjzv","vhnqvi","qm","iedzqswrsnfmnn","lt","utqfcqyrrwm","wtelvsqrru","fjwrhjcrtbcytn","qmqxceuohpiffaq","rmoybqjjgdyo","pmxttqftypfexlv","tg","qa","iqbqjlnpbf","kgaynkddbzllecd","tccvslp","curkxfoimnw","fvnyqkzlheruxr","iiygnzfov","coqs","oa","eiu","vzemmxtklis","lxu","nrwsjaxzwmh","tdayz","oxbbemejgosgcynf","ykbcn","hesvnctfvdsp","ku","rjhykpadahbhj","at","sxlngbtxmqr","wqrom","qzyabzrco","rbbyklndcqdj","cnsmgmwmpbgjq","krvnaf","qrwfajnfahyqocdb","fnlaozmff","vmoymbmytjvfcgt","cijyy","jdgwjbztl","swmalgbgpaplqgz","hfl","typttkrpfvx","tkzpzrscwbx","bwfqqvjcukjbsg","nxqmxr","x","eyavnz","il","dhthp","eyelg","npsoqsw","reogbmveofvusdsx","jvdrjkhxkq","qzjbrpljwuzpl","czqeevvbvcwh","vzuszqvhlmapty","yu","yldwwgezlqur","vorxwgdtgjilgydq","pknt","bgihl","ckorgrm","ixylxjmlfv","bpoaboylced","zea","igfagitkrext","ipvqq","dmoerc","oqxbypihdv","dtjrrkxro","rexuhucxpi","bvmuyarjwqpcoywa","qwdmfpwvamisns","bhopoqdsref","tmnm","cre","ktrniqwoofoeenbz","vlrfcsftapyujmw","updqikocrdyex","bcxw","eaum","oklsqebuzeziisw","fzgyhvnwjcns","dybjywyaodsyw","lmu","eocfru","ztlbggsuzctoc","ilfzpszgrgj","imqypqo","fump","sjvmsbrcfwretbie","oxpmplpcg","wmqigymr","qevdyd","gmuyytguexnyc","hwialkbjgzc","lmg","gijjy","lplrsxznfkoklxlv","xrbasbznvxas","twn","bhqultkyfq","saeq","xbuw","zd","kng","uoay","kfykd","armuwp","gtghfxf","gpucqwbihemixqmy","jedyedimaa","pbdrx","toxmxzimgfao","zlteob","adoshnx","ufgmypupei","rqyex","ljhqsaneicvaerqx","ng","sid","zagpiuiia","re","oadojxmvgqgdodw","jszyeruwnupqgmy","jxigaskpj","zpsbhgokwtfcisj","vep","ebwrcpafxzhb","gjykhz","mfomgxjphcscuxj","iwbdvusywqlsc","opvrnx","mkgiwfvqfkotpdz","inpobubzbvstk","vubuucilxyh","bci","dibmye","rlcnvnuuqfvhw","oorbyyiigppuft","swpksfdxicemjbf","goabwrqdoudf","yjutkeqakoarru","wuznnlyd","vfelxvtggkkk","mxlwbkbklbwfsvr","advraqovan","smkln","jxxvzdjlpyurxpj","ssebtpznwoytjefo","dynaiukctgrzjx","irzosjuncvh","hcnhhrajahitn","vwtifcoqepqyzwya","kddxywvgqxo","syxngevs","batvzmziq","mjewiyo","pzsupxoflq","byzhtvvpj","cqnlvlzr","akvmxzbaei","mwo","vg","ekfkuajjogbxhjii","isdbplotyak","jvkmxhtmyznha","lqjnqzrwrmgt","mbbhfli","bpeohsufree","ajrcsfogh","lucidbnlysamvy","tutjdfnvhahxy","urbrmmadea","hghv","acnjx","athltizloasimp","gu","rjfozvgmdakdhao","iephs","uztnpqhdl","rfuyp","crcszmgplszwfn","zihegt","xbspa","cjbmsamjyqqrasz","ghzlgnfoas","ljxl","cnumquohlcgt","jm","mfccj","hfedli","vtpieworwhyiucs","tdtuquartspkotm","pnkeluekvelj","ugrloq","zljmwt","fkyvqguqq","tpjidglpxqfxv","l","tvvimvroz","yy","opwyfovdz","pwlumocnyuoume","vjqpzkcfc","ihicd","dtttiixlhpikbv","goblttgvmndkqgg","gwsibcqahmyyeagk","prtvoju","lcblwidhjpu","kbu","pey","gkzrpc","bqajopjjlfthe","bc","lqs","zkndgojnjnxqsoqi","zyesldujjlp","drswybwlfyzph","xzluwbtmoxokk","bedrqfui","opajzeahv","lehdfnr","mnlpimduzgmwszc","velbhj","miwdn","wruqc","kscfodjxg","wcbm"}; + print_vec(Solution().findAllConcatenatedWordsInADict(words3)); + + return 0; +} diff --git a/0001-0500/0475-Heaters/cpp-0475/CMakeLists.txt b/0001-0500/0475-Heaters/cpp-0475/CMakeLists.txt new file mode 100644 index 00000000..ec7e7e06 --- /dev/null +++ b/0001-0500/0475-Heaters/cpp-0475/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0475) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0475 main.cpp) diff --git a/0001-0500/0475-Heaters/cpp-0475/main.cpp b/0001-0500/0475-Heaters/cpp-0475/main.cpp new file mode 100644 index 00000000..d053e7e2 --- /dev/null +++ b/0001-0500/0475-Heaters/cpp-0475/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/heaters/ +/// Author : liuyubobobo +/// Time : 2021-12-19 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlog(max_pos)) +/// Space Complexity: O(1) +class Solution { +public: + int findRadius(vector& houses, vector& heaters) { + + sort(houses.begin(), houses.end()); + sort(heaters.begin(), heaters.end()); + + int l = 0, r = 1e9; + while(l < r){ + int mid = (l + r) / 2; + if(ok(houses, heaters, mid)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(const vector& houses, const vector& heaters, int r){ + + int i = 0, j = 0; + while(i < houses.size()){ + if(j >= heaters.size()) return false; + if(heaters[j] - r <= houses[i] && houses[i] <= heaters[j] + r) + i ++; + else j ++; + } + return true; + } +}; + + +int main() { + + return 0; +} 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/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..a173e7aa --- /dev/null +++ b/0001-0500/0481-Magical-String/cpp-0481/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/magical-string/ +/// Author : liuyubobobo +/// Time : 2022-10-30 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +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/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/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/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/0001-0500/0488-Zuma-Game/cpp-0488/CMakeLists.txt b/0001-0500/0488-Zuma-Game/cpp-0488/CMakeLists.txt new file mode 100644 index 00000000..f48255d9 --- /dev/null +++ b/0001-0500/0488-Zuma-Game/cpp-0488/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0488) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0488 main.cpp) diff --git a/0001-0500/0488-Zuma-Game/cpp-0488/main.cpp b/0001-0500/0488-Zuma-Game/cpp-0488/main.cpp new file mode 100644 index 00000000..4afc9717 --- /dev/null +++ b/0001-0500/0488-Zuma-Game/cpp-0488/main.cpp @@ -0,0 +1,116 @@ +/// Source : https://leetcode.com/problems/zuma-game/ +/// Author : liuyubobobo +/// Time : 2021-11-09 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n!) +/// Space Complexity: O(n!) +class Solution { + +private: + const map char2num = {{'R', 0}, {'Y', 1}, {'B', 2}, {'G', 3}, {'W', 4}}; + +public: + int findMinStep(string board_str, string hand_str){ + + string board = board_str; + for(int i = 0; i < board_str.size(); i ++) board[i] = ('0' + char2num.at(board_str[i])); + + string hand_f(5, '0'); + for(int i =0; i < hand_str.size(); i ++) hand_f[char2num.at(hand_str[i])] ++; + + int res = INT_MAX; + unordered_set visited; + dfs(board, hand_f, hand_str.size(), res, visited); + return res == INT_MAX ? -1 : res; + } + + +private: + void dfs(const string& s, string& hand_f, const int handnum, int& res, + unordered_set& visited){ + + string hash = s + "#" + hand_f; + if(visited.count(hash)) return; + + if(s == ""){ + int left = 0; + for(char c: hand_f) left += (c - '0'); + res = min(res, handnum - left); + visited.insert(hash); + return; + } + + for(int start = 0, i = start + 1; i <= s.size(); i ++) + if(i == s.size() || s[i] != s[start]){ + if(i - start >= 3){ + dfs(s.substr(0, start) + s.substr(i), hand_f, handnum, res, visited); + visited.insert(hash); + return; + } + start = i; + } + + for(int card = 0; card < hand_f.size(); card ++) + if(hand_f[card] > '0'){ + string news; + for(int start = 0, i = 1; i <= s.size(); i ++) + if(i == s.size() || s[i] != s[start]){ + if(card == s[start] - '0'){ + news = s.substr(0, i) + string(1, '0' + card) + s.substr(i); + hand_f[card] --; + dfs(news, hand_f, handnum, res, visited); + hand_f[card] ++; + } + else{ + news = s.substr(0, start) + string(1, '0' + card) + s.substr(start); + hand_f[card] --; + dfs(news, hand_f, handnum, res, visited); + hand_f[card] ++; + + news = s.substr(0, i) + string(1, '0' + card) + s.substr(i); + hand_f[card] --; + dfs(news, hand_f, handnum, res, visited); + hand_f[card] ++; + + if(i - start > 1){ + news = s.substr(0, start + 1) + string(1, '0' + card) + s.substr(start + 1); + hand_f[card] --; + dfs(news, hand_f, handnum, res, visited); + hand_f[card] ++; + } + } + start = i; + } + } + + visited.insert(hash); + } +}; + + +int main() { + + cout << Solution().findMinStep("WWRRBBWW", "WRBRW") << endl; + // 2 + + cout << Solution().findMinStep("RBYYBBRRB", "YRBGB") << endl; + // 3 + + cout << Solution().findMinStep("RRWWRRBBRR", "WB") << endl; + // 2 + + cout << Solution().findMinStep("WWBBWBBWW", "BB") << endl; + // -1 + + return 0; +} 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/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/0001-0500/0492-Construct-the-Rectangle/cpp-0492/CMakeLists.txt b/0001-0500/0492-Construct-the-Rectangle/cpp-0492/CMakeLists.txt new file mode 100644 index 00000000..13cb473e --- /dev/null +++ b/0001-0500/0492-Construct-the-Rectangle/cpp-0492/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0492) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0492 main.cpp) diff --git a/0001-0500/0492-Construct-the-Rectangle/cpp-0492/main.cpp b/0001-0500/0492-Construct-the-Rectangle/cpp-0492/main.cpp new file mode 100644 index 00000000..dfc5d5b4 --- /dev/null +++ b/0001-0500/0492-Construct-the-Rectangle/cpp-0492/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/construct-the-rectangle/ +/// Author : liuyubobobo +/// Time : 2021-10-22 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(sqrt(area)) +/// Space Complexity: O(1) +class Solution { +public: + vector constructRectangle(int area) { + + for(int i = (int)(sqrt(area) + 1e-6); i >= 1; i --){ + if(area % i == 0) return {area / i, i}; + } + assert(false); + return {-1, -1}; + } +}; + + +int main() { + + return 0; +} 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 diff --git a/0001-0500/0495-Teemo-Attacking/cpp-0495/CMakeLists.txt b/0001-0500/0495-Teemo-Attacking/cpp-0495/CMakeLists.txt new file mode 100644 index 00000000..50d2431c --- /dev/null +++ b/0001-0500/0495-Teemo-Attacking/cpp-0495/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0495) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0495 main.cpp) diff --git a/0001-0500/0495-Teemo-Attacking/cpp-0495/main.cpp b/0001-0500/0495-Teemo-Attacking/cpp-0495/main.cpp new file mode 100644 index 00000000..cdf24563 --- /dev/null +++ b/0001-0500/0495-Teemo-Attacking/cpp-0495/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/teemo-attacking/ +/// Author : liuyubobobo +/// Time : 2021-11-09 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int findPoisonedDuration(vector& timeSeries, int duration) { + + int n = timeSeries.size(); + int start = timeSeries[0], end = timeSeries[0] + duration, res = 0; + for(int i = 1; i <= n; i ++) + if(i == n || timeSeries[i] > end){ + res += end - start; + if(i < n) start = timeSeries[i], end = timeSeries[i] + duration; + else break; + } + else end = timeSeries[i] + duration; + return res; + } +}; + + +int main() { + + vector t1 = {1, 2}; + cout << Solution().findPoisonedDuration(t1, 2) << endl; + // 3 + + return 0; +} diff --git a/0001-0500/0500-Keyboard-Row/cpp-0500/CMakeLists.txt b/0001-0500/0500-Keyboard-Row/cpp-0500/CMakeLists.txt new file mode 100644 index 00000000..0dccc892 --- /dev/null +++ b/0001-0500/0500-Keyboard-Row/cpp-0500/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0500) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0500 main.cpp) diff --git a/0001-0500/0500-Keyboard-Row/cpp-0500/main.cpp b/0001-0500/0500-Keyboard-Row/cpp-0500/main.cpp new file mode 100644 index 00000000..281aa3bb --- /dev/null +++ b/0001-0500/0500-Keyboard-Row/cpp-0500/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/keyboard-row/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { + +public: + vector findWords(vector& words) { + + vector keyboard = {"qwertyuiop", "asdfghjkl", "zxcvbnm"}; + vector key(26); + for(int i = 0; i < 3; i ++) + for(char c: keyboard[i]) key[c - 'a'] = i; + + vector res; + for(const string& word: words) + if(ok(key, word)) res.push_back(word); + return res; + } + +private: + bool ok(const vector& key, const string& word){ + + int t = key[tolower(word[0]) - 'a']; + for(int i = 1; i < word.size(); i ++) + if(key[tolower(word[i]) - 'a'] != t) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0504-Base-7/cpp-0504/CMakeLists.txt b/0501-1000/0504-Base-7/cpp-0504/CMakeLists.txt new file mode 100644 index 00000000..0234e019 --- /dev/null +++ b/0501-1000/0504-Base-7/cpp-0504/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0504) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0504 main.cpp) diff --git a/0501-1000/0504-Base-7/cpp-0504/main.cpp b/0501-1000/0504-Base-7/cpp-0504/main.cpp new file mode 100644 index 00000000..ed637e32 --- /dev/null +++ b/0501-1000/0504-Base-7/cpp-0504/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/base-7/ +/// Author : liuyubobobo +/// Time : 2022-03-06 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + string convertToBase7(int num) { + + bool pos = true; + if(num < 0) num = -num, pos = false; + + string res = ""; + while(num){ + res += (char)('0' + num % 7); + num /= 7; + } + reverse(res.begin(), res.end()); + if(res.empty()) res = "0"; + + return (pos ? "" : "-") + res; + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0506-Relative-Ranks/cpp-0506/CMakeLists.txt b/0501-1000/0506-Relative-Ranks/cpp-0506/CMakeLists.txt new file mode 100644 index 00000000..2caac2bc --- /dev/null +++ b/0501-1000/0506-Relative-Ranks/cpp-0506/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0506) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0506 main.cpp) diff --git a/0501-1000/0506-Relative-Ranks/cpp-0506/main.cpp b/0501-1000/0506-Relative-Ranks/cpp-0506/main.cpp new file mode 100644 index 00000000..2f801764 --- /dev/null +++ b/0501-1000/0506-Relative-Ranks/cpp-0506/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/relative-ranks/ +/// Author : liuyubobobo +/// Time : 2021-12-02 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + vector findRelativeRanks(vector& score) { + + int n = score.size(); + vector> data(n); + for(int i = 0; i < n; i ++) + data[i] = {score[i], i}; + + sort(data.begin(), data.end(), greater>()); + + vector res(n); + + if(n >= 1) res[data[0].second] = "Gold Medal"; + if(n >= 2) res[data[1].second] = "Silver Medal"; + if(n >= 3) res[data[2].second] = "Bronze Medal"; + for(int i = 3; i < n; i ++) res[data[i].second] = to_string(i + 1); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0507-Perfect-Number/cpp-0507/CMakeLists.txt b/0501-1000/0507-Perfect-Number/cpp-0507/CMakeLists.txt new file mode 100644 index 00000000..86287a35 --- /dev/null +++ b/0501-1000/0507-Perfect-Number/cpp-0507/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0507) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0507 main.cpp) diff --git a/0501-1000/0507-Perfect-Number/cpp-0507/main.cpp b/0501-1000/0507-Perfect-Number/cpp-0507/main.cpp new file mode 100644 index 00000000..4b694af1 --- /dev/null +++ b/0501-1000/0507-Perfect-Number/cpp-0507/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/perfect-number/ +/// Author : liuyubobobo +/// Time : 2021-12-30 + +#include +#include +#include + +using namespace std; + + +/// Scan all the divisors +/// Time Complexity: O(sqrt(n)) +/// Space Complexity: O(sqrt(n)) +class Solution { +public: + bool checkPerfectNumber(int num) { + + vector d; + for(int i = 1; i * i <= num; i ++){ + if(num % i == 0){ + if(i != num) d.push_back(i); + if(i * i != num && i != 1) d.push_back(num / i); + } + } + + return accumulate(d.begin(), d.end(), 0) == num; + } +}; + + +int main() { + + return 0; +} 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/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/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/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/0501-1000/0520-Detect-Capital/cpp-0520/CMakeLists.txt b/0501-1000/0520-Detect-Capital/cpp-0520/CMakeLists.txt new file mode 100644 index 00000000..7aedab23 --- /dev/null +++ b/0501-1000/0520-Detect-Capital/cpp-0520/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0520) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0520 main.cpp) diff --git a/0501-1000/0520-Detect-Capital/cpp-0520/main.cpp b/0501-1000/0520-Detect-Capital/cpp-0520/main.cpp new file mode 100644 index 00000000..8b600cdf --- /dev/null +++ b/0501-1000/0520-Detect-Capital/cpp-0520/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/detect-capital/ +/// Author : liuyubobobo +/// Time : 2021-11-12 + +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|s|) +/// Space Complexity: O(1) +class Solution { +public: + bool detectCapitalUse(string word) { + + return all_upper(word) || all_lower(word) || (isupper(word[0]) && all_lower(word.substr(1))); + } + +private: + bool all_upper(const string& s){ + for(char c: s) if(islower(c)) return false; + return true; + } + + bool all_lower(const string& s){ + for(char c: s) if(isupper(c)) return false; + return true; + } +}; + + +int main() { + + return 0; +} 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/0501-1000/0532-K-diff-Pairs-in-an-Array/cpp-0532/CMakeLists.txt b/0501-1000/0532-K-diff-Pairs-in-an-Array/cpp-0532/CMakeLists.txt new file mode 100644 index 00000000..78a5c7dd --- /dev/null +++ b/0501-1000/0532-K-diff-Pairs-in-an-Array/cpp-0532/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0532) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0532 main.cpp) diff --git a/0501-1000/0532-K-diff-Pairs-in-an-Array/cpp-0532/main.cpp b/0501-1000/0532-K-diff-Pairs-in-an-Array/cpp-0532/main.cpp new file mode 100644 index 00000000..ec29f8fb --- /dev/null +++ b/0501-1000/0532-K-diff-Pairs-in-an-Array/cpp-0532/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/k-diff-pairs-in-an-array/ +/// Author : liuyubobobo +/// Time : 2022-02-08 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int findPairs(vector& nums, int k) { + + multiset left; + set> record; + for(int e: nums) left.insert(e); + + for(int i = (int)nums.size() - 1; i >= 0; i --){ + left.erase(left.find(nums[i])); + int a = nums[i] - k, b = nums[i], c = nums[i] + k; + if(left.count(a)) record.insert({min(a, b), max(a, b)}); + if(left.count(c)) record.insert({min(b, c), max(b, c)}); + } + return record.size(); + } +}; + + +int main() { + + vector nums1 = {3, 1, 4, 1, 5}; + cout << Solution().findPairs(nums1, 2) << endl; + // 2 + + vector nums2 = {1, 3, 1, 5, 4}; + cout << Solution().findPairs(nums2, 0) << endl; + // 1 + + vector nums3 = {1,2,4,4,3,3,0,9,2,3}; + cout << Solution().findPairs(nums3, 3) << endl; + // 2 + + return 0; +} diff --git a/0501-1000/0539-Minimum-Time-Difference/cpp-0539/CMakeLists.txt b/0501-1000/0539-Minimum-Time-Difference/cpp-0539/CMakeLists.txt new file mode 100644 index 00000000..b99e8673 --- /dev/null +++ b/0501-1000/0539-Minimum-Time-Difference/cpp-0539/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0539) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0539 main.cpp) diff --git a/0501-1000/0539-Minimum-Time-Difference/cpp-0539/main.cpp b/0501-1000/0539-Minimum-Time-Difference/cpp-0539/main.cpp new file mode 100644 index 00000000..d08cfa86 --- /dev/null +++ b/0501-1000/0539-Minimum-Time-Difference/cpp-0539/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/minimum-time-difference/ +/// Author : liuyubobobo +/// Time : 2022-01-17 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Compelexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int findMinDifference(vector& timePoints) { + + int n = timePoints.size(); + + vector t(n); + for(int i = 0; i < n; i ++) + t[i] = atoi(timePoints[i].substr(0, 2).c_str()) * 60 + atoi(timePoints[i].substr(3).c_str()); + + sort(t.begin(), t.end()); + + int res = INT_MAX; + for(int i = 1; i < timePoints.size(); i ++) + res = min(res, t[i] - t[i - 1]); + res = min(res, t[0] - t.back() + 24 * 60); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0553-Optimal-Division/cpp-0553/CMakeLists.txt b/0501-1000/0553-Optimal-Division/cpp-0553/CMakeLists.txt new file mode 100644 index 00000000..eda8775f --- /dev/null +++ b/0501-1000/0553-Optimal-Division/cpp-0553/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0553) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0553 main.cpp) diff --git a/0501-1000/0553-Optimal-Division/cpp-0553/main.cpp b/0501-1000/0553-Optimal-Division/cpp-0553/main.cpp new file mode 100644 index 00000000..b14cbdd0 --- /dev/null +++ b/0501-1000/0553-Optimal-Division/cpp-0553/main.cpp @@ -0,0 +1,89 @@ +/// Source : https://leetcode.com/problems/optimal-division/ +/// Author : liuyubobobo +/// Time : 2022-02-27 + +#include +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { + +public: + string optimalDivision(vector& nums) { + + int n = nums.size(); + + vector> dp_max(n, vector(n, 0.0)); + vector> dp_max_sz(n, vector(n, 1)); + vector> dp_min(n, vector(n, 0.0)); + vector> dp_min_sz(n, vector(n, 1)); + for(int i = 0; i < n; i ++) + dp_max[i][i] = dp_min[i][i] = nums[i]; + for(int i = 0; i + 1 < n; i ++) + dp_max[i][i + 1] = dp_min[i][i + 1] = (long double)nums[i] / nums[i + 1]; + for(int sz = 2; sz <= n; sz ++) + for(int i = 0; i + sz <= n; i ++){ + // calc dp[i][i + sz - 1] + dp_max[i][i + sz - 1] = dp_max[i][i] / dp_min[i + 1][i + sz - 1]; + dp_min[i][i + sz - 1] = dp_min[i][i] / dp_max[i + 1][i + sz - 1]; + for(int sz1 = 2; sz1 < sz; sz1 ++){ + if(dp_max[i][i + sz1 - 1] / dp_min[i + sz1][i + sz - 1] > dp_max[i][i + sz - 1]) + dp_max[i][i + sz - 1] = dp_max[i][i + sz1 - 1] / dp_min[i + sz1][i + sz - 1], dp_max_sz[i][i + sz - 1] = sz1; + if(dp_min[i][i + sz1 - 1] / dp_max[i + sz1][i + sz - 1] < dp_min[i][i + sz - 1]) + dp_min[i][i + sz - 1] = dp_min[i][i + sz1 - 1] / dp_max[i + sz1][i + sz - 1], dp_min_sz[i][i + sz - 1] = sz1; + } + } + + return get_string(nums, 0, n - 1, "max", dp_max_sz, dp_min_sz); + } + +private: + string get_string(const vector& nums, int l, int r, const string& type, + const vector>& dp_max_sz, + const vector>& dp_min_sz){ + + if(l == r) return to_string(nums[l]); + if(l + 1 == r) return to_string(nums[l]) + "/" + to_string(nums[r]); + + if(type == "max"){ + int sz = r - l + 1, sz1 = dp_max_sz[l][r]; + string left_str = get_string(nums, l, l + sz1 - 1, "max", dp_max_sz, dp_min_sz); + string right_str = get_string(nums, l + sz1, l + sz - 1, "min", dp_max_sz, dp_min_sz); + if(l + sz1 == l + sz - 1) return left_str + "/" + right_str; + return left_str + "/(" + right_str + ")"; + } + else{ + assert(type == "min"); + + int sz = r - l + 1, sz1 = dp_min_sz[l][r]; + string left_str = get_string(nums, l, l + sz1 - 1, "min", dp_max_sz, dp_min_sz); + string right_str = get_string(nums, l + sz1, l + sz - 1, "max", dp_max_sz, dp_min_sz); + if(l + sz1 == l + sz - 1) return left_str + "/" + right_str; + return left_str + "/(" + right_str + ")"; + } + } +}; + + +int main() { + + vector nums1 = {1000, 100, 10, 2}; + cout << Solution().optimalDivision(nums1) << endl; + // 1000/(100/10/2) + + vector nums2 = {2, 3, 4}; + cout << Solution().optimalDivision(nums2) << endl; + // 2/(3/4) + + vector nums3 = {2}; + cout << Solution().optimalDivision(nums3) << endl; + // 2 + + return 0; +} 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/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; } } diff --git a/0501-1000/0564-Find-the-Closest-Palindrome/cpp-0564/CMakeLists.txt b/0501-1000/0564-Find-the-Closest-Palindrome/cpp-0564/CMakeLists.txt new file mode 100644 index 00000000..c229437c --- /dev/null +++ b/0501-1000/0564-Find-the-Closest-Palindrome/cpp-0564/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0564) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0564 main.cpp) diff --git a/0501-1000/0564-Find-the-Closest-Palindrome/cpp-0564/main.cpp b/0501-1000/0564-Find-the-Closest-Palindrome/cpp-0564/main.cpp new file mode 100644 index 00000000..2fbb9938 --- /dev/null +++ b/0501-1000/0564-Find-the-Closest-Palindrome/cpp-0564/main.cpp @@ -0,0 +1,97 @@ +/// Source : https://leetcode.com/problems/find-the-closest-palindrome/ +/// Author : liuyubobobo +/// Time : 2022-03-03 + +#include +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(|n|) +/// Space Complexity: O(|n|) +class Solution { +public: + string nearestPalindromic(string n) { + + long long num = atoll(n.c_str()); + + vector possible_res; + get_palindrome_number(n, possible_res); + get_palindrome_number(to_string(num + 2), possible_res); + if(num - 2 >= 0) + get_palindrome_number(to_string(num - 2), possible_res); + + int len = n.size(); + if(len % 2){ + int m = len / 2; + get_num(num, m, '+', possible_res); + get_num(num, m, '-', possible_res); + } + else{ + int m1 = len / 2 - 1, m2 = len / 2; + get_num(num, m1, '+', possible_res); + get_num(num, m1, '-', possible_res); + get_num(num, m2, '+', possible_res); + get_num(num, m2, '-', possible_res); + } + + long long res = -1, best_diff = LONG_LONG_MAX; + for(long long a: possible_res){ + if(a == num) continue; + long long diff = abs(a - num); + if(diff < best_diff) res = a, best_diff = diff; + else if(diff == best_diff) res = min(res, a); + } + assert(res != -1); + return to_string(res); + } + +private: + void get_num(long long num, int pos, char type, + vector& possible_res){ + + if(type == '+') + num += (long long)pow(10, pos); + else{ + num -= (long long)pow(10, pos); + assert(num >= 0); + } + + get_palindrome_number(to_string(num), possible_res); + } + + void get_palindrome_number(const string& n, vector& possible_res){ + + string n1 = n; + for(int i = 0, j = n1.size() - 1; i < j; i ++, j --) + n1[i] = n1[j]; + possible_res.push_back(atoll(n1.c_str())); + + string n2 = n; + for(int i = 0, j = n1.size() - 1; i < j; i ++, j --) + n2[j] = n2[i]; + possible_res.push_back(atoll(n2.c_str())); + } +}; + + +int main() { + + cout << Solution().nearestPalindromic("123") << '\n'; + // 121 + + cout << Solution().nearestPalindromic("1") << '\n'; + // 0 + + cout << Solution().nearestPalindromic("9") << '\n'; + // 8 + + cout << Solution().nearestPalindromic("11") << '\n'; + // 9 + + return 0; +} 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; -} 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..1028ffaf --- /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 main.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..f7eb4775 --- /dev/null +++ b/0501-1000/0591-Tag-Validator/cpp-0591/main.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/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/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/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/0501-1000/0638-Shopping-Offers/cpp-0638/CMakeLists.txt b/0501-1000/0638-Shopping-Offers/cpp-0638/CMakeLists.txt new file mode 100644 index 00000000..40b629c1 --- /dev/null +++ b/0501-1000/0638-Shopping-Offers/cpp-0638/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0638) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0638 main.cpp) diff --git a/0501-1000/0638-Shopping-Offers/cpp-0638/main.cpp b/0501-1000/0638-Shopping-Offers/cpp-0638/main.cpp new file mode 100644 index 00000000..e4133969 --- /dev/null +++ b/0501-1000/0638-Shopping-Offers/cpp-0638/main.cpp @@ -0,0 +1,77 @@ +/// Source : https://leetcode.com/problems/shopping-offers/ +/// Author : liuyubobobo +/// Time : 2021-10-23 + +#include +#include +#include + +using namespace std; + + +/// Backpack +/// Memoization +/// Time Complexity: O((price[i] ^ n) * m) +/// Space Complexity: O(price[i] ^ n) +class Solution { +public: + int shoppingOffers(vector& prices, vector>& specials, vector& needs) { + + int n = prices.size(); + int m = specials.size(); + map, int> dp; + return dfs(n, needs, m, specials, prices, dp); + } + +private: + int dfs(int n, const vector& need, int m, const vector>& specials, + const vector& prices, map, int>& dp){ + + if(dp.count(need)) return dp[need]; + + int res = 0; + for(int i = 0; i < n; i ++) res += need[i] * prices[i]; + + for(int i = 0; i < m; i ++) + if(less_or_equals(n, specials[i], need)) + res = min(res, specials[i].back() + dfs(n, minus(n, need, specials[i]), m, specials, prices, dp)); + + return dp[need] = res; + } + + bool less_or_equals(int n, const vector& a, const vector& b){ + for(int i = 0; i < n; i ++) + if(a[i] > b[i]) return false; + return true; + } + + vector minus(int n, const vector& a, const vector& b){ + vector res(n); + for(int i = 0; i < n; i ++) res[i] = a[i] - b[i]; + return res; + } +}; + + +int main() { + + vector prices1 = {2, 5}; + vector> specials1 = {{3, 0, 5}, {1, 2, 10}}; + vector needs1= {3, 2}; + cout << Solution().shoppingOffers(prices1, specials1, needs1) << endl; + // 14 + + vector prices2 = {2, 3, 4}; + vector> specials2 = {{1, 1, 0, 4}, {2, 2, 1, 9}}; + vector needs2 = {1, 2, 1}; + cout << Solution().shoppingOffers(prices2, specials2, needs2) << endl; + // 11 + + vector prices3 = {0, 0, 0}; + vector> specials3 = {{1, 1, 0, 4}, {2, 2, 1, 9}}; + vector needs3 = {1, 1, 1}; + cout << Solution().shoppingOffers(prices3, specials3, needs3) << endl; + // 0 + + return 0; +} 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/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/0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/CMakeLists.txt b/0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/CMakeLists.txt new file mode 100644 index 00000000..f3eaf285 --- /dev/null +++ b/0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0653) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0653 main.cpp) diff --git a/0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/main.cpp b/0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/main.cpp new file mode 100644 index 00000000..6b2fe3b7 --- /dev/null +++ b/0501-1000/0653-Two-Sum-IV-Input-is-a-BST/cpp-0653/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/two-sum-iv-input-is-a-bst/ +/// Author : liuyubobobo +/// Time : 2022-03-20 + +#include +#include + +using namespace std; + + +/// Using Set +/// 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 { + +private: + unordered_set table; + +public: + bool findTarget(TreeNode* root, int k) { + + table.clear(); + return dfs(root, k); + } + +private: + bool dfs(TreeNode* node, int t){ + + if(!node) return false; + + if(table.count(t - node->val)) return true; + table.insert(node->val); + return dfs(node->left, t) || dfs(node->right, t); + } +}; + + +int main() { + + return 0; +} 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/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/0501-1000/0661-Image-Smoother/cpp-0661/CMakeLists.txt b/0501-1000/0661-Image-Smoother/cpp-0661/CMakeLists.txt new file mode 100644 index 00000000..f8279eac --- /dev/null +++ b/0501-1000/0661-Image-Smoother/cpp-0661/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0661) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0661 main.cpp) diff --git a/0501-1000/0661-Image-Smoother/cpp-0661/main.cpp b/0501-1000/0661-Image-Smoother/cpp-0661/main.cpp new file mode 100644 index 00000000..6538ee30 --- /dev/null +++ b/0501-1000/0661-Image-Smoother/cpp-0661/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/image-smoother/ +/// Author : liuyubobobo +/// Time : 2022-03-23 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(R * C) +/// Space Complexity: O(1) +class Solution { +private: + int R, C; + +public: + vector> imageSmoother(vector>& img) { + + R = img.size(), C = img[0].size(); + vector> res(R, vector(C)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++){ + int sum = 0, cnt = 0; + for(int r = -1; r <= 1; r ++) + for(int c = -1; c <= 1; c ++) + if(in_area(i + r, j + c)) + sum += img[i + r][j + c], cnt ++; + res[i][j] = sum / cnt; + } + 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/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 new file mode 100644 index 00000000..8e5f9d1f --- /dev/null +++ b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0662) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0662 main2.cpp) diff --git a/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main.cpp b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main.cpp new file mode 100644 index 00000000..b7da22b3 --- /dev/null +++ b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/maximum-width-of-binary-tree/ +/// Author : liuyubobobo +/// Time : 2022-02-27 + +#include +#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: + int widthOfBinaryTree(TreeNode* root) { + + int max_depth = get_depth(root); + + vector left(max_depth, ULONG_LONG_MAX), right(max_depth,0); + dfs(root, 0, 0, left, right); + + long long res = 0; + for(int i = 0; i < max_depth; i ++){ + res = max(res, (long long)(right[i] - left[i] + 1)); + } + return res; + } + +private: + void dfs(TreeNode* node, int d, unsigned long long num, + vector& left, vector& right){ + + if(!node) return; + + left[d] = min(left[d], num); + right[d] = max(right[d], num); + + dfs(node->left, d + 1, num * 2, left, right); + dfs(node->right, d + 1, num * 2 + 1, left, right); + } + + int get_depth(TreeNode* node){ + + if(!node) return 0; + + return 1 + max(get_depth(node->left), get_depth(node->right)); + } +}; + + +int main() { + + return 0; +} 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..8e471089 --- /dev/null +++ b/0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/main2.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/maximum-width-of-binary-tree/ +/// Author : liuyubobobo +/// 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}); + vector list; + + unsigned long long res = 0; + while(!q.empty()){ + + vector> v; + while(!q.empty()){ + TreeNode* node = q.front().first; + unsigned long long seq = q.front().second; + q.pop(); + v.push_back({node, seq}); + } + + 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}); + } + + } + return res; + } +}; + + +int main() { + + return 0; +} 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/0501-1000/0677-Map-Sum-Pairs/cpp-0677/CMakeLists.txt b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/CMakeLists.txt index 9538df7b..40a12f9e 100644 --- a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/CMakeLists.txt +++ b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/CMakeLists.txt @@ -3,5 +3,5 @@ project(cpp_0677) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main4.cpp) +set(SOURCE_FILES main3.cpp) add_executable(cpp_0677 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main.cpp b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main.cpp index fd33231d..914c5c25 100644 --- a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main.cpp +++ b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main.cpp @@ -7,6 +7,7 @@ using namespace std; + /// HashMap + Brute Force /// Time Complexity: insert: O(1) /// sum: O(n * len(prefix)) @@ -35,6 +36,7 @@ class MapSum { } }; + int main() { MapSum obj; diff --git a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main2.cpp b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main2.cpp index bc5420a3..85ab9d0f 100644 --- a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main2.cpp +++ b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main2.cpp @@ -16,6 +16,7 @@ class MapSum { private: unordered_map prefixScore; unordered_map summap; + public: /** Initialize your data structure here. */ MapSum() { @@ -30,8 +31,9 @@ class MapSum { prefixScore[key.substr(0, i)] += val; } else{ + int old_value = summap[key]; for(int i = 1 ; i <= key.size() ; i ++) - prefixScore[key.substr(0, i)] = val; + prefixScore[key.substr(0, i)] += val - old_value; } summap[key] = val; @@ -42,6 +44,7 @@ class MapSum { } }; + int main() { MapSum obj; diff --git a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main3.cpp b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main3.cpp index b36f3fdf..903260ca 100644 --- a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main3.cpp +++ b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main3.cpp @@ -8,11 +8,11 @@ using namespace std; + /// Trie /// Time Complexity: insert: O(len(prefix)) /// sum: O(sum(len(wi))) /// Space Complexity: O(sum(len(wi))) where wi is the length of the ith word. - class Trie{ private: @@ -91,6 +91,7 @@ class MapSum { } }; + int main() { MapSum obj; diff --git a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main4.cpp b/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main4.cpp deleted file mode 100644 index b4babe53..00000000 --- a/0501-1000/0677-Map-Sum-Pairs/cpp-0677/main4.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/// Source : https://leetcode.com/problems/map-sum-pairs/description/ -/// Author : liuyubobobo -/// Time : 2017-11-05 - -#include -#include -#include - -using namespace std; - -/// Trie -/// Update key value when key already in the trie -/// Time Complexity: insert: O(len(prefix)) -/// sum: O(len(prefix)) -/// Space Complexity: O(sum(len(wi))) where wi is the length of the ith word. - -class Trie{ - -private: - struct Node{ - map next; - int val = 0; - bool end = false; - }; - vector trie; - -public: - Trie(){ - trie.clear(); - trie.push_back(Node()); - } - - void insert(const string& word, int val){ - insert(0, word, 0, val); -// cout << "After insert " << word << ", trie is:" << endl; -// print(); - } - - int sum(const string& prefix){ - int treeID = findTreeID(0, prefix, 0); - if(treeID == -1) - return 0; - return trie[treeID].val; - } - -private: - int insert(int treeID, const string& word, int index, int val){ - - if(index == word.size()) { - - if(trie[treeID].end){ - int change = val - trie[treeID].val; - trie[treeID].val = val; - return change; - } - else{ - trie[treeID].end = true; - trie[treeID].val += val; - return val; - } - } - - if(trie[treeID].next.find(word[index]) == trie[treeID].next.end()){ - trie[treeID].next[word[index]] = trie.size(); - trie.push_back(Node()); - } - - int change = insert(trie[treeID].next[word[index]], word, index + 1, val); - trie[treeID].val += change; - - return change; - } - - int findTreeID(int treeID, const string& word, int index){ - - if(index == word.size()) - return treeID; - - if(trie[treeID].next.find(word[index]) == trie[treeID].next.end()) - return -1; - - return findTreeID(trie[treeID].next[word[index]], word, index + 1); - } - - void print(){ - for(Node node: trie) - cout << node.val << " "; - cout << endl; - } -}; - -class MapSum { - -private: - Trie trie; - -public: - - void insert(string key, int val) { - trie.insert(key, val); - } - - int sum(string prefix) { - return trie.sum(prefix); - } -}; - -int main() { - - MapSum obj; - obj.insert("apple", 3); - cout << obj.sum("ap") << endl; // 3 - obj.insert("app", 2); - cout << obj.sum("ap") << endl; // 5 - - return 0; -} \ No newline at end of file diff --git a/0501-1000/0680-Valid-Palindrome-II/cpp-0680/CMakeLists.txt b/0501-1000/0680-Valid-Palindrome-II/cpp-0680/CMakeLists.txt new file mode 100644 index 00000000..dc6a47c8 --- /dev/null +++ b/0501-1000/0680-Valid-Palindrome-II/cpp-0680/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0680) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0680 main.cpp) diff --git a/0501-1000/0680-Valid-Palindrome-II/cpp-0680/main.cpp b/0501-1000/0680-Valid-Palindrome-II/cpp-0680/main.cpp new file mode 100644 index 00000000..9db57f7d --- /dev/null +++ b/0501-1000/0680-Valid-Palindrome-II/cpp-0680/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/valid-palindrome-ii/ +/// Author : liuyubobobo +/// Time : 2022-04-01 + +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool validPalindrome(string s) { + + int n = s.size(); + if(is_palindrome(s, 0, n - 1)) return true; + + int i = 0, j = n - 1; + while(s[i] == s[j]) i ++, j --; + return is_palindrome(s, i + 1, j) || is_palindrome(s, i, j - 1); + } + +private: + bool is_palindrome(const string& s, int l, int r){ + for(int i = l, j = r; i < j; i ++, j --) + if(s[i] != s[j]) return false; + return true; + } +}; + + +int main() { + + cout << Solution().validPalindrome("abca") << '\n'; + + return 0; +} diff --git a/0501-1000/0682-Baseball-Game/cpp-0682/CMakeLists.txt b/0501-1000/0682-Baseball-Game/cpp-0682/CMakeLists.txt new file mode 100644 index 00000000..e011de9f --- /dev/null +++ b/0501-1000/0682-Baseball-Game/cpp-0682/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0682) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0682 main.cpp) diff --git a/0501-1000/0682-Baseball-Game/cpp-0682/main.cpp b/0501-1000/0682-Baseball-Game/cpp-0682/main.cpp new file mode 100644 index 00000000..75f4016d --- /dev/null +++ b/0501-1000/0682-Baseball-Game/cpp-0682/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/baseball-game/ +/// Author : liuyubobobo +/// Time : 2022-03-25 + +#include +#include +#include + +using namespace std; + + +/// Using Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int calPoints(vector& ops) { + + vector v; + for(const string& e: ops) + if(isdigit(e[0]) || e[0] == '-'){ + int x = atoi(e.c_str()); + v.push_back(x); + } + else if(e == "+") + v.push_back(v.back() + v[v.size() - 2]); + else if(e == "D") + v.push_back(2 * v.back()); + else + v.pop_back(); + + return accumulate(v.begin(), v.end(), 0); + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0686-Repeated-String-Match/cpp-0686/CMakeLists.txt b/0501-1000/0686-Repeated-String-Match/cpp-0686/CMakeLists.txt new file mode 100644 index 00000000..75c46141 --- /dev/null +++ b/0501-1000/0686-Repeated-String-Match/cpp-0686/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0686) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0686 main.cpp) diff --git a/0501-1000/0686-Repeated-String-Match/cpp-0686/main.cpp b/0501-1000/0686-Repeated-String-Match/cpp-0686/main.cpp new file mode 100644 index 00000000..12cabdcc --- /dev/null +++ b/0501-1000/0686-Repeated-String-Match/cpp-0686/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/repeated-string-match/ +/// Author : liuyubobobo +/// Time : 2021-12-21 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int repeatedStringMatch(string a, string b) { + + string s = ""; + int t = 0; + while(s.size() < b.size()) s += a, t ++; + + if(s.find(b) != string::npos) return t; + s += a, t ++; + if(s.find(b) != string::npos) return t; + + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0688-Knight-Probability-in-Chessboard/cpp-0688/CMakeLists.txt b/0501-1000/0688-Knight-Probability-in-Chessboard/cpp-0688/CMakeLists.txt new file mode 100644 index 00000000..aa925335 --- /dev/null +++ b/0501-1000/0688-Knight-Probability-in-Chessboard/cpp-0688/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0688) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0688 main.cpp) diff --git a/0501-1000/0688-Knight-Probability-in-Chessboard/cpp-0688/main.cpp b/0501-1000/0688-Knight-Probability-in-Chessboard/cpp-0688/main.cpp new file mode 100644 index 00000000..848a14f7 --- /dev/null +++ b/0501-1000/0688-Knight-Probability-in-Chessboard/cpp-0688/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/knight-probability-in-chessboard/ +/// Author : liuyubobobo +/// Time : 2022-02-16 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(k * n^2) +/// Space Complexity: O(k * n^2) +class Solution { + +private: + const int dirs[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, + {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}}; +public: + double knightProbability(int n, int k, int r, int c) { + + vector>> dp(k + 1, vector>(n, vector(n, -1))); + return dfs(n, k, r, c, dp); + } + +private: + double dfs(int n, int k, int r, int c, + vector>>& dp) { + + if(k == 0) return 1; + if(dp[k][r][c] >= 0.0) return dp[k][r][c]; + + double res = 0.0; + for(int d = 0; d < 8; d ++){ + int nr = r + dirs[d][0], nc = c + dirs[d][1]; + if(in_area(nr, nc, n)) + res += 0.125 * dfs(n, k - 1, nr, nc, dp); + } + return dp[k][r][c] = res; + } + + bool in_area(int x, int y, int n){ + return 0 <= x && x < n && 0 <= y && y < n; + } +}; + + +int main() { + + cout << Solution().knightProbability(8, 30, 6, 4) << endl; + + return 0; +} diff --git a/0501-1000/0689-Maximum-Sum-of-3-Non-Overlapping-Subarrays/cpp-0689/CMakeLists.txt b/0501-1000/0689-Maximum-Sum-of-3-Non-Overlapping-Subarrays/cpp-0689/CMakeLists.txt new file mode 100644 index 00000000..ef9add75 --- /dev/null +++ b/0501-1000/0689-Maximum-Sum-of-3-Non-Overlapping-Subarrays/cpp-0689/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0689) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0689 main.cpp) diff --git a/0501-1000/0689-Maximum-Sum-of-3-Non-Overlapping-Subarrays/cpp-0689/main.cpp b/0501-1000/0689-Maximum-Sum-of-3-Non-Overlapping-Subarrays/cpp-0689/main.cpp new file mode 100644 index 00000000..481a93b4 --- /dev/null +++ b/0501-1000/0689-Maximum-Sum-of-3-Non-Overlapping-Subarrays/cpp-0689/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ +/// Author : liuyubobobo +/// Time : 2021-12-07 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector maxSumOfThreeSubarrays(vector& nums, int k) { + + int n = nums.size(); + vector presum(n + 1, 0ll); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + nums[i]; + + vector max1(n, presum[n] - presum[n - k]); + vector max1_index(n, n - k); + for(int i = n - k - 1; i >= 0; i --){ + max1[i] = max1[i + 1], max1_index[i] = max1_index[i + 1]; + if(presum[i + k] - presum[i] >= max1[i + 1]) + max1[i] = presum[i + k] - presum[i], + max1_index[i] = i; + } + + vector max2(n, presum[n] - presum[n - 2 * k]); + vector> max2_index(n, {n - 2 * k, n - k}); + for(int i = n - 2 * k - 1; i >= 0; i --){ + max2[i] = max2[i + 1], max2_index[i] = max2_index[i + 1]; + if(presum[i + k] - presum[i] + max1[i + k] >= max2[i + 1]) + max2[i] = presum[i + k] - presum[i] + max1[i + k], + max2_index[i] = {i, max1_index[i + k]}; + } + + long long cur_max = presum[n] - presum[n - 3 * k]; + vector max3_index = {n - 3 * k, n - 2 * k, n - k}; + for(int i = n - 3 * k - 1; i >= 0; i --){ + if(presum[i + k] - presum[i] + max2[i + k] >= cur_max) + cur_max = presum[i + k] - presum[i] + max2[i + k], + max3_index = {i, max2_index[i + k].first, max2_index[i + k].second}; + } + return max3_index; + }; +}; + + +int main() { + + return 0; +} 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..fb9992e5 --- /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 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 new file mode 100644 index 00000000..66a2eb5f --- /dev/null +++ b/0501-1000/0691-Stickers-to-Spell-Word/cpp-0691/main.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; +} diff --git a/0501-1000/0693-Binary-Number-with-Alternating-Bits/cpp-0693/CMakeLists.txt b/0501-1000/0693-Binary-Number-with-Alternating-Bits/cpp-0693/CMakeLists.txt new file mode 100644 index 00000000..1ba37285 --- /dev/null +++ b/0501-1000/0693-Binary-Number-with-Alternating-Bits/cpp-0693/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0693) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0693 main.cpp) diff --git a/0501-1000/0693-Binary-Number-with-Alternating-Bits/cpp-0693/main.cpp b/0501-1000/0693-Binary-Number-with-Alternating-Bits/cpp-0693/main.cpp new file mode 100644 index 00000000..50d18d24 --- /dev/null +++ b/0501-1000/0693-Binary-Number-with-Alternating-Bits/cpp-0693/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/binary-number-with-alternating-bits/ +/// Author : liuyubobobo +/// Time : 2022-03-27 + +#include +#include + +using namespace std; + + +/// int to binary +/// Time Complexity: O(logn) +/// Space Complexity: O(logn) +class Solution { +public: + bool hasAlternatingBits(int n) { + + vector binary; + while(n) + binary.push_back(n & 1), n >>= 1; + + for(int i = 1; i < binary.size(); i ++) + if(binary[i] == binary[i - 1]) return false; + return true; + } +}; + + +int main() { + + + return 0; +} 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 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 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/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 diff --git a/0501-1000/0728-Self-Dividing-Numbers/cpp-0728/main.cpp b/0501-1000/0728-Self-Dividing-Numbers/cpp-0728/main.cpp index ed8c62a4..186385ac 100644 --- a/0501-1000/0728-Self-Dividing-Numbers/cpp-0728/main.cpp +++ b/0501-1000/0728-Self-Dividing-Numbers/cpp-0728/main.cpp @@ -1,14 +1,16 @@ /// Source : https://leetcode.com/problems/longest-increasing-subsequence/description/ /// Author : liuyubobobo /// Time : 2017-11-18 +/// Updated: 2022-03-30 #include #include using namespace std; -/// Ad-Hoc -/// Time Complexity: O((right-left+1)*log10(right)) + +/// Brute Force +/// Time Complexity: O((right-left+1) * log(right)) /// Space Complexity: O(1) class Solution { @@ -17,13 +19,13 @@ class Solution { vector res; for(int i = left ; i <= right ; i ++) - if(selfDividing(i)) + if(is_self_dividing(i)) res.push_back(i); return res; } private: - bool selfDividing(int num){ + bool is_self_dividing(int num){ int t = num; while(t){ int x = t % 10; @@ -36,15 +38,15 @@ class Solution { }; -void printVec(const vector& vec){ +void print_vec(const vector& vec){ for(int e: vec) - cout << e << " "; + cout << e << ' '; cout << endl; } int main() { - printVec(Solution().selfDividingNumbers(1, 22)); + print_vec(Solution().selfDividingNumbers(1, 22)); return 0; } \ No newline at end of file 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/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..c1d818ce --- /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 main2.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/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; +} 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/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/0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/CMakeLists.txt b/0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/CMakeLists.txt new file mode 100644 index 00000000..01ffd5e7 --- /dev/null +++ b/0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0762) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0762 main.cpp) diff --git a/0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/main.cpp b/0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/main.cpp new file mode 100644 index 00000000..30b306ae --- /dev/null +++ b/0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/ +/// Author : liuyubobobo +/// Time : 2022-04-04 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(right - left) +/// Space Complexity: O(1) +class Solution { +public: + int countPrimeSetBits(int left, int right) { + + set primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; + int res = 0; + for(int e = left; e <= right; e ++){ + int b = __builtin_popcount(e); + res += primes.count(b); + } + return res; + } +}; + + +int main() { + + + return 0; +} diff --git a/0501-1000/0763-Partition-Labels/cpp-0763/CMakeLists.txt b/0501-1000/0763-Partition-Labels/cpp-0763/CMakeLists.txt new file mode 100644 index 00000000..8828aec5 --- /dev/null +++ b/0501-1000/0763-Partition-Labels/cpp-0763/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0763) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0763 main.cpp) diff --git a/0501-1000/0763-Partition-Labels/cpp-0763/main.cpp b/0501-1000/0763-Partition-Labels/cpp-0763/main.cpp new file mode 100644 index 00000000..43b23e3d --- /dev/null +++ b/0501-1000/0763-Partition-Labels/cpp-0763/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/partition-labels/ +/// Author : liuyubobobo +/// Time : 2022-03-20 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector partitionLabels(string s) { + + vector f(26, 0); + for(char c: s) f[c - 'a'] ++; + + vector res; + vector curf(26, 0); + int l = 0; + for(int i = 0; i < s.size(); i ++){ + curf[s[i] - 'a'] ++; + if(ok(curf, f)){ + res.push_back(i - l + 1); + l = i + 1; + fill(curf.begin(), curf.end(), 0); + } + } + return res; + } + +private: + bool ok(const vector& curf, const vector& f){ + + for(int i = 0; i < 26; i ++) + if(curf[i] && curf[i] != f[i]) return false; + return true; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + print_vec(Solution().partitionLabels("ababcbacadefegdehijhklij")); + // 9 7 8 + + return 0; +} 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/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/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 diff --git a/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/CMakeLists.txt b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/CMakeLists.txt new file mode 100644 index 00000000..6055682b --- /dev/null +++ b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0798) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0798 main3.cpp) diff --git a/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main.cpp b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main.cpp new file mode 100644 index 00000000..e3350b46 --- /dev/null +++ b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main.cpp @@ -0,0 +1,334 @@ +/// Source : https://leetcode.com/problems/smallest-rotation-with-highest-score/ +/// Author : liuyubobobo +/// Time : 2022-03-08 + +#include +#include + +using namespace std; + + +/// Using AVLTree (as Sort List) +/// 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: + int bestRotation(vector& nums) { + + AVLTreeMultiSet tree; + + int n = nums.size(); + for(int i = 0; i < n; i ++) + tree.add(nums[i] - i); + + int best_score = 0, k = 0; + for(int i = 0; i < n; i ++){ + int score = tree.size_less_than_or_equal_to(-i); +// cout << "k : " << i << " score : " << score << '\n'; + + if(score > best_score) best_score = score, k = i; + + tree.remove(nums[i] - i); + tree.add(nums[i] - (n - 1) - (i + 1)); + } + return k; + } +}; + + +int main() { + + vector nums1 = {2, 3, 1, 4, 0}; + cout << Solution().bestRotation(nums1) << '\n'; + // 3 + + return 0; +} diff --git a/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main2.cpp b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main2.cpp new file mode 100644 index 00000000..46cb5a5b --- /dev/null +++ b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main2.cpp @@ -0,0 +1,135 @@ +/// Source : https://leetcode.com/problems/smallest-rotation-with-highest-score/ +/// Author : liuyubobobo +/// Time : 2022-03-08 + +#include +#include + +using namespace std; + + +/// Using Segment Tree (as Sort List) +/// 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); + } + + void add(int index, T v){ + if(index < 0) return; + data[index] += v; + update(0, 0, n - 1, index, data[index]); + } + + 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 bestRotation(vector& nums) { + + const int MAXN = 2e5; + SegmentTree tree(MAXN, [](int a, int b){return a + b;}); + + int n = nums.size(); + for(int i = 0; i < n; i ++) + tree.add(-(nums[i] - i), 1); + + int best_score = 0, k = 0; + for(int i = 0; i < n; i ++){ + int score = tree.query(i, MAXN - 1); +// cout << "k : " << i << " score : " << score << '\n'; + + if(score > best_score) best_score = score, k = i; + + tree.add(-(nums[i] - i), -1); + tree.add(-(nums[i] - (n - 1) - (i + 1)), 1); + } + return k; + } +}; + + +int main() { + + vector nums1 = {2, 3, 1, 4, 0}; + cout << Solution().bestRotation(nums1) << '\n'; + // 3 + + return 0; +} diff --git a/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main3.cpp b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main3.cpp new file mode 100644 index 00000000..58b51aab --- /dev/null +++ b/0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main3.cpp @@ -0,0 +1,101 @@ +/// Source : https://leetcode.com/problems/smallest-rotation-with-highest-score/ +/// Author : liuyubobobo +/// Time : 2022-03-08 + +#include +#include + +using namespace std; + + +/// Using BIT (as Sort List) +/// 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); + + if(index < 0) return; + 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 bestRotation(vector& nums) { + + const int MAXN = 2e5; + BIT bit(vector(MAXN, 0)); + + int n = nums.size(); + for(int i = 0; i < n; i ++) + bit.add(-(nums[i] - i), 1); + + int best_score = 0, k = 0; + for(int i = 0; i < n; i ++){ + int score = bit.query(i, MAXN - 1); +// cout << "k : " << i << " score : " << score << '\n'; + + if(score > best_score) best_score = score, k = i; + + bit.add(-(nums[i] - i), -1); + bit.add(-(nums[i] - (n - 1) - (i + 1)), 1); + } + return k; + } +}; + + +int main() { + + vector nums1 = {2, 3, 1, 4, 0}; + cout << Solution().bestRotation(nums1) << '\n'; + // 3 + + return 0; +} 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 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..3c239dd1 --- /dev/null +++ b/0501-1000/0813-Largest-Sum-of-Averages/cpp-0813/main.cpp @@ -0,0 +1,51 @@ +/// 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) { + + 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/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/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/0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/CMakeLists.txt b/0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/CMakeLists.txt new file mode 100644 index 00000000..c612dd24 --- /dev/null +++ b/0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0825) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0825 main.cpp) diff --git a/0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/main.cpp b/0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/main.cpp new file mode 100644 index 00000000..3012585b --- /dev/null +++ b/0501-1000/0825-Friends-Of-Appropriate-Ages/cpp-0825/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/friends-of-appropriate-ages/ +/// Author : liuyubobobo +/// Time : 2021-12-26 + +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n + max_age) +/// Space Complexity: O(max_age) +class Solution { +public: + int numFriendRequests(vector& ages) { + + vector presum(121, 0); + for(int age: ages) presum[age] ++; + for(int i = 1; i <= 120; i ++) presum[i] += presum[i - 1]; + + int res = 0; + for(int age = 1; age <= 120; age ++){ + int l = age / 2 + 7 + 1, r = age; + if(age < 100) r = min(100, r); + if(l > r) continue; + + int t = presum[r] - presum[l - 1]; + if(l <= age && age <= r) t --; + res += (presum[age] - presum[age - 1]) * t; + } + return res; + } +}; + + +int main() { + + return 0; +} 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/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/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/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/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/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/0501-1000/0846-Hand-of-Straights/cpp-0846/CMakeLists.txt b/0501-1000/0846-Hand-of-Straights/cpp-0846/CMakeLists.txt new file mode 100644 index 00000000..1ff68d96 --- /dev/null +++ b/0501-1000/0846-Hand-of-Straights/cpp-0846/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0846) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0846 main.cpp) diff --git a/0501-1000/0846-Hand-of-Straights/cpp-0846/main.cpp b/0501-1000/0846-Hand-of-Straights/cpp-0846/main.cpp new file mode 100644 index 00000000..3d82b2ee --- /dev/null +++ b/0501-1000/0846-Hand-of-Straights/cpp-0846/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/hand-of-straights/ +/// Author : liuyubobobo +/// Time : 2021-12-29 + +#include +#include +#include + +using namespace std; + + +/// Using Multiset +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + bool isNStraightHand(vector& hand, int groupSize) { + + multiset set; + for(int e: hand) set.insert(e); + + while(!set.empty()){ + int start = *set.begin(); + for(int e = start; e < start + groupSize; e ++) { + if (!set.count(e)) return false; + set.erase(set.find(e)); + } + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0849-Maximize-Distance-to-Closest-Person/cpp-0849/CMakeLists.txt b/0501-1000/0849-Maximize-Distance-to-Closest-Person/cpp-0849/CMakeLists.txt new file mode 100644 index 00000000..a34e7a62 --- /dev/null +++ b/0501-1000/0849-Maximize-Distance-to-Closest-Person/cpp-0849/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_0849) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0849 main.cpp) diff --git a/0501-1000/0849-Maximize-Distance-to-Closest-Person/cpp-0849/main.cpp b/0501-1000/0849-Maximize-Distance-to-Closest-Person/cpp-0849/main.cpp new file mode 100644 index 00000000..752ede5b --- /dev/null +++ b/0501-1000/0849-Maximize-Distance-to-Closest-Person/cpp-0849/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/maximize-distance-to-closest-person/ +/// Author : liuyubobobo +/// Time : 2022-01-16 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int maxDistToClosest(vector& seats) { + + vector pos; + for(int i = 0; i < seats.size(); i ++) + if(seats[i]) pos.push_back(i); + + int res = max(pos[0], (int)seats.size() - 1 - pos.back()); + for(int i = 0; i + 1 < pos.size(); i ++) + res = max(res, (pos[i + 1] - pos[i]) / 2); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0851-Loud-and-Rich/cpp-0851/CMakeLists.txt b/0501-1000/0851-Loud-and-Rich/cpp-0851/CMakeLists.txt new file mode 100644 index 00000000..3373d497 --- /dev/null +++ b/0501-1000/0851-Loud-and-Rich/cpp-0851/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_0851) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_0851 main.cpp) diff --git a/0501-1000/0851-Loud-and-Rich/cpp-0851/main.cpp b/0501-1000/0851-Loud-and-Rich/cpp-0851/main.cpp new file mode 100644 index 00000000..c1a5664b --- /dev/null +++ b/0501-1000/0851-Loud-and-Rich/cpp-0851/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/loud-and-rich/ +/// Author : liuyubobobo +/// Time : 2021-12-14 + +#include +#include + +using namespace std; + + +/// DAG DP +/// Time Complexity: O(V + E) +/// Space Complexity: O(V + E) +class Solution { +public: + vector loudAndRich(vector>& richer, vector& quiet) { + + int n = quiet.size(); + + vector> g(n); + for(const vector& e: richer) g[e[1]].push_back(e[0]); + + vector visited(n, false); + vector> res(n, {-1, -1}); // value, index + for(int i = 0; i < n; i ++) + if(!visited[i]) + dfs(g, i, quiet, visited, res); + + vector ret(n); + for(int i = 0; i < n; i ++) ret[i] = res[i].second; + return ret; + } + +private: + void dfs(const vector>& g, int u, const vector& quiet, + vector& visited, vector>& res){ + + visited[u] = true; + res[u] = {quiet[u], u}; + for(int v: g[u]){ + if(!visited[v]) + dfs(g, v, quiet, visited, res); + if(res[v].first < res[u].first) res[u] = res[v]; + } + } +}; + + +int main() { + + return 0; +} diff --git a/0501-1000/0859-Buddy-Strings/cpp-0859/CMakeLists.txt b/0501-1000/0859-Buddy-Strings/cpp-0859/CMakeLists.txt index 557a8807..b3fafe13 100644 --- a/0501-1000/0859-Buddy-Strings/cpp-0859/CMakeLists.txt +++ b/0501-1000/0859-Buddy-Strings/cpp-0859/CMakeLists.txt @@ -3,5 +3,5 @@ project(A) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main2.cpp) +set(SOURCE_FILES main.cpp) add_executable(A ${SOURCE_FILES}) \ No newline at end of file diff --git a/0501-1000/0859-Buddy-Strings/cpp-0859/main.cpp b/0501-1000/0859-Buddy-Strings/cpp-0859/main.cpp index 4f45e54f..af70697f 100644 --- a/0501-1000/0859-Buddy-Strings/cpp-0859/main.cpp +++ b/0501-1000/0859-Buddy-Strings/cpp-0859/main.cpp @@ -1,15 +1,17 @@ /// Source : https://leetcode.com/problems/buddy-strings/description/ /// Author : liuyubobobo -/// Time : 2018-06-23 +/// Time : 2018-06-25 +/// Updated: 2021-11-22 #include -#include +#include using namespace std; + /// Scan and Compare -/// Time Complexity: O(nlogn) -/// Space Complexity: O(n) +/// Time Complexity: O(n) +/// Space Complexity: O(1) class Solution { public: bool buddyStrings(string A, string B) { @@ -17,30 +19,26 @@ class Solution { if(A.size() != B.size()) return false; - unordered_map diff; - int diff_num = 0; - for(int i = 0 ; i < A.size() ; i ++) - if(A[i] != B[i]){ - diff[A[i]] = B[i]; - diff_num ++; - } - - if(diff_num == 0) + if(A == B) return atLeastTwoSame(A); - if(diff_num != 2) - return false; + int first = -1, second = -1; + for(int i = 0 ; i < A.size(); i++) + if(A[i] != B[i]){ + if(first == -1) + first = i; + else if(second == -1) + second = i; + else + return false; + } - unordered_map::iterator iter1 = diff.begin(); - unordered_map::iterator iter2 = diff.begin(); - iter2 ++; - return iter1->first == iter2->second && iter1->second == iter2->first; + return second != -1 && A[first] == B[second] && A[second] == B[first]; } private: bool atLeastTwoSame(const string& s){ - int freq[26]; - memset(freq, 0, sizeof(freq)); + vector freq(26, 0); for(char c: s){ freq[c - 'a'] ++; if(freq[c - 'a'] >= 2) @@ -51,17 +49,22 @@ class Solution { }; -void print_bool(bool res){ - cout << (res ? "True" : "False") << endl; -} - int main() { - print_bool(Solution().buddyStrings("ab", "ba")); // true - print_bool(Solution().buddyStrings("ab", "ab")); // false - print_bool(Solution().buddyStrings("ab", "ba")); // true - print_bool(Solution().buddyStrings("aaaaaaabc", "aaaaaaacb")); // true - print_bool(Solution().buddyStrings("", "aa")); // false + cout << Solution().buddyStrings("ab", "ba") << endl; + // 1 + + cout << Solution().buddyStrings("ab", "ab") << endl; + // 0 + + cout << Solution().buddyStrings("ab", "ba") << endl; + // 1 + + cout << Solution().buddyStrings("aaaaaaabc", "aaaaaaacb") << endl; + // 1 + + cout << Solution().buddyStrings("", "aa") << endl; + // 0 return 0; } \ No newline at end of file diff --git a/0501-1000/0859-Buddy-Strings/cpp-0859/main2.cpp b/0501-1000/0859-Buddy-Strings/cpp-0859/main2.cpp deleted file mode 100644 index ba2bfb6a..00000000 --- a/0501-1000/0859-Buddy-Strings/cpp-0859/main2.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/// Source : https://leetcode.com/problems/buddy-strings/description/ -/// Author : liuyubobobo -/// Time : 2018-06-25 - -#include - -using namespace std; - -/// Scan and Compare -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - bool buddyStrings(string A, string B) { - - if(A.size() != B.size()) - return false; - - if(A == B) - return atLeastTwoSame(A); - - int first = -1, second = -1; - for(int i = 0 ; i < A.size(); i++) - if(A[i] != B[i]){ - if(first == -1) - first = i; - else if(second == -1) - second = i; - else - return false; - } - return A[first] == B[second] && A[second] == B[first]; - } - -private: - bool atLeastTwoSame(const string& s){ - int freq[26]; - memset(freq, 0, sizeof(freq)); - for(char c: s){ - freq[c - 'a'] ++; - if(freq[c - 'a'] >= 2) - return true; - } - return false; - } -}; - - -void print_bool(bool res){ - cout << (res ? "True" : "False") << endl; -} - -int main() { - - print_bool(Solution().buddyStrings("ab", "ba")); // true - print_bool(Solution().buddyStrings("ab", "ab")); // false - print_bool(Solution().buddyStrings("ab", "ba")); // true - print_bool(Solution().buddyStrings("aaaaaaabc", "aaaaaaacb")); // true - print_bool(Solution().buddyStrings("", "aa")); // false - - return 0; -} \ No newline at end of file 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 diff --git a/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt b/0501-1000/0877-Stone-Game/cpp-0877/CMakeLists.txt index 45cd0786..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 main4.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 879ce308..00000000 --- a/0501-1000/0877-Stone-Game/cpp-0877/main5.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 diff --git a/1001-1500/1001-Grid-Illumination/cpp-1001/main.cpp b/1001-1500/1001-Grid-Illumination/cpp-1001/main.cpp index cc711aad..4dcaa5f9 100644 --- a/1001-1500/1001-Grid-Illumination/cpp-1001/main.cpp +++ b/1001-1500/1001-Grid-Illumination/cpp-1001/main.cpp @@ -18,15 +18,18 @@ class Solution { vector gridIllumination(int N, vector>& lamps, vector>& queries) { set> lamp_set; - unordered_map row, col, dia1, dia2; for(const vector& lamp: lamps){ int x = lamp[0], y = lamp[1]; + lamp_set.insert(make_pair(x, y)); + } + + unordered_map row, col, dia1, dia2; + for(const pair& lamp: lamp_set){ + int x = lamp.first, y = lamp.second; row[x] ++; col[y] ++; dia1[x - y] ++; dia2[x + y] ++; - - lamp_set.insert(make_pair(x, y)); } vector res; @@ -65,5 +68,10 @@ int main() { vector> queries1 = {{1, 1}, {1, 0}}; print_vec(Solution().gridIllumination(5, lamps1, queries1)); + vector> lamps2 = {{2,5},{4,2},{0,3},{0,5},{1,4},{4,2},{3,3},{1,0}}; + vector> queries2 = {{4,3},{3,1},{5,3},{0,5},{4,4},{3,3}}; + print_vec(Solution().gridIllumination(6, lamps2, queries2)); + // 1 0 1 1 0 1 + return 0; } \ No newline at end of file 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 diff --git a/1001-1500/1036-Escape-a-Large-Maze/cpp-1036/main.cpp b/1001-1500/1036-Escape-a-Large-Maze/cpp-1036/main.cpp index 16451b09..66944e7f 100644 --- a/1001-1500/1036-Escape-a-Large-Maze/cpp-1036/main.cpp +++ b/1001-1500/1036-Escape-a-Large-Maze/cpp-1036/main.cpp @@ -1,57 +1,83 @@ /// Source : https://leetcode.com/problems/escape-a-large-maze/ /// Author : liuyubobobo -/// Time : 2019-04-27 +/// Time : 2022-01-10 + #include #include #include +#include +#include using namespace std; -/// DFS -/// Time Complexity: O(max_blocks^2) -/// Space Complexity: O(max_blocks^2) + +/// BFS +/// Time Complexity: O(|blocks|^2) +/// Space Complexity: O(|blocks|^2) class Solution { private: - const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + int N = 1000000; public: bool isEscapePossible(vector>& blocked, vector& source, vector& target) { set> blockset; for(const vector& e: blocked) - blockset.insert(make_pair(e[0], e[1])); + blockset.insert({e[0], e[1]}); - set> visited; - if(dfs(source[0], source[1], source[0], source[1], blockset, visited)) - return true; - return visited.count(make_pair(target[0], target[1])); + return bfs(source[0], source[1], target[0], target[1], blockset) && + bfs(target[0], target[1], source[0], source[1], blockset); } private: - bool dfs(int x, int y, int sx, int sy, - set>& blockset, set>& visited){ - - visited.insert(make_pair(x, y)); - if(dis(x, y, sx, sy) > 200) return true; - for(int i = 0; i < 4; i ++){ - int nextx = x + d[i][0], nexty = y + d[i][1]; - pair next = make_pair(nextx, nexty); - if(nextx >= 0 && nextx < 1000000 && nexty >= 0 && nexty < 1000000 && - !blockset.count(next) && !visited.count(next)) - if(dfs(nextx, nexty, sx, sy, blockset, visited)) - return true; + bool bfs(int sx, int sy, int tx, int ty, + set>& blockset){ + + int L = blockset.size(); + + queue> q; + map, int> dis; + q.push({sx, sy}); + dis[{sx, sy}] = 0; + + int maxv = 0; + while(!q.empty()){ + int cx = q.front().first, cy = q.front().second, v = dis[{cx, cy}]; + q.pop(); + + if(cx == tx && cy == ty) return true; + + maxv = max(maxv, v); + if(v == L) continue; + + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny) && !blockset.count({nx, ny}) && !dis.count({nx, ny})){ + q.push({nx, ny}); + dis[{nx, ny}] = v + 1; + } + } } - return false; + return maxv >= L; } - int dis(int x0, int y0, int x1, int y1){ - return abs(x0 - x1) + abs(y0 - y1); + bool in_area(int x, int y){ + return x >= 0 && x < N && y >= 0 && y < N; } }; int main() { + vector> block1 = { + {691938,300406},{710196,624190},{858790,609485},{268029,225806}, + {200010,188664},{132599,612099},{329444,633495},{196657,757958},{628509,883388} + }; + vector source1 = {655988,180910}, target1 = {267728,840949}; + cout << Solution().isEscapePossible(block1, source1, target1) << endl; + // 1 + return 0; } \ No newline at end of file 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/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/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/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/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/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/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/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/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/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; } }; 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/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/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/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/CMakeLists.txt b/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/CMakeLists.txt new file mode 100644 index 00000000..b041bdce --- /dev/null +++ b/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_1248) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1248 main2.cpp) diff --git a/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/main.cpp b/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/main.cpp new file mode 100644 index 00000000..4a1b5948 --- /dev/null +++ b/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/count-number-of-nice-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-02-04 + +#include +#include +#include + +using namespace std; + + +/// Presum + Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int numberOfSubarrays(vector& nums, int k) { + + // table[i] 存储的是当前看到有多少个位置,满足从 0 到这个位置,包含的奇数个数为 i + // table[3] == 5,表示当前已经看到有 5 个位置,从 0 到这个位置包含有 3 个奇数 + unordered_map table; // odd_num -> freq + + // 初始的空区间,包含有 0 个奇数 + table[0] = 1; + + // cur 记录 [0, i] 区间一共有多少个奇数 + int cur = 0, res = 0; + for(int i = 0; i < nums.size(); i ++){ + cur += nums[i] % 2; // 如果 nums[i] 是奇数,cur ++ + + // 查找 cur 之前有多少个位置,其奇数个数位 cur - k。 + // 那么从 这些位置 + 1,到 cur,就一共有 k 个奇数 + // 有 k 个奇数的区间数量 += table[cur - k] + if(table.count(cur - k)) + res += table[cur - k]; + + // 把 i 这个位置计算到 table 中 + // 因为 [0, i] 中有 cur 个奇数,所以 table[cur] ++ + table[cur] ++; + } + return res; + } +}; + + +int main() { + + vector nums1 = {1, 1, 2, 1, 1}; + cout << Solution().numberOfSubarrays(nums1, 3) << endl; + // 2 + + return 0; +} diff --git a/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/main2.cpp b/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/main2.cpp new file mode 100644 index 00000000..458d240f --- /dev/null +++ b/1001-1500/1248-Count-Number-of-Nice-Subarrays/cpp-1248/main2.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/count-number-of-nice-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-02-04 + +#include +#include + +using namespace std; + + +/// Presum + Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int numberOfSubarrays(vector& nums, int k) { + + int n = nums.size(); + return f(n, nums, k) - f(n, nums, k - 1); + } + +private: + long long f(int n, const vector& nums, int k){ + + int l = 0, r = -1, cur_odd = 0; + long long res = 0; + while(l < n){ + if(r + 1 < n && cur_odd + nums[r + 1] % 2 <= k){ + r ++; + res += (r - l + 1); + cur_odd += nums[r] % 2; + } + else{ + cur_odd -= nums[l] % 2; + l ++; + } + } + return res; + } +}; + + +int main() { + + vector nums1 = {1, 1, 2, 1, 1}; + cout << Solution().numberOfSubarrays(nums1, 3) << endl; + // 2 + + return 0; +} diff --git a/1001-1500/1248-Count-Number-of-Nice-Subarrays/java-1248/src/Solution.java b/1001-1500/1248-Count-Number-of-Nice-Subarrays/java-1248/src/Solution.java new file mode 100644 index 00000000..9567a325 --- /dev/null +++ b/1001-1500/1248-Count-Number-of-Nice-Subarrays/java-1248/src/Solution.java @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/count-number-of-nice-subarrays/ +/// Author : liuyubobobo +/// Time : 2022-02-06 + +import java.util.*; + + +/// Presum + Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + public int numberOfSubarrays(int[] nums, int k) { + + HashMap table = new HashMap<>(); + table.put(0, 1); + + int cur = 0, res = 0; + for(int i = 0; i < nums.length; i ++){ + + cur += nums[i] % 2; + + if(table.containsKey(cur - k)) + res += table.get(cur - k); + + table.put(cur, table.getOrDefault(cur, 0) + 1); + } + return res; + } +} 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/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/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/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/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..6c04b837 --- /dev/null +++ b/1001-1500/1255-Maximum-Score-Words-Formed-by-Letters/cpp-1255/main.cpp @@ -0,0 +1,51 @@ +/// 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) { + + 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/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/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/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/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/1001-1500/1286-Iterator-for-Combination/cpp-1286/CMakeLists.txt b/1001-1500/1286-Iterator-for-Combination/cpp-1286/CMakeLists.txt new file mode 100644 index 00000000..365c6894 --- /dev/null +++ b/1001-1500/1286-Iterator-for-Combination/cpp-1286/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_1286) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1286 main.cpp) diff --git a/1001-1500/1286-Iterator-for-Combination/cpp-1286/main.cpp b/1001-1500/1286-Iterator-for-Combination/cpp-1286/main.cpp new file mode 100644 index 00000000..98a14b8e --- /dev/null +++ b/1001-1500/1286-Iterator-for-Combination/cpp-1286/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/iterator-for-combination/ +/// Author : liuyubobobo +/// Time : 2021-11-14 + +#include +#include + +using namespace std; + + +/// Time Complexity: init: O(2^n * n) +/// next, has_next: O(1) +/// Space Complexity: O(2^n) +class CombinationIterator { + +private: + vector table; + int cur = 0; + +public: + CombinationIterator(string characters, int combinationLength){ + + int n = characters.size(); + for(int state = 1; state < (1 << n); state ++) + if(__builtin_popcount(state) == combinationLength){ + string s = ""; + for(int i = 0; i < n; i ++) + if(state & (1 << i)) s += characters[i]; + table.push_back(s); + } + sort(table.begin(), table.end()); + } + + string next() { + + return table[cur ++]; + } + + bool hasNext() { + return cur < table.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/1001-1500/1288-Remove-Covered-Intervals/cpp-1288/CMakeLists.txt b/1001-1500/1288-Remove-Covered-Intervals/cpp-1288/CMakeLists.txt new file mode 100644 index 00000000..379b231c --- /dev/null +++ b/1001-1500/1288-Remove-Covered-Intervals/cpp-1288/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_1288) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1288 main.cpp) diff --git a/1001-1500/1288-Remove-Covered-Intervals/cpp-1288/main.cpp b/1001-1500/1288-Remove-Covered-Intervals/cpp-1288/main.cpp new file mode 100644 index 00000000..ec766b8f --- /dev/null +++ b/1001-1500/1288-Remove-Covered-Intervals/cpp-1288/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/remove-covered-intervals/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlogn + n^2) +/// Space Complexity: O(n) +class Solution { +public: + int removeCoveredIntervals(vector>& intervals) { + + sort(intervals.begin(), intervals.end(), + [](const vector& va, const vector& vb){ + if(va[1] - va[0] != vb[1] - vb[0]) + return va[1] - va[0] > vb[1] - vb[0]; + return va[0] < vb[0]; + }); + + vector> res; + for(const vector& v: intervals) + if(!contains(res, v)) res.push_back(v); + return res.size(); + } + +private: + bool contains(const vector>& res, const vector& a){ + + for(const vector& v: res) + if(v[0] <= a[0] && a[1] <= v[1]) return true; + return false; + } +}; + +int main() { + + return 0; +} 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/1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/CMakeLists.txt b/1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/CMakeLists.txt new file mode 100644 index 00000000..28d39a09 --- /dev/null +++ b/1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_1290) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1290 main.cpp) diff --git a/1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/main.cpp b/1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/main.cpp new file mode 100644 index 00000000..ef1c611a --- /dev/null +++ b/1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/ +/// Author : liuyubobobo +/// Time : 2021-12-06 + +#include + + +/// Linked List +/// 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: + int getDecimalValue(ListNode* head) { + + int cur = 0; + while(head){ + cur = cur * 2 + head->val; + head = head->next; + } + return cur; + } +}; + + +int main() { + + return 0; +} diff --git a/1001-1500/1291-Sequential-Digits/cpp-1291/CMakeLists.txt b/1001-1500/1291-Sequential-Digits/cpp-1291/CMakeLists.txt new file mode 100644 index 00000000..7ed65672 --- /dev/null +++ b/1001-1500/1291-Sequential-Digits/cpp-1291/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_1291) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1291 main.cpp) diff --git a/1001-1500/1291-Sequential-Digits/cpp-1291/main.cpp b/1001-1500/1291-Sequential-Digits/cpp-1291/main.cpp new file mode 100644 index 00000000..483454f9 --- /dev/null +++ b/1001-1500/1291-Sequential-Digits/cpp-1291/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/sequential-digits/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + vector sequentialDigits(int low, int high) { + + vector res; + for(int start = 1; start < 9; start ++){ + long long cur = 0; + for(int d = start; d <= 9 && cur <= high; d ++){ + cur = cur * 10 + d; + if(cur >= low && cur <= high) res.push_back(cur); + } + } + sort(res.begin(), res.end()); + return res; + } +}; + + +int main() { + + return 0; +} 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/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/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/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/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; } 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; } }; 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'); 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/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/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/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/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/1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/CMakeLists.txt b/1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/CMakeLists.txt new file mode 100644 index 00000000..ad222f88 --- /dev/null +++ b/1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_1559) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_1559 main.cpp) diff --git a/1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/main.cpp b/1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/main.cpp new file mode 100644 index 00000000..b6e1b966 --- /dev/null +++ b/1501-2000/1559-Detect-Cycles-in-2D-Grid/cpp-1559/main.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode.com/problems/detect-cycles-in-2d-grid/ +/// Author : liuyubobobo +/// Time : 2022-03-25 + +#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}, {-1, 0}, {0, 1}, {0, -1}}; + int R, C; + +public: + bool containsCycle(vector>& grid) { + + R = grid.size(), C = grid[0].size(); + + vector> visited(R, vector(C, -1)); + int tag = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(visited[i][j] == -1 && dfs(grid, i, j, -1, -1, tag ++, visited)) + return true; + return false; + } + +private: + bool dfs(const vector>& grid, int cx, int cy, int px, int py, + int tag, vector>& visited){ + + visited[cx][cy] = tag; + + for(int d = 0; d < 4; d ++){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(!in_area(nx, ny)) continue; + if(grid[nx][ny] != grid[cx][cy]) continue; + if(nx == px && ny == py) continue; + + if(visited[nx][ny] == tag) return true; + if(dfs(grid, nx, ny, cx, cy, tag, visited)) return true; + } + return false; + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + return 0; +} 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/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/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/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 ++; diff --git a/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main.cpp b/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main.cpp index 7c7118c2..c13d2238 100644 --- a/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main.cpp +++ b/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/maximum-number-of-visible-points/ /// Author : liuyubobobo /// Time : 2020-10-03 +/// Updated: 2021-11-15 #include #include @@ -39,10 +40,10 @@ class Solution { double a = angle * M_PI / (double)180.0; int res = 0; - for(int i = 0; i < n; i ++){ + for(int i = 0; i < v.size(); i ++){ vector::iterator iter = upper_bound(v.begin(), v.end(), v[i] + a + 1e-6); int pos = (iter - v.begin()); - int tres = base + pos - i; + int tres = pos - i; if(v[i] + a >= 2 * M_PI){ iter = upper_bound(v.begin(), v.end(), v[i] + a - 2 * M_PI+ 1e-6); pos = (iter - v.begin()); @@ -50,7 +51,7 @@ class Solution { } res = max(res, tres); } - return res; + return res + base; } }; @@ -72,5 +73,10 @@ int main() { cout << Solution().visiblePoints(points3, 64, loc3) << endl; // 6 + vector> points4 = {{1,1},{1,1},{1,1}}; + vector loc4 = {1,1}; + cout << Solution().visiblePoints(points4, 1, loc4) << endl; + // 3 + return 0; } diff --git a/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main2.cpp b/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main2.cpp index 3421c60b..a45b6643 100644 --- a/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main2.cpp +++ b/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main2.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/maximum-number-of-visible-points/ /// Author : liuyubobobo /// Time : 2020-10-04 +/// Updated: 2021-11-15 #include #include @@ -36,17 +37,18 @@ class Solution { sort(v.begin(), v.end()); // for(double e: v) cout << e << " "; cout << endl; - for(int i = 0; i < n; i ++) + int m = v.size(); + for(int i = 0; i < m; i ++) v.push_back(v[i] + 2 * M_PI); double a = angle * M_PI / (double)180.0; int res = 0; - for(int i = 0; i < n; i ++){ + for(int i = 0; i < m; i ++){ vector::iterator iter = upper_bound(v.begin(), v.end(), v[i] + a + 1e-6); int pos = (iter - v.begin()); - res = max(res, base + pos - i); + res = max(res, pos - i); } - return res; + return res + base; } }; @@ -68,5 +70,10 @@ int main() { cout << Solution().visiblePoints(points3, 64, loc3) << endl; // 6 + vector> points4 = {{1,1},{1,1},{1,1}}; + vector loc4 = {1,1}; + cout << Solution().visiblePoints(points4, 1, loc4) << endl; + // 3 + return 0; } diff --git a/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main3.cpp b/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main3.cpp index 840e6d19..27daf11a 100644 --- a/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main3.cpp +++ b/1501-2000/1610-Maximum-Number-of-Visible-Points/cpp-1610/main3.cpp @@ -1,6 +1,7 @@ /// Source : https://leetcode.com/problems/maximum-number-of-visible-points/ /// Author : liuyubobobo /// Time : 2020-10-04 +/// Updated: 2021-11-15 #include #include @@ -36,19 +37,21 @@ class Solution { sort(v.begin(), v.end()); // for(double e: v) cout << e << " "; cout << endl; - for(int i = 0; i < n; i ++) + + int m = v.size(); + for(int i = 0; i < m; i ++) v.push_back(v[i] + 2 * M_PI); double a = angle * M_PI / (double)180.0 + eps; int res = 0, l = 0, r = -1; - while(l < n){ + while(l < m){ if(r + 1 < v.size() && v[r + 1] - v[l] <= a){ r ++; - res = max(res, base + r - l + 1); + res = max(res, r - l + 1); } else l ++; } - return res; + return res + base; } }; @@ -70,5 +73,10 @@ int main() { cout << Solution().visiblePoints(points3, 64, loc3) << endl; // 6 + vector> points4 = {{1,1},{1,1},{1,1}}; + vector loc4 = {1,1}; + cout << Solution().visiblePoints(points4, 1, loc4) << endl; + // 3 + return 0; } 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: diff --git a/1501-2000/1705-Maximum-Number-of-Eaten-Apples/cpp-1705/main.cpp b/1501-2000/1705-Maximum-Number-of-Eaten-Apples/cpp-1705/main.cpp index f89254d8..9429a64b 100644 --- a/1501-2000/1705-Maximum-Number-of-Eaten-Apples/cpp-1705/main.cpp +++ b/1501-2000/1705-Maximum-Number-of-Eaten-Apples/cpp-1705/main.cpp @@ -20,29 +20,20 @@ class Solution { priority_queue, vector>, greater>> pq; - int res = 0; - for(int i = 0; i < n; i ++){ - pq.push({i + days[i], apples[i]}); - while(!pq.empty() && pq.top().first <= i) pq.pop(); + int res = 0, cur_day = 0; + while(cur_day < n || !pq.empty()){ + if(cur_day < n && apples[cur_day]) pq.push({cur_day + days[cur_day], apples[cur_day]}); + while(!pq.empty() && pq.top().first <= cur_day) pq.pop(); if(!pq.empty()){ + res ++; + pair p = pq.top(); pq.pop(); p.second --; - res ++; if(p.second) pq.push(p); } - } - - int d = n; - while(!pq.empty()){ - int endday = pq.top().first, cnt = pq.top().first; - pq.pop(); - if(endday <= d) continue; - - int c = min(cnt, endday - d); - res += c; - d += c; + cur_day ++; } return res; } 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; } }; diff --git a/2001-2500/2046-Sort-Linked-List-Already-Sorted-Using-Absolute-Values/cpp-2046/CMakeLists.txt b/2001-2500/2046-Sort-Linked-List-Already-Sorted-Using-Absolute-Values/cpp-2046/CMakeLists.txt new file mode 100644 index 00000000..3745a097 --- /dev/null +++ b/2001-2500/2046-Sort-Linked-List-Already-Sorted-Using-Absolute-Values/cpp-2046/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2046) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2046 main.cpp) diff --git a/2001-2500/2046-Sort-Linked-List-Already-Sorted-Using-Absolute-Values/cpp-2046/main.cpp b/2001-2500/2046-Sort-Linked-List-Already-Sorted-Using-Absolute-Values/cpp-2046/main.cpp new file mode 100644 index 00000000..32424b8c --- /dev/null +++ b/2001-2500/2046-Sort-Linked-List-Already-Sorted-Using-Absolute-Values/cpp-2046/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/sort-linked-list-already-sorted-using-absolute-values/ +/// Author : liuyubobobo +/// Time : 2021-10-20 + +#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() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution { +public: + ListNode* sortLinkedList(ListNode* head) { + + ListNode* neg_head = nullptr, *neg_tail = nullptr; + + ListNode* dummy = new ListNode(0, head); + ListNode* cur = dummy; + while(cur->next){ + if(cur->next->val < 0){ + ListNode* neg_node = cur->next; + cur->next = neg_node->next; + neg_node->next = neg_head; + neg_head = neg_node; + if(neg_head->next == nullptr) neg_tail = neg_head; + } + else cur = cur->next; + } + + if(!neg_head) return dummy->next; + + neg_tail->next = dummy->next; + dummy->next = neg_head; + return dummy->next; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP39/cpp-LCP39/CMakeLists.txt b/2001-2500/2047-Number-of-Valid-Words-in-a-Sentence/cpp-2047/CMakeLists.txt similarity index 100% rename from LC/LCP39/cpp-LCP39/CMakeLists.txt rename to 2001-2500/2047-Number-of-Valid-Words-in-a-Sentence/cpp-2047/CMakeLists.txt diff --git a/2001-2500/2047-Number-of-Valid-Words-in-a-Sentence/cpp-2047/main.cpp b/2001-2500/2047-Number-of-Valid-Words-in-a-Sentence/cpp-2047/main.cpp new file mode 100644 index 00000000..85e817ae --- /dev/null +++ b/2001-2500/2047-Number-of-Valid-Words-in-a-Sentence/cpp-2047/main.cpp @@ -0,0 +1,80 @@ +/// Source : https://leetcode.com/problems/number-of-valid-words-in-a-sentence/ +/// Author : liuyubobobo +/// Time : 2021-10-23 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int countValidWords(string s) { + + int res = 0; + for(int start = next_non_space(s, 0), i = start + 1; i <= s.size(); i ++) + if(i == s.size() || s[i] == ' '){ + string word = s.substr(start, i - start); + res += check(word); + + start = next_non_space(s, i); + i = start; + } + return res; + } + +private: + bool check(const string& s){ + + for(char c: s) if(isdigit(c)) return false; + + int hyphen = -1; + for(int i = 0; i < s.size(); i ++) + if(s[i] == '-'){ + if(hyphen != -1) return false; + hyphen = i; + } + if(hyphen != -1) { + if (hyphen == 0 || hyphen == s.size() - 1) return false; + if (!islower(s[hyphen - 1])) return false; + if (!islower(s[hyphen + 1])) return false; + } + + int dot = -1; + for(int i = 0; i < s.size(); i ++) + if(s[i] == '!' || s[i] == '.' || s[i] == ','){ + if(dot != -1) return false; + dot = i; + } + return dot == -1 || dot == s.size() - 1; + } + + int next_non_space(const string& s, int start){ + + for(int i = start; i < s.size(); i ++) + if(s[i] != ' ') return i; + return s.size(); + } +}; + + +int main() { + + cout << Solution().countValidWords("cat and dog") << endl; + // 3 + + cout << Solution().countValidWords("!this 1-s b8d!") << endl; + // 0 + + cout << Solution().countValidWords("alice and bob are playing stone-game10") << endl; + // 5 + + cout << Solution().countValidWords("he bought 2 pencils, 3 erasers, and 1 pencil-sharpener.") << endl; + // 6 + + return 0; +} diff --git a/LC/LCP40/cpp-LCP40/CMakeLists.txt b/2001-2500/2048-Next-Greater-Numerically-Balanced-Number/cpp-2048/CMakeLists.txt similarity index 100% rename from LC/LCP40/cpp-LCP40/CMakeLists.txt rename to 2001-2500/2048-Next-Greater-Numerically-Balanced-Number/cpp-2048/CMakeLists.txt diff --git a/2001-2500/2048-Next-Greater-Numerically-Balanced-Number/cpp-2048/main.cpp b/2001-2500/2048-Next-Greater-Numerically-Balanced-Number/cpp-2048/main.cpp new file mode 100644 index 00000000..28ce5dd0 --- /dev/null +++ b/2001-2500/2048-Next-Greater-Numerically-Balanced-Number/cpp-2048/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/next-greater-numerically-balanced-number/ +/// Author : liuyubobobo +/// Time : 2021-10-23 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int nextBeautifulNumber(int n) { + + for(int x = n + 1;;x ++) + if(ok(x)) return x; + assert(false); + return -1; + } + +private: + bool ok(int x){ + + vector f(10, 0); + while(x) f[x % 10] ++, x /= 10; + + if(f[0]) return false; + for(int i = 1; i < 10; i ++) + if(f[i] && f[i] != i) return false; + return true; + } +}; + + +int main() { + + cout << Solution().nextBeautifulNumber(1) << endl; + // 22 + + cout << Solution().nextBeautifulNumber(1000) << endl; + // 1333 + + cout << Solution().nextBeautifulNumber(3000) << endl; + // 3133 + + return 0; +} diff --git a/LC/LCP41/cpp-LCP41/CMakeLists.txt b/2001-2500/2049-Count-Nodes-With-the-Highest-Score/cpp-2049/CMakeLists.txt similarity index 100% rename from LC/LCP41/cpp-LCP41/CMakeLists.txt rename to 2001-2500/2049-Count-Nodes-With-the-Highest-Score/cpp-2049/CMakeLists.txt diff --git a/2001-2500/2049-Count-Nodes-With-the-Highest-Score/cpp-2049/main.cpp b/2001-2500/2049-Count-Nodes-With-the-Highest-Score/cpp-2049/main.cpp new file mode 100644 index 00000000..ed28175e --- /dev/null +++ b/2001-2500/2049-Count-Nodes-With-the-Highest-Score/cpp-2049/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/count-nodes-with-the-highest-score/ +/// Author : liuyubobobo +/// Time : 2021-10-23 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int countHighestScoreNodes(vector& parents) { + + int n = parents.size(); + vector> g(n); + for(int u = 1; u < n; u ++){ + int v = parents[u]; + g[v].push_back(u); + } + + vector sz(n, 0); + dfs_sz(g, 0, sz); + + long long max_score = -1; + int max_score_f = 0; + dfs(n, g, 0, sz, max_score, max_score_f); + return max_score_f; + } + +private: + void dfs(int n, const vector>& g, int u, const vector& sz, + long long& max_score, int& max_score_f){ + + long long score = 1; + int count = 1; + for(int v: g[u]){ + score *= sz[v]; + count += sz[v]; + } + if(n - count) score *= (n - count); + + if(score > max_score) max_score = score, max_score_f = 1; + else if(score == max_score) max_score_f ++; + + for(int v: g[u]) + dfs(n, g, v, sz, max_score, max_score_f); + } + + void dfs_sz(const vector>& g, int u, vector& sz){ + + sz[u] = 1; + for(int v: g[u]){ + dfs_sz(g, v, sz); + sz[u] += sz[v]; + } + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP42/cpp-LCP42/CMakeLists.txt b/2001-2500/2050-Parallel-Courses-III/cpp-2050/CMakeLists.txt similarity index 100% rename from LC/LCP42/cpp-LCP42/CMakeLists.txt rename to 2001-2500/2050-Parallel-Courses-III/cpp-2050/CMakeLists.txt diff --git a/2001-2500/2050-Parallel-Courses-III/cpp-2050/main.cpp b/2001-2500/2050-Parallel-Courses-III/cpp-2050/main.cpp new file mode 100644 index 00000000..ca655e7d --- /dev/null +++ b/2001-2500/2050-Parallel-Courses-III/cpp-2050/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/parallel-courses-iii/ +/// Author : liuyubobobo +/// Time : 2021-10-23 + +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O((n + m) * logn) +/// Space Complexity: O(n + m) +class Solution { +public: + int minimumTime(int n, vector>& relations, vector& time) { + + vector> g(n); + vector in_degrees(n, 0); + for(const vector& e: relations){ + int u = e[0] - 1, v = e[1] - 1; + g[u].push_back(v); + in_degrees[v] ++; + } + + priority_queue, vector>, greater>> pq; // complete time, index + for(int u = 0; u < n; u ++) + if(in_degrees[u] == 0) pq.push({time[u], u}); + + int res = 0; + while(!pq.empty()){ + int complete_time = pq.top().first, u = pq.top().second; + pq.pop(); + + res = max(res, complete_time); + + for(int v: g[u]){ + in_degrees[v] --; + if(in_degrees[v] == 0) + pq.push({complete_time + time[v], v}); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2052-Minimum-Cost-to-Separate-Sentence-Into-Rows/cpp-2052/CMakeLists.txt b/2001-2500/2052-Minimum-Cost-to-Separate-Sentence-Into-Rows/cpp-2052/CMakeLists.txt new file mode 100644 index 00000000..3c25da9b --- /dev/null +++ b/2001-2500/2052-Minimum-Cost-to-Separate-Sentence-Into-Rows/cpp-2052/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2052) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2052 main.cpp) diff --git a/2001-2500/2052-Minimum-Cost-to-Separate-Sentence-Into-Rows/cpp-2052/main.cpp b/2001-2500/2052-Minimum-Cost-to-Separate-Sentence-Into-Rows/cpp-2052/main.cpp new file mode 100644 index 00000000..2994ece6 --- /dev/null +++ b/2001-2500/2052-Minimum-Cost-to-Separate-Sentence-Into-Rows/cpp-2052/main.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/minimum-cost-to-separate-sentence-into-rows/ +/// Author : liuyubobobo +/// Time : 2021-10-29 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int minimumCost(string sentence, int k) { + + vector v; + for(int start = 0, i = 1; i <= sentence.size(); i ++){ + if(i == sentence.size() || sentence[i] == ' '){ + v.push_back(i - start); + start = i + 1; + i = start; + } + } + + int last = v.size() - 1; + int cur = v.back(); + for(int i = (int)v.size() - 2; i >= 0; i --) + if(cur + 1 + v[i] <= k){ + last = i; + cur += 1 + v[i]; + } + else break; + + vector dp(v.size(), -1); + return dfs(v, 0, k, last, dp); + } + +private: + int dfs(const vector& v, int index, int k, int last, vector& dp){ + + if(index >= last) return 0; + if(dp[index] != -1) return dp[index]; + + int cur = v[index]; + int res = (cur - k) * (cur - k) + dfs(v, index + 1, k, last, dp); + for(int i = index + 1; i < v.size(); i ++) + if(cur + 1 + v[i] <= k){ + if(i + 1 == v.size()) return 0; + cur += (1 + v[i]); + res = min(res, (cur - k) * (cur - k) + dfs(v, i + 1, k, last, dp)); + } + else break; + return dp[index] = res; + } +}; + + +int main() { + + string s1 = "a"; + while(s1.size() + 2 < 5000) s1 += " a"; + cout << Solution().minimumCost(s1, 5000) << endl; + // + + return 0; +} diff --git a/LC/LCP44/cpp-LCP44/CMakeLists.txt b/2001-2500/2053-Kth-Distinct-String-in-an-Array/cpp-2053/CMakeLists.txt similarity index 100% rename from LC/LCP44/cpp-LCP44/CMakeLists.txt rename to 2001-2500/2053-Kth-Distinct-String-in-an-Array/cpp-2053/CMakeLists.txt diff --git a/2001-2500/2053-Kth-Distinct-String-in-an-Array/cpp-2053/main.cpp b/2001-2500/2053-Kth-Distinct-String-in-an-Array/cpp-2053/main.cpp new file mode 100644 index 00000000..48db1526 --- /dev/null +++ b/2001-2500/2053-Kth-Distinct-String-in-an-Array/cpp-2053/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/kth-distinct-string-in-an-array/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + string kthDistinct(vector& arr, int k) { + + map f; + for(string& s: arr) f[s] ++; + + for(string& s: arr){ + if(f[s] == 1) k--; + if(!k) return s; + } + return ""; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP45/cpp-LCP45/CMakeLists.txt b/2001-2500/2054-Two-Best-Non-Overlapping-Events/cpp-2054/CMakeLists.txt similarity index 100% rename from LC/LCP45/cpp-LCP45/CMakeLists.txt rename to 2001-2500/2054-Two-Best-Non-Overlapping-Events/cpp-2054/CMakeLists.txt diff --git a/2001-2500/2054-Two-Best-Non-Overlapping-Events/cpp-2054/main.cpp b/2001-2500/2054-Two-Best-Non-Overlapping-Events/cpp-2054/main.cpp new file mode 100644 index 00000000..adb77ff1 --- /dev/null +++ b/2001-2500/2054-Two-Best-Non-Overlapping-Events/cpp-2054/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/two-best-non-overlapping-events/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maxTwoEvents(vector>& events) { + + sort(events.begin(), events.end()); + + int n = events.size(); + vector max_value(n, events.back()[2]); + for(int i = n - 2; i >= 0; i --) + max_value[i] = max(events[i][2], max_value[i + 1]); + + int res = 0; + for(int i = 0; i < n; i ++){ + res = max(res, events[i][2]); + + vector t = {events[i][1] + 1, INT_MIN, INT_MIN}; + vector>::iterator iter = lower_bound(events.begin(), events.end(), t); + if(iter != events.end()){ + int index = iter - events.begin(); + res = max(res, events[i][2] + max_value[index]); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2055-Plates-Between-Candles/cpp-2055/CMakeLists.txt b/2001-2500/2055-Plates-Between-Candles/cpp-2055/CMakeLists.txt new file mode 100644 index 00000000..0ffd6e97 --- /dev/null +++ b/2001-2500/2055-Plates-Between-Candles/cpp-2055/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2055-Plates-Between-Candles/cpp-2055/main.cpp b/2001-2500/2055-Plates-Between-Candles/cpp-2055/main.cpp new file mode 100644 index 00000000..cb70b410 --- /dev/null +++ b/2001-2500/2055-Plates-Between-Candles/cpp-2055/main.cpp @@ -0,0 +1,106 @@ +/// Source : https://leetcode.com/problems/plates-between-candles/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include + +using namespace std; + + +/// Using BIT +/// Time Complexity: O(nlogn + qlogn) +/// 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){ + 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: + vector platesBetweenCandles(string s, vector>& queries) { + + int n = s.size(); + + vector left(n, -1); + int cur = -1; + for(int i = 0; i < n; i ++){ + if(s[i] == '|') cur = i; + left[i] = cur; + } + + vector right(n, n); + cur = n; + for(int i = n - 1; i >= 0; i --){ + if(s[i] == '|') cur = i; + right[i] = cur; + } + + vector data(n, 0); + for(int i = 0; i < n; i ++) + if(s[i] == '*') data[i] = 1; + BIT tree(data); + + vector res(queries.size(), 0); + for(int i = 0; i < queries.size(); i ++){ + int ql = queries[i][0], qr = queries[i][1]; + int l = right[ql], r = left[qr]; + if(l <= r) res[i] = tree.query(l, r); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2055-Plates-Between-Candles/cpp-2055/main2.cpp b/2001-2500/2055-Plates-Between-Candles/cpp-2055/main2.cpp new file mode 100644 index 00000000..0253b805 --- /dev/null +++ b/2001-2500/2055-Plates-Between-Candles/cpp-2055/main2.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/plates-between-candles/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn + qlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector platesBetweenCandles(string s, vector>& queries) { + + int n = s.size(); + + vector bars; + for(int i = 0; i < n; i ++) + if(s[i] == '|') bars.push_back(i); + + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) + presum[i + 1] = presum[i] + (s[i] == '*'); + + vector res(queries.size(), 0); + vector::iterator iter; + for(int i = 0; i < queries.size(); i ++){ + int ql = queries[i][0], qr = queries[i][1], l, r; + + iter = lower_bound(bars.begin(), bars.end(), ql); + if(iter == bars.end()) continue; + l = *iter; + + iter = lower_bound(bars.begin(), bars.end(), qr); + if(iter == bars.begin() && *iter > qr) continue; + if(iter == bars.end() || *iter > qr) iter --; + r = *iter; + + if(l <= r) res[i] = presum[r + 1] - presum[l]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2056-Number-of-Valid-Move-Combinations-On-Chessboard/cpp-2056/CMakeLists.txt b/2001-2500/2056-Number-of-Valid-Move-Combinations-On-Chessboard/cpp-2056/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2056-Number-of-Valid-Move-Combinations-On-Chessboard/cpp-2056/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2056-Number-of-Valid-Move-Combinations-On-Chessboard/cpp-2056/main.cpp b/2001-2500/2056-Number-of-Valid-Move-Combinations-On-Chessboard/cpp-2056/main.cpp new file mode 100644 index 00000000..a8719cd7 --- /dev/null +++ b/2001-2500/2056-Number-of-Valid-Move-Combinations-On-Chessboard/cpp-2056/main.cpp @@ -0,0 +1,155 @@ +/// Source : https://leetcode.com/problems/number-of-valid-move-combinations-on-chessboard/ +/// Author : liuyubobobo +/// Time : 2021-11-03 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Compelxity: O(9^n * 7*n * n) +/// Space Complexity: O(n) +class Solution { + +private: + vector>> dirs; + +public: + int countCombinations(vector& pieces, vector>& positions) { + + dirs.assign(3, vector>()); + + // rook + dirs[0] = {{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + // queen + dirs[1] = {{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}, + {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; + // bishop + dirs[2] = {{0, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; + + int n = pieces.size(); + vector p(n, 0); + vector> pos(n); + for(int i = 0; i < n; i ++){ + pos[i].first = positions[i][0] - 1; + pos[i].second = positions[i][1] - 1; + if(pieces[i] == "queen") p[i] = 1; + if(pieces[i] == "bishop") p[i] = 2; + } + + vector d(n), s(n); + return dfs_directions(n, p, pos, d, s, 0); + } + +private: + int dfs_directions(int n, const vector& pieces, const vector>& pos, + vector& d, vector& s, int index){ + + if(index == n) + return dfs_steps(n, pieces, pos, d, s, 0); + + int res = 0, L = dirs[pieces[index]].size(); + for(int i = 0; i < L; i ++){ + d[index] = i; + res += dfs_directions(n, pieces, pos, d, s, index + 1); + } + return res; + } + + int dfs_steps(int n, const vector& pieces, const vector>& pos, + const vector& d, vector&s, int index){ + + if(index == n){ + vector> cur = pos; + vector left = s; + return dfs_check(n, cur, pieces, d, left); + } + + int res = 0; + if(d[index] == 0){ + s[index] = 0; + res += dfs_steps(n, pieces, pos, d, s, index + 1); + } + else{ + int dx = dirs[pieces[index]][d[index]].first, dy = dirs[pieces[index]][d[index]].second; + int cx = pos[index].first, cy = pos[index].second; + for(int i = 1; i < 8; i ++){ + if(in_area(cx += dx, cy += dy)){ + s[index] = i; + res += dfs_steps(n, pieces, pos, d, s, index + 1); + } + else break; + } + } + return res; + } + + bool dfs_check(int n, vector>& pos, const vector& pieces, + const vector& d, vector& left){ + + bool all_zero = true; + for(int i = 0; i < n; i ++) + if(left[i]){ + pos[i].first += dirs[pieces[i]][d[i]].first; + pos[i].second += dirs[pieces[i]][d[i]].second; + all_zero = false; + left[i] --; + } + + if(all_zero) return true; + + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + if(pos[i] == pos[j]) return false; + + return dfs_check(n, pos, pieces, d, left); + } + + bool in_area(int x, int y){ + return x >= 0 && x < 8 && y >= 0 && y < 8; + } +}; + + +int main() { + + vector pieces1 = {"rook"}; + vector> pos1 = {{1, 1}}; + cout << Solution().countCombinations(pieces1, pos1) << endl; + // 15 + + vector pieces2 = {"queen"}; + vector> pos2 = {{1, 1}}; + cout << Solution().countCombinations(pieces2, pos2) << endl; + // 22 + + vector pieces3 = {"bishop"}; + vector> pos3 = {{4, 3}}; + cout << Solution().countCombinations(pieces3, pos3) << endl; + // 12 + + vector pieces4 = {"rook", "rook"}; + vector> pos4 = {{1, 1}, {8, 8}}; + cout << Solution().countCombinations(pieces4, pos4) << endl; + // 223 + + vector pieces5 = {"queen", "bishop"}; + vector> pos5 = {{5, 7}, {3, 4}}; + cout << Solution().countCombinations(pieces5, pos5) << endl; + // 281 + + vector pieces6 = {"rook", "queen", "rook", "rook"}; + vector> pos6 = {{3, 8}, {6, 8}, {5, 3}, {2, 3}}; + cout << Solution().countCombinations(pieces6, pos6) << endl; + // 55717 + + vector pieces7 = {"bishop", "queen", "bishop", "bishop"}; + vector> pos7 = {{3, 7}, {7, 3}, {2, 4}, {4, 1}}; + cout << Solution().countCombinations(pieces7, pos7) << endl; + // 15126 + + return 0; +} diff --git a/2001-2500/2057-Smallest-Index-With-Equal-Value/cpp-2057/CMakeLists.txt b/2001-2500/2057-Smallest-Index-With-Equal-Value/cpp-2057/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/2001-2500/2057-Smallest-Index-With-Equal-Value/cpp-2057/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2057-Smallest-Index-With-Equal-Value/cpp-2057/main.cpp b/2001-2500/2057-Smallest-Index-With-Equal-Value/cpp-2057/main.cpp new file mode 100644 index 00000000..1fd2bcbb --- /dev/null +++ b/2001-2500/2057-Smallest-Index-With-Equal-Value/cpp-2057/main.cpp @@ -0,0 +1,28 @@ +/// Source : https://leetcode.com/problems/smallest-index-with-equal-value/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int smallestEqual(vector& nums) { + + for(int i = 0; i < nums.size(); i ++) + if(i % 10 == nums[i]) return i; + return -1; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/CMakeLists.txt b/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/CMakeLists.txt new file mode 100644 index 00000000..084dd354 --- /dev/null +++ b/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main2.cpp) diff --git a/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/main.cpp b/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/main.cpp new file mode 100644 index 00000000..a7352994 --- /dev/null +++ b/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include + +using namespace std; + + +/// Using Array +/// Time Compelxity: 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: + vector nodesBetweenCriticalPoints(ListNode* head) { + + vector data; + while(head){ + data.push_back(head->val); + head = head->next; + } + + int n = data.size(); + vector pos; + for(int i = 1; i + 1 < n; i ++){ + if(data[i - 1] < data[i] && data[i] > data[i + 1]) + pos.push_back(i); + else if(data[i - 1] > data[i] && data[i] < data[i + 1]) + pos.push_back(i); + } + + if(pos.size() < 2) return {-1, -1}; + + int min_dis = INT_MAX; + for(int i = 1; i < pos.size(); i ++) + min_dis = min(min_dis, pos[i] - pos[i - 1]); + return {min_dis, pos.back() - pos[0]}; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/main2.cpp b/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/main2.cpp new file mode 100644 index 00000000..3d26708a --- /dev/null +++ b/2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/main2.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/ +/// Author : liuyubobobo +/// Time : 2021-10-31 + +#include +#include + +using namespace std; + + +/// One Pass in Linked List and O(1) Space +/// Time Compelxity: O(n) +/// Space Compelxity: 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 nodesBetweenCriticalPoints(ListNode* head) { + + int prev_node_val = head->val, prev_local = -1, first_local = -1, last_local = -1, min_dis = INT_MAX; + ListNode* cur = head->next; + for(int i = 1; cur->next; i ++, cur = cur->next){ + if((prev_node_val < cur->val && cur->val > cur->next->val) || + (prev_node_val > cur->val && cur->val < cur->next->val)){ + + if(first_local == -1) first_local = i; + last_local = max(last_local, i); + if(prev_local != -1) min_dis = min(min_dis, i - prev_local); + prev_local = i; + } + prev_node_val = cur->val; + } + + if(min_dis == INT_MAX) return {-1, -1}; + return {min_dis, last_local - first_local}; + } +}; + + +int main() { + + return 0; +} diff --git a/LC/LCP46/cpp-LCP46/CMakeLists.txt b/2001-2500/2059-Minimum-Operations-to-Convert-Number/cpp-2059/CMakeLists.txt similarity index 100% rename from LC/LCP46/cpp-LCP46/CMakeLists.txt rename to 2001-2500/2059-Minimum-Operations-to-Convert-Number/cpp-2059/CMakeLists.txt diff --git a/2001-2500/2059-Minimum-Operations-to-Convert-Number/cpp-2059/main.cpp b/2001-2500/2059-Minimum-Operations-to-Convert-Number/cpp-2059/main.cpp new file mode 100644 index 00000000..667b0c50 --- /dev/null +++ b/2001-2500/2059-Minimum-Operations-to-Convert-Number/cpp-2059/main.cpp @@ -0,0 +1,77 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-convert-number/ +/// Author : liuyubobobo +/// Time : 2021-10-30 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Compelxity: O(1000n) +/// Space Compelxity: O(1000n) +class Solution { +public: + int minimumOperations(vector& nums, int start, int goal) { + + vector dis(1001, -1); + dis[start] = 0; + + queue q; + q.push(start); + + while(!q.empty()){ + int cur = q.front(); q.pop(); + + int d = dis[cur]; + for(int num: nums){ + int a = cur + num; + if(a == goal) return d + 1; + if(0 <= a && a <= 1000 && dis[a] == -1) q.push(a), dis[a] = d + 1; + + int b = cur - num; + if(b == goal) return d + 1; + if(0 <= b && b <= 1000 && dis[b] == -1) q.push(b), dis[b] = d + 1; + + int c = cur ^ num; + if(c == goal) return d + 1; + if(0 <= c && c <= 1000 && dis[c] == -1) q.push(c), dis[c] = d + 1; + } + } + return -1; + } +}; + + +int main() { + + vector nums1 = {1, 3}; + cout << Solution().minimumOperations(nums1, 6, 4) << endl; + // 2 + + vector nums2 = {2, 4, 12}; + cout << Solution().minimumOperations(nums2, 2, 12) << endl; + // 2 + + vector nums3 = {3, 5, 7}; + cout << Solution().minimumOperations(nums3, 0, -4) << endl; + // 2 + + vector nums4 = {2, 8, 16}; + cout << Solution().minimumOperations(nums4, 0, 1) << endl; + // -1 + + vector nums5 = {1}; + cout << Solution().minimumOperations(nums5, 0, 3) << endl; + // 3 + + vector nums6 = {274504599,-437121852,-283671014,-795227961,504793587,-655990799,-903306522,44919307,-873921107,435103173,-233943318,-364255446,-559614087,-727247460,-187800152,-228958874,373958305,637920438,235436095,-596133383,464339218,-67824712,-155028908,168017302,361823236,195637352,-495807030,-283876713,517899793,-562939689,884969425,-144330241,140538827,-536479469,768251206,-640028608,806134401,243968407,928102192,-480188410,-652340704,-862758854,137351112,648721527,249762676,624620853,-392218813,-999179129,731919183,330383470,861417443,-679928265,-850093860,105372736,-331113772,377702286,265400395,-511047215,492824547,118533263,802729743,91293283,-596122218,503153602,-392161752,-536359573,469464778,-519480260,-437818942,43191911,-325148088,914414872,-779527485,499668349,604223142,158035493,-133335863,452500770,593642940,881994837,-393244441,-740955862,369681444,-649744897,-329622834,-727346152,216135938,660402034,157047130,98520751,389074028,332247407,-209362397,-405846547,-976164396,179697702,482640852,113156802,-764726927,21196820,475924861,-501387010,-521285998,-73340730,-341407987,-410283280,-618814157,388062458,-52090234,-943486285,438909665,386411224,829785577,878471402,306505406,-4019276,242674243,133684521,626711549,427600365,-907767977,-58201536,-768231545,357227318,-830719646,608805401,-189212199,288239474,692386759,908023931,-120320781,-239958589,680092226,207253648,274039857,157541875,216046740,577951839,345003653,-380997882,837933318,-372547856,-593257214,-376937702,-215397501,-490627366,397047895,171228292,239947581,574138161,133786400,-349032470,224529998,812411777,647669892,488917,-674063913,768370921,-37596428,-656866372,367475563,4811396,-864970305,-22343842,781134974,-320627062,-943951001,-724118919,23519930,-372268648,714884514,-921267107,-629610933,-210498943,629593893,-543099666,989641699,-520568809,302398795,462461247,-493362512,-517267543,896586688,738136629,792759663,77849471,163099930,-294652452,-586051267,138373545,-45443242,-178006635,126656767,-370740793,-142028216,-656478789,-909865539,-45016267,-331506071,-875778621,-575234905,-795010054,-217915587,28709532,410822428,-253100539,542188248,-292261868,-628494325,-137807006,-62295850,-960968938,651503967,-804449486,-912908442,407895859,-312220179,-373281077,-126441710,587251290,807148112,-542439701,-437800095,136518757,947027117,-162031226,-883838131,-441985931,105920835,300258318,-461749685,312341616,-722559772,553077273,100170653,-791911093,260884109,-515528241,519874204,726741845,116213300,396117093,-646099763,896174142,484441559,170623996,731971679,-683531213,429220971,51501373,-980518207,337856564,932447220,790777174,-164285774,184598181,542501755,-295103903,322292600,-515696143,466063434,268858116,19617585,-668148351,595835834,-875679692,306780299,465852737,-749476049,496973136,108307092,-62169373,-613000035,-15802981,380806050,-15599477,224196161,-786436446,-102721720,966016112,-449681577,-571359760,-30870357,907947772,-397757285,-415978672,204756474,-676646192,879829138,154543119,-37915616,353546466,121384343,679059598,-575886433,-58814987,375756597,-737810759,-640442425,-830225084,-131054299,-448790015,-121208252,-182631226,-828929325,860254702,-966027942,693239701,221755949,954184244,609025156,602818238,348029234,191806330,-776743553,217516433,625978431,146580109,-176159863,142705862,332507932,-911208273,607714224,-487331269,-491001799,31873551,-890999237,203866747,831181692,-474880013,-414589485,708339893,-68899679,35636137,43747302,-970885272,301068191,175227438,906089806,-622934104,613550487,212679441,237390198,-383871498,359829413,-18858272,-564035296,-68723821,902159573,925503754,841238795,-550055927,-710708273,473810306,-942061728,-630129098,473871569,-38610927,798344511,-585307353,17981580,575350042,-708955832,438058040,-225128624,441551583,-669250624,-904359642,67471216,836221913,377382388,986685062,614075075,-969748019,-688247479,234834944,1}; + cout << Solution().minimumOperations(nums6, 704, 884969425) << endl; + // 705 + + return 0; +} diff --git a/2001-2500/2060-Check-if-an-Original-String-Exists-Given-Two-Encoded Strings/cpp-2060/CMakeLists.txt b/2001-2500/2060-Check-if-an-Original-String-Exists-Given-Two-Encoded Strings/cpp-2060/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2060-Check-if-an-Original-String-Exists-Given-Two-Encoded Strings/cpp-2060/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2060-Check-if-an-Original-String-Exists-Given-Two-Encoded Strings/cpp-2060/main.cpp b/2001-2500/2060-Check-if-an-Original-String-Exists-Given-Two-Encoded Strings/cpp-2060/main.cpp new file mode 100644 index 00000000..3541fef5 --- /dev/null +++ b/2001-2500/2060-Check-if-an-Original-String-Exists-Given-Two-Encoded Strings/cpp-2060/main.cpp @@ -0,0 +1,123 @@ +/// Source : https://leetcode.com/problems/check-if-an-original-string-exists-given-two-encoded-strings/ +/// Author : liuyubobobo +/// Time : 2021-11-01 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Compelxity: O(n1 * n2 * 10000) +/// Space Compelxity: O(n1 * n2 * 10000) +class Solution { +public: + bool possiblyEquals(string s1, string s2) { + + map, int>, bool> dp; // (i1, i2), diff = line1 - line2 + return dfs(s1, s2, 0, 0, 0, dp); + } + +private: + bool dfs(const string& s1, const string& s2, int i1, int i2, int diff, + map, int>, bool>& dp){ + + if(i1 == s1.size() && i2 == s2.size()) return diff == 0; + + pair, int> cur_state = {{i1, i2}, diff}; + if(dp.count(cur_state)) return dp[cur_state]; + + if(i1 == s1.size()){ + if(diff <= 0) return false; + + if(!isdigit(s2[i2])) + return dp[cur_state] = dfs(s1, s2, i1, i2 + 1, diff - 1, dp); + + for(int len = 0; len < 3 && i2 + len < s2.size() && isdigit(s2[i2 + len]); len ++) + if(dfs(s1, s2, i1, i2 + len + 1, diff - atoi(s2.substr(i2, len + 1).c_str()), dp)) + return dp[cur_state] = true; + return dp[cur_state] = false; + } + + if(i2 == s2.size()){ + if(diff >= 0) return false; + + if(!isdigit(s1[i1])) + return dp[cur_state] = dfs(s1, s2, i1 + 1, i2, diff + 1, dp); + + for(int len = 0; len < 3 && i1 + len < s1.size() && isdigit(s1[i1 + len]); len ++) + if(dfs(s1, s2, i1 + len + 1, i2, diff + atoi(s1.substr(i1, len + 1).c_str()), dp)) + return dp[cur_state] = true; + return dp[cur_state] = false; + } + + if(!isdigit(s1[i1]) && !isdigit(s2[i2])){ + if(diff){ + if(diff > 0) return dp[cur_state] = dfs(s1, s2, i1, i2 + 1, diff - 1, dp); + return dp[cur_state] = dfs(s1, s2, i1 + 1, i2, diff + 1, dp); + } + + if(s1[i1] == s2[i2]) + return dp[cur_state] = dfs(s1, s2, i1 + 1, i2 + 1, diff, dp); + return dp[cur_state] = false; + } + + if(!isdigit(s1[i1]) && isdigit(s2[i2])){ + if(diff < 0) + return dp[cur_state] = dfs(s1, s2, i1 + 1, i2, diff + 1, dp); + + for(int len = 0; len < 3 && i2 + len < s2.size() && isdigit(s2[i2 + len]); len ++) + if(dfs(s1, s2, i1, i2 + len + 1, diff - atoi(s2.substr(i2, len + 1).c_str()), dp)) + return dp[cur_state] = true; + return dp[cur_state] = false; + } + + if(isdigit(s1[i1]) && !isdigit(s2[i2])){ + if(diff > 0) + return dp[cur_state] = dfs(s1, s2, i1, i2 + 1, diff - 1, dp); + + for(int len = 0; len < 3 && i1 + len < s1.size() && isdigit(s1[i1 + len]); len ++) + if(dfs(s1, s2, i1 + len + 1, i2, diff + atoi(s1.substr(i1, len + 1).c_str()), dp)) + return dp[cur_state] = true; + return dp[cur_state] = false; + } + + assert(isdigit(s1[i1]) && isdigit(s2[i2])); + for(int len1 = 0; len1 < 3 && i1 + len1 < s1.size() && isdigit(s1[i1 + len1]); len1 ++) + if(dfs(s1, s2, i1 + len1 + 1, i2, diff + atoi(s1.substr(i1, len1 + 1).c_str()), dp)) + return dp[cur_state] = true; + return dp[cur_state] = false; + } +}; + + +int main() { + + cout << Solution().possiblyEquals("internationalization", "i18n") << endl; + // true + + cout << Solution().possiblyEquals("l123e", "44") << endl; + // true + + cout << Solution().possiblyEquals("a5b", "c5b") << endl; + // false + + cout << Solution().possiblyEquals("112s", "g841") << endl; + // true + + cout << Solution().possiblyEquals("ab", "a2") << endl; + // false + + cout << Solution().possiblyEquals("1", "z") << endl; + // true + + cout << Solution().possiblyEquals("a312b4a77", "a428a1a8a") << endl; + // true + + cout << Solution().possiblyEquals("98u8v8v8v89u888u998v88u98v88u9v99u989v8u", "9v898u98v888v89v998u98v9v888u9v899v998u9") << endl; + // false + + return 0; +} diff --git a/2001-2500/2061-Number-of-Spaces-Cleaning-Robot-Cleaned/cpp-2061/CMakeLists.txt b/2001-2500/2061-Number-of-Spaces-Cleaning-Robot-Cleaned/cpp-2061/CMakeLists.txt new file mode 100644 index 00000000..5e9840d7 --- /dev/null +++ b/2001-2500/2061-Number-of-Spaces-Cleaning-Robot-Cleaned/cpp-2061/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2061) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2061 main.cpp) diff --git a/2001-2500/2061-Number-of-Spaces-Cleaning-Robot-Cleaned/cpp-2061/main.cpp b/2001-2500/2061-Number-of-Spaces-Cleaning-Robot-Cleaned/cpp-2061/main.cpp new file mode 100644 index 00000000..1d305e88 --- /dev/null +++ b/2001-2500/2061-Number-of-Spaces-Cleaning-Robot-Cleaned/cpp-2061/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/number-of-spaces-cleaning-robot-cleaned/ +/// Author : liuyubobobo +/// Time : 2021-11-04 + +#include +#include +#include + +using namespace std; + + +/// Simualtion +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { + +private: + const int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + int R, C; + +public: + int numberOfCleanRooms(vector>& room) { + + R = room.size(), C = room[0].size(); + + set> visited; + set res; + int curx = 0, cury = 0, d = 0; + visited.insert({curx * C + cury, d}); + res.insert(curx * C + cury); + while(true){ + if(in_area(curx + dirs[d][0], cury + dirs[d][1]) && room[curx + dirs[d][0]][cury + dirs[d][1]] == 0) + curx += dirs[d][0], cury += dirs[d][1]; + else + d = (d + 1) % 4; + + if(visited.count({curx * C + cury, d})) break; + visited.insert({curx * C + cury, d}); + res.insert(curx * C + cury); + } + return res.size(); + } + +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/2062-Count-Vowel-Substrings-of-a-String/cpp-2062/CMakeLists.txt b/2001-2500/2062-Count-Vowel-Substrings-of-a-String/cpp-2062/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/2001-2500/2062-Count-Vowel-Substrings-of-a-String/cpp-2062/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2062-Count-Vowel-Substrings-of-a-String/cpp-2062/main.cpp b/2001-2500/2062-Count-Vowel-Substrings-of-a-String/cpp-2062/main.cpp new file mode 100644 index 00000000..bb7c2972 --- /dev/null +++ b/2001-2500/2062-Count-Vowel-Substrings-of-a-String/cpp-2062/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/count-vowel-substrings-of-a-string/ +/// Author : liuyubobobo +/// Time : 2021-11-06 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Co,plexity: O(n^3) +/// Space Complexity: O(1) +class Solution { +public: + int countVowelSubstrings(string word) { + + int n = word.size(), res = 0; + for(int i = 0; i < n; i ++) + for(int j = i; j < n; j ++) + res += check(word.substr(i, j - i + 1)); + return res; + } + +private: + bool check(const string& s){ + + if(s.size() < 5) return false; + + vector f(26, 0); + int cnt = 0; + for(char c: s){ + if(c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u') + return false; + f[c - 'a'] ++; + cnt += f[c - 'a'] == 1; + } + return cnt == 5; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2063-Vowels-of-All-Substrings/cpp-2063/CMakeLists.txt b/2001-2500/2063-Vowels-of-All-Substrings/cpp-2063/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/2001-2500/2063-Vowels-of-All-Substrings/cpp-2063/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2063-Vowels-of-All-Substrings/cpp-2063/main.cpp b/2001-2500/2063-Vowels-of-All-Substrings/cpp-2063/main.cpp new file mode 100644 index 00000000..36828bd6 --- /dev/null +++ b/2001-2500/2063-Vowels-of-All-Substrings/cpp-2063/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/vowels-of-all-substrings/ +/// Author : liuyubobobo +/// Time : 2021-11-06 + +#include + +using namespace std; + + +/// Mathematics +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long countVowels(string word) { + + int n = word.size(); + long long res = 0; + for(int i = 0; i < n; i ++) + if(is_vowel(word[i])) + res += (long long)(i + 1) * (n - i); + return res; + } + +private: + bool is_vowel(char c){ + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + } +}; + + +int main() { + + cout << Solution().countVowels("aba") << endl; + // 6 + + cout << Solution().countVowels("abc") << endl; + // 3 + + cout << Solution().countVowels("ltcd") << endl; + // 0 + + return 0; +} diff --git a/2001-2500/2064-Minimized-Maximum-of-Products-Distributed-to-Any-Store/cpp-2064/CMakeLists.txt b/2001-2500/2064-Minimized-Maximum-of-Products-Distributed-to-Any-Store/cpp-2064/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/2001-2500/2064-Minimized-Maximum-of-Products-Distributed-to-Any-Store/cpp-2064/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2064-Minimized-Maximum-of-Products-Distributed-to-Any-Store/cpp-2064/main.cpp b/2001-2500/2064-Minimized-Maximum-of-Products-Distributed-to-Any-Store/cpp-2064/main.cpp new file mode 100644 index 00000000..67452fb9 --- /dev/null +++ b/2001-2500/2064-Minimized-Maximum-of-Products-Distributed-to-Any-Store/cpp-2064/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/ +/// Author : liuyubobobo +/// Time : 2021-11-06 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(m * log(sum(quantities))) +/// Space Compelxity: O(1) +class Solution { +public: + int minimizedMaximum(int n, vector& quantities) { + + long long sum = accumulate(quantities.begin(), quantities.end(), 0ll); + + long long l = 1, r = sum; + while(l < r){ + long long m = (l + r) / 2; + if(ok(n, quantities, m)) r = m; + else l = m + 1; + } + return l; + } + +private: + bool ok(int n, const vector& q, long long x){ + + int cur = 0; + for(int e: q) + cur += e / x + !!(e % x); + return cur <= n; + } +}; + + +int main() { + + vector q1 = {11, 6}; + cout << Solution().minimizedMaximum(6, q1) << endl; + // 3 + + vector q2 = {15, 10, 10}; + cout << Solution().minimizedMaximum(7, q2) << endl; + // 5 + + vector q3 = {100000}; + cout << Solution().minimizedMaximum(1, q3) << endl; + // 100000 + + vector q4 = {24,18,12,6,3,24,5,19,10,20,2,18,27,3,13,22,11,16,19,13}; + cout << Solution().minimizedMaximum(26, q4) << endl; + // 19 + + return 0; +} diff --git a/2001-2500/2065-Maximum-Path-Quality-of-a-Graph/cpp-2065/CMakeLists.txt b/2001-2500/2065-Maximum-Path-Quality-of-a-Graph/cpp-2065/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2065-Maximum-Path-Quality-of-a-Graph/cpp-2065/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2065-Maximum-Path-Quality-of-a-Graph/cpp-2065/main.cpp b/2001-2500/2065-Maximum-Path-Quality-of-a-Graph/cpp-2065/main.cpp new file mode 100644 index 00000000..de2aac53 --- /dev/null +++ b/2001-2500/2065-Maximum-Path-Quality-of-a-Graph/cpp-2065/main.cpp @@ -0,0 +1,78 @@ +/// Source : https://leetcode.com/problems/maximum-path-quality-of-a-graph/ +/// Author : liuyubobobo +/// Time : 2021-11-06 + +#include +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(|d|^10) where d is the degree of vertexs and at most 4 according to the problem +/// Space Complexity: O(n + m) +class Solution { +public: + int maximalPathQuality(vector& values, vector>& edges, int maxTime) { + + int n = values.size(); + vector>> g(n); + for(const vector& e: edges){ + g[e[0]].push_back({e[1], e[2]}); + g[e[1]].push_back({e[0], e[2]}); + } + + long long res = 0ll; + vector path = {0}; + dfs(n, g, 0, maxTime, path, values, res); + return res; + } + +private: + void dfs(int n, const vector>>& g, int u, int t, + vector& path, const vector& values, long long& res){ + + if(u == 0){ + set vset(path.begin(), path.end()); + long long tres = 0; + for(int v: vset) tres += values[v]; + res = max(res, tres); + } + + for(const pair& p: g[u]){ + int v = p.first, w = p.second; + if(t - w >= 0){ + path.push_back(v); + dfs(n, g, v, t - w, path, values, res); + path.pop_back(); + } + } + } +}; + + +int main() { + + vector values1 = {0, 32, 10, 43}; + vector> edges1 = {{0, 1, 10}, {1, 2, 15}, {0, 3, 10}}; + cout << Solution().maximalPathQuality(values1, edges1, 49) << endl; + // 75 + + vector values2 = {5, 10, 15, 20}; + vector> edges2 = {{0, 1, 10}, {1, 2, 10}, {0, 3, 10}}; + cout << Solution().maximalPathQuality(values2, edges2, 30) << endl; + // 25 + + vector values3 = {1, 2, 3, 4}; + vector> edges3 = {{0, 1, 10}, {1, 2, 11}, {2, 3, 12}, {1, 3, 13}}; + cout << Solution().maximalPathQuality(values3, edges3, 50) << endl; + // 7 + + vector values4 = {0, 1, 2}; + vector> edges4 = {{1, 2, 10}}; + cout << Solution().maximalPathQuality(values4, edges4, 10) << endl; + // 0 + + return 0; +} diff --git a/2001-2500/2067-Number-of-Equal-Count-Substrings/cpp-2067/CMakeLists.txt b/2001-2500/2067-Number-of-Equal-Count-Substrings/cpp-2067/CMakeLists.txt new file mode 100644 index 00000000..c8d34e67 --- /dev/null +++ b/2001-2500/2067-Number-of-Equal-Count-Substrings/cpp-2067/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2067) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2067 main.cpp) diff --git a/2001-2500/2067-Number-of-Equal-Count-Substrings/cpp-2067/main.cpp b/2001-2500/2067-Number-of-Equal-Count-Substrings/cpp-2067/main.cpp new file mode 100644 index 00000000..30264299 --- /dev/null +++ b/2001-2500/2067-Number-of-Equal-Count-Substrings/cpp-2067/main.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/number-of-equal-count-substrings/ +/// Author : liuyubobobo +/// Time : 2021-11-16 + +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int equalCountSubstrings(string s, int count) { + + int res = 0; + for(int k = 1; k <= 26; k ++){ + + vector f(26, 0); + int cur_k = 0, ok_k = 0, l = 0, r = -1; + while(l < s.size()){ + if(r + 1 < s.size() && cur_k + !f[s[r + 1] - 'a'] <= k && f[s[r + 1] - 'a'] + 1 <= count){ + cur_k += !f[s[r + 1] - 'a']; + f[s[r + 1] - 'a'] ++; + if(f[s[r + 1] - 'a'] == count) ok_k ++; + r ++; + } + else{ + if(f[s[l] - 'a'] == count) ok_k --; + f[s[l] - 'a'] --; + if(f[s[l] - 'a'] == 0) cur_k --; + l ++; + } + + res += ok_k == k; + } + } + return res; + } +}; + + +int main() { + + cout << Solution().equalCountSubstrings("aaabcbbcc", 3) << endl; + // 3 + + cout << Solution().equalCountSubstrings("abcd", 2) << endl; + // 0 + + cout << Solution().equalCountSubstrings("a", 5) << endl; + // 0 + + cout << Solution().equalCountSubstrings("abcdabcdabcdabcd", 4) << endl; + // 1 + + cout << Solution().equalCountSubstrings("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv" + , 1) << endl; + // 779675 + + return 0; +} diff --git a/2001-2500/2068-Check-Whether-Two-Strings-are-Almost-Equivalent/cpp-2068/CMakeLists.txt b/2001-2500/2068-Check-Whether-Two-Strings-are-Almost-Equivalent/cpp-2068/CMakeLists.txt new file mode 100644 index 00000000..e0ab18f1 --- /dev/null +++ b/2001-2500/2068-Check-Whether-Two-Strings-are-Almost-Equivalent/cpp-2068/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2068) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2068 main.cpp) diff --git a/2001-2500/2068-Check-Whether-Two-Strings-are-Almost-Equivalent/cpp-2068/main.cpp b/2001-2500/2068-Check-Whether-Two-Strings-are-Almost-Equivalent/cpp-2068/main.cpp new file mode 100644 index 00000000..89c85588 --- /dev/null +++ b/2001-2500/2068-Check-Whether-Two-Strings-are-Almost-Equivalent/cpp-2068/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent/ +/// Author : liuyubobobo +/// Time : 2021-11-14 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool checkAlmostEquivalent(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(abs(f1[i] - f2[i]) > 3) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2069-Walking-Robot-Simulation-II/cpp-2069/CMakeLists.txt b/2001-2500/2069-Walking-Robot-Simulation-II/cpp-2069/CMakeLists.txt new file mode 100644 index 00000000..709a56f5 --- /dev/null +++ b/2001-2500/2069-Walking-Robot-Simulation-II/cpp-2069/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2069) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2069 main.cpp) diff --git a/2001-2500/2069-Walking-Robot-Simulation-II/cpp-2069/main.cpp b/2001-2500/2069-Walking-Robot-Simulation-II/cpp-2069/main.cpp new file mode 100644 index 00000000..5447d378 --- /dev/null +++ b/2001-2500/2069-Walking-Robot-Simulation-II/cpp-2069/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.com/problems/walking-robot-simulation-ii/ +/// Author : liuyubobobo +/// Time : 2021-11-14 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: init: O(1) +/// move: O(with + height) +/// get: O(1) +class Robot { + +private: + int W, H; + int cx = 0, cy = 0; + int d = 3; + const int dirs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; + bool first = true; + +public: + Robot(int width, int height) : W(width), H(height){} + + void move(int num) { + + if(num) first = false; + num %= (2 * (W + H - 2)); + + while(num){ + int nx = cx + dirs[d][0], ny = cy + dirs[d][1]; + if(in_area(nx, ny)){ + cx = nx, cy = ny; + num --; + } + else d = (d + 1) % 4; + } + } + + vector getPos() { + return {cx, cy}; + } + + string getDir() { + if(first || d == 0) return "East"; + if(d == 1) return "North"; + if(d == 2) return "West"; + assert(d == 3); + return "South"; + } + +private: + bool in_area(int x, int y){ + return x >= 0 && x < W && y >= 0 && y < H; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2070-Most-Beautiful-Item-for-Each-Query/cpp-2070/CMakeLists.txt b/2001-2500/2070-Most-Beautiful-Item-for-Each-Query/cpp-2070/CMakeLists.txt new file mode 100644 index 00000000..e5b871e0 --- /dev/null +++ b/2001-2500/2070-Most-Beautiful-Item-for-Each-Query/cpp-2070/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2070) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2070 main.cpp) diff --git a/2001-2500/2070-Most-Beautiful-Item-for-Each-Query/cpp-2070/main.cpp b/2001-2500/2070-Most-Beautiful-Item-for-Each-Query/cpp-2070/main.cpp new file mode 100644 index 00000000..5cbcf232 --- /dev/null +++ b/2001-2500/2070-Most-Beautiful-Item-for-Each-Query/cpp-2070/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/most-beautiful-item-for-each-query/ +/// Author : liuyubobobo +/// Time : 2021-11-14 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn + qlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector maximumBeauty(vector>& items, vector& queries) { + + vector> data(items.size()); + for(int i = 0; i < items.size(); i ++) data[i] = {items[i][0], items[i][1]}; + sort(data.begin(), data.end()); + + for(int i = 1; i < data.size(); i ++) + data[i].second = max(data[i].second, data[i - 1].second); + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i ++){ + int t = queries[i]; + vector>::iterator iter = upper_bound(data.begin(), data.end(), make_pair(t, INT_MAX)); + int index = iter - data.begin() - 1; + res[i] = (index == -1 ? 0 : data[index].second); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2071-Maximum-Number-of-Tasks-You-Can-Assign/cpp-2071/CMakeLists.txt b/2001-2500/2071-Maximum-Number-of-Tasks-You-Can-Assign/cpp-2071/CMakeLists.txt new file mode 100644 index 00000000..29523910 --- /dev/null +++ b/2001-2500/2071-Maximum-Number-of-Tasks-You-Can-Assign/cpp-2071/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2071) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2071 main.cpp) diff --git a/2001-2500/2071-Maximum-Number-of-Tasks-You-Can-Assign/cpp-2071/main.cpp b/2001-2500/2071-Maximum-Number-of-Tasks-You-Can-Assign/cpp-2071/main.cpp new file mode 100644 index 00000000..ed103a30 --- /dev/null +++ b/2001-2500/2071-Maximum-Number-of-Tasks-You-Can-Assign/cpp-2071/main.cpp @@ -0,0 +1,73 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-tasks-you-can-assign/ +/// Author : liuyubobobo +/// Time : 2021-11-16 + +#include +#include +#include + +using namespace std; + + +/// Binary Serch + Greedy +/// Time Complexity: O(log(min(m, n)) * mlogn), where m = |tasks|, n = |workers| +/// Space Complexity: O(n) +class Solution { +public: + int maxTaskAssign(vector& tasks, vector& workers, int pills, int strength) { + + sort(tasks.begin(), tasks.end()); + multiset worker_set(workers.begin(), workers.end()); + + int l = 0, r = tasks.size(); + while(l < r){ + int mid = (l + r + 1) / 2; + if(ok(tasks, worker_set, mid, pills, strength)) + l = mid; + else + r = mid - 1; + } + return l; + } + +private: + bool ok(const vector& tasks, multiset workers, int k, int pills, int str){ + + for(int i = k - 1; i >= 0; i --) + if(!workers.empty() && *workers.rbegin() >= tasks[i]) + workers.erase(prev(workers.end())); + else if(pills){ + auto it = workers.lower_bound(tasks[i] - str); + if(it == workers.end()) return false; + workers.erase(it); + pills --; + } + else return false; + return true; + } +}; + +int main() { + + vector task1 = {3, 2, 1}, workers1 = {0, 3, 3}; + cout << Solution().maxTaskAssign(task1, workers1, 1, 1) << endl; + // 3 + + vector task2 = {5, 4}, workers2 = {0, 0, 0}; + cout << Solution().maxTaskAssign(task2, workers2, 1, 5) << endl; + // 1 + + vector task3 = {10, 15, 30}, workers3 = {0, 10, 10, 10, 10}; + cout << Solution().maxTaskAssign(task3, workers3, 3, 10) << endl; + // 2 + + vector task4 = {5, 9, 8, 5, 9}, workers4 = {1, 6, 4, 2, 6}; + cout << Solution().maxTaskAssign(task4, workers4, 1, 5) << endl; + // 3 + + vector task5 = {74,41,64,20,28,52,30,4,4,63}, workers5 = {38}; + cout << Solution().maxTaskAssign(task5, workers5, 0, 68) << endl; + // 1 + + return 0; +} diff --git a/2001-2500/2073-Time-Needed-to-Buy-Tickets/cpp-2073/CMakeLists.txt b/2001-2500/2073-Time-Needed-to-Buy-Tickets/cpp-2073/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/2001-2500/2073-Time-Needed-to-Buy-Tickets/cpp-2073/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2073-Time-Needed-to-Buy-Tickets/cpp-2073/main.cpp b/2001-2500/2073-Time-Needed-to-Buy-Tickets/cpp-2073/main.cpp new file mode 100644 index 00000000..a7303a43 --- /dev/null +++ b/2001-2500/2073-Time-Needed-to-Buy-Tickets/cpp-2073/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/time-needed-to-buy-tickets/ +/// Author : liuyubobobo +/// Time : 2021-11-13 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(sum(tickets)) +/// Space Complexity: O(1) +class Solution { +public: + int timeRequiredToBuy(vector& tickets, int k) { + + int res = 0; + while(tickets[k] > 1){ + for(int& e: tickets) + if(e) res ++, e --; + } + + for(int i = 0; i <= k; i ++) + if(tickets[i] > 0) res ++; + return res; + } +}; + + +int main() { + + vector t1 = {84,49,5,24,70,77,87,8}; + cout << Solution().timeRequiredToBuy(t1, 3) << endl; + // 154 + + return 0; +} diff --git a/2001-2500/2074-Reverse-Nodes-in-Even-Length-Groups/cpp-2074/CMakeLists.txt b/2001-2500/2074-Reverse-Nodes-in-Even-Length-Groups/cpp-2074/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/2001-2500/2074-Reverse-Nodes-in-Even-Length-Groups/cpp-2074/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2074-Reverse-Nodes-in-Even-Length-Groups/cpp-2074/main.cpp b/2001-2500/2074-Reverse-Nodes-in-Even-Length-Groups/cpp-2074/main.cpp new file mode 100644 index 00000000..5c6dcc80 --- /dev/null +++ b/2001-2500/2074-Reverse-Nodes-in-Even-Length-Groups/cpp-2074/main.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode.com/problems/reverse-nodes-in-even-length-groups/ +/// Author : liuyubobobo +/// Time : 2021-11-13 + +#include +#include + +using namespace std; + + +/// Using Array to store +/// Time Complexity: O(n) +/// Space Complexity: 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* reverseEvenLengthGroups(ListNode* head) { + + vector data; + ListNode* cur = head; + while(cur) data.push_back(cur->val), cur = cur->next; + + int n = data.size(); + int len = 1, index = 0; + while(index < n){ + int start = index, end = min(index + len - 1, n - 1); + if((end - start + 1) % 2 == 0) + reverse(data, start, end); + index = end + 1; + len ++; + } + + ListNode* dummy = new ListNode(-1); + cur = dummy; + for(int i = 0; i < n; i ++) + cur->next = new ListNode(data[i]), cur = cur->next; + return dummy->next; + } + +private: + void reverse(vector& data, int start, int end){ + + int l = start, r = end; + while(l < r) swap(data[l], data[r]), l ++, r --; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2075-Decode-the-Slanted-Ciphertext/cpp-2075/CMakeLists.txt b/2001-2500/2075-Decode-the-Slanted-Ciphertext/cpp-2075/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/2001-2500/2075-Decode-the-Slanted-Ciphertext/cpp-2075/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2075-Decode-the-Slanted-Ciphertext/cpp-2075/main.cpp b/2001-2500/2075-Decode-the-Slanted-Ciphertext/cpp-2075/main.cpp new file mode 100644 index 00000000..6598e57c --- /dev/null +++ b/2001-2500/2075-Decode-the-Slanted-Ciphertext/cpp-2075/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/decode-the-slanted-ciphertext/ +/// Author : liuyubobobo +/// Time : 2021-11-13 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(|s|) +/// Space Complextiy: O(|s|) +class Solution { +public: + string decodeCiphertext(string encodedText, int R) { + + int n = encodedText.size(); + assert(n % R == 0); + int C = n / R; + + vector M(R, string(C, ' ')); + for(int i = 0; i < n; i ++) + M[i / C][i % C] = encodedText[i]; + + string res = ""; + for(int c = 0; c < C; c ++) + for(int r = 0; r < R && c + r < C; r ++) + res += M[r][c + r]; + while(res.back() == ' ') res.pop_back(); + return res; + } +}; + + +int main() { + + cout << Solution().decodeCiphertext("ch ie pr", 3) << endl; + // cipher + + cout << Solution().decodeCiphertext("iveo eed l te olc", 4) << endl; + // i love leetcode + + cout << Solution().decodeCiphertext("coding", 1) << endl; + // coding + + cout << Solution().decodeCiphertext(" b ac", 2) << endl; + // _abc + + return 0; +} diff --git a/2001-2500/2076-Process-Restricted-Friend-Requests/cpp-2076/CMakeLists.txt b/2001-2500/2076-Process-Restricted-Friend-Requests/cpp-2076/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2076-Process-Restricted-Friend-Requests/cpp-2076/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2076-Process-Restricted-Friend-Requests/cpp-2076/main.cpp b/2001-2500/2076-Process-Restricted-Friend-Requests/cpp-2076/main.cpp new file mode 100644 index 00000000..1f3acd2f --- /dev/null +++ b/2001-2500/2076-Process-Restricted-Friend-Requests/cpp-2076/main.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/process-restricted-friend-requests/ +/// Author : liuyubobobo +/// Time : 2021-11-13 + +#include +#include + +using namespace std; + + +/// UF - restore the old UF +/// Time Complexity: O(m * n) +/// Space Complexity: O(n) +class UF{ + +public: + vector parent; + + 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 friendRequests(int n, vector>& restrictions, vector>& requests) { + + UF uf(n); + + int q = requests.size(); + vector res(q); + for(int i = 0; i < q; i ++){ + + vector old_uf = uf.parent; + + int a = requests[i][0], b = requests[i][1]; + uf.union_elements(a, b); + + bool ok = true; + for(const vector& r: restrictions) + if(uf.is_connected(r[0], r[1])){ok = false; break;} + + res[i] = ok; + if(!ok) uf.parent = old_uf; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2077-Paths-in-Maze-That-Lead-to-Same-Room/cpp-2077/CMakeLists.txt b/2001-2500/2077-Paths-in-Maze-That-Lead-to-Same-Room/cpp-2077/CMakeLists.txt new file mode 100644 index 00000000..0afb07ba --- /dev/null +++ b/2001-2500/2077-Paths-in-Maze-That-Lead-to-Same-Room/cpp-2077/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2077) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2077 main.cpp) diff --git a/2001-2500/2077-Paths-in-Maze-That-Lead-to-Same-Room/cpp-2077/main.cpp b/2001-2500/2077-Paths-in-Maze-That-Lead-to-Same-Room/cpp-2077/main.cpp new file mode 100644 index 00000000..4eb8e6b1 --- /dev/null +++ b/2001-2500/2077-Paths-in-Maze-That-Lead-to-Same-Room/cpp-2077/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/paths-in-maze-that-lead-to-same-room/ +/// Author : liuyubobobo +/// Time : 2021-11-19 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n + m) +/// Space Complexity: O(n^2) +class Solution { +public: + int numberOfPaths(int n, vector>& corridors) { + + vector> M(n, vector(n, false)); + vector> g(n); + vector> edges(corridors.size()); + for(int i = 0; i < corridors.size(); i ++){ + int a = corridors[i][0] - 1, b = corridors[i][1] - 1; + M[a][b] = M[b][a] = true; + g[a].push_back(b), g[b].push_back(a); + edges[i] = {a, b}; + } + + int res = 0; + for(const pair& e: edges) + for(int v: g[e.first]) + res += (v != e.second && M[v][e.second]); + return res / 3; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2078-Two-Furthest-Houses-With-Different-Colors/cpp-2078/CMakeLists.txt b/2001-2500/2078-Two-Furthest-Houses-With-Different-Colors/cpp-2078/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/2001-2500/2078-Two-Furthest-Houses-With-Different-Colors/cpp-2078/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2078-Two-Furthest-Houses-With-Different-Colors/cpp-2078/main.cpp b/2001-2500/2078-Two-Furthest-Houses-With-Different-Colors/cpp-2078/main.cpp new file mode 100644 index 00000000..57a23451 --- /dev/null +++ b/2001-2500/2078-Two-Furthest-Houses-With-Different-Colors/cpp-2078/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/two-furthest-houses-with-different-colors/ +/// Author : liuyubobobo +/// Time : 2021-11-20 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int maxDistance(vector& colors) { + + int res = 0; + for(int i = 0; i < colors.size(); i ++) + for(int j = i + 1; j < colors.size(); j ++) + if(colors[i] != colors[j]) res = max(res, j - i); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2079-Watering-Plants/cpp-2079/CMakeLists.txt b/2001-2500/2079-Watering-Plants/cpp-2079/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/2001-2500/2079-Watering-Plants/cpp-2079/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2079-Watering-Plants/cpp-2079/main.cpp b/2001-2500/2079-Watering-Plants/cpp-2079/main.cpp new file mode 100644 index 00000000..a225014f --- /dev/null +++ b/2001-2500/2079-Watering-Plants/cpp-2079/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/watering-plants/ +/// Author : liuyubobobo +/// Time : 2021-11-20 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int wateringPlants(vector& plants, int capacity) { + + int res = 0, cur = -1, left = capacity; + for(int i = 0; i < plants.size();) + if(left >= plants[i]){ + res ++; + left -= plants[i]; + cur ++; + i ++; + } + else{ + res += (cur + 1) * 2; + left = capacity; + } + return res; + } +}; + + +int main() { + + vector plants1 = {2, 2, 3, 3}; + cout << Solution().wateringPlants(plants1, 5) << endl; + // 14 + + vector plants2 = {1, 1, 1, 4, 2, 3}; + cout << Solution().wateringPlants(plants2, 4) << endl; + // 30 + + vector plants3 = {7,7,7,7,7,7,7}; + cout << Solution().wateringPlants(plants3, 8) << endl; + // 49 + + return 0; +} diff --git a/2001-2500/2080-Range-Frequency-Queries/cpp-2080/CMakeLists.txt b/2001-2500/2080-Range-Frequency-Queries/cpp-2080/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/2001-2500/2080-Range-Frequency-Queries/cpp-2080/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2080-Range-Frequency-Queries/cpp-2080/main.cpp b/2001-2500/2080-Range-Frequency-Queries/cpp-2080/main.cpp new file mode 100644 index 00000000..c2b9812e --- /dev/null +++ b/2001-2500/2080-Range-Frequency-Queries/cpp-2080/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/range-frequency-queries/ +/// Author : liuyubobobo +/// Time : 2021-11-20 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: init: O(n) +/// query: O(logn) +/// Space Complexity: O(maxa) +class RangeFreqQuery { + +private: + vector> f; + +public: + RangeFreqQuery(vector& arr) : f(10001) { + for(int i = 0; i < arr.size(); i ++) + f[arr[i]].push_back(i); + } + + int query(int left, int right, int value) { + + int l = lower_bound(f[value].begin(), f[value].end(), left) - f[value].begin(); + int r = upper_bound(f[value].begin(), f[value].end(), right) - f[value].begin(); + return max(r - l, 0); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2081-Sum-of-k-Mirror-Numbers/cpp-2081/CMakeLists.txt b/2001-2500/2081-Sum-of-k-Mirror-Numbers/cpp-2081/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2081-Sum-of-k-Mirror-Numbers/cpp-2081/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2081-Sum-of-k-Mirror-Numbers/cpp-2081/main.cpp b/2001-2500/2081-Sum-of-k-Mirror-Numbers/cpp-2081/main.cpp new file mode 100644 index 00000000..c6e9dbfa --- /dev/null +++ b/2001-2500/2081-Sum-of-k-Mirror-Numbers/cpp-2081/main.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/sum-of-k-mirror-numbers/ +/// Author : liuyubobobo +/// Time : 2021-11-20 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(sqrt(max_num) * logn) +/// Space Complexity: O(log(max_num) + logn) +class Solution { +public: + long long kMirror(int k, int n) { + + long long sum = 0; + vector numk(1000); + for(int sz = 1;n;sz ++){ + vector num(sz); + dfs(sz, num, numk, 0, k, n, sum); + } + return sum; + } + +private: + void dfs(int sz, vector& num, vector& numk, + int index, int k, int& n, long long& sum){ + + if(n == 0) return; + + if(index >= ((sz + 1) >> 1)){ + long long a = 0ll; + for(int e: num) a = a * 10 + e; + if(ok(a, k, numk)) sum += a, n --; + return; + } + + for(int i = (index == 0 ? 1 : 0); i <= 9 && n; i ++){ + num[index] = num[sz - 1 - index] = i; + dfs(sz, num, numk, index + 1, k, n, sum); + } + } + + bool ok(long long a, int k, vector& numk){ + + int w = 0; + while(a) numk[w ++] = a % k, a /= k; + + int half = w / 2; + for(int i = 0; i < half; i ++) + if(numk[i] != numk[w - 1 - i]) return false; + return true; + } +}; + + +int main() { + + cout << Solution().kMirror(2, 5) << endl; + // 25 + + cout << Solution().kMirror(3, 7) << endl; + // 499 + + cout << Solution().kMirror(4, 30) << endl; + // 53393239260 + + for(int k = 2; k <= 9; k ++) + cout << Solution().kMirror(k, 30) << endl; + + return 0; +} diff --git a/2001-2500/2083-Substrings-That-Begin-and-End-With-the-Same-Letter/cpp-2083/CMakeLists.txt b/2001-2500/2083-Substrings-That-Begin-and-End-With-the-Same-Letter/cpp-2083/CMakeLists.txt new file mode 100644 index 00000000..91ae7865 --- /dev/null +++ b/2001-2500/2083-Substrings-That-Begin-and-End-With-the-Same-Letter/cpp-2083/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2083) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2083 main.cpp) diff --git a/2001-2500/2083-Substrings-That-Begin-and-End-With-the-Same-Letter/cpp-2083/main.cpp b/2001-2500/2083-Substrings-That-Begin-and-End-With-the-Same-Letter/cpp-2083/main.cpp new file mode 100644 index 00000000..9acd1455 --- /dev/null +++ b/2001-2500/2083-Substrings-That-Begin-and-End-With-the-Same-Letter/cpp-2083/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/substrings-that-begin-and-end-with-the-same-letter/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long numberOfSubstrings(string s) { + + vector f(26, 0); + long long res = 0; + for(int i = 0; i < s.size(); i ++){ + f[s[i] - 'a'] ++; + res += f[s[i] - 'a']; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2085-Count-Common-Words-With-One-Occurrence/cpp-2085/CMakeLists.txt b/2001-2500/2085-Count-Common-Words-With-One-Occurrence/cpp-2085/CMakeLists.txt new file mode 100644 index 00000000..2d8278c0 --- /dev/null +++ b/2001-2500/2085-Count-Common-Words-With-One-Occurrence/cpp-2085/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2085) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2085 main.cpp) diff --git a/2001-2500/2085-Count-Common-Words-With-One-Occurrence/cpp-2085/main.cpp b/2001-2500/2085-Count-Common-Words-With-One-Occurrence/cpp-2085/main.cpp new file mode 100644 index 00000000..0f88931e --- /dev/null +++ b/2001-2500/2085-Count-Common-Words-With-One-Occurrence/cpp-2085/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/count-common-words-with-one-occurrence/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n + m) +/// Space Complexity: O(n + m) +class Solution { +public: + int countWords(vector& words1, vector& words2) { + + map map1, map2; + for(const string& s: words1) map1[s] ++; + for(const string& s: words2) map2[s] ++; + + int res = 0; + for(const pair& p: map1) + if(p.second == 1 && map2.count(p.first) && map2[p.first] == 1) res ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2086-Minimum-Number-of-Buckets-Required-to-Collect-Rainwater-from-Houses/cpp-2086/CMakeLists.txt b/2001-2500/2086-Minimum-Number-of-Buckets-Required-to-Collect-Rainwater-from-Houses/cpp-2086/CMakeLists.txt new file mode 100644 index 00000000..3d936d76 --- /dev/null +++ b/2001-2500/2086-Minimum-Number-of-Buckets-Required-to-Collect-Rainwater-from-Houses/cpp-2086/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2086) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2086 main.cpp) diff --git a/2001-2500/2086-Minimum-Number-of-Buckets-Required-to-Collect-Rainwater-from-Houses/cpp-2086/main.cpp b/2001-2500/2086-Minimum-Number-of-Buckets-Required-to-Collect-Rainwater-from-Houses/cpp-2086/main.cpp new file mode 100644 index 00000000..bb7730d9 --- /dev/null +++ b/2001-2500/2086-Minimum-Number-of-Buckets-Required-to-Collect-Rainwater-from-Houses/cpp-2086/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumBuckets(string s) { + + int n = s.size(); + int res = 0; + for(int i = 0; i < n; i ++) + if(s[i] == 'H'){ + if(i - 1 >= 0 && s[i - 1] == 'W') continue; + else if(i + 1 < n && s[i + 1] == '.'){s[i + 1] = 'W', res ++;} + else if(i - 1 >= 0 && s[i - 1] == '.'){s[i - 1] = 'W', res ++;} + else return -1; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2087-Minimum-Cost-Homecoming-of-a-Robot-in-a-Grid/cpp-2087/CMakeLists.txt b/2001-2500/2087-Minimum-Cost-Homecoming-of-a-Robot-in-a-Grid/cpp-2087/CMakeLists.txt new file mode 100644 index 00000000..15a2d509 --- /dev/null +++ b/2001-2500/2087-Minimum-Cost-Homecoming-of-a-Robot-in-a-Grid/cpp-2087/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2087) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2087 main.cpp) diff --git a/2001-2500/2087-Minimum-Cost-Homecoming-of-a-Robot-in-a-Grid/cpp-2087/main.cpp b/2001-2500/2087-Minimum-Cost-Homecoming-of-a-Robot-in-a-Grid/cpp-2087/main.cpp new file mode 100644 index 00000000..e6639ff4 --- /dev/null +++ b/2001-2500/2087-Minimum-Cost-Homecoming-of-a-Robot-in-a-Grid/cpp-2087/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(m + n) +/// Space Complexity: O(1) +class Solution { +public: + int minCost(vector& startPos, vector& homePos, + vector& rowCosts, vector& colCosts) { + + int res = 0; + + int x0 = startPos[0], x1 = homePos[0]; + if(x0 < x1) { + for (int i = x0 + 1; i <= x1; i ++) + res += rowCosts[i]; + } + if(x0 > x1){ + for (int i = x0 - 1; i >= x1; i --) + res += rowCosts[i]; + } + + int y0 = startPos[1], y1 = homePos[1]; + if(y0 < y1){ + for(int i = y0 + 1; i <= y1; i ++) + res += colCosts[i]; + } + if(y0 > y1){ + for(int i = y0 - 1; i >= y1; i --) + res += colCosts[i]; + } + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/CMakeLists.txt b/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/CMakeLists.txt new file mode 100644 index 00000000..5c7d391b --- /dev/null +++ b/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2088) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2088 main2.cpp) diff --git a/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/main.cpp b/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/main.cpp new file mode 100644 index 00000000..98122b45 --- /dev/null +++ b/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/main.cpp @@ -0,0 +1,85 @@ +/// Source : https://leetcode.com/problems/count-fertile-pyramids-in-a-land/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// DP + Two Pointers +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { +public: + int countPyramids(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + vector> A(R, vector(C, 0)); + vector left(C, 0), right(C, 0); + for(int i = 0; i < R; i ++){ + left[0] = grid[i][0]; + for(int j = 1; j < C; j ++) + left[j] = (grid[i][j] ? left[j - 1] + 1 : 0); + + right[C - 1] = grid[i][C - 1]; + for(int j = C - 2; j >= 0; j --) + right[j] = (grid[i][j] ? right[j + 1] + 1 : 0); + + for(int j = 0; j < C; j ++) A[i][j] = min(left[j], right[j]); + } + +// for(int i = 0; i < R; i ++){ +// for(int j = 0; j < C; j ++) cout << A[i][j] << ' '; +// cout << '\n'; +// } + + int res = 0; + for(int j = 1; j + 1 < C; j ++){ + vector v(R); + for(int i = 0; i < R; i ++) v[i] = A[i][j]; + res += solve(R, v); + + reverse(v.begin(), v.end()); + res += solve(R, v); + } + + return res; + } + +private: + int solve(int n, const vector& v){ + + int res = 0; + for(int start = 0, end = 0; start < n; start ++){ + if(v[start] == 0) continue; + if(end < start) end = start; + while(end < n && v[end] >= end - start + 1) end ++; + if(end - start > 1) res += end - start - 1; + } + return res; + } +}; + +int main() { + + vector> grid1 = {{0, 1, 1, 0}, {1, 1, 1, 1}}; + cout << Solution().countPyramids(grid1) << endl; + // 2 + + vector> grid2 = {{1, 1, 1}, {1, 1, 1}}; + cout << Solution().countPyramids(grid2) << endl; + // 2 + + vector> grid3 = {{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}; + cout << Solution().countPyramids(grid3) << endl; + // 0 + + vector> grid4 = {{1,1,1,1,0}, {1,1,1,1,1}, {1,1,1,1,1}, {0,1,0,0,1}}; + cout << Solution().countPyramids(grid4) << endl; + // 13 + + return 0; +} diff --git a/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/main2.cpp b/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/main2.cpp new file mode 100644 index 00000000..e73675f8 --- /dev/null +++ b/2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/main2.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode.com/problems/count-fertile-pyramids-in-a-land/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { +public: + int countPyramids(vector>& grid) { + + int res = solve(grid); + reverse(grid.begin(), grid.end()); + res += solve(grid); + + return res; + } + +private: + int solve(vector>& g){ + + int R = g.size(), C = g[0].size(); + vector> dp(R, vector(C, 0)); + + int res = 0; + for(int i = R - 2; i >= 0; i --) + for(int j = 1; j + 1 < C; j ++){ + if(g[i][j] && g[i + 1][j - 1] && g[i + 1][j] && g[i + 1][j + 1]) + dp[i][j] = 1 + min(min(dp[i + 1][j - 1], dp[i + 1][j]), dp[i + 1][j + 1]); + res += dp[i][j]; + } + return res; + } +}; + +int main() { + + vector> grid1 = {{0, 1, 1, 0}, {1, 1, 1, 1}}; + cout << Solution().countPyramids(grid1) << endl; + // 2 + + vector> grid2 = {{1, 1, 1}, {1, 1, 1}}; + cout << Solution().countPyramids(grid2) << endl; + // 2 + + vector> grid3 = {{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}; + cout << Solution().countPyramids(grid3) << endl; + // 0 + + vector> grid4 = {{1,1,1,1,0}, {1,1,1,1,1}, {1,1,1,1,1}, {0,1,0,0,1}}; + cout << Solution().countPyramids(grid4) << endl; + // 13 + + return 0; +} diff --git a/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/CMakeLists.txt b/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/CMakeLists.txt new file mode 100644 index 00000000..76479ef2 --- /dev/null +++ b/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main2.cpp) diff --git a/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/main.cpp b/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/main.cpp new file mode 100644 index 00000000..f1f08815 --- /dev/null +++ b/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/main.cpp @@ -0,0 +1,31 @@ +/// Source : https://leetcode.com/problems/find-target-indices-after-sorting-array/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Sorting + Brute Force +/// Time Complexity: O(nlogn + n) +/// Space Complexity: O(1) +class Solution { +public: + vector targetIndices(vector& nums, int target) { + + sort(nums.begin(), nums.end()); + + vector res; + for(int i = 0; i < nums.size(); i ++) + if(nums[i] == target) res.push_back(i); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/main2.cpp b/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/main2.cpp new file mode 100644 index 00000000..52cb0861 --- /dev/null +++ b/2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/main2.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/find-target-indices-after-sorting-array/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Sorting + Binary Search +/// Time Complexity: O(nlogn + n) +/// Space Complexity: O(1) +class Solution { +public: + vector targetIndices(vector& nums, int target) { + + sort(nums.begin(), nums.end()); + + int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); + int r = upper_bound(nums.begin(), nums.end(), target) - nums.begin(); + + vector res; + for(int i = l; i < r; i ++) res.push_back(i); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2090-K-Radius-Subarray-Averages/cpp-2090/CMakeLists.txt b/2001-2500/2090-K-Radius-Subarray-Averages/cpp-2090/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/2001-2500/2090-K-Radius-Subarray-Averages/cpp-2090/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2090-K-Radius-Subarray-Averages/cpp-2090/main.cpp b/2001-2500/2090-K-Radius-Subarray-Averages/cpp-2090/main.cpp new file mode 100644 index 00000000..a1177024 --- /dev/null +++ b/2001-2500/2090-K-Radius-Subarray-Averages/cpp-2090/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/k-radius-subarray-averages/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector getAverages(vector& nums, int k) { + + int n = nums.size(); + vector presum(n + 1, 0ll); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + nums[i]; + + vector res(n, -1); + for(int i = k; i + k < n; i ++) + res[i] = (presum[i + k + 1] - presum[i - k]) / (2ll * k + 1); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2091-Removing-Minimum-and-Maximum-From-Array/cpp-2091/CMakeLists.txt b/2001-2500/2091-Removing-Minimum-and-Maximum-From-Array/cpp-2091/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/2001-2500/2091-Removing-Minimum-and-Maximum-From-Array/cpp-2091/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2091-Removing-Minimum-and-Maximum-From-Array/cpp-2091/main.cpp b/2001-2500/2091-Removing-Minimum-and-Maximum-From-Array/cpp-2091/main.cpp new file mode 100644 index 00000000..9f51e47e --- /dev/null +++ b/2001-2500/2091-Removing-Minimum-and-Maximum-From-Array/cpp-2091/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/removing-minimum-and-maximum-from-array/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumDeletions(vector& nums) { + + int n = nums.size(); + if(n == 1) return 1; + + int a = min_element(nums.begin(), nums.end()) - nums.begin(); + int b = max_element(nums.begin(), nums.end()) - nums.begin(); + if(a > b) swap(a, b); + + int res = b + 1; + res = min(res, n - a); + res = min(res, a + 1 + n - b); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2092-Find-All-People-With-Secret/cpp-2092/CMakeLists.txt b/2001-2500/2092-Find-All-People-With-Secret/cpp-2092/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2092-Find-All-People-With-Secret/cpp-2092/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2092-Find-All-People-With-Secret/cpp-2092/main.cpp b/2001-2500/2092-Find-All-People-With-Secret/cpp-2092/main.cpp new file mode 100644 index 00000000..8e7bb02a --- /dev/null +++ b/2001-2500/2092-Find-All-People-With-Secret/cpp-2092/main.cpp @@ -0,0 +1,119 @@ +/// Source : https://leetcode.com/problems/find-all-people-with-secret/ +/// Author : liuyubobobo +/// Time : 2021-11-27 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// UF + BFS +/// Time Complexity: O(n + m) +/// 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 findAllPeople(int n, vector>& meetings, int firstPerson) { + + UF uf(n); + uf.union_elements(0, firstPerson); + + sort(meetings.begin(), meetings.end(), + [](const vector& v1, const vector& v2){ + return v1[2] < v2[2]; + }); + + for(int start = 0, i = 1; i <= meetings.size(); i ++) + if(i == meetings.size() || meetings[i][2] != meetings[start][2]){ + + set visited; + map> g; + for(int j = start; j < i; j ++){ + int u = meetings[j][0], v = meetings[j][1]; + g[u].insert(v), g[v].insert(u); + if(uf.is_connected(0, u)) visited.insert(u); + if(uf.is_connected(0, v)) visited.insert(v); + } + + queue q; + for(int u: visited) q.push(u); + while(!q.empty()){ + int u = q.front(); q.pop(); + for(int v: g[u]) + if(!visited.count(v)){ + q.push(v); + visited.insert(v); + } + } + + for(int u: visited) uf.union_elements(0, u); + + start = i; + } + + vector res; + for(int i = 0; i < n; i ++) + if(uf.is_connected(0, i)) res.push_back(i); + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector> meetings1 = {{1, 2, 5}, {2, 3, 8}, {1, 5, 10}}; + print_vec(Solution().findAllPeople(6, meetings1, 1) ); + // 0 1 2 3 5 + + vector> meetings2 = {{3, 1, 3}, {1, 2, 2}, {0, 3, 3}}; + print_vec(Solution().findAllPeople(4, meetings2, 3) ); + // 0 1 3 + + vector> meetings3 = {{3, 4, 2}, {1, 2, 1}, {2, 3, 1}}; + print_vec(Solution().findAllPeople(5, meetings3, 1) ); + // 0 1 2 3 4 + + vector> meetings4 = {{0, 2, 1}, {1, 3, 1}, {4, 5, 1}}; + print_vec(Solution().findAllPeople(6, meetings4, 1) ); + // 0 1 2 3 + + return 0; +} diff --git a/2001-2500/2093-Minimum-Cost-to-Reach-City-With-Discounts/cpp-2093/CMakeLists.txt b/2001-2500/2093-Minimum-Cost-to-Reach-City-With-Discounts/cpp-2093/CMakeLists.txt new file mode 100644 index 00000000..d168fa2d --- /dev/null +++ b/2001-2500/2093-Minimum-Cost-to-Reach-City-With-Discounts/cpp-2093/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2093) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2093 main.cpp) diff --git a/2001-2500/2093-Minimum-Cost-to-Reach-City-With-Discounts/cpp-2093/main.cpp b/2001-2500/2093-Minimum-Cost-to-Reach-City-With-Discounts/cpp-2093/main.cpp new file mode 100644 index 00000000..32c91467 --- /dev/null +++ b/2001-2500/2093-Minimum-Cost-to-Reach-City-With-Discounts/cpp-2093/main.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/minimum-cost-to-reach-city-with-discounts/ +/// Author : liuyubobobo +/// Time : 2021-12-03 + +#include +#include +#include + +using namespace std; + + +/// Dijkstra +/// Time Complexity: O(discount * E * log(discount * E)) +/// Space Complexity: O(discount * V) +class Solution { +public: + int minimumCost(int n, vector>& highways, int discounts) { + + 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> dis(discounts + 1, vector(n, INT_MAX / 2)); + vector> visited(discounts + 1, vector(n, false)); + + // dis, {discount, u} + priority_queue>, vector>>, greater>>> pq; + pq.push({0, {discounts, 0}}); + while(!pq.empty()){ + int d = pq.top().first; + int discount = pq.top().second.first, u = pq.top().second.second; + pq.pop(); + + if(visited[discount][u]) continue; + visited[discount][u] = true; + + for(const pair& p: g[u]){ + int v = p.first, w = p.second; + if(d + w < dis[discount][v]){ + dis[discount][v] = d + w; + pq.push({d + w, {discount, v}}); + } + if(discount && d + w / 2 < dis[discount - 1][v]){ + dis[discount - 1][v] = d + w / 2; + pq.push({d + w / 2, {discount - 1, v}}); + } + } + } + + int res = INT_MAX / 2; + for(int discount = 0; discount <= discounts; discount ++) + res = min(res, dis[discount][n - 1]); + return res == INT_MAX / 2 ? -1 : res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/CMakeLists.txt b/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/CMakeLists.txt new file mode 100644 index 00000000..74b3bc2c --- /dev/null +++ b/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main2.cpp) diff --git a/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/main.cpp b/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/main.cpp new file mode 100644 index 00000000..0ed53368 --- /dev/null +++ b/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/finding-3-digit-even-numbers/ +/// Author : liuyubobobo +/// Time : 2021-12-04 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^3) +/// Space Complexity: O(1) +class Solution { +public: + vector findEvenNumbers(vector& d) { + + int n = d.size(); + + set res; + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + for(int k = j + 1; k < n; k ++){ + if(d[i]){ + if(d[k] % 2 == 0) res.insert(d[i] * 100 + d[j] * 10 + d[k]); + if(d[j] % 2 == 0) res.insert(d[i] * 100 + d[k] * 10 + d[j]); + } + if(d[j]){ + if(d[k] % 2 == 0) res.insert(d[j] * 100 + d[i] * 10 + d[k]); + if(d[i] % 2 == 0) res.insert(d[j] * 100 + d[k] * 10 + d[i]); + } + if(d[k]){ + if(d[j] % 2 == 0) res.insert(d[k] * 100 + d[i] * 10 + d[j]); + if(d[i] % 2 == 0) res.insert(d[k] * 100 + d[j] * 10 + d[i]); + } + } + + return vector(res.begin(), res.end()); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/main2.cpp b/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/main2.cpp new file mode 100644 index 00000000..8f816260 --- /dev/null +++ b/2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/main2.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/finding-3-digit-even-numbers/ +/// Author : liuyubobobo +/// Time : 2021-12-04 + +#include +#include +#include + +using namespace std; + + +/// Check every even number between [100, 999] +/// Time Complexity: O(999) +/// Space Complexity: O(1) +class Solution { +public: + vector findEvenNumbers(vector& d) { + + vector f(10, 0); + for(int e: d) f[e] ++; + + vector curf(10, 0), res; + for(int i = 100; i <= 999; i += 2){ + fill(curf.begin(), curf.end(), 0); + int x = i; + while(x) curf[x % 10] ++, x /= 10; + + bool ok = true; + for(int i = 0; i < 10; i ++) + if(curf[i] > f[i]){ok = false; break;} + if(ok) res.push_back(i); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/CMakeLists.txt b/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/CMakeLists.txt new file mode 100644 index 00000000..029d64e5 --- /dev/null +++ b/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main2.cpp) diff --git a/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/main.cpp b/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/main.cpp new file mode 100644 index 00000000..133363b6 --- /dev/null +++ b/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/ +/// Author : liuyubobobo +/// Time : 2021-12-04 + +#include +#include + +using namespace std; + + +/// Two Pass +/// 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: + ListNode* deleteMiddle(ListNode* head) { + + int len = 0; + ListNode* cur = head; + while(cur) len ++, cur = cur->next; + + int x = len / 2; + ListNode* dummy_head = new ListNode(-1, head); + cur = dummy_head; + for(int i = 0; i < x; i ++) cur = cur -> next; + + cur->next = cur->next->next; + + return dummy_head->next; + } +}; + +int main() { + + return 0; +} diff --git a/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/main2.cpp b/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/main2.cpp new file mode 100644 index 00000000..0e5b7b50 --- /dev/null +++ b/2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/main2.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/ +/// Author : liuyubobobo +/// Time : 2021-12-04 + +#include +#include + +using namespace std; + + +/// One Pass +/// 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: + ListNode* deleteMiddle(ListNode* head) { + + ListNode* dummy_head = new ListNode(-1, head); + ListNode* fast = head, *slow = dummy_head; + + while(fast && fast->next){ + fast = fast->next; + if(fast) fast = fast->next; + slow = slow->next; + } + slow->next = slow->next->next; + + return dummy_head->next; + } +}; + +int main() { + + return 0; +} diff --git a/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/CMakeLists.txt b/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/CMakeLists.txt new file mode 100644 index 00000000..e85d4767 --- /dev/null +++ b/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/main.cpp b/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/main.cpp new file mode 100644 index 00000000..2fef2f5e --- /dev/null +++ b/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/main.cpp @@ -0,0 +1,102 @@ +/// Source : https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/ +/// Author : liuyubobobo +/// Time : 2021-12-04 + +#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: + string getDirections(TreeNode* root, int s, int t) { + + int n = sz(root); + + vector>> g(n + 1); + dfs(root, g); + + vector> pre(n + 1, {-1, ' '}); + queue q; + q.push(s); + pre[s] = {s, ' '}; + while(!q.empty()){ + int u = q.front(); + q.pop(); + + if(u == t) break; + + for(const pair& p: g[u]){ + int v = p.first; + if(pre[v].first == -1){ + pre[v] = {u, p.second}; + q.push(v); + } + } + } + + string res = ""; + int cur = t; + while(pre[cur].first != cur){ + res += pre[cur].second; + cur = pre[cur].first; + } + reverse(res.begin(), res.end()); + return res; + } + +private: + void dfs(TreeNode* node, vector>>& g){ + + if(!node) return; + + if(node->left){ + g[node->val].push_back({node->left->val, 'L'}); + g[node->left->val].push_back({node->val, 'U'}); + } + if(node->right){ + g[node->val].push_back({node->right->val, 'R'}); + g[node->right->val].push_back({node->val, 'U'}); + } + + dfs(node->left, g); + dfs(node->right, g); + } + + int sz(TreeNode* node){ + + if(!node) return 0; + + int res = 1; + if(node->left) res += sz(node->left); + if(node->right) res += sz(node->right); + return res; + } +}; + +int main() { + + TreeNode* left = new TreeNode(1, new TreeNode(3), nullptr); + TreeNode* right = new TreeNode(2, new TreeNode(6), new TreeNode(4)); + TreeNode* root = new TreeNode(5, left, right); + cout << Solution().getDirections(root, 3, 6) << endl; + // UURL + + return 0; +} diff --git a/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/main2.cpp b/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/main2.cpp new file mode 100644 index 00000000..09e143b8 --- /dev/null +++ b/2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/main2.cpp @@ -0,0 +1,76 @@ +/// Source : https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/ +/// Author : liuyubobobo +/// Time : 2021-12-04 + +#include +#include +#include + +using namespace std; + + +/// LCA +/// 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: + string getDirections(TreeNode* root, int s, int t) { + + root = lca(root, s, t); + + string path1 = "", path2 = ""; + get_path(root, s, path1); + get_path(root, t, path2); + return string(path1.size(), 'U') + path2; + } + +private: + bool get_path(TreeNode* node, int x, string& path){ + + if(!node) return false; + if(node->val == x) return true; + + path += "L"; + if(get_path(node->left, x, path)) return true; + + path.back() = 'R'; + if(get_path(node->right, x, path)) return true; + path.pop_back(); + + return false; + } + + TreeNode* lca(TreeNode* node, int a, int b){ + + if(!node) return nullptr; + if(node->val == a || node->val == b) return node; + + TreeNode* l = lca(node->left, a, b); + TreeNode* r = lca(node->right, a, b); + if(l && r) return node; + return l ? l : r; + } +}; + + +int main() { + + TreeNode* left = new TreeNode(1, new TreeNode(3), nullptr); + TreeNode* right = new TreeNode(2, new TreeNode(6), new TreeNode(4)); + TreeNode* root = new TreeNode(5, left, right); + cout << Solution().getDirections(root, 3, 6) << endl; + // UURL + + return 0; +} diff --git a/2001-2500/2097-Valid-Arrangement-of-Pairs/cpp-2097/CMakeLists.txt b/2001-2500/2097-Valid-Arrangement-of-Pairs/cpp-2097/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2097-Valid-Arrangement-of-Pairs/cpp-2097/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/2097-Valid-Arrangement-of-Pairs/cpp-2097/main.cpp b/2001-2500/2097-Valid-Arrangement-of-Pairs/cpp-2097/main.cpp new file mode 100644 index 00000000..fba12beb --- /dev/null +++ b/2001-2500/2097-Valid-Arrangement-of-Pairs/cpp-2097/main.cpp @@ -0,0 +1,78 @@ +/// Source : https://leetcode.com/problems/valid-arrangement-of-pairs/ +/// Author : liuyubobobo +/// Time : 2021-12-05 + +#include +#include +#include +#include + +using namespace std; + + +/// Hierholzer’s Algorithm +/// Time Complexity: O(V + E) +/// Space Complexity: O(V + E) +class Solution { +public: + vector> validArrangement(vector>& pairs) { + + unordered_map> g; + unordered_map in_degrees; + for(const vector& p: pairs){ + g[p[0]].push_back(p[1]); + in_degrees[p[1]] ++; + } + + int s = g.begin()->first; + for(const pair>& p: g) + if(p.second.size() - in_degrees[p.first] == 1){ + s = p.first; + break; + } + + vector res; + + stack stack; + int curv = s; + stack.push(curv); + while(!stack.empty()){ + if(g[stack.top()].size()){ + int u = stack.top(), v = g[u].back(); + stack.push(v); + g[u].pop_back(); + } + else{ + res.push_back(stack.top()); + stack.pop(); + } + } + + vector> ret; + for(int i = res.size() - 1; i >= 1; i --) + ret.push_back({res[i], res[i - 1]}); + return ret; + } +}; + + +void print_vec(const vector>& v){ + + for(const vector& p: v) + cout << "(" << p[0] << "," << p[1] << ")"; + cout << endl; +} + +int main() { + + vector> pairs1 = {{5, 1}, {4, 5}, {11, 9}, {9, 4}}; + print_vec(Solution().validArrangement(pairs1)); + + vector> pairs2 = {{1, 3}, {3, 2}, {2, 1}}; + print_vec(Solution().validArrangement(pairs2)); + + vector> pairs3 = {{1, 2}, {1, 3}, {2, 1}}; + print_vec(Solution().validArrangement(pairs3)); + + return 0; +} diff --git a/2001-2500/2098-Subsequence-of-Size-K-With-the-Largest-Even-Sum/cpp-2098/CMakeLists.txt b/2001-2500/2098-Subsequence-of-Size-K-With-the-Largest-Even-Sum/cpp-2098/CMakeLists.txt new file mode 100644 index 00000000..c848ea8f --- /dev/null +++ b/2001-2500/2098-Subsequence-of-Size-K-With-the-Largest-Even-Sum/cpp-2098/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2098) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2098 main.cpp) diff --git a/2001-2500/2098-Subsequence-of-Size-K-With-the-Largest-Even-Sum/cpp-2098/main.cpp b/2001-2500/2098-Subsequence-of-Size-K-With-the-Largest-Even-Sum/cpp-2098/main.cpp new file mode 100644 index 00000000..1a66429c --- /dev/null +++ b/2001-2500/2098-Subsequence-of-Size-K-With-the-Largest-Even-Sum/cpp-2098/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/subsequence-of-size-k-with-the-largest-even-sum/ +/// Author : liuyubobobo +/// Time : 2021-12-11 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long largestEvenSum(vector& nums, int k) { + + vector odd, even; + for(int e: nums) + if(e % 2) odd.push_back(e); + else even.push_back(e); + + sort(odd.begin(), odd.end(), greater()); + sort(even.begin(), even.end(), greater()); + + vector odd_presum(odd.size() + 1, 0), even_presum(even.size() + 1, 0); + for(int i = 0; i < odd.size(); i ++) + odd_presum[i + 1] = odd_presum[i] + odd[i]; + for(int i = 0; i < even.size(); i ++) + even_presum[i + 1] = even_presum[i] + even[i]; + + long long res = -1; + for(int odd_num = 0; odd_num <= odd.size(); odd_num += 2){ + int even_num = k - odd_num; + if(even_num < 0 || even_num > even.size()) continue; + + res = max(res, odd_presum[odd_num] + even_presum[even_num]); + } + return res; + } +}; + + +int main() { + + vector nums1 = {4, 1, 5, 3, 1}; + cout << Solution().largestEvenSum(nums1, 3) << endl; + // 12 + + vector nums2 = {4, 6, 2}; + cout << Solution().largestEvenSum(nums2, 3) << endl; + // 12 + + vector nums3 = {1, 3, 5}; + cout << Solution().largestEvenSum(nums3, 1) << endl; + // -1 + + return 0; +} diff --git a/2001-2500/2099-Find-Subsequence-of-Length-K-With-the-Largest-Sum/cpp-2099/CMakeLists.txt b/2001-2500/2099-Find-Subsequence-of-Length-K-With-the-Largest-Sum/cpp-2099/CMakeLists.txt new file mode 100644 index 00000000..892bf351 --- /dev/null +++ b/2001-2500/2099-Find-Subsequence-of-Length-K-With-the-Largest-Sum/cpp-2099/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2099) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2099 main.cpp) diff --git a/2001-2500/2099-Find-Subsequence-of-Length-K-With-the-Largest-Sum/cpp-2099/main.cpp b/2001-2500/2099-Find-Subsequence-of-Length-K-With-the-Largest-Sum/cpp-2099/main.cpp new file mode 100644 index 00000000..6a6debb0 --- /dev/null +++ b/2001-2500/2099-Find-Subsequence-of-Length-K-With-the-Largest-Sum/cpp-2099/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/ +/// Author : liuyubobobo +/// Time : 2021-12-11 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn + klogk) +/// Space Complexity: O(n) +class Solution { +public: + vector maxSubsequence(vector& nums, int k) { + + vector> data(nums.size()); + for(int i = 0; i < nums.size(); i ++) data[i] = {nums[i], i}; + + sort(data.begin(), data.end(), greater>()); + while(data.size() > k) data.pop_back(); + + sort(data.begin(), data.end(), + [](const pair& p1, const pair& p2){ + return p1.second < p2.second; + }); + + vector res(k); + for(int i = 0; i < k; i ++) + res[i] = data[i].first; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2100-Find-Good-Days-to-Rob-the-Bank/cpp-2100/CMakeLists.txt b/2001-2500/2100-Find-Good-Days-to-Rob-the-Bank/cpp-2100/CMakeLists.txt new file mode 100644 index 00000000..b837f0a2 --- /dev/null +++ b/2001-2500/2100-Find-Good-Days-to-Rob-the-Bank/cpp-2100/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2100) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2100 main.cpp) diff --git a/2001-2500/2100-Find-Good-Days-to-Rob-the-Bank/cpp-2100/main.cpp b/2001-2500/2100-Find-Good-Days-to-Rob-the-Bank/cpp-2100/main.cpp new file mode 100644 index 00000000..f220332c --- /dev/null +++ b/2001-2500/2100-Find-Good-Days-to-Rob-the-Bank/cpp-2100/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/find-good-days-to-rob-the-bank/ +/// Author : liuyubobobo +/// Time : 2021-12-12 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector goodDaysToRobBank(vector& security, int time) { + + int n = security.size(); + + vector a(n, 1), b(n, 1); + for(int i = 1; i < n; i ++) + if(security[i] <= security[i - 1]) a[i] = a[i - 1] + 1; +// for(int e: a) cout << e << ' '; cout << '\n'; + + for(int i = n - 2; i >= 0; i --) + if(security[i] <= security[i + 1]) b[i] = b[i + 1] + 1; +// for(int e: b) cout << e << ' '; cout << '\n'; + + vector res; + for(int i = 0; i < n; i ++) + if(a[i] >= time + 1 && b[i] >= time + 1) res.push_back(i); + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector security1 = {5, 3, 3, 3, 5, 6, 2}; + print_vec(Solution().goodDaysToRobBank(security1, 2)); + // 2 3 + + vector security2 = {1, 1, 1, 1, 1}; + print_vec(Solution().goodDaysToRobBank(security2, 0)); + // 0 1 2 3 4 + + vector security3 = {1, 2, 3, 4, 5, 6}; + print_vec(Solution().goodDaysToRobBank(security3, 2)); + // empty + + vector security4 = {1}; + print_vec(Solution().goodDaysToRobBank(security4, 5)); + // empty + + return 0; +} diff --git a/2001-2500/2101-Detonate-the-Maximum-Bombs/cpp-2101/CMakeLists.txt b/2001-2500/2101-Detonate-the-Maximum-Bombs/cpp-2101/CMakeLists.txt new file mode 100644 index 00000000..40ff3fb6 --- /dev/null +++ b/2001-2500/2101-Detonate-the-Maximum-Bombs/cpp-2101/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2101) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2101 main.cpp) diff --git a/2001-2500/2101-Detonate-the-Maximum-Bombs/cpp-2101/main.cpp b/2001-2500/2101-Detonate-the-Maximum-Bombs/cpp-2101/main.cpp new file mode 100644 index 00000000..7c3d833e --- /dev/null +++ b/2001-2500/2101-Detonate-the-Maximum-Bombs/cpp-2101/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/detonate-the-maximum-bombs/ +/// Author : liuyubobobo +/// Time : 2021-12-12 + +#include +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { +public: + int maximumDetonation(vector>& bombs) { + + int n = bombs.size(); + + vector> g(n); + for(int i = 0; i < n; i ++) + for(int j = 0; j < n; j ++){ + if(i == j) continue; + if(dis_sq(bombs[i][0], bombs[i][1], bombs[j][0], bombs[j][1]) <= (long long)bombs[i][2] * bombs[i][2]) + g[i].push_back(j); + } + + int res = 1; + for(int i = 0; i < n; i ++){ + vector visited(n, false); + res = max(res, dfs(g, i, visited)); + } + return res; + } + +private: + int dfs(const vector>& g, int u, vector& visited){ + + visited[u] = true; + int res = 1; + for(int v: g[u]) + if(!visited[v]) res += dfs(g, v, visited); + return res; + } + + long long dis_sq(long long x1, long long y1, long long x2, long long y2){ + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2102-Sequentially-Ordinal-Rank-Tracker/cpp-2102/CMakeLists.txt b/2001-2500/2102-Sequentially-Ordinal-Rank-Tracker/cpp-2102/CMakeLists.txt new file mode 100644 index 00000000..599acd05 --- /dev/null +++ b/2001-2500/2102-Sequentially-Ordinal-Rank-Tracker/cpp-2102/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2102) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2102 main.cpp) diff --git a/2001-2500/2102-Sequentially-Ordinal-Rank-Tracker/cpp-2102/main.cpp b/2001-2500/2102-Sequentially-Ordinal-Rank-Tracker/cpp-2102/main.cpp new file mode 100644 index 00000000..cd3e6f37 --- /dev/null +++ b/2001-2500/2102-Sequentially-Ordinal-Rank-Tracker/cpp-2102/main.cpp @@ -0,0 +1,327 @@ +/// Source : https://leetcode.com/problems/sequentially-ordinal-rank-tracker/ +/// Author : liuyubobobo +/// Time : 2021-12-12 + +#include + +using namespace std; + + +/// Using AVLTree +/// Time Complexity: init: O(1) +/// add: O(logn) +/// query: O(logn) +/// Space Complexity: O(n) +template +class AVLTreeSet{ + +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: + AVLTreeSet(){} + + 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); + else + return node; + + 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 SORTracker { + +private: + AVLTreeSet> tree; + int time; + +public: + SORTracker() : time(0) {} + + void add(string name, int score) { + tree.add({-score, name}); + } + + string get() { + return tree.select(time ++).second; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2103-Rings-and-Rods/cpp-2103/CMakeLists.txt b/2001-2500/2103-Rings-and-Rods/cpp-2103/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/2001-2500/2103-Rings-and-Rods/cpp-2103/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2103-Rings-and-Rods/cpp-2103/main.cpp b/2001-2500/2103-Rings-and-Rods/cpp-2103/main.cpp new file mode 100644 index 00000000..0645d92b --- /dev/null +++ b/2001-2500/2103-Rings-and-Rods/cpp-2103/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/rings-and-rods/ +/// Author : liuyubobobo +/// Time : 2021-12-11 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int countPoints(string rings) { + + vector> poles(10); + for(int i = 0; i < rings.size(); i += 2) + poles[rings[i + 1] - '0'].insert(rings[i]); + + int res = 0; + for(int i = 0; i < 10; i ++) + res += poles[i].size() == 3; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/CMakeLists.txt b/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/CMakeLists.txt new file mode 100644 index 00000000..084dd354 --- /dev/null +++ b/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main2.cpp) diff --git a/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/main.cpp b/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/main.cpp new file mode 100644 index 00000000..8928008c --- /dev/null +++ b/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/main.cpp @@ -0,0 +1,74 @@ +/// Source : https://leetcode.com/problems/sum-of-subarray-ranges/ +/// Author : liuyubobobo +/// Time : 2021-12-11 + +#include +#include + +using namespace std; + + +/// ST +/// Time Complexity: O(nlogn + n^2) +/// Space Complexity: O(n) +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: + long long subArrayRanges(vector& nums) { + + auto max_f = [](int a, int b){return max(a, b);}; + auto min_f = [](int a, int b){return min(a, b);}; + SparseTable st_min(nums, min_f); + SparseTable st_max(nums, max_f); + + int n = nums.size(); + long long res = 0; + for(int l = 0; l < n; l ++) + for(int r = l + 1; r < n; r ++) + res += (long long)st_max.query(l, r) - st_min.query(l, r); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/main2.cpp b/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/main2.cpp new file mode 100644 index 00000000..222e9d13 --- /dev/null +++ b/2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/main2.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/sum-of-subarray-ranges/ +/// Author : liuyubobobo +/// Time : 2021-12-11 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + long long subArrayRanges(vector& nums) { + + int n = nums.size(); + long long res = 0; + for(int l = 0; l < n; l ++){ + int minv = nums[l], maxv = nums[l]; + for(int r = l + 1; r < n; r ++){ + minv = min(minv, nums[r]), maxv = max(maxv, nums[r]); + res += (maxv - minv); + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2105-Watering-Plants-II/cpp-2105/CMakeLists.txt b/2001-2500/2105-Watering-Plants-II/cpp-2105/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/2001-2500/2105-Watering-Plants-II/cpp-2105/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2105-Watering-Plants-II/cpp-2105/main.cpp b/2001-2500/2105-Watering-Plants-II/cpp-2105/main.cpp new file mode 100644 index 00000000..8775fbad --- /dev/null +++ b/2001-2500/2105-Watering-Plants-II/cpp-2105/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/watering-plants-ii/ +/// Author : liuyubobobo +/// Time : 2021-12-11 + +#include +#include + +using namespace std; + + +/// Two Pointers Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumRefill(vector& plants, int capacityA, int capacityB) { + + int n = plants.size(); + int l = 0, r = n - 1, res = 0, A = capacityA, B = capacityB; + while(l < r){ + water(plants[l], A, res, capacityA); + water(plants[r], B, res, capacityB); + l ++, r --; + } + + if(l == r){ + if(A >= B) water(plants[l], A, res, capacityA); + else water(plants[l], B, res, capacityB); + } + return res; + } + +private: + void water(const int need, int& cur, int& res, const int cap){ + if(cur >= need) cur -= need; + else{ + res ++; + cur = cap - need; + } + } +}; + + +int main() { + + vector plants1 = {2, 2, 3, 3}; + cout << Solution().minimumRefill(plants1, 5, 5) << endl; + // 1 + + vector plants2 = {2, 2, 3, 3}; + cout << Solution().minimumRefill(plants2, 3, 4) << endl; + // 2 + + vector plants3 = {5}; + cout << Solution().minimumRefill(plants3, 10, 8) << endl; + // 0 + + vector plants4 = {1, 2, 4, 5, 5}; + cout << Solution().minimumRefill(plants4, 6, 5) << endl; + // 2 + + vector plants5 = {2, 2, 5, 2, 2}; + cout << Solution().minimumRefill(plants5, 5, 5) << endl; + // 1 + + return 0; +} diff --git a/2001-2500/2106-Maximum-Fruits-Harvested-After-at-Most-K-Steps/cpp-2106/CMakeLists.txt b/2001-2500/2106-Maximum-Fruits-Harvested-After-at-Most-K-Steps/cpp-2106/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2106-Maximum-Fruits-Harvested-After-at-Most-K-Steps/cpp-2106/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2106-Maximum-Fruits-Harvested-After-at-Most-K-Steps/cpp-2106/main.cpp b/2001-2500/2106-Maximum-Fruits-Harvested-After-at-Most-K-Steps/cpp-2106/main.cpp new file mode 100644 index 00000000..d63028ff --- /dev/null +++ b/2001-2500/2106-Maximum-Fruits-Harvested-After-at-Most-K-Steps/cpp-2106/main.cpp @@ -0,0 +1,71 @@ +/// Source : https://leetcode.com/problems/maximum-fruits-harvested-after-at-most-k-steps/ +/// Author : liuyubobobo +/// Time : 2021-12-11 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(max_pos) +/// Space Complexity: O(max_pos) +class Solution { +public: + int maxTotalFruits(vector>& fruits, int startPos, int k) { + + int max_pos = startPos; + for(const vector& e: fruits) + max_pos = max(max_pos, e[0]); + + vector data(max_pos + 1, 0); + for(const vector& e: fruits) + data[e[0]] += e[1]; + + vector presum(data.size() + 1, 0); + for(int i = 0; i < data.size(); i ++) + presum[i + 1] = presum[i] + data[i]; + + long long res = 0; + for(int i = 0; startPos + i < data.size() && i <= k; i ++) { + int r = startPos + i; + int l = startPos; + if(k - 2 * i > 0) l = max(0, startPos - (k - 2 * i)); + res = max(res, presum[r + 1] - presum[l]); + } + for(int i = 0; startPos - i >= 0 && i <= k; i ++){ + int l = startPos - i; + int r = startPos; + if(k - 2 * i > 0) r = min(startPos + (k - 2 * i), (int)data.size() - 1); + res = max(res, presum[r + 1] - presum[l]); + } + return res; + } +}; + + +int main() { + + vector> fruits1 = {{2, 8}, {6, 3}, {8, 6}}; + cout << Solution().maxTotalFruits(fruits1, 5, 4) << endl; + // 9 + + vector> fruits2 = {{0, 9}, {4, 1}, {5, 7}, {6, 2}, {7, 4}, {10, 9}}; + cout << Solution().maxTotalFruits(fruits2, 5, 4) << endl; + // 14 + + vector> fruits3 = {{0, 3}, {6, 4}, {8, 5}}; + cout << Solution().maxTotalFruits(fruits3, 3, 2) << endl; + // 0 + + vector> fruits4 = {{200000, 10000}}; + cout << Solution().maxTotalFruits(fruits4, 200000, 0) << endl; + // 10000 + + vector> fruits5 = {{0, 10000}}; + cout << Solution().maxTotalFruits(fruits5, 200000, 200000) << endl; + // 10000 + + return 0; +} diff --git a/2001-2500/2107-Number-of-Unique-Flavors-After-Sharing-K-Candies/cpp-2107/CMakeLists.txt b/2001-2500/2107-Number-of-Unique-Flavors-After-Sharing-K-Candies/cpp-2107/CMakeLists.txt new file mode 100644 index 00000000..40263f94 --- /dev/null +++ b/2001-2500/2107-Number-of-Unique-Flavors-After-Sharing-K-Candies/cpp-2107/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2107) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2107 main.cpp) diff --git a/2001-2500/2107-Number-of-Unique-Flavors-After-Sharing-K-Candies/cpp-2107/main.cpp b/2001-2500/2107-Number-of-Unique-Flavors-After-Sharing-K-Candies/cpp-2107/main.cpp new file mode 100644 index 00000000..d5ff92fd --- /dev/null +++ b/2001-2500/2107-Number-of-Unique-Flavors-After-Sharing-K-Candies/cpp-2107/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/number-of-unique-flavors-after-sharing-k-candies/ +/// Author : liuyubobobo +/// Time : 2021-12-16 + +#include +#include +#include + +using namespace std; + + +/// Sliding Window +/// Time Complexity: O(n) +/// Space Complexity: O(k) +class Solution { +public: + int shareCandies(vector& candies, int k) { + + map f; + int n = candies.size(); + for(int i = k; i < n; i ++) + f[candies[i]] ++; + + int res = f.size(); + for(int i = k; i < n; i ++){ + + f[candies[i - k]] ++; + f[candies[i]] --; + if(f[candies[i]] == 0) f.erase(candies[i]); + + res = max(res, (int)f.size()); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2108-Find-First-Palindromic-String-in-the-Array/cpp-2108/CMakeLists.txt b/2001-2500/2108-Find-First-Palindromic-String-in-the-Array/cpp-2108/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/2001-2500/2108-Find-First-Palindromic-String-in-the-Array/cpp-2108/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2108-Find-First-Palindromic-String-in-the-Array/cpp-2108/main.cpp b/2001-2500/2108-Find-First-Palindromic-String-in-the-Array/cpp-2108/main.cpp new file mode 100644 index 00000000..d5b368f6 --- /dev/null +++ b/2001-2500/2108-Find-First-Palindromic-String-in-the-Array/cpp-2108/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/find-first-palindromic-string-in-the-array/ +/// Author : liuyubobobo +/// Time : 2021-12-18 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n * |s|) +/// Space Complexity: O(1) +class Solution { +public: + string firstPalindrome(vector& words) { + + for(const string& s: words) + if(is_palindrome(s)) return s; + return ""; + } + +private: + bool is_palindrome(const string& s){ + + int n = s.size(); + for(int i = 0; i < n; i ++) + if(s[i] != s[n - 1 - i]) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2109-Adding-Spaces-to-a-String/cpp-2109/CMakeLists.txt b/2001-2500/2109-Adding-Spaces-to-a-String/cpp-2109/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/2001-2500/2109-Adding-Spaces-to-a-String/cpp-2109/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2109-Adding-Spaces-to-a-String/cpp-2109/main.cpp b/2001-2500/2109-Adding-Spaces-to-a-String/cpp-2109/main.cpp new file mode 100644 index 00000000..02a90ce3 --- /dev/null +++ b/2001-2500/2109-Adding-Spaces-to-a-String/cpp-2109/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/adding-spaces-to-a-string/ +/// Author : liuyubobobo +/// Time : 2021-12-18 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string addSpaces(string s, vector& spaces) { + + string res = ""; + for(int i = 0, p = 0; i < s.size(); i ++){ + if(p < spaces.size() && i == spaces[p]) + res += (char)(' '), p ++; + res += s[i]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2110-Number-of-Smooth-Descent-Periods-of-a-Stock/cpp-2110/CMakeLists.txt b/2001-2500/2110-Number-of-Smooth-Descent-Periods-of-a-Stock/cpp-2110/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/2001-2500/2110-Number-of-Smooth-Descent-Periods-of-a-Stock/cpp-2110/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) diff --git a/2001-2500/2110-Number-of-Smooth-Descent-Periods-of-a-Stock/cpp-2110/main.cpp b/2001-2500/2110-Number-of-Smooth-Descent-Periods-of-a-Stock/cpp-2110/main.cpp new file mode 100644 index 00000000..e8ce2c61 --- /dev/null +++ b/2001-2500/2110-Number-of-Smooth-Descent-Periods-of-a-Stock/cpp-2110/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/ +/// Author : liuyubobobo +/// Time : 2021-12-18 + +#include +#include + +using namespace std; + + +/// Split +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long getDescentPeriods(vector& prices) { + + int n = prices.size(); + + long long res = 0; + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || prices[i] + 1 != prices[i - 1]){ + long long len = i - start; + res += (len + 1ll) * len / 2ll; + + start = i; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2111-Minimum-Operations-to-Make-the-Array-K-Increasing/cpp-2111/CMakeLists.txt b/2001-2500/2111-Minimum-Operations-to-Make-the-Array-K-Increasing/cpp-2111/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2111-Minimum-Operations-to-Make-the-Array-K-Increasing/cpp-2111/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2111-Minimum-Operations-to-Make-the-Array-K-Increasing/cpp-2111/main.cpp b/2001-2500/2111-Minimum-Operations-to-Make-the-Array-K-Increasing/cpp-2111/main.cpp new file mode 100644 index 00000000..fb1775ec --- /dev/null +++ b/2001-2500/2111-Minimum-Operations-to-Make-the-Array-K-Increasing/cpp-2111/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/ +/// Author : liuyubobobo +/// Time : 2021-12-18 + +#include +#include + +using namespace std; + + +/// LIS +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int kIncreasing(vector& arr, int k) { + + int n = arr.size(); + + int res = 0; + for(int start = 0; start < k; start ++){ + vector A; + for(int i = start; i < n; i += k) A.push_back(arr[i]); + res += A.size() - lis(A); + } + return res; + } + +private: + int lis(const vector& A){ + + vector dp; + for(int e: A) + if(dp.empty() || e >= dp.back()) + dp.push_back(e); + else + dp[upper_bound(dp.begin(), dp.end(), e) - dp.begin()] = e; + return dp.size(); + } +}; + + +int main() { + + vector arr1 = {5,4,3,2,1}; + cout << Solution().kIncreasing(arr1, 1) << endl; + // 4 + + vector arr2 = {4,1,5,2,6,2}; + cout << Solution().kIncreasing(arr2, 2) << endl; + // 0 + + vector arr3 = {4,1,5,2,6,2}; + cout << Solution().kIncreasing(arr3, 3) << endl; + // 2 + + vector arr4 = {2,2,2,2,2,1,1,4,4,3,3,3,3,3}; + cout << Solution().kIncreasing(arr4, 1) << endl; + // 4 + + return 0; +} diff --git a/2001-2500/2113-Elements-in-Array-After-Removing-and-Replacing-Elements/cpp-2113/CMakeLists.txt b/2001-2500/2113-Elements-in-Array-After-Removing-and-Replacing-Elements/cpp-2113/CMakeLists.txt new file mode 100644 index 00000000..967f1941 --- /dev/null +++ b/2001-2500/2113-Elements-in-Array-After-Removing-and-Replacing-Elements/cpp-2113/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2113) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2113 main.cpp) diff --git a/2001-2500/2113-Elements-in-Array-After-Removing-and-Replacing-Elements/cpp-2113/main.cpp b/2001-2500/2113-Elements-in-Array-After-Removing-and-Replacing-Elements/cpp-2113/main.cpp new file mode 100644 index 00000000..b1808625 --- /dev/null +++ b/2001-2500/2113-Elements-in-Array-After-Removing-and-Replacing-Elements/cpp-2113/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/elements-in-array-after-removing-and-replacing-elements/ +/// Author : liuyubobobo +/// Time : 2021-12-24 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(q) +/// Space Complexity: O(1) +class Solution { +public: + vector elementInNums(vector& nums, vector>& queries) { + + int n = nums.size(); + + vector res(queries.size()); + for(int i = 0; i < queries.size(); i ++){ + int t = queries[i][0], index = queries[i][1]; + + t %= (2 * n); + int m; + if(t < n) m = n - t; + else m = t - n; + + if(index >= m) res[i] = -1; + else if(t < n) res[i] = nums[index + t]; + else res[i] = nums[index]; + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector nums1 = {0, 1, 2}; + vector> queries1 = {{0, 2}, {2, 0}, {3, 2}, {5, 0}}; + print_vec(Solution().elementInNums(nums1, queries1)); + // 2 2 -1 0 + + vector nums2 = {2}; + vector> queries2 = {{0, 0}, {1, 0}, {2, 0}, {3, 0}}; + print_vec(Solution().elementInNums(nums2, queries2)); + // 2 -1 2 -1 + + return 0; +} diff --git a/2001-2500/2114-Maximum-Number-of-Words-Found-in-Sentences/cpp-2114/CMakeLists.txt b/2001-2500/2114-Maximum-Number-of-Words-Found-in-Sentences/cpp-2114/CMakeLists.txt new file mode 100644 index 00000000..83aeba7d --- /dev/null +++ b/2001-2500/2114-Maximum-Number-of-Words-Found-in-Sentences/cpp-2114/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2114) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2114 main.cpp) diff --git a/2001-2500/2114-Maximum-Number-of-Words-Found-in-Sentences/cpp-2114/main.cpp b/2001-2500/2114-Maximum-Number-of-Words-Found-in-Sentences/cpp-2114/main.cpp new file mode 100644 index 00000000..8475fa58 --- /dev/null +++ b/2001-2500/2114-Maximum-Number-of-Words-Found-in-Sentences/cpp-2114/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/ +/// Author : liuyubobobo +/// Time : 2021-08-02 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n) +/// Space Compelxity: O(1) +class Solution { +public: + int mostWordsFound(vector& sentences) { + + int res = 0; + for(const string& s: sentences) + res = max(res, count_words(s)); + return res; + } + +private: + int count_words(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/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/CMakeLists.txt b/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/CMakeLists.txt new file mode 100644 index 00000000..25bc8ba6 --- /dev/null +++ b/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2115) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2115 main2.cpp) diff --git a/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/main.cpp b/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/main.cpp new file mode 100644 index 00000000..a81ceec4 --- /dev/null +++ b/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/main.cpp @@ -0,0 +1,120 @@ +/// Source : https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/ +/// Author : liuyubobobo +/// Time : 2021-12-26 + +#include +#include +#include +#include + +using namespace std; + + +/// Brute Force DFS +/// Directed Graph Cycle Finding +/// Time Complexity: O(q(n + m)) +/// Space Compplexity: O(n + m) +class Solution { + +private: + // 0-recipes; 1-ingredients + map str2index; + vector index2str; + vector is_ing; + +public: + vector findAllRecipes(vector& recipes, vector>& ingredients, vector& supplies) { + + for(const string& recipe: recipes) + if(!str2index.count(recipe)){ + str2index[recipe] = index2str.size(); + index2str.push_back(recipe); + is_ing.push_back(0); + } + + for(const vector& v: ingredients) + for(const string& ing: v) + if(!str2index.count(ing)){ + str2index[ing] = index2str.size(); + index2str.push_back(ing); + is_ing.push_back(1); + } + + vector> g(str2index.size(), vector()); + for(int i = 0; i < ingredients.size(); i ++){ + assert(str2index.count(recipes[i])); + int u = str2index[recipes[i]]; + for(const string& ing: ingredients[i]){ + assert(str2index.count(ing)); + int v = str2index[ing]; + g[u].push_back(v); + } + } + + set all(supplies.begin(), supplies.end()); + + vector res; + for(const string& recipe: recipes){ + assert(str2index.count(recipe)); + int s = str2index[recipe]; + vector visited(g.size(), false); + vector instack(g.size(), false); + if(!dfs(g, s, visited, instack)) continue; + + bool ok = true; + for(int i = 0; i < g.size(); i ++) + if(visited[i] && is_ing[i] && !all.count(index2str[i])){ + ok = false; + break; + } + if(ok) res.push_back(recipe); + } + return res; + } + +private: + bool dfs(const vector>& g, int u, + vector& visited, vector& instack){ + + assert(0 <= u && u < visited.size()); + visited[u] = true; + instack[u] = true; + for(int v: g[u]) + if(!visited[v]){ + if(!dfs(g, v, visited, instack)) return false; + } + else if(instack[v]) return false; + instack[u] = false; + return true; + } +}; + + +void print_vec(const vector& v){ + for(const string& s: v) cout << s << ' '; cout << endl; +} + +int main() { + + vector recipes1 = {"bread","sandwich","burger"}; + vector> ingredients1 = {{"yeast","flour"},{"bread","meat"},{"sandwich","meat","bread"}}; + vector supplies1 = {"yeast","flour","meat"}; + print_vec(Solution().findAllRecipes(recipes1, ingredients1, supplies1)); + // "bread","sandwich","burger" + + vector recipe2 = {"ju","fzjnm","x","e","zpmcz","h","q"}; + vector> ingredients2 = { + {"d"}, + {"hveml","f","cpivl"}, + {"cpivl","zpmcz","h","e","fzjnm","ju"}, + {"cpivl","hveml","zpmcz","ju","h"}, + {"h","fzjnm","e","q","x"}, + {"d","hveml","cpivl","q","zpmcz","ju","e","x"}, + {"f","hveml","cpivl"} + }; + vector supplies2 = {"f","hveml","cpivl","d"}; + print_vec(Solution().findAllRecipes(recipe2, ingredients2, supplies2)); + // ju fzjnm q + + return 0; +} diff --git a/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/main2.cpp b/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/main2.cpp new file mode 100644 index 00000000..12b92a7e --- /dev/null +++ b/2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/main2.cpp @@ -0,0 +1,110 @@ +/// Source : https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/ +/// Author : liuyubobobo +/// Time : 2021-12-26 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Topological Sorting +/// Time Complexity: O((n + m)) +/// Space Compplexity: O(n + m) +class Solution { + +private: + // 0-recipes; 1-ingredients + map str2index; + vector index2str; + vector is_ing; + +public: + vector findAllRecipes(vector& recipes, vector>& ingredients, vector& supplies) { + + for(const string& recipe: recipes) + if(!str2index.count(recipe)){ + str2index[recipe] = index2str.size(); + index2str.push_back(recipe); + is_ing.push_back(0); + } + + for(const vector& v: ingredients) + for(const string& ing: v) + if(!str2index.count(ing)){ + str2index[ing] = index2str.size(); + index2str.push_back(ing); + is_ing.push_back(1); + } + + vector> g(str2index.size(), vector()); + vector degrees(g.size(), 0); + for(int i = 0; i < ingredients.size(); i ++){ + assert(str2index.count(recipes[i])); + int u = str2index[recipes[i]]; + for(const string& ing: ingredients[i]){ + assert(str2index.count(ing)); + int v = str2index[ing]; + g[v].push_back(u); + degrees[u] ++; + } + } + + set all(supplies.begin(), supplies.end()); + vector res; + + queue q; + for(string s: supplies) + if(str2index.count(s)) q.push(str2index[s]); + + while(!q.empty()){ + int u = q.front(); q.pop(); + if(is_ing[u] == 0) res.push_back(index2str[u]); + + for(int v: g[u]){ + degrees[v] --; + if(degrees[v] == 0) q.push(v); + } + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(const string& s: v) cout << s << ' '; cout << endl; +} + +int main() { + + vector recipes1 = {"bread","sandwich","burger"}; + vector> ingredients1 = {{"yeast","flour"},{"bread","meat"},{"sandwich","meat","bread"}}; + vector supplies1 = {"yeast","flour","meat"}; + print_vec(Solution().findAllRecipes(recipes1, ingredients1, supplies1)); + // "bread","sandwich","burger" + + vector recipe2 = {"ju","fzjnm","x","e","zpmcz","h","q"}; + vector> ingredients2 = { + {"d"}, + {"hveml","f","cpivl"}, + {"cpivl","zpmcz","h","e","fzjnm","ju"}, + {"cpivl","hveml","zpmcz","ju","h"}, + {"h","fzjnm","e","q","x"}, + {"d","hveml","cpivl","q","zpmcz","ju","e","x"}, + {"f","hveml","cpivl"} + }; + vector supplies2 = {"f","hveml","cpivl","d"}; + print_vec(Solution().findAllRecipes(recipe2, ingredients2, supplies2)); + // ju fzjnm q + + vector recipes3 = {"bread"}; + vector> ingredients3 = {{"yeast","flour"}}; + vector supplies3 = {"yeast","flour","corn"}; + print_vec(Solution().findAllRecipes(recipes3, ingredients3, supplies3)); + // "bread" + + return 0; +} diff --git a/2001-2500/2116-Check-if-a-Parentheses-String-Can-Be-Valid/cpp-2116/CMakeLists.txt b/2001-2500/2116-Check-if-a-Parentheses-String-Can-Be-Valid/cpp-2116/CMakeLists.txt new file mode 100644 index 00000000..15cf0291 --- /dev/null +++ b/2001-2500/2116-Check-if-a-Parentheses-String-Can-Be-Valid/cpp-2116/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(cpp_2116) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2116 main.cpp) diff --git a/2001-2500/2116-Check-if-a-Parentheses-String-Can-Be-Valid/cpp-2116/main.cpp b/2001-2500/2116-Check-if-a-Parentheses-String-Can-Be-Valid/cpp-2116/main.cpp new file mode 100644 index 00000000..bb2fddb0 --- /dev/null +++ b/2001-2500/2116-Check-if-a-Parentheses-String-Can-Be-Valid/cpp-2116/main.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/ +/// Author : liuyubobobo +/// Time : 2022-01-28 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool canBeValid(string s, string locked) { + + int n = s.size(); + if(n % 2 == 1) return false; + + int open = 0, locked_num = 0; + for(int i = 0; i < n; i ++){ + if(locked[i] == '1'){ + locked_num ++; + if(s[i] == '(') open ++; + else open --; + } + int t = (i + 1) - locked_num; + if(open < 0 && t < abs(open)) return false; + } + + open = 0, locked_num = 0; + for(int i = n - 1; i >= 0; i --){ + if(locked[i] == '1'){ + locked_num ++; + if(s[i] == ')') open ++; + else open --; + } + int t = (n - i) - locked_num; + if(open < 0 && t < abs(open)) return false; + } + return true; + } +}; + + +int main() { + + string s1 = "))()))", locked1 = "010100"; + cout << Solution().canBeValid(s1, locked1) << endl; + // 1 + + string s2 = "())(()(()(())()())(())((())(()())((())))))(((((((())(()))))("; + string locked2 = "100011110110011011010111100111011101111110000101001101001111"; + cout << Solution().canBeValid(s2, locked2) << endl; + // 0 + + string s3 = "((()(()()))()((()()))))()((()(()"; + string locked3 = "10111100100101001110100010001001"; + cout << Solution().canBeValid(s3, locked3) << endl; + // 1 + + return 0; +} diff --git a/2001-2500/2117-Abbreviating-the-Product-of-a-Range/cpp-2117/CMakeLists.txt b/2001-2500/2117-Abbreviating-the-Product-of-a-Range/cpp-2117/CMakeLists.txt new file mode 100644 index 00000000..e88017ff --- /dev/null +++ b/2001-2500/2117-Abbreviating-the-Product-of-a-Range/cpp-2117/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2117) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2117 main.cpp) diff --git a/2001-2500/2117-Abbreviating-the-Product-of-a-Range/cpp-2117/main.cpp b/2001-2500/2117-Abbreviating-the-Product-of-a-Range/cpp-2117/main.cpp new file mode 100644 index 00000000..ec9eacd7 --- /dev/null +++ b/2001-2500/2117-Abbreviating-the-Product-of-a-Range/cpp-2117/main.cpp @@ -0,0 +1,131 @@ +/// Source : https://leetcode.com/problems/abbreviating-the-product-of-a-range/ +/// Author : liuyubobobo +/// Time : 2022-01-28 + +#include +#include +#include +#include + +using namespace std; + + +/// Big Number Simulation +/// Time Complexity: O(right - left) +/// Space Complexity: O(right - left) +class BigNumber{ + +private: + long long pre = 0, suf = 0; + const int d = 14; + const long long pow10e14 = 100000000000000ll; + +public: + BigNumber(int num) : suf(num){}; + + BigNumber& operator*=(int num){ + + if(pre == 0){ + suf *= num; + if(to_string(suf).size() <= d) return *this; + + pre = suf / pow10e14; + suf %= pow10e14; + return *this; + } + + suf *= num; + + long long carry = suf / pow10e14; + suf %= pow10e14; + + if(to_string(pre).size() < 19){ + pre *= num; + pre += carry; + } + else pre *= num; + + string pres = to_string(pre); + pre = stoll(pres.substr(0, d).c_str()); + + return *this; + } + + string str(){ + if(pre == 0){ + string sufs = to_string(suf); + if(sufs.size() <= 10) return sufs; + + string a = sufs.substr(0, 5), b = sufs.substr(sufs.size() - 5); + return a + "..." + b; + } + + string pres = to_string(pre), sufs = to_string(suf); + if(pres.size() + sufs.size() < 2 * d){ + string s = pres + sufs; + string a = s.substr(0, 5), b = s.substr(s.size() - 5); + return a + "..." + b; + } + + return pres.substr(0, 5) + "..." + sufs.substr(sufs.size() - 5); + } +}; + +class Solution { +public: + string abbreviateProduct(int left, int right) { + + vector nums; + int twos = 0, fives = 0; + for(int i = left; i <= right; i ++){ + nums.push_back(i); + twos += get(i, 2); + fives += get(i, 5); + } + + int zeros = min(twos, fives); + int cnt_twos = zeros, cnt_fives = zeros; + for(int i = 0; i < nums.size() && (cnt_twos || cnt_fives); i ++){ + erase(nums[i], 2, cnt_twos); + erase(nums[i], 5, cnt_fives); + } + + BigNumber number(nums[0]); + for(int i = 1; i < nums.size(); i ++) + number *= nums[i]; + return number.str() + "e" + to_string(zeros); + } + +private: + void erase(int& num, int p, int& cnt){ + while(cnt && num % p == 0) + cnt --, num /= p; + } + + int get(int num, int p){ + int res = 0; + while(num % p == 0) res ++, num /= p; + return res; + } +}; + + +int main() { + + cout << Solution().abbreviateProduct(1, 4) << endl; + // 24e0 + + cout << Solution().abbreviateProduct(2, 11) << endl; + // 399168e2 + + cout << Solution().abbreviateProduct(371, 375) << endl; + // 7219856259e3 + + cout << Solution().abbreviateProduct(981, 3082) << endl; + // 34865...63744e524 + + cout << Solution().abbreviateProduct(4838, 6186) << endl; + // 36088...36896e337 + + return 0; +} diff --git a/2001-2500/2119-A-Number-After-a-Double-Reversal/cpp-2119/CMakeLists.txt b/2001-2500/2119-A-Number-After-a-Double-Reversal/cpp-2119/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/2001-2500/2119-A-Number-After-a-Double-Reversal/cpp-2119/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) diff --git a/2001-2500/2119-A-Number-After-a-Double-Reversal/cpp-2119/main.cpp b/2001-2500/2119-A-Number-After-a-Double-Reversal/cpp-2119/main.cpp new file mode 100644 index 00000000..fb5c5438 --- /dev/null +++ b/2001-2500/2119-A-Number-After-a-Double-Reversal/cpp-2119/main.cpp @@ -0,0 +1,27 @@ +/// Source : https://leetcode.com/problems/a-number-after-a-double-reversal/ +/// Author : liuyubobobo +/// Time : 2021-12-25 + +#include +#include + +using namespace std; + + +/// Mathematics +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + bool isSameAfterReversals(int num) { + + if(num == 0) return true; + return num % 10 != 0; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2120-Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/cpp-2120/CMakeLists.txt b/2001-2500/2120-Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/cpp-2120/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/2001-2500/2120-Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/cpp-2120/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) diff --git a/2001-2500/2120-Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/cpp-2120/main.cpp b/2001-2500/2120-Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/cpp-2120/main.cpp new file mode 100644 index 00000000..2109a02c --- /dev/null +++ b/2001-2500/2120-Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/cpp-2120/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/ +/// Author : liuyubobobo +/// Time : 2021-12-25 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(m * n) +/// Space Compelxity: O(1) +class Solution { +private: + int N; + +public: + vector executeInstructions(int n, vector& startPos, string s) { + + N = n; + + int m = s.size(); + vector res(m); + for(int i = 0; i < m; i ++) + res[i] = go(s, startPos[0], startPos[1], i); + return res; + } + +private: + int go(const string& s, int x, int y, int start){ + + int res = 0; + for(int i = start; i < s.size(); i ++){ + if(s[i] == 'R') y ++; + else if(s[i] == 'L') y --; + else if(s[i] == 'U') x --; + else if(s[i] == 'D') x ++; + if((x >= 0 && x < N && y >= 0 && y < N)) res ++; + else break; + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector start1 = {0, 1}; + print_vec(Solution().executeInstructions(3, start1, "RRDDLU")); + + return 0; +} diff --git a/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/CMakeLists.txt b/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/CMakeLists.txt new file mode 100644 index 00000000..0ffd6e97 --- /dev/null +++ b/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/main.cpp b/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/main.cpp new file mode 100644 index 00000000..0362e442 --- /dev/null +++ b/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/intervals-between-identical-elements/ +/// Author : liuyubobobo +/// Time : 2021-12-25 + +#include +#include +#include + +using namespace std; + + +/// Using Map and Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector getDistances(vector& arr) { + + int n = arr.size(); + map> table; + for(int i = 0; i < n; i ++) + table[arr[i]].push_back(i); + + vector res(n, 0); + for(const pair>& p: table){ + const vector& v = p.second; + long long sum = 0ll; + for(int i = 1; i < v.size(); i ++) sum += v[i] - v[0]; + res[v[0]] = sum; + + long long x = v.size() - 1, y = 1; + for(int i = 1; i < v.size(); i ++){ + sum -= x * (v[i] - v[i - 1]); + sum += y * (v[i] - v[i - 1]); + res[v[i]] = sum; + x --; y ++; + } + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(long long e: v) cout << e << ' '; + cout << '\n'; +} + +int main() { + + vector arr1 = {2,1,3,1,2,3,3}; + print_vec(Solution().getDistances(arr1)); + // 4 2 7 2 4 4 5 + + vector arr2 = {10,5,10,10}; + print_vec(Solution().getDistances(arr2)); + // 5 0 3 4 + + return 0; +} diff --git a/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/main2.cpp b/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/main2.cpp new file mode 100644 index 00000000..8fe176a7 --- /dev/null +++ b/2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/main2.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/intervals-between-identical-elements/ +/// Author : liuyubobobo +/// Time : 2021-12-25 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Pre + Suf, more intuitive +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector getDistances(vector& arr) { + + int n = arr.size(); + map> table; + for(int i = 0; i < n; i ++) + table[arr[i]].push_back(i); + + vector res(n, 0); + for(const pair>& p: table){ + const vector& v = p.second; + + vector pre(v.size(), 0); + for(int i = 1; i < v.size(); i ++) pre[i] = pre[i - 1] + (long long)i * (v[i] - v[i - 1]); + + vector suf(v.size(), 0); + for(int i = (int)suf.size() - 2; i >= 0; i --) suf[i] = suf[i + 1] + ((long long)suf.size() - 1 - i) * (v[i + 1] - v[i]); + + for(int i = 0; i < v.size(); i ++) + res[v[i]] = pre[i] + suf[i]; + } + return res; + } +}; + + +void print_vec(const vector& v){ + for(long long e: v) cout << e << ' '; + cout << '\n'; +} + +int main() { + + vector arr1 = {2,1,3,1,2,3,3}; + print_vec(Solution().getDistances(arr1)); + // 4 2 7 2 4 4 5 + + vector arr2 = {10,5,10,10}; + print_vec(Solution().getDistances(arr2)); + // 5 0 3 4 + + return 0; +} diff --git a/2001-2500/2122-Recover-the-Original-Array/cpp-2122/CMakeLists.txt b/2001-2500/2122-Recover-the-Original-Array/cpp-2122/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/2001-2500/2122-Recover-the-Original-Array/cpp-2122/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) diff --git a/2001-2500/2122-Recover-the-Original-Array/cpp-2122/main.cpp b/2001-2500/2122-Recover-the-Original-Array/cpp-2122/main.cpp new file mode 100644 index 00000000..6ef61af4 --- /dev/null +++ b/2001-2500/2122-Recover-the-Original-Array/cpp-2122/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/recover-the-original-array/ +/// Author : liuyubobobo +/// Time : 2021-12-25 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Compelxity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + vector recoverArray(vector& nums) { + + int n2 = nums.size(), n = n2 / 2; + + sort(nums.begin(), nums.end()); + for(int i = 1; i < n2; i ++){ + vector res = solve(nums, i); + if(!res.empty()) return res; + } + return {}; + } + +private: + vector solve(const vector& nums, int right){ + + int r = nums[right] - nums[0]; + if(r == 0 || r % 2) return {}; + + int k = r / 2; + + multiset set; + for(int e: nums) set.insert(e); + vector res; + while(!set.empty()){ + int a = *set.begin(); + if(set.count(a + 2 * k)){ + res.push_back(a + k); + set.erase(set.begin()); + set.erase(set.find(a + 2 * k)); + } + else return {}; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/CMakeLists.txt b/2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/CMakeLists.txt new file mode 100644 index 00000000..10042729 --- /dev/null +++ b/2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2123) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2123 main.cpp) diff --git a/2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/main.cpp b/2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/main.cpp new file mode 100644 index 00000000..33b4fffc --- /dev/null +++ b/2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/main.cpp @@ -0,0 +1,153 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-remove-adjacent-ones-in-matrix/ +/// Author : liuyubobobo +/// Time : 2022-01-06 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Bipartite Graph +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Hungarian{ + +private: + int n, m; // m - row size, n = column size + vector> g; + vector matching; + + bool is_solved = false; + int max_matching_value; + vector> best_matching; + +public: + Hungarian(int n, int m) : n(n), m(m), g(n), is_solved(false), matching(m, -1){} + + void add_edge(int u, int v){ + assert(u >= 0 && u < n); + assert(v >= 0 && v < m); + g[u].push_back(v); + } + + int get_max_matching_value(){ + if(!is_solved) solve(); + return max_matching_value; + } + + const vector> get_matching(){ + + if(!is_solved) solve(); + return best_matching; + } + +private: + void solve(){ + + int res = 0; + for(int i = 0; i < n; i ++){ + vector visited(m, false); + if(dfs(i, visited)) res ++; + } + + max_matching_value = res; + + best_matching.clear(); + for(int i = 0; i < m; i ++) + if(matching[i] != -1) best_matching.push_back({matching[i], i}); + + is_solved = true; + } + + bool dfs(int u, vector& visited){ + + for(int v: g[u]) + if(!visited[v]){ + visited[v] = true; + if(matching[v] == -1 || dfs(matching[v], visited)){ + matching[v] = u; + return true; + } + } + return false; + } +}; + +class Solution { + +private: + int R, C; + const int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + +public: + int minimumOperations(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] == 1 && !visited[i][j]){ + + set> points; + dfs(grid, i, j, visited, points); + + map, int> odd, even; + for(const pair& p: points) + if((p.first + p.second) % 2) odd[p] = odd.size(); + else even[p] = even.size(); + + Hungarian solver(odd.size(), even.size()); + for(const pair, int>& p: odd){ + int x = p.first.first, y = p.first.second, index = p.second; + for(int d = 0; d < 4; d ++){ + if(even.count({x + dirs[d][0], y + dirs[d][1]})) + solver.add_edge(index, even[{x + dirs[d][0], y + dirs[d][1]}]); + } + } + res += solver.get_max_matching_value(); + } + return res; + } + +private: + void dfs(const vector>& grid, int x, int y, + vector>& visited, set>& points){ + + visited[x][y] = true; + points.insert({x, y}); + + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny) && grid[nx][ny] == 1 && !visited[nx][ny]) + dfs(grid, nx, ny, visited, points); + } + } + + bool in_area(int x, int y){ + return 0 <= x && x < R && 0 <= y && y < C; + } +}; + + +int main() { + + vector> grid1 = { + {1, 1, 0}, {0, 1, 1}, {1, 1, 1} + }; + cout << Solution().minimumOperations(grid1) << endl; + // 3 + + vector> grid2 = { + {0, 1, 0, 0, 1, 0}, {1, 1, 1, 1, 1, 1}, {0, 1, 0, 0, 1, 0} + }; + cout << Solution().minimumOperations(grid2) << endl; + // 3 + + return 0; +} diff --git a/2001-2500/2124-Check-if-All-As-Appears-Before-All-Bs/cpp-2124/CMakeLists.txt b/2001-2500/2124-Check-if-All-As-Appears-Before-All-Bs/cpp-2124/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2124-Check-if-All-As-Appears-Before-All-Bs/cpp-2124/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/2124-Check-if-All-As-Appears-Before-All-Bs/cpp-2124/main.cpp b/2001-2500/2124-Check-if-All-As-Appears-Before-All-Bs/cpp-2124/main.cpp new file mode 100644 index 00000000..70899c3c --- /dev/null +++ b/2001-2500/2124-Check-if-All-As-Appears-Before-All-Bs/cpp-2124/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/ +/// Author : liuyubobobo +/// Time : 2022-01-01 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + bool checkString(string s) { + + int lasta = -1, firstb = s.size(); + for(int i = 0; i < s.size(); i ++) + if(s[i] == 'a') lasta = max(lasta, i); + else firstb = min(firstb, i); + return lasta < firstb; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2125-Number-of-Laser-Beams-in-a-Bank/cpp-2125/CMakeLists.txt b/2001-2500/2125-Number-of-Laser-Beams-in-a-Bank/cpp-2125/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2125-Number-of-Laser-Beams-in-a-Bank/cpp-2125/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/2125-Number-of-Laser-Beams-in-a-Bank/cpp-2125/main.cpp b/2001-2500/2125-Number-of-Laser-Beams-in-a-Bank/cpp-2125/main.cpp new file mode 100644 index 00000000..dba38b34 --- /dev/null +++ b/2001-2500/2125-Number-of-Laser-Beams-in-a-Bank/cpp-2125/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/number-of-laser-beams-in-a-bank/ +/// Author : liuyubobobo +/// Time : 2022-01-01 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(R * C) +class Solution { +public: + int numberOfBeams(vector& bank) { + + vector data; + for(const string& s: bank){ + int num = 0; + for(char c: s) num += c == '1'; + + if(num) data.push_back(num); + } + + int res = 0; + for(int i = 1; i < data.size(); i ++) + res += data[i - 1] * data[i]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2126-Destroying-Asteroids/cpp-2126/CMakeLists.txt b/2001-2500/2126-Destroying-Asteroids/cpp-2126/CMakeLists.txt new file mode 100644 index 00000000..e85d4767 --- /dev/null +++ b/2001-2500/2126-Destroying-Asteroids/cpp-2126/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2126-Destroying-Asteroids/cpp-2126/main.cpp b/2001-2500/2126-Destroying-Asteroids/cpp-2126/main.cpp new file mode 100644 index 00000000..3e1afc2a --- /dev/null +++ b/2001-2500/2126-Destroying-Asteroids/cpp-2126/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/destroying-asteroids/ +/// Author : liuyubobobo +/// Time : 2022-01-01 + +#include +#include +#include + +using namespace std; + + +/// Using Multiset +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + bool asteroidsDestroyed(int mass, vector& asteroids) { + + multiset> data; + for(int e: asteroids) data.insert((long long)e); + + long long cur = mass; + while(!data.empty()){ + multiset>::iterator iter = data.lower_bound(cur); + if(iter == data.end()) return false; + + cur += *iter; + data.erase(iter); + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2126-Destroying-Asteroids/cpp-2126/main2.cpp b/2001-2500/2126-Destroying-Asteroids/cpp-2126/main2.cpp new file mode 100644 index 00000000..0af949f7 --- /dev/null +++ b/2001-2500/2126-Destroying-Asteroids/cpp-2126/main2.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/destroying-asteroids/ +/// Author : liuyubobobo +/// Time : 2022-01-01 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + bool asteroidsDestroyed(int mass, vector& asteroids) { + + sort(asteroids.begin(), asteroids.end()); + + long long cur = mass; + for(int e: asteroids) + if(cur >= e) cur += e; + else return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/CMakeLists.txt b/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/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/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 new file mode 100644 index 00000000..e0e8218b --- /dev/null +++ b/2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/main.cpp @@ -0,0 +1,114 @@ +/// Source : https://leetcode.com/problems/maximum-employees-to-be-invited-to-a-meeting/ +/// Author : liuyubobobo +/// Time : 2022-01-01 + +#include +#include + +using namespace std; + + +/// 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 { +public: + int maximumInvitations(vector& favorite) { + + int n = favorite.size(); + + vector in_degrees(n, 0); + for(int u = 0; u < n; u ++) + in_degrees[favorite[u]] ++; + + vector in_tree(n, false); + for(int u = 0; u < n; u ++) + if(!in_tree[u] && in_degrees[u] == 0){ + int cur = u; + while(in_degrees[cur] == 0){ + in_tree[cur] = true; + cur = favorite[cur]; + in_degrees[cur] --; + } + } + + vector> g(n); + for(int u = 0; u < n; u ++) + g[favorite[u]].push_back(u); + + vector max_d(n, 0); + vector visited(n, false); + int res = 0, max2 = 0; + for(int u = 0; u < n; u ++){ + if(in_tree[u]) continue; + + int s = u; + vector cycle_points; + while(!visited[s]){ + cycle_points.push_back(s); + visited[s] = true; + s = favorite[s]; + } + + int first = 0, second = 0; + for(int v: cycle_points){ + dfs(g, v, in_tree, visited, max_d); + if(max_d[v] - 1 > first) second = first, first = max_d[v] - 1; + else if(max_d[v] - 1 > second) second = max_d[v] - 1; + } + + if(cycle_points.size() == 2) max2 += 2 + first + second; + else res = max(res, (int)cycle_points.size()); + } + return max(res, max2); + } + +private: + void dfs(const vector>& g, int u, const vector& in_tree, + vector& visited, vector& max_d){ + + visited[u] = true; + int res = 1; + for(int v: g[u]) + if(!visited[v] && in_tree[v]){ + dfs(g, v, in_tree, visited, max_d); + res = max(res, 1 + max_d[v]); + } + max_d[u] = res; + } +}; + + +int main() { + + vector fav1 = {2, 2, 1, 2}; + cout << Solution().maximumInvitations(fav1) << endl; + // 3 + + vector fav2 = {1, 2, 0}; + cout << Solution().maximumInvitations(fav2) << endl; + // 3 + + vector fav3 = {3, 0, 1, 4, 1}; + cout << Solution().maximumInvitations(fav3) << endl; + // 4 + + vector fav4 = {1,2,3,4,5,6,3,8,9,10,11,8}; + cout << Solution().maximumInvitations(fav4) << endl; + // 4 + + vector fav5 = {1,0,0,2,1,4,7,8,9,6,7,10,8}; + cout << Solution().maximumInvitations(fav5) << endl; + // 6 + + vector fav6 = {1,0,3,2,5,6,7,4,9,8,11,10,11,12,10}; + cout << Solution().maximumInvitations(fav6) << endl; + // 11 + + vector fav7 = {6,10,10,0,6,0,4,4,1,3,3}; + cout << Solution().maximumInvitations(fav7) << endl; + // 8 + + return 0; +} diff --git a/2001-2500/2128-Remove-All-Ones-With-Row-and-Column-Flips/cpp-2128/CMakeLists.txt b/2001-2500/2128-Remove-All-Ones-With-Row-and-Column-Flips/cpp-2128/CMakeLists.txt new file mode 100644 index 00000000..118dc64c --- /dev/null +++ b/2001-2500/2128-Remove-All-Ones-With-Row-and-Column-Flips/cpp-2128/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2128) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2128 main.cpp) diff --git a/2001-2500/2128-Remove-All-Ones-With-Row-and-Column-Flips/cpp-2128/main.cpp b/2001-2500/2128-Remove-All-Ones-With-Row-and-Column-Flips/cpp-2128/main.cpp new file mode 100644 index 00000000..c4fb9090 --- /dev/null +++ b/2001-2500/2128-Remove-All-Ones-With-Row-and-Column-Flips/cpp-2128/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/remove-all-ones-with-row-and-column-flips/ +/// Author : liuyubobobo +/// Time : 2022-01-06 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(R * C) +/// Space Complexity: O(1) +class Solution { +public: + bool removeOnes(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + for(int j = 0; j < C; j ++) + if(grid[0][j]){ + for(int i = 0; i < R; i ++) grid[i][j] ^= 1; + } + + for(int i = 0; i < R; i ++) + if(!ok(grid[i])) return false; + return true; + } + +private: + bool ok(const vector& v){ + + for(int i = 1; i < v.size(); i ++) + if(v[i] != v[0]) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2129-Capitalize-the-Title/cpp-2129/CMakeLists.txt b/2001-2500/2129-Capitalize-the-Title/cpp-2129/CMakeLists.txt new file mode 100644 index 00000000..dcdb5d86 --- /dev/null +++ b/2001-2500/2129-Capitalize-the-Title/cpp-2129/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2129) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2129 main.cpp) diff --git a/2001-2500/2129-Capitalize-the-Title/cpp-2129/main.cpp b/2001-2500/2129-Capitalize-the-Title/cpp-2129/main.cpp new file mode 100644 index 00000000..96b21309 --- /dev/null +++ b/2001-2500/2129-Capitalize-the-Title/cpp-2129/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/capitalize-the-title/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string capitalizeTitle(string title) { + + int n = title.size(); + for(int start = 0, i = 1; i <= n; i ++) + if(i == n || title[i] == ' '){ + int len = i - start; + if(len <= 2){ + for(int j = start; j < i; j ++) title[j] = tolower(title[j]); + } + else{ + title[start] = toupper(title[start]); + for(int j = start + 1; j < i; j ++) title[j] = tolower(title[j]); + } + + start = i + 1; + i = start; + } + return title; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2130-Maximum-Twin-Sum-of-a-Linked-List/cpp-2130/CMakeLists.txt b/2001-2500/2130-Maximum-Twin-Sum-of-a-Linked-List/cpp-2130/CMakeLists.txt new file mode 100644 index 00000000..7083108a --- /dev/null +++ b/2001-2500/2130-Maximum-Twin-Sum-of-a-Linked-List/cpp-2130/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2130) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2130 main.cpp) diff --git a/2001-2500/2130-Maximum-Twin-Sum-of-a-Linked-List/cpp-2130/main.cpp b/2001-2500/2130-Maximum-Twin-Sum-of-a-Linked-List/cpp-2130/main.cpp new file mode 100644 index 00000000..0d6ee9e6 --- /dev/null +++ b/2001-2500/2130-Maximum-Twin-Sum-of-a-Linked-List/cpp-2130/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include + +using namespace std; + + +/// Using Array +/// Time Complexity: O(n) +/// Space Complexity: 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: + int pairSum(ListNode* head) { + + vector data; + for(ListNode* node = head; node; node = node->next) + data.push_back(node->val); + + int n = data.size(); + int res = 0; + for(int i = 0; i + i < n; i ++) + res = max(res, data[i] + data[n - i - 1]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2131-Longest-Palindrome-by-Concatenating-Two-Letter-Words/cpp-2131/CMakeLists.txt b/2001-2500/2131-Longest-Palindrome-by-Concatenating-Two-Letter-Words/cpp-2131/CMakeLists.txt new file mode 100644 index 00000000..82ba3543 --- /dev/null +++ b/2001-2500/2131-Longest-Palindrome-by-Concatenating-Two-Letter-Words/cpp-2131/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2131) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2131 main.cpp) diff --git a/2001-2500/2131-Longest-Palindrome-by-Concatenating-Two-Letter-Words/cpp-2131/main.cpp b/2001-2500/2131-Longest-Palindrome-by-Concatenating-Two-Letter-Words/cpp-2131/main.cpp new file mode 100644 index 00000000..5171eca3 --- /dev/null +++ b/2001-2500/2131-Longest-Palindrome-by-Concatenating-Two-Letter-Words/cpp-2131/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int longestPalindrome(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]) * 4; + + bool middle = false; + for(int i = 0; i < 26; i ++){ + res += f[i][i] / 2 * 2 * 2; + if(f[i][i] % 2) middle = true; + } + + if(middle) res += 2; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2132-Stamping-the-Grid/cpp-2132/CMakeLists.txt b/2001-2500/2132-Stamping-the-Grid/cpp-2132/CMakeLists.txt new file mode 100644 index 00000000..7efea53c --- /dev/null +++ b/2001-2500/2132-Stamping-the-Grid/cpp-2132/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2132) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2132 main.cpp) diff --git a/2001-2500/2132-Stamping-the-Grid/cpp-2132/main.cpp b/2001-2500/2132-Stamping-the-Grid/cpp-2132/main.cpp new file mode 100644 index 00000000..7557f2d1 --- /dev/null +++ b/2001-2500/2132-Stamping-the-Grid/cpp-2132/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/stamping-the-grid/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(R * C) +/// Space Complexity: O(R * C) +class Solution { +public: + bool possibleToStamp(vector>& grid, int H, int W) { + + int R = grid.size(), C = grid[0].size(); + vector> presum(R + 1, vector(C + 1, 0)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + presum[i + 1][j + 1] = presum[i + 1][j] + presum[i][j + 1] + grid[i][j] - presum[i][j]; + + vector> draw(R, vector(C, 0)); + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(!grid[i][j]){ + if(draw[i][j] == 0){ + if(i + H - 1 >= R || j + W - 1 >= C || + presum[i + H][j + W] - presum[i + H][j] - presum[i][j + W] + presum[i][j]) + return false; + + for(int ii = 0; ii < H && draw[i + ii][j] == 0; ii ++) + for(int jj = 0; jj < W && draw[i + ii][j + jj] == 0; jj ++) + draw[i + ii][j + jj] = 1; + } + else if(i + H - 1 < R && j + W - 1 < C && + presum[i + H][j + W] - presum[i + H][j] - presum[i][j + W] + presum[i][j] == 0){ + + for(int ii = H - 1; ii >= 0 && draw[i + ii][j + W - 1] == 0; ii --) + for(int jj = W - 1; jj >= 0 && draw[i + ii][j + jj] == 0; jj --) + draw[i + ii][j + jj] = 1; + } + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2133-Check-if-Every-Row-and-Column-Contains-All-Numbers/cpp-2133/CMakeLists.txt b/2001-2500/2133-Check-if-Every-Row-and-Column-Contains-All-Numbers/cpp-2133/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2133-Check-if-Every-Row-and-Column-Contains-All-Numbers/cpp-2133/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/2133-Check-if-Every-Row-and-Column-Contains-All-Numbers/cpp-2133/main.cpp b/2001-2500/2133-Check-if-Every-Row-and-Column-Contains-All-Numbers/cpp-2133/main.cpp new file mode 100644 index 00000000..3b1320be --- /dev/null +++ b/2001-2500/2133-Check-if-Every-Row-and-Column-Contains-All-Numbers/cpp-2133/main.cpp @@ -0,0 +1,43 @@ +/// Source : https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { +public: + bool checkValid(vector>& matrix) { + + int n = matrix.size(); + for(int i = 0; i < n; i ++){ + vector ok(n + 1, false); + for(int j = 0; j < n; j ++) + ok[matrix[i][j]] = true; + for(int k = 1; k <= n; k ++) + if(!ok[k]) return false; + } + + for(int j = 0; j < n; j ++){ + vector ok(n + 1, false); + for(int i = 0; i < n; i ++) + ok[matrix[i][j]] = true; + for(int k = 1; k <= n; k ++) + if(!ok[k]) return false; + } + + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2134-Minimum-Swaps-to-Group-All-1s-Together-II/cpp-2134/CMakeLists.txt b/2001-2500/2134-Minimum-Swaps-to-Group-All-1s-Together-II/cpp-2134/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2134-Minimum-Swaps-to-Group-All-1s-Together-II/cpp-2134/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/2134-Minimum-Swaps-to-Group-All-1s-Together-II/cpp-2134/main.cpp b/2001-2500/2134-Minimum-Swaps-to-Group-All-1s-Together-II/cpp-2134/main.cpp new file mode 100644 index 00000000..d3066e18 --- /dev/null +++ b/2001-2500/2134-Minimum-Swaps-to-Group-All-1s-Together-II/cpp-2134/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include +#include + +using namespace std; + + +/// Presum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minSwaps(vector& nums) { + + int n = nums.size(); + + vector A = nums; + for(int e: nums) A.push_back(e); + + vector presum(2 * n + 1, 0); + for(int i = 0; i < A.size(); i ++) + presum[i + 1] = presum[i] + A[i]; + + int k = accumulate(nums.begin(), nums.end(), 0); + int res = INT_MAX; + for(int i = 0; i < n; i ++) + res = min(res, k - (presum[i + k] - presum[i])); + return res; + } +}; + + +int main() { + + vector nums1 = {0, 1, 0, 1, 1, 0, 0}; + cout << Solution().minSwaps(nums1) << endl; + // 1 + + vector nums2 = {0,1,1,1,0,0,1,1,0}; + cout << Solution().minSwaps(nums2) << endl; + // 2 + + vector nums3 = {1,1,0,0,1}; + cout << Solution().minSwaps(nums3) << endl; + // 0 + + return 0; +} diff --git a/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/CMakeLists.txt b/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/CMakeLists.txt new file mode 100644 index 00000000..e85d4767 --- /dev/null +++ b/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/main.cpp b/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/main.cpp new file mode 100644 index 00000000..0dbea1cc --- /dev/null +++ b/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include +#include + +using namespace std; + + +/// Map +/// Time Complexity: O(|startWords| + |targetWords|) +/// Space Complexity: O(|targetWords|) +class Solution { +public: + int wordCount(vector& startWords, vector& targetWords) { + + unordered_map target_map; + for(string word: targetWords){ + sort(word.begin(), word.end()); + target_map[word] ++; + } + + int res = 0; + vector used(26); + for(const string& word: startWords){ + used.assign(26, false); + for(char c: word) used[c - 'a'] = true; + for(int i = 0; i < 26; i ++) + if(!used[i]){ + string new_word = word + (char)('a' + i); + sort(new_word.begin(), new_word.end()); + auto iter = target_map.find(new_word); + if(iter != target_map.end()){ + res += iter->second; + target_map.erase(iter); + } + } + } + return res; + } +}; + + +int main() { + + vector startWords1 = {"ant","act","tack"}; + vector targetWords1 = {"tack","act","acti"}; + cout << Solution().wordCount(startWords1, targetWords1) << endl; + // 2 + + vector startWords2 = {"ab","a"}; + vector targetWords2 = {"abc","abcd"}; + cout << Solution().wordCount(startWords2, targetWords2) << endl; + // 1 + + return 0; +} diff --git a/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/main2.cpp b/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/main2.cpp new file mode 100644 index 00000000..e21e6b3b --- /dev/null +++ b/2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/main2.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include +#include + +using namespace std; + + +/// Map, Using Bitwise +/// Time Complexity: O(|startWords| + |targetWords|) +/// Space Complexity: O(|targetWords|) +class Solution { +public: + int wordCount(vector& startWords, vector& targetWords) { + + unordered_map target_map; + for(string word: targetWords){ + int state = 0; + for(char c: word) state |= (1 << (c - 'a')); + target_map[state] ++; + } + + int res = 0; + for(const string& word: startWords){ + int state = 0; + for(char c: word) state |= (1 << (c - 'a')); + for(int i = 0; i < 26; i ++) + if((state & (1 << i)) == 0){ + auto iter = target_map.find(state | (1 << i)); + if(iter != target_map.end()){ + res += iter->second; + target_map.erase(iter); + } + } + } + return res; + } +}; + + +int main() { + + vector startWords1 = {"ant","act","tack"}; + vector targetWords1 = {"tack","act","acti"}; + cout << Solution().wordCount(startWords1, targetWords1) << endl; + // 2 + + vector startWords2 = {"ab","a"}; + vector targetWords2 = {"abc","abcd"}; + cout << Solution().wordCount(startWords2, targetWords2) << endl; + // 1 + + return 0; +} diff --git a/2001-2500/2136-Earliest-Possible-Day-of-Full-Bloom/cpp-2136/CMakeLists.txt b/2001-2500/2136-Earliest-Possible-Day-of-Full-Bloom/cpp-2136/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2136-Earliest-Possible-Day-of-Full-Bloom/cpp-2136/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/2136-Earliest-Possible-Day-of-Full-Bloom/cpp-2136/main.cpp b/2001-2500/2136-Earliest-Possible-Day-of-Full-Bloom/cpp-2136/main.cpp new file mode 100644 index 00000000..52397dea --- /dev/null +++ b/2001-2500/2136-Earliest-Possible-Day-of-Full-Bloom/cpp-2136/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/earliest-possible-day-of-full-bloom/ +/// Author : liuyubobobo +/// Time : 2022-01-08 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int earliestFullBloom(vector& plantTime, vector& growTime) { + + int n = plantTime.size(); + + vector> data(n); + for(int i = 0; i < n; i ++) + data[i].first = plantTime[i], data[i].second = growTime[i]; + + sort(data.begin(), data.end(), + [](const pair& p1, const pair& p2){ + if(p1.second != p2.second) return p1.second > p2.second; + return p1.first + p1.second > p2.first + p2.second; + }); + + int res = 0, cur = 0; + for(const pair& p: data){ + res = max(res, cur + p.first + p.second); + cur += p.first; + } + return res; + } +}; + + +int main() { + + vector plantTime1 = {1, 4, 3}, growTime1 = {2, 3 , 1}; + cout << Solution().earliestFullBloom(plantTime1, growTime1) << endl; + // 9 + + vector plantTime2 = {1, 2, 3, 2}, growTime2 = {2, 1, 2, 1}; + cout << Solution().earliestFullBloom(plantTime2, growTime2) << endl; + // 9 + + vector plantTime3 = {1}, growTime3 = {1}; + cout << Solution().earliestFullBloom(plantTime3, growTime3) << endl; + // 2 + + return 0; +} diff --git a/2001-2500/2137-Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal/cpp-2137/CMakeLists.txt b/2001-2500/2137-Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal/cpp-2137/CMakeLists.txt new file mode 100644 index 00000000..8b51d62f --- /dev/null +++ b/2001-2500/2137-Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal/cpp-2137/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2137) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2137 main.cpp) diff --git a/2001-2500/2137-Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal/cpp-2137/main.cpp b/2001-2500/2137-Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal/cpp-2137/main.cpp new file mode 100644 index 00000000..56068513 --- /dev/null +++ b/2001-2500/2137-Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal/cpp-2137/main.cpp @@ -0,0 +1,85 @@ +/// Source : https://leetcode.com/problems/pour-water-between-buckets-to-make-water-levels-equal/ +/// Author : liuyubobobo +/// Time : 2022-01-13 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlog(MAX_RANGE)) +/// Space Complexity: O(n) +class Solution { + +private: + const double eps = 1e-6; + +public: + double equalizeWater(vector& buckets, int loss) { + + int n = buckets.size(); + + vector B(n); + for(int i = 0; i < n; i ++) B[i] = buckets[i]; + sort(B.begin(), B.end()); + + double p = 1.0 - (double)loss / 100.0; + + double l = B[0], r = accumulate(B.begin(), B.end(), 0.0) / n; + while(!eq(l, r)){ + double m = (l + r) / 2; + if(ok(n, B, m, p)) l = m; + else r = m; + } + return l; + } + +private: + bool ok(int n, vector B, double t, double p){ + + for(int i = 0, j = 0; i < n;) + if(B[i] < t){ + while(j < n && B[j] <= t) j ++; + if(j == n) return false; + + double need = t - B[i]; + double can_pour = (B[j] - t) * p; + if(need >= can_pour){ + B[i] += can_pour; + B[j] = t; + if(B[i] >= t) i ++; + } + else{ + B[i] = t; + B[j] -= need / p; + i ++; + } + } + else i ++; + return true; + } + + bool eq(double a, double b){ + return abs(a - b) <= eps; + } +}; + +int main() { + + vector bucket1 = {1, 2, 7}; + cout << Solution().equalizeWater(bucket1, 80) << endl; + // 2.0 + + vector bucket2 = {2, 4, 6}; + cout << Solution().equalizeWater(bucket2, 50) << endl; + // 3.5 + + vector bucket3 = {3, 3, 3, 3}; + cout << Solution().equalizeWater(bucket3, 40) << endl; + // 3 + + return 0; +} diff --git a/2001-2500/2138-Divide-a-String-Into-Groups-of-Size-k/cpp-2138/CMakeLists.txt b/2001-2500/2138-Divide-a-String-Into-Groups-of-Size-k/cpp-2138/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2138-Divide-a-String-Into-Groups-of-Size-k/cpp-2138/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/2138-Divide-a-String-Into-Groups-of-Size-k/cpp-2138/main.cpp b/2001-2500/2138-Divide-a-String-Into-Groups-of-Size-k/cpp-2138/main.cpp new file mode 100644 index 00000000..85721d86 --- /dev/null +++ b/2001-2500/2138-Divide-a-String-Into-Groups-of-Size-k/cpp-2138/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/ +/// Author : liuyubobobo +/// Time : 2022-01-15 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector divideString(string s, int k, char fill) { + + vector res; + for(int i = 0; i < s.size(); i += k) + res.push_back(s.substr(i, k)); + while(res.back().size() != k) res.back() += fill; + return res; + } +}; + + +int main() { + + Solution().divideString("abcdefghi", 3, 'x'); + + return 0; +} diff --git a/2001-2500/2139-Minimum-Moves-to-Reach-Target-Score/cpp-2139/CMakeLists.txt b/2001-2500/2139-Minimum-Moves-to-Reach-Target-Score/cpp-2139/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2139-Minimum-Moves-to-Reach-Target-Score/cpp-2139/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/2139-Minimum-Moves-to-Reach-Target-Score/cpp-2139/main.cpp b/2001-2500/2139-Minimum-Moves-to-Reach-Target-Score/cpp-2139/main.cpp new file mode 100644 index 00000000..5e4941c6 --- /dev/null +++ b/2001-2500/2139-Minimum-Moves-to-Reach-Target-Score/cpp-2139/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/minimum-moves-to-reach-target-score/ +/// Author : liuyubobobo +/// Time : 2022-01-15 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(min(log(taarget), maxDoubles)) +/// Space Complexity: O(1) +class Solution { +public: + int minMoves(int target, int maxDoubles) { + + if(maxDoubles == 0) return target - 1; + + int res = 0, cur = target; + while(maxDoubles && cur != 1){ + if(cur & 1) cur --, res ++; + else cur >>= 1, res ++, maxDoubles --; + } + res += (cur - 1); + return res; + } +}; + + +int main() { + + cout << Solution().minMoves(5, 0) << endl; + // 4 + + cout << Solution().minMoves(19, 2) << endl; + // 7 + + cout << Solution().minMoves(10, 4) << endl; + // 4 + + cout << Solution().minMoves(4, 4) << endl; + // 2 + + return 0; +} diff --git a/2001-2500/2140-Solving-Questions-With-Brainpower/cpp-2140/CMakeLists.txt b/2001-2500/2140-Solving-Questions-With-Brainpower/cpp-2140/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2140-Solving-Questions-With-Brainpower/cpp-2140/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/2140-Solving-Questions-With-Brainpower/cpp-2140/main.cpp b/2001-2500/2140-Solving-Questions-With-Brainpower/cpp-2140/main.cpp new file mode 100644 index 00000000..29a8b0a7 --- /dev/null +++ b/2001-2500/2140-Solving-Questions-With-Brainpower/cpp-2140/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/solving-questions-with-brainpower/ +/// Author : liuyubobobo +/// Time : 2022-01-15 + +#include +#include + +using namespace std; + + +/// DP +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { + +public: + long long mostPoints(vector>& questions) { + + int n = questions.size(); + vector dp(n, -1); + return dfs(n, questions, 0, dp); + } + +private: + long long dfs(int n, const vector>& questions, int index, + vector& dp){ + + if(index >= n) return 0; + if(dp[index] != -1) return dp[index]; + + long long res = dfs(n, questions, index + 1, dp); + res = max(res, (long long)questions[index][0] + dfs(n, questions, index + questions[index][1] + 1, dp)); + return dp[index] = res; + } +}; + + +int main() { + + vector> q1 = {{3,2},{4,3},{4,4},{2,5}}; + cout << Solution().mostPoints(q1) << endl; + // 5 + + vector> q2 = {{1,1},{2,2},{3, 3}, {4,4},{5,5}}; + cout << Solution().mostPoints(q2) << endl; + // 7 + + return 0; +} diff --git a/2001-2500/2141-Maximum-Running-Time-of-N-Computers/cpp-2141/CMakeLists.txt b/2001-2500/2141-Maximum-Running-Time-of-N-Computers/cpp-2141/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2141-Maximum-Running-Time-of-N-Computers/cpp-2141/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/2141-Maximum-Running-Time-of-N-Computers/cpp-2141/main.cpp b/2001-2500/2141-Maximum-Running-Time-of-N-Computers/cpp-2141/main.cpp new file mode 100644 index 00000000..df86d654 --- /dev/null +++ b/2001-2500/2141-Maximum-Running-Time-of-N-Computers/cpp-2141/main.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/maximum-running-time-of-n-computers/ +/// Author : liuyubobobo +/// Time : 2022-01-15 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(nlogn + nlog(sum/n)) +/// Space Complexity: O(1) +class Solution { + +public: + long long maxRunTime(int n, vector& batteries) { + + long long l = 1, r = accumulate(batteries.begin(), batteries.end(), 0ll) / n; + sort(batteries.begin(), batteries.end(), greater()); + while(l < r){ + long long mid = (l + r + 1) / 2; + if(ok(n, batteries, mid)) l = mid; + else r = mid - 1; + } + return l; + } + +private: + bool ok(int n, const vector& v, long long t){ + + int cnt = 0, i; + for(i = 0; i < n; i ++){ + if(v[i] >= t) cnt ++; + else break; + } + + long long cur = 0; + for(; i < v.size(); i ++){ + cur += v[i]; + if(cur >= t) cnt ++, cur -= t; + } + return cnt >= n; + } +}; + + +int main() { + + vector battery1 = {3, 3, 3}; + cout << Solution().maxRunTime(2, battery1) << endl; + // 4 + + vector battery2 = {1, 1, 1, 1}; + cout << Solution().maxRunTime(2, battery2) << endl; + // 2 + + vector battery3 = {6251,4133,9898,1536,5475,6340,7430,4413,2558,3941,6934,7326,7948,7736,1408,5752,836,4511,7684,3939,1376,2305,2090,8419,3813,4430,890,4120,3415,9706,879,158,2239,5462,5773,5285,5540,305,2211,691,4335,5912,3542,5229,996,2609,2173,87,5683,2946,1456,9590,3625,1807,6909,1328,1548,8182,1690,7440,8310,8502,320,2654,2254,1306,7696,7187,3977,3270,2989,1629,5725,7937,5317,9763,9238,3712,1925,2,1463,6309,4997,7786,1811,7579,3290,8355,63,5010,3574,5364,748,1877,106,1735,7809,5060,9677,4831,1524,9663,6557,9399,5976,801,8800,4297,9636,4828,3972,6946,6170,9984,5710,3318,4156,7838,6856,2866,5900,4623,5228,8063,2514,9149,3509,4033,854,2884,7160,8195,1936,8134,4277,9442,5263,555,5515,2341,2820,3095,2974,7648,9116,6886,8545,4055,2398,2425,4861,2114,9280,5045,3678,6569,7948,1912,7856,6831,4286,1645,9654,5552,880,1864,6386,6616,3961,7427,8649,2323,8084,7334,8256,6187,2244,9738,6869,5888,3862,886,1419,2067,15,428,4732,2098,7710,5586,1972,5388,5171,9239,4810,2461,3639,8120,5296,9522,3475,5703,2358,1957,9823,6561,3690,1856,8202,1343,1868,5851,3177,7036,4901,4891,4640,5927,7610,9204,3538,7508,411,3814,7545,7094,6841,3125,2884,2205,8271,9920,2322,20,8850,1747,3317,2873,558,7201,6688,9097,9399,2096,5846,9502,5028,2907,1667,4049,5810,5850,4628,4511,2415,1194,6477,9198,6645,1281,3920,7117,3301,1892,4402,8180,2915,3548,211,5855,2190,6552,5992,7280,1291,4064,6139,9585,6729,3481,5274,2498,9484,3486,863,1913,2921,4733,544,62,5139,4408,6174,9175,1119,7858,6215,2901,4862,4020,7371,2778,3086,6559,9187,7591,6007,8762,3577,8984,6653,4975,3939,1385,4076,6350,3248,3102,8331,194,9552,409,1527,6387,8477,5410,3223,1168,6578,4004,3442,1767,5979,8644,4893,8495,5152,5450,2221,7171,8308,6288,8536,5671,6642,6999,4134,5572,3639,884,9915,7104,8288,994,9765,9467,627,2549,2237,713,9487,9727,569,5755,2975,5248,894,3083,7820,7696,4780,952,9787,6791,1939,682,2917,6001,1781,2712,5098,2002,5641,9202,7651,4817,7685,3900,8720,7808,8628,2196,4155,3357,7890,1361,7537,6157,7107,2972,5548,2297,9888,6257,2596,9319,850,2181,2284,6877,9344,1177,2238,5140,6476,6289,2673,3266,5291,3098,541,81,8251,8235,8118,8717,2289,4159,2926,478,9137,3921,2591,669,8164,6245,5742,6076,3839,4243,4656,2448,1168,4481,3734,2909,4499,1479,4204,3693,5440,2921,4044,9615,7430,2716,7520,3354,3683,6058,4212,4940,5645,5101,9339,2422,3201,813,2753,8796,5485,1080,4433,3875,831,33,1689,6643,5753,7352,1670,2525,3606,34,5768,5256,531,427,4931,8174,8413,6014,3464,3793,6932,3943,7916,3954,1452,4165,4047,2844,9685,6882,9535,4995,7836,5296,1140,2403,7723,4388,4791,4967,3788,3694,9258,370,7174,8340,9182,7522,7852,242,1429,5904,7794,7736,2421,3885,819,3136,8815,5737,2043,6398,9528,9011,5056,7761,6120,9561,6053,8264,1648,4598,3448,2563,4717,9078,5032,1163,7788,2227,4478,4172}; + cout << Solution().maxRunTime(13, battery3) << endl; + // 207113 + + vector battery4 = {10, 10, 3, 5}; + cout << Solution().maxRunTime(3, battery4) << endl; + // 8 + + return 0; +} diff --git a/2001-2500/2143-Choose-Numbers-From-Two-Arrays-in-Range/cpp-2143/CMakeLists.txt b/2001-2500/2143-Choose-Numbers-From-Two-Arrays-in-Range/cpp-2143/CMakeLists.txt new file mode 100644 index 00000000..96a735a2 --- /dev/null +++ b/2001-2500/2143-Choose-Numbers-From-Two-Arrays-in-Range/cpp-2143/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2143) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2143 main.cpp) diff --git a/2001-2500/2143-Choose-Numbers-From-Two-Arrays-in-Range/cpp-2143/main.cpp b/2001-2500/2143-Choose-Numbers-From-Two-Arrays-in-Range/cpp-2143/main.cpp new file mode 100644 index 00000000..92bd1202 --- /dev/null +++ b/2001-2500/2143-Choose-Numbers-From-Two-Arrays-in-Range/cpp-2143/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/choose-numbers-from-two-arrays-in-range/ +/// Author : liuyubobobo +/// Time : 2022-01-28 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n * (sum1 + sum2)) +/// Space Complexity: O(n * (sum1 + sum2)) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int countSubranges(vector& nums1, vector& nums2) { + + int n = nums1.size(); + int sum1 = accumulate(nums1.begin(), nums1.end(), 0); + int sum2 = accumulate(nums2.begin(), nums2.end(), 0); + vector> dp(n, vector(sum1 + sum2 + 1, -1)); // index, sum -> cnt + + int res = 0; + for(int i = 0; i < n; i ++) + res += dfs(n, nums1, nums2, i, 0, dp, sum2), res %= MOD; + return res; + } + +private: + int dfs(int n, const vector& nums1, const vector& nums2, int index, int target, + vector>& dp, int offset){ + + if(index == n) return 0; + if(dp[index][target + offset] != -1) return dp[index][target + offset]; + + int res = 0; + if(nums1[index] == target) res ++; + if(nums2[index] == -target) res ++; + res += dfs(n, nums1, nums2, index + 1, target - nums1[index], dp, offset); + res += dfs(n, nums1, nums2, index + 1, target + nums2[index], dp, offset); + return dp[index][target + offset] = res % MOD; + } +}; + + +int main() { + + vector nums11 = {1, 2, 5}, nums12 = {2, 6, 3}; + cout << Solution().countSubranges(nums11, nums12) << endl; + // 3 + + vector nums21 = {0, 1}, nums22 = {1, 0}; + cout << Solution().countSubranges(nums21, nums22) << endl; + // 4 + + return 0; +} diff --git a/2001-2500/2144-Minimum-Cost-of-Buying-Candies-With-Discount/cpp-2144/CMakeLists.txt b/2001-2500/2144-Minimum-Cost-of-Buying-Candies-With-Discount/cpp-2144/CMakeLists.txt new file mode 100644 index 00000000..85873c27 --- /dev/null +++ b/2001-2500/2144-Minimum-Cost-of-Buying-Candies-With-Discount/cpp-2144/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2144) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2144 main.cpp) diff --git a/2001-2500/2144-Minimum-Cost-of-Buying-Candies-With-Discount/cpp-2144/main.cpp b/2001-2500/2144-Minimum-Cost-of-Buying-Candies-With-Discount/cpp-2144/main.cpp new file mode 100644 index 00000000..028a8d91 --- /dev/null +++ b/2001-2500/2144-Minimum-Cost-of-Buying-Candies-With-Discount/cpp-2144/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int minimumCost(vector& cost) { + + sort(cost.begin(), cost.end(), greater()); + + int res = 0; + for(int i = 0; i < cost.size(); i += 3){ + res += cost[i]; + if(i + 1 < cost.size()) res += cost[i + 1]; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2145-Count-the-Hidden-Sequences/cpp-2145/CMakeLists.txt b/2001-2500/2145-Count-the-Hidden-Sequences/cpp-2145/CMakeLists.txt new file mode 100644 index 00000000..c147d6af --- /dev/null +++ b/2001-2500/2145-Count-the-Hidden-Sequences/cpp-2145/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2145) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2145 main.cpp) diff --git a/2001-2500/2145-Count-the-Hidden-Sequences/cpp-2145/main.cpp b/2001-2500/2145-Count-the-Hidden-Sequences/cpp-2145/main.cpp new file mode 100644 index 00000000..e5f73012 --- /dev/null +++ b/2001-2500/2145-Count-the-Hidden-Sequences/cpp-2145/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/count-the-hidden-sequences/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int numberOfArrays(vector& differences, int lower, int upper) { + + int n = differences.size(); + + long long minv = differences[0], maxv = differences[0]; + long long cur = differences[0]; + for(int i = 1; i < n; i ++) + cur += differences[i], minv = min(minv, cur), maxv = max(maxv, cur); + + int l = max(lower, (int)(lower - minv)), r = min(upper, (int)(upper - maxv)); + return max(0, r - l + 1); + } +}; + + +int main() { + + vector diff1 = {1, -3, 4}; + cout << Solution().numberOfArrays(diff1, 1, 6) << endl; + // 2 + + vector diff2 = {3, -4, 5, 1, -2}; + cout << Solution().numberOfArrays(diff2, -4, 5) << endl; + // 4 + + vector diff3 = {4, -7, 2}; + cout << Solution().numberOfArrays(diff3, 3, 6) << endl; + // 0 + + vector diff4 = {-40}; + cout << Solution().numberOfArrays(diff4, -46, 53) << endl; + // 60 + + return 0; +} diff --git a/2001-2500/2146-K-Highest-Ranked-Items-Within-a-Price-Range/cpp-2146/CMakeLists.txt b/2001-2500/2146-K-Highest-Ranked-Items-Within-a-Price-Range/cpp-2146/CMakeLists.txt new file mode 100644 index 00000000..a14b3c7f --- /dev/null +++ b/2001-2500/2146-K-Highest-Ranked-Items-Within-a-Price-Range/cpp-2146/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2146) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2146 main.cpp) diff --git a/2001-2500/2146-K-Highest-Ranked-Items-Within-a-Price-Range/cpp-2146/main.cpp b/2001-2500/2146-K-Highest-Ranked-Items-Within-a-Price-Range/cpp-2146/main.cpp new file mode 100644 index 00000000..489ce307 --- /dev/null +++ b/2001-2500/2146-K-Highest-Ranked-Items-Within-a-Price-Range/cpp-2146/main.cpp @@ -0,0 +1,64 @@ +/// Source : https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include +#include + +using namespace std; + + +/// BFS +/// Time Complexity: O(R * C + klogk) +/// Space Complexity: O(R * C + k) +class Solution { + +private: + const int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + int R, C; + +public: + vector> highestRankedKItems(vector>& grid, vector& pricing, vector& start, int k) { + + R = grid.size(), C = grid[0].size(); + vector> dis(R, vector(C, -1)); + + queue> q; + q.push({start[0], start[1]}); + dis[start[0]][start[1]] = 0; + vector, pair>> items; // dis, price, r, c + while(!q.empty()){ + int x = q.front().first, y = q.front().second; + q.pop(); + + if(grid[x][y] > 1 && pricing[0] <= grid[x][y] && grid[x][y] <= pricing[1]) + items.push_back({{dis[x][y], grid[x][y]}, {x, y}}); + + for(int d = 0; d < 4; d ++){ + int nx = x + dirs[d][0], ny = y + dirs[d][1]; + if(in_area(nx, ny) && grid[nx][ny] && dis[nx][ny] == -1){ + q.push({nx, ny}); + dis[nx][ny] = dis[x][y] + 1; + } + } + } + + sort(items.begin(), items.end()); + vector> res; + for(int i = 0; i < items.size() && i < k; i ++) + res.push_back({items[i].second.first, items[i].second.second}); + 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/2147-Number-of-Ways-to-Divide-a-Long-Corridor/cpp-2147/CMakeLists.txt b/2001-2500/2147-Number-of-Ways-to-Divide-a-Long-Corridor/cpp-2147/CMakeLists.txt new file mode 100644 index 00000000..b1fbb527 --- /dev/null +++ b/2001-2500/2147-Number-of-Ways-to-Divide-a-Long-Corridor/cpp-2147/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2147) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2147 main.cpp) diff --git a/2001-2500/2147-Number-of-Ways-to-Divide-a-Long-Corridor/cpp-2147/main.cpp b/2001-2500/2147-Number-of-Ways-to-Divide-a-Long-Corridor/cpp-2147/main.cpp new file mode 100644 index 00000000..6825bc27 --- /dev/null +++ b/2001-2500/2147-Number-of-Ways-to-Divide-a-Long-Corridor/cpp-2147/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include + +using namespace std; + + +/// Mathematics +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int numberOfWays(string corridor) { + + int n = corridor.size(); + vector total_seats(n); + total_seats[0] = corridor[0] == 'S'; + for(int i = 1; i < n; i ++) + total_seats[i] = total_seats[i - 1] + (corridor[i] == 'S'); + + if(total_seats.back() & 1) return 0; + if(total_seats.back() <= 1) return 0; +// if(total_seats.back() == 2) return 1; + + long long res = 1; + int cur = 2; + while(cur != total_seats.back()){ + int l = lower_bound(total_seats.begin(), total_seats.end(), cur) - total_seats.begin(); + int r = lower_bound(total_seats.begin(), total_seats.end(), cur + 1) - total_seats.begin(); + res *= (r - l); + res %= MOD; + + cur += 2; + } + return res; + } +}; + + +int main() { + + string corridor1 = "SSPPSPS"; + cout << Solution().numberOfWays(corridor1) << endl; + // 3 + + string corridor2 = "PPSPSP"; + cout << Solution().numberOfWays(corridor2) << endl; + // 1 + + string corridor3 = "S"; + cout << Solution().numberOfWays(corridor3) << endl; + // 0 + + return 0; +} diff --git a/2001-2500/2148-Count-Elements-With-Strictly-Smaller-and-Greater-Elements/cpp-2148/CMakeLists.txt b/2001-2500/2148-Count-Elements-With-Strictly-Smaller-and-Greater-Elements/cpp-2148/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2148-Count-Elements-With-Strictly-Smaller-and-Greater-Elements/cpp-2148/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/2148-Count-Elements-With-Strictly-Smaller-and-Greater-Elements/cpp-2148/main.cpp b/2001-2500/2148-Count-Elements-With-Strictly-Smaller-and-Greater-Elements/cpp-2148/main.cpp new file mode 100644 index 00000000..90840bd5 --- /dev/null +++ b/2001-2500/2148-Count-Elements-With-Strictly-Smaller-and-Greater-Elements/cpp-2148/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int countElements(vector& nums) { + + map f; + for(int e: nums) f[e] ++; + + int res = 0; + for(const pair& p: f) + if(p.first != f.begin()->first && p.first != f.rbegin()->first) + res += p.second; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2149-Rearrange-Array-Elements-by-Sign/cpp-2149/CMakeLists.txt b/2001-2500/2149-Rearrange-Array-Elements-by-Sign/cpp-2149/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2149-Rearrange-Array-Elements-by-Sign/cpp-2149/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/2149-Rearrange-Array-Elements-by-Sign/cpp-2149/main.cpp b/2001-2500/2149-Rearrange-Array-Elements-by-Sign/cpp-2149/main.cpp new file mode 100644 index 00000000..e6eed84b --- /dev/null +++ b/2001-2500/2149-Rearrange-Array-Elements-by-Sign/cpp-2149/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/rearrange-array-elements-by-sign/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector rearrangeArray(vector& nums) { + + int n = nums.size(); + vector res(n); + for(int i = 0, posi = 0, negi = 1; i < n; i ++) + if(nums[i] > 0) + res[posi] = nums[i], posi += 2; + else + res[negi] = nums[i], negi += 2; + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2150-Find-All-Lonely-Numbers-in-the-Array/cpp-2150/CMakeLists.txt b/2001-2500/2150-Find-All-Lonely-Numbers-in-the-Array/cpp-2150/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2150-Find-All-Lonely-Numbers-in-the-Array/cpp-2150/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/2150-Find-All-Lonely-Numbers-in-the-Array/cpp-2150/main.cpp b/2001-2500/2150-Find-All-Lonely-Numbers-in-the-Array/cpp-2150/main.cpp new file mode 100644 index 00000000..96cc99dc --- /dev/null +++ b/2001-2500/2150-Find-All-Lonely-Numbers-in-the-Array/cpp-2150/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector findLonely(vector& nums) { + + map f; + for(int e: nums) f[e] ++; + + vector res; + for(const pair& p: f) + if(p.second == 1 && !f.count(p.first - 1) && !f.count(p.first + 1)) + res.push_back(p.first); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/CMakeLists.txt b/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/CMakeLists.txt new file mode 100644 index 00000000..2552f642 --- /dev/null +++ b/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/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/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/main.cpp b/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/main.cpp new file mode 100644 index 00000000..c9410a8c --- /dev/null +++ b/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/maximum-good-people-based-on-statements/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(2^n * (n^2)) +/// Space Complexity: O(2^n) +class Solution { +public: + int maximumGood(vector>& statements) { + + int n = statements.size(); + + vector> states(n + 1); + for(int state = 0; state < (1 << n); state ++) + states[__builtin_popcount(state)].push_back(state); + + for(int good_num = n; good_num > 0; good_num --) + for(int state: states[good_num]) + if(ok(n, state, statements)) return good_num; + return 0; + } + +private: + bool ok(int n, int state, const vector>& say){ + + vector good(n, false); + for(int i = 0; i < n; i ++) + good[i] = !!(state & (1 << i)); + + for(int i = 0; i < n; i ++){ + if(!good[i]) continue; + + for(int j = 0; j < n; j ++){ + if(say[i][j] == 2) continue; + + if(say[i][j] == 0 && good[j]) return false; + if(say[i][j] == 1 && !good[j]) return false; + } + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/main2.cpp b/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/main2.cpp new file mode 100644 index 00000000..1639e0ab --- /dev/null +++ b/2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/main2.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/maximum-good-people-based-on-statements/ +/// Author : liuyubobobo +/// Time : 2022-01-22 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(2^n * (n^2)) +/// Space Complexity: O(1) +class Solution { +public: + int maximumGood(vector>& statements) { + + int n = statements.size(); + + vector> states(n + 1); + int res = 0; + for(int state = 0; state < (1 << n); state ++){ + int bitcnt = __builtin_popcount(state); + if(bitcnt <= res) continue; + if(ok(n, state, statements)) res = bitcnt; + } + return res; + } + +private: + bool ok(int n, int state, const vector>& say){ + + vector good(n, false); + for(int i = 0; i < n; i ++) + good[i] = !!(state & (1 << i)); + + for(int i = 0; i < n; i ++){ + if(!good[i]) continue; + + for(int j = 0; j < n; j ++){ + if(say[i][j] == 2) continue; + + if(say[i][j] == 0 && good[j]) return false; + if(say[i][j] == 1 && !good[j]) return false; + } + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2152-Minimum-Number-of-Lines-to-Cover-Points/cpp-2152/CMakeLists.txt b/2001-2500/2152-Minimum-Number-of-Lines-to-Cover-Points/cpp-2152/CMakeLists.txt new file mode 100644 index 00000000..bb65fecd --- /dev/null +++ b/2001-2500/2152-Minimum-Number-of-Lines-to-Cover-Points/cpp-2152/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2152) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2152 main.cpp) diff --git a/2001-2500/2152-Minimum-Number-of-Lines-to-Cover-Points/cpp-2152/main.cpp b/2001-2500/2152-Minimum-Number-of-Lines-to-Cover-Points/cpp-2152/main.cpp new file mode 100644 index 00000000..a0563968 --- /dev/null +++ b/2001-2500/2152-Minimum-Number-of-Lines-to-Cover-Points/cpp-2152/main.cpp @@ -0,0 +1,70 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-lines-to-cover-points/ +/// Author : liuyubobobo +/// Time : 2022-01-27 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(2^n * n^2) +/// Space Complexity: O(2^n) +class Solution { + +public: + int minimumLines(vector>& points) { + + int n = points.size(); + vector dp(1 << n, -1); + return dfs(n, points, 0, dp); + } + +private: + int dfs(int n, const vector>& points, int state, vector& dp){ + + int used_total = __builtin_popcount(state); + if(n - used_total == 0) return 0; + if(n - used_total <= 2) return 1; + if(dp[state] != -1) return dp[state]; + + int res = INT_MAX; + for(int i = 0; i < n; i ++){ + if(state & (1 << i)) continue; + for(int j = i + 1; j < n; j ++){ + if(state & (1 << j)) continue; + + vector in_line_index = {i, j}; + for(int k = j + 1; k < n; k ++){ + if(state & (1 << k)) continue; + if(in_line(points[i][0], points[i][1], points[j][0], points[j][1], points[k][0], points[k][1])) + in_line_index.push_back(k); + } + + for(int index: in_line_index) state |= (1 << index); + res = min(res, 1 + dfs(n, points, state, dp)); + for(int index: in_line_index) state -= (1 << index); + } + } + return dp[state] = res; + } + + bool in_line(int x1, int y1, int x2, int y2, int x, int y){ + return (y - y1) * (x - x2) == (y - y2) * (x - x1); + } +}; + + +int main() { + + vector> points1 = { + {4,-8},{-7,5},{9,7},{-10,10},{6,8}, + {-3,-6},{-1,3},{-10,9},{1,-3},{8,3} + }; + cout << Solution().minimumLines(points1) << endl; + // 5 + + return 0; +} diff --git a/2001-2500/2154-Keep-Multiplying-Found-Values-by-Two/cpp-2154/CMakeLists.txt b/2001-2500/2154-Keep-Multiplying-Found-Values-by-Two/cpp-2154/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2154-Keep-Multiplying-Found-Values-by-Two/cpp-2154/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/2154-Keep-Multiplying-Found-Values-by-Two/cpp-2154/main.cpp b/2001-2500/2154-Keep-Multiplying-Found-Values-by-Two/cpp-2154/main.cpp new file mode 100644 index 00000000..d3f70dd1 --- /dev/null +++ b/2001-2500/2154-Keep-Multiplying-Found-Values-by-Two/cpp-2154/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/keep-multiplying-found-values-by-two/ +/// Author : liuyubobobo +/// Time : 2022-01-29 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(log(max_num)) +/// Space Compexity: O(n) +class Solution { +public: + int findFinalValue(vector& nums, int original) { + + unordered_set table(nums.begin(), nums.end()); + while(table.count(original)) + original *= 2; + return original; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2155-All-Divisions-With-the-Highest-Score-of-a-Binary-Array/cpp-2155/CMakeLists.txt b/2001-2500/2155-All-Divisions-With-the-Highest-Score-of-a-Binary-Array/cpp-2155/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2155-All-Divisions-With-the-Highest-Score-of-a-Binary-Array/cpp-2155/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/2155-All-Divisions-With-the-Highest-Score-of-a-Binary-Array/cpp-2155/main.cpp b/2001-2500/2155-All-Divisions-With-the-Highest-Score-of-a-Binary-Array/cpp-2155/main.cpp new file mode 100644 index 00000000..fd62429a --- /dev/null +++ b/2001-2500/2155-All-Divisions-With-the-Highest-Score-of-a-Binary-Array/cpp-2155/main.cpp @@ -0,0 +1,44 @@ +/// Source : https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/ +/// Author : liuyubobobo +/// Time : 2022-01-29 + +#include +#include + +using namespace std; + + +/// Presum + Sufsum +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector maxScoreIndices(vector& nums) { + + int n = nums.size(); + + vector pre0(n + 1, 0); + for(int i = 0; i < n; i ++) pre0[i + 1] = pre0[i] + (nums[i] == 0); + + vector suf1(n + 1, 0); + for(int i = n - 1; i >= 0; i --) suf1[i] = suf1[i + 1] + (nums[i] == 1); + + int max_score = -1; + vector res; + for(int i = 0; i <= n; i ++){ + int score = pre0[i] + suf1[i]; + if(score > max_score){ + max_score = score; res = {i}; + } + else if(score == max_score) res.push_back(i); + } + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2156-Find-Substring-With-Given-Hash-Value/cpp-2156/CMakeLists.txt b/2001-2500/2156-Find-Substring-With-Given-Hash-Value/cpp-2156/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2156-Find-Substring-With-Given-Hash-Value/cpp-2156/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/2156-Find-Substring-With-Given-Hash-Value/cpp-2156/main.cpp b/2001-2500/2156-Find-Substring-With-Given-Hash-Value/cpp-2156/main.cpp new file mode 100644 index 00000000..09d590dd --- /dev/null +++ b/2001-2500/2156-Find-Substring-With-Given-Hash-Value/cpp-2156/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/find-substring-with-given-hash-value/ +/// Author : liuyubobobo +/// Time : 2022-01-29 + +#include +#include + +using namespace std; + + +/// String Hash +/// Time Complexity: O(n) +/// Space Comlexity: O(n) +template +class StringHash{ + +private: + int n; + T B, MOD; + vector h, p; + +public: + StringHash(const string& s, T B = 128, T MOD = 1e9+ 7) : + n(s.size()), h(n + 1, 0), p(n + 1, 1), B(B), MOD(MOD){ + + for(int i = 0; i < n; i ++){ + h[i + 1] = (h[i] * B + (s[i] - 'a' + 1)) % MOD; + p[i + 1] = p[i] * B % MOD; + } + } + + T get_hash(int l, int r){ +// assert(l >= 0 && l < n); +// assert(r >= 0 && r < n); + + T res = (h[r + 1] - h[l] * p[r - l + 1]) % MOD; + return res < 0 ? res + MOD : res; + } +}; + +class Solution { +public: + string subStrHash(string s, int power, int modulo, int k, int hashValue) { + + reverse(s.begin(), s.end()); + StringHash hash(s, power, modulo); + + string res = ""; + for(int i = 0; i + k - 1 < s.size(); i ++) + if(hash.get_hash(i, i + k - 1) == hashValue) + res = s.substr(i, k); + reverse(res.begin(), res.end()); + return res; + } +}; + + +int main() { + + cout << Solution().subStrHash("leetcode", 7, 20, 2, 0) << endl; + // ee + + cout << Solution().subStrHash("fbxzaad", 31, 100, 3, 32) << endl; + // fbx + + return 0; +} diff --git a/2001-2500/2157-Groups-of-Strings/cpp-2157/CMakeLists.txt b/2001-2500/2157-Groups-of-Strings/cpp-2157/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2157-Groups-of-Strings/cpp-2157/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/2157-Groups-of-Strings/cpp-2157/main.cpp b/2001-2500/2157-Groups-of-Strings/cpp-2157/main.cpp new file mode 100644 index 00000000..e7cc27d1 --- /dev/null +++ b/2001-2500/2157-Groups-of-Strings/cpp-2157/main.cpp @@ -0,0 +1,138 @@ +/// Source : https://leetcode.com/problems/groups-of-strings/ +/// Author : liuyubobobo +/// Time : 2022-01-29 + +#include +#include +#include +#include + +using namespace std; + + +/// UF +/// Time Complexity: O(26 * 26 * n) +/// Space Complexity: O(2^26 + 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 { +public: + vector groupStrings(vector& words) { + + int n = words.size(); + + vector data(n); + for(int i = 0; i < n; i ++){ + int state = 0; + for(char c: words[i]) + state |= (1 << (c - 'a')); + data[i] = state; + } + + sort(data.begin(), data.end()); + + unordered_map state2index; + for(int i = 0; i < n; i ++) state2index[data[i]] = i; + + UF uf(n); + for(int start = 0, i = 1; i <= n; i ++){ + + if(i < n && data[i] == data[start]) continue; + + for(int index = start + 1; index < i; index ++) + uf.union_elements(start, index); + + for(int p = 0; p < 26; p ++){ + int state = data[start] ^ (1 << p); + auto iter = state2index.find(state); + if(iter != state2index.end()){ + uf.union_elements(start, iter->second); + } + } + + for(int p1 = 0; p1 < 26; p1 ++){ + if(data[start] & (1 << p1)) continue; + // data[i][p1] = 0; + for(int p2 = 0; p2 < 26; p2 ++){ + if((data[start] & (1 << p2)) == 0) continue; + // data[i][p2] = 1; + int state = data[start] ^ (1 << p1) ^ (1 << p2); + auto iter = state2index.find(state); + if(iter != state2index.end()){ + uf.union_elements(start, iter->second); + } + } + } + + start = i; + } + + unordered_set groups; + int max_sz = 0; + for(int i = 0; i < n; i ++){ + groups.insert(uf.find(i)); + max_sz = max(max_sz, uf.size(i)); + } + return {(int)groups.size(), max_sz}; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector words1 = {"a", "b", "ab", "cde"}; + print_vec(Solution().groupStrings(words1)); + // 2, 3 + + vector words2 = {"a", "ab", "abc"}; + print_vec(Solution().groupStrings(words2)); + // 1, 3 + + vector words3 = {"a", "b", "bc", "cd"}; + print_vec(Solution().groupStrings(words3)); + // 1, 4 + + vector words4 = {"a", "z", "bc", "zb"}; + print_vec(Solution().groupStrings(words4)); + // 1, 4 + + return 0; +} diff --git a/2001-2500/2158-Amount-of-New-Area-Painted-Each-Day/cpp-2158/CMakeLists.txt b/2001-2500/2158-Amount-of-New-Area-Painted-Each-Day/cpp-2158/CMakeLists.txt new file mode 100644 index 00000000..2db18c3a --- /dev/null +++ b/2001-2500/2158-Amount-of-New-Area-Painted-Each-Day/cpp-2158/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2158) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2158 main.cpp) diff --git a/2001-2500/2158-Amount-of-New-Area-Painted-Each-Day/cpp-2158/main.cpp b/2001-2500/2158-Amount-of-New-Area-Painted-Each-Day/cpp-2158/main.cpp new file mode 100644 index 00000000..eaf9ab11 --- /dev/null +++ b/2001-2500/2158-Amount-of-New-Area-Painted-Each-Day/cpp-2158/main.cpp @@ -0,0 +1,126 @@ +/// Source : https://leetcode.com/problems/amount-of-new-area-painted-each-day/ +/// Author : liuyubobobo +/// Time : 2022-02-04 + +#include +#include + +using namespace std; + + +/// Segment Tree +/// Time Complexity: O(q * log(max_end)) +/// Space Complexity: O(max_end) +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, 0){ + this->combine = combine; + } + + void update(int uL, int uR, T v){ + assert(0 <= uL && uL < n); + assert(0 <= uR && uR < n); + assert(uL <= uR); + update(0, 0, n-1, uL, uR, v); + } + + 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 update(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]) + push(treeID, treeL, treeR); + + int mid = (treeL + treeR) / 2; + update(2 * treeID + 1, treeL, mid, uL, uR, v); + update(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]) + 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] = 0; + } +}; + +class Solution { +public: + vector amountPainted(vector>& paint) { + + int max_end = 0; + for(const vector& seg: paint) max_end = max(max_end, seg[1]); + + SegmentTree seg_tree(max_end, [](int a, int b){return a + b;}); + + int n = paint.size(); + vector res(n); + for(int i = 0; i < n; i ++){ + int l = paint[i][0], r = paint[i][1] - 1; + int sum = seg_tree.query(l, r); + res[i] = (r - l + 1) - sum; + seg_tree.update(l, r, 1); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2160-Minimum-Sum-of-Four-Digit-Number-After-Splitting-Digits/cpp-2160/CMakeLists.txt b/2001-2500/2160-Minimum-Sum-of-Four-Digit-Number-After-Splitting-Digits/cpp-2160/CMakeLists.txt new file mode 100644 index 00000000..50e95cdc --- /dev/null +++ b/2001-2500/2160-Minimum-Sum-of-Four-Digit-Number-After-Splitting-Digits/cpp-2160/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2160) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2160 main.cpp) diff --git a/2001-2500/2160-Minimum-Sum-of-Four-Digit-Number-After-Splitting-Digits/cpp-2160/main.cpp b/2001-2500/2160-Minimum-Sum-of-Four-Digit-Number-After-Splitting-Digits/cpp-2160/main.cpp new file mode 100644 index 00000000..b1c06b5f --- /dev/null +++ b/2001-2500/2160-Minimum-Sum-of-Four-Digit-Number-After-Splitting-Digits/cpp-2160/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/ +/// Author : liuyubobobo +/// Time : 2022-02-06 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(4! * 3 * 4) +/// Space Complexity: O(1) +class Solution { +public: + int minimumSum(int num) { + + string s = to_string(num); + sort(s.begin(), s.end()); + + int res = INT_MAX; + do{ + for(int i = 1; i < 4; i ++) + res = min(res, atoi(s.substr(0, i).c_str()) + atoi(s.substr(i).c_str())); + }while(next_permutation(s.begin(), s.end())); + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2161-Partition-Array-According-to-Given-Pivot/cpp-2161/CMakeLists.txt b/2001-2500/2161-Partition-Array-According-to-Given-Pivot/cpp-2161/CMakeLists.txt new file mode 100644 index 00000000..4fdc18e8 --- /dev/null +++ b/2001-2500/2161-Partition-Array-According-to-Given-Pivot/cpp-2161/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2161) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2161 main.cpp) diff --git a/2001-2500/2161-Partition-Array-According-to-Given-Pivot/cpp-2161/main.cpp b/2001-2500/2161-Partition-Array-According-to-Given-Pivot/cpp-2161/main.cpp new file mode 100644 index 00000000..6c576335 --- /dev/null +++ b/2001-2500/2161-Partition-Array-According-to-Given-Pivot/cpp-2161/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/partition-array-according-to-given-pivot/ +/// Author : liuyubobobo +/// Time : 2022-02-06 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector pivotArray(vector& nums, int pivot) { + + vector pre, middle, post; + for(int e: nums) + if(e < pivot) pre.push_back(e); + else if(e == pivot) middle.push_back(e); + else post.push_back(e); + + pre.insert(pre.end(), middle.begin(), middle.end()); + pre.insert(pre.end(), post.begin(), post.end()); + return pre; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2162-Minimum-Cost-to-Set-Cooking-Time/cpp-2162/CMakeLists.txt b/2001-2500/2162-Minimum-Cost-to-Set-Cooking-Time/cpp-2162/CMakeLists.txt new file mode 100644 index 00000000..998db4a4 --- /dev/null +++ b/2001-2500/2162-Minimum-Cost-to-Set-Cooking-Time/cpp-2162/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2162) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2162 main.cpp) diff --git a/2001-2500/2162-Minimum-Cost-to-Set-Cooking-Time/cpp-2162/main.cpp b/2001-2500/2162-Minimum-Cost-to-Set-Cooking-Time/cpp-2162/main.cpp new file mode 100644 index 00000000..025d7fd1 --- /dev/null +++ b/2001-2500/2162-Minimum-Cost-to-Set-Cooking-Time/cpp-2162/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/minimum-cost-to-set-cooking-time/ +/// Author : liuyubobobo +/// Time : 2021-02-06 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(targetSeconds / 60) +/// Space Complexity: O(1) +class Solution { +public: + int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) { + + int res = INT_MAX; + for(int minutes = 0; minutes <= 99 && minutes * 60 <= targetSeconds; minutes ++){ + int seconds = targetSeconds - minutes * 60; + if(seconds > 99) continue; + res = min(res, get_time(startAt, moveCost, pushCost, minutes, seconds)); + } + return res; + } + +private: + int get_time(int startAt, int moveCost, int pushCost, int minute, int seconds){ + + vector v(4); + v[0] = minute / 10, v[1] = minute % 10, v[2] = seconds / 10, v[3] = seconds % 10; + + while(!v.empty() && v[0] == 0) v.erase(v.begin()); + assert(!v.empty()); + + int cur_pos = startAt, res = 0; + for(int e: v){ + if(cur_pos != e) res += moveCost, cur_pos = e; + res += pushCost; + } + return res; + } +}; + + +int main() { + + cout << Solution().minCostSetTime(1, 2, 1, 600) << endl; + // 6 + + cout << Solution().minCostSetTime(0, 1, 2, 76) << endl; + // 6 + + return 0; +} diff --git a/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/CMakeLists.txt b/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/CMakeLists.txt new file mode 100644 index 00000000..75f32f2f --- /dev/null +++ b/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2163) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2163 main2.cpp) diff --git a/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/main.cpp b/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/main.cpp new file mode 100644 index 00000000..cecde7a3 --- /dev/null +++ b/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/main.cpp @@ -0,0 +1,126 @@ +/// Source : https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/ +/// Author : liuyubobobo +/// Time : 2022-02-06 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Custom define two structures +/// Time Complexity: O(nlogn) +/// Space Compelxity: O(n) +class MinNSum{ + +private: + priority_queue pq; + long long min_sum = 0; + int n; + +public: + MinNSum(const vector& nums) : n(nums.size()){ + for(int e: nums){ + pq.push(e); + min_sum += e; + } + } + + long long get_min_sum(){ + return min_sum; + } + + void add(int e){ + pq.push(e); + min_sum += e; + + int maxv = pq.top(); + pq.pop(); + min_sum -= maxv; + +// assert(pq.size() == n); + } +}; + +class MaxNSum{ + +private: + multiset min_set, max_set; + long long max_sum = 0; + int n; + +public: + MaxNSum(vector nums){ + int n2 = nums.size(); + n = n2 / 2; + + sort(nums.begin(), nums.end()); + for(int i = 0; i < n; i ++) min_set.insert(nums[i]); + for(int i = n; i < n2; i ++){ + max_set.insert(nums[i]); + max_sum += nums[i]; + } + } + + long long get_max_sum(){ + return max_sum; + } + + void remove(int e){ + auto iter = min_set.find(e); + if(iter != min_set.end()){ + min_set.erase(iter); + return; + } + +// assert(max_set.count(e)); + max_set.erase(max_set.find(e)); + max_sum -= e; + +// assert(!min_set.empty()); + int maxv = *min_set.rbegin(); + iter = min_set.end(); + iter --; + min_set.erase(iter); + max_set.insert(maxv); + max_sum += maxv; + } +}; + +class Solution { +public: + long long minimumDifference(vector& nums) { + + int n3 = nums.size(), n = n3 / 3; + + vector first(nums.begin(), nums.begin() + n); + vector second(nums.begin() + n, nums.end()); + + MinNSum minNSum(first); + MaxNSum maxNSum(second); + long long res = minNSum.get_min_sum() - maxNSum.get_max_sum(); + for(int i = n ; i < n + n; i ++){ + minNSum.add(nums[i]); + maxNSum.remove(nums[i]); + res = min(res, minNSum.get_min_sum() - maxNSum.get_max_sum()); + } + return res; + } +}; + + +int main() { + + vector nums1 = {3, 1, 2}; + cout << Solution().minimumDifference(nums1) << endl; + // -1 + + vector nums2 = {7, 9, 5, 8, 1, 3}; + cout << Solution().minimumDifference(nums2) << endl; + // 1 + + return 0; +} diff --git a/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/main2.cpp b/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/main2.cpp new file mode 100644 index 00000000..c3b2600e --- /dev/null +++ b/2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/main2.cpp @@ -0,0 +1,70 @@ +/// Source : https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/ +/// Author : liuyubobobo +/// Time : 2022-02-06 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Premin + Sufmax, only using priority queues +/// Time Complexity: O(nlogn) +/// Space Compelxity: O(n) +class Solution { +public: + long long minimumDifference(vector& nums) { + + int n3 = nums.size(), n = n3 / 3, n2 = 2 * n; + + // premin + vector premin(n3, 0); + priority_queue pq; + long long sum = 0; + for(int i = 0; i < n; i ++) + pq.push(nums[i]), sum += nums[i]; + + premin[n - 1] = sum; + for(int i = n; i < n2; i ++){ + pq.push(nums[i]), sum += nums[i]; + int maxv = pq.top(); pq.pop(); sum -= maxv; + premin[i] = sum; + } + + // sufmax + vector sufmax(n3, 0); + priority_queue, greater> pq2; + sum = 0; + for(int i = n3 - 1; i >= n2; i --) + pq2.push(nums[i]), sum += nums[i]; + + sufmax[n2] = sum; + for(int i = n2 - 1; i >= n; i --){ + pq2.push(nums[i]), sum += nums[i]; + int minv = pq2.top(); pq2.pop(); sum -= minv; + sufmax[i] = sum; + } + + long long res = LONG_LONG_MAX; + for(int i = n - 1; i < n2; i ++) + res = min(res, premin[i] - sufmax[i + 1]); + return res; + } +}; + + +int main() { + + vector nums1 = {3, 1, 2}; + cout << Solution().minimumDifference(nums1) << endl; + // -1 + + vector nums2 = {7, 9, 5, 8, 1, 3}; + cout << Solution().minimumDifference(nums2) << endl; + // 1 + + return 0; +} diff --git a/2001-2500/2164-Sort-Even-and-Odd-Indices-Independently/cpp-2164/CMakeLists.txt b/2001-2500/2164-Sort-Even-and-Odd-Indices-Independently/cpp-2164/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2164-Sort-Even-and-Odd-Indices-Independently/cpp-2164/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/2164-Sort-Even-and-Odd-Indices-Independently/cpp-2164/main.cpp b/2001-2500/2164-Sort-Even-and-Odd-Indices-Independently/cpp-2164/main.cpp new file mode 100644 index 00000000..36dd377c --- /dev/null +++ b/2001-2500/2164-Sort-Even-and-Odd-Indices-Independently/cpp-2164/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/sort-even-and-odd-indices-independently/ +/// Author : liuyubobobo +/// Time : 2021-02-06 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + vector sortEvenOdd(vector& nums) { + + vector odd, even; + for(int i = 0; i < nums.size(); i ++) + if(i % 2 == 0) even.push_back(nums[i]); + else odd.push_back(nums[i]); + + sort(odd.begin(), odd.end(), greater()); + sort(even.begin(), even.end()); + + vector res; + for(int k = 0, i = 0, j = 0; k < nums.size(); k ++) + if(k % 2 == 0) res.push_back(even[i ++]); + else res.push_back(odd[j ++]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2165-Smallest-Value-of-the-Rearranged-Number/cpp-2165/CMakeLists.txt b/2001-2500/2165-Smallest-Value-of-the-Rearranged-Number/cpp-2165/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2165-Smallest-Value-of-the-Rearranged-Number/cpp-2165/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/2165-Smallest-Value-of-the-Rearranged-Number/cpp-2165/main.cpp b/2001-2500/2165-Smallest-Value-of-the-Rearranged-Number/cpp-2165/main.cpp new file mode 100644 index 00000000..6248ef65 --- /dev/null +++ b/2001-2500/2165-Smallest-Value-of-the-Rearranged-Number/cpp-2165/main.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/smallest-value-of-the-rearranged-number/ +/// Author : liuyubobobo +/// Time : 2022-02-06 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(|num|log|num|) +/// Space Complexity: O(|num|) +class Solution { +public: + long long smallestNumber(long long num) { + + if(num == 0) return 0; + + if(num > 0){ + string s = to_string(num); + sort(s.begin(), s.end()); + + int non_zero = 0; + for(;non_zero < s.size() && s[non_zero] == '0'; non_zero ++); + string res = string(1, s[non_zero]) + s.substr(0, non_zero) + s.substr(non_zero + 1); + return atoll(res.c_str()); + } + + string s = to_string(num).substr(1); + sort(s.begin(), s.end(), greater()); + return atoll(s.c_str()) * -1ll; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2166-Design-Bitset/cpp-2166/CMakeLists.txt b/2001-2500/2166-Design-Bitset/cpp-2166/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2166-Design-Bitset/cpp-2166/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/2166-Design-Bitset/cpp-2166/main.cpp b/2001-2500/2166-Design-Bitset/cpp-2166/main.cpp new file mode 100644 index 00000000..7d83905f --- /dev/null +++ b/2001-2500/2166-Design-Bitset/cpp-2166/main.cpp @@ -0,0 +1,79 @@ +/// Source : https://leetcode.com/problems/design-bitset/ +/// Author : liuyubobobo +/// Time : 2021-02-06 + +#include +#include +#include + +using namespace std; + + +/// Using Array +/// Time Complexity: init: O(1) +/// fix: O(1) +/// unfix: O(1) +/// flip: O(1) +/// all: O(1) +/// one: O(1) +/// count: O(1) +/// toString: O(sz) +/// Space Complexity: O(sz) +class Bitset { + +private: + int sz; + vector v; + int ones, flipped; + +public: + Bitset(int size) : sz(size), v(sz, 0), ones(0), flipped(0) { + } + + void fix(int idx) { + if(!flipped){ + if(v[idx] == 0) v[idx] = 1, ones ++; + } + else{ + if(v[idx] == 1) v[idx] = 0, ones ++; + } + } + + void unfix(int idx) { + if(!flipped){ + if(v[idx] == 1) v[idx] = 0, ones --; + } + else{ + if(v[idx] == 0) v[idx] = 1, ones --; + } + } + + void flip() { + flipped ^= 1; + ones = sz - ones; + } + + bool all() { + return ones == sz; + } + + bool one() { + return ones; + } + + int count() { + return ones; + } + + string toString() { + string res = ""; + for(int e: v) res += (char)('0' + (flipped ? 1 - e : e)); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2166-Design-Bitset/cpp-2166/main2.cpp b/2001-2500/2166-Design-Bitset/cpp-2166/main2.cpp new file mode 100644 index 00000000..5cb40c78 --- /dev/null +++ b/2001-2500/2166-Design-Bitset/cpp-2166/main2.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/design-bitset/ +/// Author : liuyubobobo +/// Time : 2021-02-06 + +#include +#include +#include + +using namespace std; + + +/// Using C++ STL Bitset +/// Time Complexity: init: O(sz) +/// fix: O(1) +/// unfix: O(1) +/// flip: O(MAXN) +/// all: O(MAXN) +/// one: O(MAXN) +/// count: O(MAXN) +/// toString: O(sz) +/// Space Complexity: O(MAXN) +class Bitset { + +private: + int sz; + bitset<100000> o, mask; + +public: + Bitset(int size) : sz(size), mask(0) { + for(int i = 0; i < size; i ++) + mask.set(i); + } + + void fix(int idx) { + o.set(sz - 1- idx); + } + + void unfix(int idx) { + o.reset(sz - 1- idx); + } + + void flip() { + o.flip(); + o &= mask; + } + + bool all() { + return (o & mask).count() == sz; + } + + bool one() { + return (o & mask).any(); + } + + int count() { + return (o & mask).count(); + } + + string toString() { + return o.to_string().substr(100000 - sz); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2167-Minimum-Time-to-Remove-All-Cars-Containing-Illegal-Goods/cpp-2167/CMakeLists.txt b/2001-2500/2167-Minimum-Time-to-Remove-All-Cars-Containing-Illegal-Goods/cpp-2167/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2167-Minimum-Time-to-Remove-All-Cars-Containing-Illegal-Goods/cpp-2167/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/2167-Minimum-Time-to-Remove-All-Cars-Containing-Illegal-Goods/cpp-2167/main.cpp b/2001-2500/2167-Minimum-Time-to-Remove-All-Cars-Containing-Illegal-Goods/cpp-2167/main.cpp new file mode 100644 index 00000000..0224caef --- /dev/null +++ b/2001-2500/2167-Minimum-Time-to-Remove-All-Cars-Containing-Illegal-Goods/cpp-2167/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/ +/// Author : liuyubobobo +/// Time : 2022-02-06 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int minimumTime(string s) { + + int n = s.size(); + + int res = n, minv = 0; + for(int r = 0; r < n; r ++){ + minv = min(r, minv) + 2 * (s[r] == '1'); + res = min(res, n - 1 - r + minv); + } + return res; + } +}; + + +int main() { + + string s1 = "1100101"; + cout << Solution().minimumTime(s1) << endl; + // 5 + + string s2 = "0010"; + cout << Solution().minimumTime(s2) << endl; + // 2 + + string s3 = "011001111111101001010000001010011"; + cout << Solution().minimumTime(s3) << endl; + // 25 + + return 0; +} diff --git a/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/CMakeLists.txt b/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/CMakeLists.txt new file mode 100644 index 00000000..26408b80 --- /dev/null +++ b/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2168) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2168 main2.cpp) diff --git a/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/main.cpp b/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/main.cpp new file mode 100644 index 00000000..dbe35006 --- /dev/null +++ b/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/unique-substrings-with-equal-digit-frequency/ +/// Author : liuyubobobo +/// Time : 2022-02-13 + +#include +#include +#include + +using namespace std; + + +/// Presum + HashSet +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { +public: + int equalDigitFrequency(string s) { + + int n = s.size(); + + vector> presum(n + 1, vector(10, 0)); + for(int i = 0; i < s.size(); i ++){ + presum[i + 1][s[i] - '0'] ++; + for(int j = 0; j < 10; j ++) + presum[i + 1][j] += presum[i][j]; + } + + unordered_set set; + for(int r = 0; r < n; r ++){ + for(int l = 0; l <= r; l ++){ + int f = 0; + bool ok = true; + for(int k = 0; k < 10 && ok; k ++) + if(presum[r + 1][k] - presum[l][k]){ + if(f && presum[r + 1][k] - presum[l][k] != f) + ok = false; + else f = presum[r + 1][k] - presum[l][k]; + } + if(ok) set.insert(s.substr(l, r - l + 1)); + } + } + return set.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/main2.cpp b/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/main2.cpp new file mode 100644 index 00000000..f9077b28 --- /dev/null +++ b/2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/main2.cpp @@ -0,0 +1,76 @@ +/// Source : https://leetcode.com/problems/unique-substrings-with-equal-digit-frequency/ +/// Author : liuyubobobo +/// Time : 2022-02-13 + +#include +#include +#include + +using namespace std; + + +/// Presum + HashSet + Rolling Hash +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class StringHashU{ + +private: + int n; + unsigned long long B; + vector h, p; + +public: + StringHashU(const string& s, unsigned long long B = 131) : + 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 equalDigitFrequency(string s) { + + int n = s.size(); + + vector> presum(n + 1, vector(10, 0)); + for(int i = 0; i < s.size(); i ++){ + presum[i + 1][s[i] - '0'] ++; + for(int j = 0; j < 10; j ++) + presum[i + 1][j] += presum[i][j]; + } + + StringHashU stringHash(s); + + unordered_set set; + for(int r = 0; r < n; r ++){ + for(int l = 0; l <= r; l ++){ + int f = 0; + bool ok = true; + for(int k = 0; k < 10 && ok; k ++) + if(presum[r + 1][k] - presum[l][k]){ + if(f && presum[r + 1][k] - presum[l][k] != f) + ok = false; + else f = presum[r + 1][k] - presum[l][k]; + } + if(ok) set.insert(stringHash.get_hash(l, r)); + } + } + return set.size(); + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2169-Count-Operations-to-Obtain-Zero/cpp-2169/CMakeLists.txt b/2001-2500/2169-Count-Operations-to-Obtain-Zero/cpp-2169/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2169-Count-Operations-to-Obtain-Zero/cpp-2169/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/2169-Count-Operations-to-Obtain-Zero/cpp-2169/main.cpp b/2001-2500/2169-Count-Operations-to-Obtain-Zero/cpp-2169/main.cpp new file mode 100644 index 00000000..1b15c8ef --- /dev/null +++ b/2001-2500/2169-Count-Operations-to-Obtain-Zero/cpp-2169/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/count-operations-to-obtain-zero/ +/// Author : liuyubobobo +/// Time : 2022-02-12 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(max(num1, num2) / min(num1, num2)) +/// Space Compelxity: O(1) +class Solution { +public: + int countOperations(int num1, int num2) { + + int res = 0; + while(num1 && num2){ + if(num1 >= num2) num1 -= num2; + else num2 -= num1; + res ++; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2170-Minimum-Operations-to-Make-the-Array-Alternating/cpp-2170/CMakeLists.txt b/2001-2500/2170-Minimum-Operations-to-Make-the-Array-Alternating/cpp-2170/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2170-Minimum-Operations-to-Make-the-Array-Alternating/cpp-2170/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/2170-Minimum-Operations-to-Make-the-Array-Alternating/cpp-2170/main.cpp b/2001-2500/2170-Minimum-Operations-to-Make-the-Array-Alternating/cpp-2170/main.cpp new file mode 100644 index 00000000..c1d950b5 --- /dev/null +++ b/2001-2500/2170-Minimum-Operations-to-Make-the-Array-Alternating/cpp-2170/main.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/ +/// Author : liuyubobobo +/// Time : 2022-02-12 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minimumOperations(vector& nums) { + + int n = nums.size(); + if(n == 1) return 0; + + unordered_map sen, others; + for(int i = 0; i < n; i ++) { + if (i % 2 == 0) sen[nums[i]]++; + else others[nums[i]]++; + } + + vector> sen_v, others_v; + for(const pair& p: sen) + sen_v.push_back({p.second, p.first}); + for(const pair& p: others) + others_v.push_back({p.second, p.first}); + + sort(sen_v.begin(), sen_v.end(), greater>()); + sort(others_v.begin(), others_v.end(), greater>()); + + int sen_num = (n + 1) / 2, others_num = n - sen_num; + int res = n; + if(sen_v[0].second != others_v[0].second){ + res = min(res, n - sen_v[0].first - others_v[0].first); + } + else{ + if(others_v.size() >= 2) + res = min(res, n - sen_v[0].first - others_v[1].first); + else res = min(res, n - sen_v[0].first); + + if(sen_v.size() >= 2) + res = min(res, n - sen_v[1].first - others_v[0].first); + else res = min(res, n - others_v[0].first); + } + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2171-Removing-Minimum-Number-of-Magic-Beans/cpp-2171/CMakeLists.txt b/2001-2500/2171-Removing-Minimum-Number-of-Magic-Beans/cpp-2171/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2171-Removing-Minimum-Number-of-Magic-Beans/cpp-2171/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/2171-Removing-Minimum-Number-of-Magic-Beans/cpp-2171/main.cpp b/2001-2500/2171-Removing-Minimum-Number-of-Magic-Beans/cpp-2171/main.cpp new file mode 100644 index 00000000..cb934077 --- /dev/null +++ b/2001-2500/2171-Removing-Minimum-Number-of-Magic-Beans/cpp-2171/main.cpp @@ -0,0 +1,49 @@ +/// Source : https://leetcode.com/problems/removing-minimum-number-of-magic-beans/ +/// Author : liuyubobobo +/// Time : 2022-02-12 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { +public: + long long minimumRemoval(vector& beans) { + + int n = beans.size(); + + sort(beans.begin(), beans.end()); + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + beans[i]; + + long long res = presum.back(); + for(int i = 0; i < n; i ++){ + if(i && beans[i] == beans[i - 1]) continue; + + long long tres = presum[i]; + tres += presum[n] - presum[i] - (long long)(n - i) * beans[i]; + res = min(res, tres); + } + return res; + } +}; + + +int main() { + + vector beans1 = {4, 1, 6, 5}; + cout << Solution().minimumRemoval(beans1) << endl; + // 4 + + vector beans2 = {2, 10, 3, 2}; + cout << Solution().minimumRemoval(beans2) << endl; + // 7 + + return 0; +} diff --git a/2001-2500/2172-Maximum-AND-Sum-of-Array/cpp-2172/CMakeLists.txt b/2001-2500/2172-Maximum-AND-Sum-of-Array/cpp-2172/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2172-Maximum-AND-Sum-of-Array/cpp-2172/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/2172-Maximum-AND-Sum-of-Array/cpp-2172/main.cpp b/2001-2500/2172-Maximum-AND-Sum-of-Array/cpp-2172/main.cpp new file mode 100644 index 00000000..0a10d7e5 --- /dev/null +++ b/2001-2500/2172-Maximum-AND-Sum-of-Array/cpp-2172/main.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/maximum-and-sum-of-array/ +/// Author : liuyubobobo +/// Time : 2022-02-12 + +#include +#include +#include + +using namespace std; + + +/// State Compression DP +/// Time Complexity: O(n * 3^|numSlots| * numSlots) +/// Space Complexity: O(n * 3^|numSlots|) +class Solution { + +public: + int maximumANDSum(vector& nums, int numSlots) { + + int n = nums.size(); + vector slots(numSlots, 2); + + vector> dp(n, vector(get_state(slots) + 1, INT_MIN)); + return dfs(n, nums, 0, slots, dp); + } + +private: + int dfs(int n, const vector& nums, int index, vector& slots, + vector>& dp){ + + if(index == n) return 0; + + int slots_state = get_state(slots); + + if(dp[index][slots_state] != INT_MIN) + return dp[index][slots_state]; + + int res = INT_MIN; + for(int i = 0; i < slots.size(); i ++) + if(slots[i]){ + slots[i] --; + res = max(res, (nums[index] & (i + 1)) + dfs(n, nums, index + 1, slots, dp)); + slots[i] ++; + } + return dp[index][slots_state] = res; + } + + int get_state(const vector& v){ + + int res = 0; + for(int e: v) res = res * 3 + e; + return res; + } +}; + + +int main() { + + vector nums1 = {1, 2, 3, 4, 5, 6}; + cout << Solution().maximumANDSum(nums1, 3) << endl; + // 9 + + vector nums2 = {1, 3, 10, 4, 7, 1}; + cout << Solution().maximumANDSum(nums2, 9) << endl; + // 24 + + return 0; +} diff --git a/2001-2500/2174-Remove-All-Ones-With-Row-and-Column-Flips-II/cpp-2174/CMakeLists.txt b/2001-2500/2174-Remove-All-Ones-With-Row-and-Column-Flips-II/cpp-2174/CMakeLists.txt new file mode 100644 index 00000000..9ee5693b --- /dev/null +++ b/2001-2500/2174-Remove-All-Ones-With-Row-and-Column-Flips-II/cpp-2174/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2174) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2174 main.cpp) diff --git a/2001-2500/2174-Remove-All-Ones-With-Row-and-Column-Flips-II/cpp-2174/main.cpp b/2001-2500/2174-Remove-All-Ones-With-Row-and-Column-Flips-II/cpp-2174/main.cpp new file mode 100644 index 00000000..2a802b92 --- /dev/null +++ b/2001-2500/2174-Remove-All-Ones-With-Row-and-Column-Flips-II/cpp-2174/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/remove-all-ones-with-row-and-column-flips-ii/ +/// Author : liuyubobobo +/// Time : 2022-02-18 + +#include +#include + +using namespace std; + + +/// State-Comppression Memoization +/// Time Complexity: O(R * C * 2^(R * C)) +/// Space Complexity: O(2^(R * C)) +class Solution { + +public: + int removeOnes(vector>& grid) { + + int R = grid.size(), C = grid[0].size(); + + vector dp(1 << (R * C), -1); + return dfs(R, C, grid, dp); + } + +private: + int dfs(int R, int C, const vector> grid, vector& dp){ + + int state = 0; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(grid[i][j]) state |= (1 << (i * C + j)); + + if(state == 0) return 0; + if(dp[state] != -1) return dp[state]; + + int res = INT_MAX; + for(int i = 0; i < R; i ++) + for(int j = 0; j < C; j ++) + if(grid[i][j]){ + vector> t = grid; + for(int k = 0; k < R; k ++) t[k][j] = 0; + for(int k = 0; k < C; k ++) t[i][k] = 0; + res = min(res, 1 + dfs(R, C, t, dp)); + } + return dp[state] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2176-Count-Equal-and-Divisible-Pairs-in-an-Array/cpp-2176/CMakeLists.txt b/2001-2500/2176-Count-Equal-and-Divisible-Pairs-in-an-Array/cpp-2176/CMakeLists.txt new file mode 100644 index 00000000..5d16bd4b --- /dev/null +++ b/2001-2500/2176-Count-Equal-and-Divisible-Pairs-in-an-Array/cpp-2176/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2176) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2176 main.cpp) diff --git a/2001-2500/2176-Count-Equal-and-Divisible-Pairs-in-an-Array/cpp-2176/main.cpp b/2001-2500/2176-Count-Equal-and-Divisible-Pairs-in-an-Array/cpp-2176/main.cpp new file mode 100644 index 00000000..9293787e --- /dev/null +++ b/2001-2500/2176-Count-Equal-and-Divisible-Pairs-in-an-Array/cpp-2176/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int countPairs(vector& nums, int k) { + + int n = nums.size(), res = 0; + for(int i = 0; i < n; i ++) + for(int j = i + 1; j < n; j ++) + if(nums[i] == nums[j] && (i * j) % k == 0) res ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2177-Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/cpp-2177/CMakeLists.txt b/2001-2500/2177-Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/cpp-2177/CMakeLists.txt new file mode 100644 index 00000000..3c233e08 --- /dev/null +++ b/2001-2500/2177-Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/cpp-2177/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2177) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2177 main.cpp) diff --git a/2001-2500/2177-Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/cpp-2177/main.cpp b/2001-2500/2177-Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/cpp-2177/main.cpp new file mode 100644 index 00000000..345d841f --- /dev/null +++ b/2001-2500/2177-Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/cpp-2177/main.cpp @@ -0,0 +1,27 @@ +/// Source : https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(1) +/// Space Complexity: O(1) +class Solution { +public: + vector sumOfThree(long long num) { + + if(num % 3ll) return {}; + return {num / 3 - 1, num / 3, num / 3 + 1}; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2178-Maximum-Split-of-Positive-Even-Integers/cpp-2178/CMakeLists.txt b/2001-2500/2178-Maximum-Split-of-Positive-Even-Integers/cpp-2178/CMakeLists.txt new file mode 100644 index 00000000..36385f67 --- /dev/null +++ b/2001-2500/2178-Maximum-Split-of-Positive-Even-Integers/cpp-2178/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2178) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2178 main.cpp) diff --git a/2001-2500/2178-Maximum-Split-of-Positive-Even-Integers/cpp-2178/main.cpp b/2001-2500/2178-Maximum-Split-of-Positive-Even-Integers/cpp-2178/main.cpp new file mode 100644 index 00000000..e897709c --- /dev/null +++ b/2001-2500/2178-Maximum-Split-of-Positive-Even-Integers/cpp-2178/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/maximum-split-of-positive-even-integers/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(sqrt(sum)) +/// Space Complexity: O(1) +class Solution { +public: + vector maximumEvenSplit(long long finalSum) { + + if(finalSum % 2ll) return {}; + + vector res; + long long cur = 2; + while(finalSum >= cur){ + res.push_back(cur); + finalSum -= cur; + cur += 2; + } + + if(finalSum == 0) return res; + res.back() += finalSum; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2179-Count-Good-Triplets-in-an-Array/cpp-2179/CMakeLists.txt b/2001-2500/2179-Count-Good-Triplets-in-an-Array/cpp-2179/CMakeLists.txt new file mode 100644 index 00000000..e69f946e --- /dev/null +++ b/2001-2500/2179-Count-Good-Triplets-in-an-Array/cpp-2179/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2179) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2179 main.cpp) diff --git a/2001-2500/2179-Count-Good-Triplets-in-an-Array/cpp-2179/main.cpp b/2001-2500/2179-Count-Good-Triplets-in-an-Array/cpp-2179/main.cpp new file mode 100644 index 00000000..abab1e63 --- /dev/null +++ b/2001-2500/2179-Count-Good-Triplets-in-an-Array/cpp-2179/main.cpp @@ -0,0 +1,136 @@ +/// Source : https://leetcode.com/problems/count-good-triplets-in-an-array/ +/// Author : liuyubobobo +/// Time : 2022-02-20 + +#include +#include + +using namespace std; + + +/// Using Segment Tree +/// Time Complexity: O(nlogn) +/// Space Compelxity: 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 add(int index, T value){ +// if(data[index] == value) return; + data[index] += value; + add(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 && 0 <= l && l < n && 0 <= r && r < n) + return query(0, 0, n - 1, l, r); + return 0; + } + +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 add(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) add(treeID * 2 + 1, l, mid, index, value); + else add(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: + long long goodTriplets(vector& nums1, vector& nums2) { + + int n = nums1.size(); + + vector nums2pos(n); + for(int i = 0; i < nums2.size(); i ++) + nums2pos[nums2[i]] = i; + + auto add = [](long long a, long long b){return a + b;}; + SegmentTree seg_tree(n, add); + SegmentTree good2_for_nums1(n, add), good2_for_nums2(n, add); + long long res = 0; + for(int i = n - 1; i >= 0; i --){ + long long t = seg_tree.query(nums2pos[nums1[i]], n - 1); + good2_for_nums1.add(i, t); + good2_for_nums2.add(nums2pos[nums1[i]], t); + + res += min(good2_for_nums1.query(i + 1, n - 1), good2_for_nums2.query(nums2pos[nums1[i]] + 1, n - 1)); + seg_tree.add(nums2pos[nums1[i]], 1); + + } + + return res; + } +}; + + +int main() { + + vector nums11 = {2, 0, 1, 3}, nums12 = {0, 1, 2, 3}; + cout << Solution().goodTriplets(nums11, nums12) << endl; + // 1 + + vector nums21 = {4, 0, 1, 3, 2}, nums22 = {4, 1, 0, 2, 3}; + cout << Solution().goodTriplets(nums21, nums22) << endl; + // 4 + + return 0; +} diff --git a/2001-2500/2180-Count-Integers-With-Even-Digit-Sum/cpp-2180/CMakeLists.txt b/2001-2500/2180-Count-Integers-With-Even-Digit-Sum/cpp-2180/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2180-Count-Integers-With-Even-Digit-Sum/cpp-2180/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/2180-Count-Integers-With-Even-Digit-Sum/cpp-2180/main.cpp b/2001-2500/2180-Count-Integers-With-Even-Digit-Sum/cpp-2180/main.cpp new file mode 100644 index 00000000..879a05ce --- /dev/null +++ b/2001-2500/2180-Count-Integers-With-Even-Digit-Sum/cpp-2180/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/count-integers-with-even-digit-sum/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + int countEven(int num) { + + int res = 0; + for(int e = 1; e <= num; e ++) + res += ok(e); + return res; + } + +private: + bool ok(int e){ + int res = 0; + while(e) res += e % 10, e /= 10; + return res % 2 == 0; + } +}; + + +int main() { + + cout << Solution().countEven(4) << endl; + // 2 + + cout << Solution().countEven(30) << endl; + // 14 + + return 0; +} diff --git a/2001-2500/2181-Merge-Nodes-in-Between-Zeros/cpp-2181/CMakeLists.txt b/2001-2500/2181-Merge-Nodes-in-Between-Zeros/cpp-2181/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2181-Merge-Nodes-in-Between-Zeros/cpp-2181/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/2181-Merge-Nodes-in-Between-Zeros/cpp-2181/main.cpp b/2001-2500/2181-Merge-Nodes-in-Between-Zeros/cpp-2181/main.cpp new file mode 100644 index 00000000..2dfa9a5a --- /dev/null +++ b/2001-2500/2181-Merge-Nodes-in-Between-Zeros/cpp-2181/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/merge-nodes-in-between-zeros/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#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() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution { +public: + ListNode* mergeNodes(ListNode* head) { + + ListNode* dummy_head = new ListNode(-1); + ListNode* res_cur = dummy_head; + + int sum = 0; + for(ListNode* cur = head->next; cur; cur = cur->next){ + if(cur->val == 0){ + res_cur->next = new ListNode(sum); + res_cur = res_cur->next; + sum = 0; + } + else sum += cur->val; + } + return dummy_head->next; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2182-Construct-String-With-Repeat-Limit/cpp-2182/CMakeLists.txt b/2001-2500/2182-Construct-String-With-Repeat-Limit/cpp-2182/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2182-Construct-String-With-Repeat-Limit/cpp-2182/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/2182-Construct-String-With-Repeat-Limit/cpp-2182/main.cpp b/2001-2500/2182-Construct-String-With-Repeat-Limit/cpp-2182/main.cpp new file mode 100644 index 00000000..d4b72d16 --- /dev/null +++ b/2001-2500/2182-Construct-String-With-Repeat-Limit/cpp-2182/main.cpp @@ -0,0 +1,69 @@ +/// Source : https://leetcode.com/problems/construct-string-with-repeat-limit/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + string repeatLimitedString(string s, int repeatLimit) { + + vector f(26, 0); + for(char c: s) f[c - 'a'] ++; + + string res = ""; + while(true){ + pair p = get_first_and_second(f); + int first = p.first, second = p.second; + if(first == -1 || first == res.back() - 'a') break; + + int k = min(f[first], repeatLimit); + res += string(k, 'a' + first); + f[first] -= k; + + if(f[first] && second != -1){ + res += (char)('a' + second); + f[second] -= 1; + } + } + return res; + } + +private: + pair get_first_and_second(const vector& f){ + + int first = -1; + for(int i = 25; i >= 0; i --) + if(f[i]){ + first = i; + break; + } + + int second = -1; + for(int i = first - 1; i >= 0; i --) + if(f[i]){ + second = i; + break; + } + return {first, second}; + } +}; + +int main() { + + cout << Solution().repeatLimitedString("cczazcc", 3) << endl; + // zzcccac + + cout << Solution().repeatLimitedString("aababab", 2) << endl; + // bbabaa + + return 0; +} diff --git a/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/CMakeLists.txt b/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/CMakeLists.txt new file mode 100644 index 00000000..2552f642 --- /dev/null +++ b/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/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/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/main.cpp b/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/main.cpp new file mode 100644 index 00000000..c83e1a58 --- /dev/null +++ b/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/count-array-pairs-divisible-by-k/ +/// Author : liuyubobobo +/// Time : 2022-02-19 + +#include +#include +#include +#include + +using namespace std; + + +/// Get all divisors for every number +/// Time Complexity: O(n*sqrt(n)) +/// Space Complexity: O(maxn) +class Solution { +public: + long long countPairs(vector& nums, int k) { + + int maxn = max(*max_element(nums.begin(), nums.end()), k); + + vector f(maxn + 1, 0); + long long res = 0; + for(int e: nums){ + vector divisors; + for(int d = 1; d * d <= e; d ++) + if(e % d == 0){ + divisors.push_back(d); + if(d * d < e) divisors.push_back(e / d); + } + sort(divisors.begin(), divisors.end()); + + int left = -1; + for(int i = (int)divisors.size() - 1; i >= 0; i --) + if(k % divisors[i] == 0){ + left = k / divisors[i]; + break; + } + res += f[left]; + + for(int d: divisors) f[d] ++; + } + return res; + } +}; + + +int main() { + + vector nums1 = {1, 2, 3, 4, 5}; + cout << Solution().countPairs(nums1, 2) << endl; + // 7 + + vector nums2 = {1, 2, 3, 4}; + cout << Solution().countPairs(nums2, 5) << endl; + // 0 + + return 0; +} diff --git a/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/main2.cpp b/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/main2.cpp new file mode 100644 index 00000000..ecd12835 --- /dev/null +++ b/2001-2500/2183-Count-Array-Pairs-Divisible-by-K/cpp-2183/main2.cpp @@ -0,0 +1,58 @@ +/// Source : https://leetcode.com/problems/count-array-pairs-divisible-by-k/ +/// Author : liuyubobobo +/// Time : 2022-02-20 + +#include +#include +#include +#include + +using namespace std; + + +/// Get all divisors for k +/// Time Complexity: O(n*sqrt(k)) +/// Space Complexity: O(k) +class Solution { +public: + long long countPairs(vector& nums, int k) { + + vector divisors; + for(int d = 1; d * d <= k; d ++) + if(k % d == 0){ + divisors.push_back(d); + if(d * d < k) divisors.push_back(k / d); + } + sort(divisors.begin(), divisors.end()); + + vector f(k + 1, 0); + long long res = 0; + for(int e: nums){ + + int left = -1; + for(int i = (int)divisors.size() - 1; i >= 0; i --) + if(e % divisors[i] == 0){ + left = k / divisors[i]; + break; + } + res += f[left]; + + for(int d: divisors) if(e % d == 0) f[d] ++; + } + return res; + } +}; + + +int main() { + + vector nums1 = {1, 2, 3, 4, 5}; + cout << Solution().countPairs(nums1, 2) << endl; + // 7 + + vector nums2 = {1, 2, 3, 4}; + cout << Solution().countPairs(nums2, 5) << endl; + // 0 + + return 0; +} diff --git a/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/CMakeLists.txt b/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/CMakeLists.txt new file mode 100644 index 00000000..1b860ca2 --- /dev/null +++ b/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2184) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2184 main.cpp) diff --git a/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/main.cpp b/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/main.cpp new file mode 100644 index 00000000..8c707d57 --- /dev/null +++ b/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/main.cpp @@ -0,0 +1,96 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-build-sturdy-brick-wall/ +/// Author : liuyubobobo +/// Time : 2022-02-25 + +#include +#include +#include + +using namespace std; + + +/// Backtrack + DP +/// Time Complexity: O(|brick|^|brick| + k^2 * height) where k is the way to fill one row +/// Space Complexity: O(k) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int buildWall(int height, int width, vector& bricks) { + + vector> rows; + vector row; + dfs(bricks, width, row, rows); + + for(vector& row: rows){ + for(int i = 1; i < row.size(); i ++) + row[i] += row[i - 1]; + assert(row.back() == width); + row.pop_back(); + } + + int n = rows.size(); + if(n == 0) return 0; + + vector> ok(n, vector(n, false)); + for(int i = 0; i < n; i ++) + for(int j = i; j < n; j ++) + ok[i][j] = get_ok(rows[i], rows[j]), ok[j][i] = ok[i][j]; + + vector dp(n, 1); + for(int h = 2; h <= height; h ++){ + vector tdp(n, 0); + for(int i = 0; i < n; i ++){ + for(int j = 0; j < n; j ++) + tdp[i] += dp[j] * ok[i][j]; + tdp[i] %= MOD; + } + dp = tdp; + } + return accumulate(dp.begin(), dp.end(), 0ll) % MOD; + } + +private: + bool get_ok(const vector& r1, const vector& r2){ + int i = 0, j = 0; + while(i < r1.size() && j < r2.size()){ + if(r1[i] == r2[j]) return false; + if(r1[i] < r2[j]) i ++; + else j ++; + } + return true; + } + + void dfs(const vector& bricks, int left, vector& row, vector>& rows){ + + if(left == 0) rows.push_back(row); + if(left <= 0) return; + + for(int brick: bricks){ + row.push_back(brick); + dfs(bricks, left - brick, row, rows); + row.pop_back(); + } + return; + } +}; + + +int main() { + + vector bricks1 = {1, 2}; + cout << Solution().buildWall(2, 3, bricks1) << endl; + // 2 + + vector bricks2 = {5}; + cout << Solution().buildWall(1, 1, bricks2) << endl; + // 0 + + vector bricks3 = {6, 3, 5, 1, 9}; + cout << Solution().buildWall(76, 9, bricks3) << endl; + // 201495766 + + return 0; +} diff --git a/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/main2.cpp b/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/main2.cpp new file mode 100644 index 00000000..2fae1f40 --- /dev/null +++ b/2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/main2.cpp @@ -0,0 +1,75 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-build-sturdy-brick-wall/ +/// Author : liuyubobobo +/// Time : 2022-02-25 + +#include +#include +#include + +using namespace std; + + +/// Backtrack + DP +/// Using bit compression +/// Time Complexity: O(|brick|^|brick| + k^2 * height) where k is the way to fill one row +/// Space Complexity: O(k) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int buildWall(int height, int width, vector& bricks) { + + vector rows; + dfs(bricks, width, 0, 0, rows); + + int n = rows.size(); + if(n == 0) return 0; + + vector dp(n, 1), tdp(n, 0); + for(int h = 2; h <= height; h ++){ + tdp.assign(n, 0); + for(int i = 0; i < n; i ++){ + for(int j = 0; j < n; j ++) + tdp[i] += dp[j] * !!!(rows[j] & rows[i]); + tdp[i] %= MOD; + } + dp = tdp; + } + + return accumulate(dp.begin(), dp.end(), 0ll) % MOD; + } + +private: + void dfs(const vector& bricks, int width, int cur, int state, vector& rows){ + + if(cur == width){ + rows.push_back(state ^ (1 << width)); + return; + } + + for(int brick: bricks) + if(cur + brick <= width) + dfs(bricks, width, cur + brick, state | (1 << (cur + brick)), rows); + return; + } +}; + + +int main() { + + vector bricks1 = {1, 2}; + cout << Solution().buildWall(2, 3, bricks1) << endl; + // 2 + + vector bricks2 = {5}; + cout << Solution().buildWall(1, 1, bricks2) << endl; + // 0 + + vector bricks3 = {6, 3, 5, 1, 9}; + cout << Solution().buildWall(76, 9, bricks3) << endl; + // 201495766 + + return 0; +} diff --git a/2001-2500/2185-Counting-Words-With-a-Given-Prefix/A/CMakeLists.txt b/2001-2500/2185-Counting-Words-With-a-Given-Prefix/A/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2185-Counting-Words-With-a-Given-Prefix/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/2001-2500/2185-Counting-Words-With-a-Given-Prefix/A/main.cpp b/2001-2500/2185-Counting-Words-With-a-Given-Prefix/A/main.cpp new file mode 100644 index 00000000..118c2a6b --- /dev/null +++ b/2001-2500/2185-Counting-Words-With-a-Given-Prefix/A/main.cpp @@ -0,0 +1,29 @@ +/// Source : https://leetcode.com/problems/counting-words-with-a-given-prefix/ +/// Author : liuyubobobo +/// Time : 2022-02-26 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n * len) +/// Space Complexity: O(1) +class Solution { +public: + int prefixCount(vector& words, string pref) { + + int res = 0; + for(const string& word: words) + if(word.find(pref) == 0) res ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2186-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram-II/cpp-2186/CMakeLists.txt b/2001-2500/2186-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram-II/cpp-2186/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2186-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram-II/cpp-2186/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/2186-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram-II/cpp-2186/main.cpp b/2001-2500/2186-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram-II/cpp-2186/main.cpp new file mode 100644 index 00000000..b8b20487 --- /dev/null +++ b/2001-2500/2186-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram-II/cpp-2186/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/ +/// Author : liuyubobobo +/// Time : 2022-02-26 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(|s| + |t|) +/// Space Complexity: O(1) +class Solution { +public: + int minSteps(string s, string t) { + + vector sf(26, 0), tf(26, 0); + for(char c: s) sf[c - 'a'] ++; + for(char c: t) tf[c - 'a'] ++; + + int res = 0; + for(int i = 0; i < 26; i ++) + res += abs(sf[i] - tf[i]); + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2187-Minimum-Time-to-Complete-Trips/cpp-2187/CMakeLists.txt b/2001-2500/2187-Minimum-Time-to-Complete-Trips/cpp-2187/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2187-Minimum-Time-to-Complete-Trips/cpp-2187/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/2187-Minimum-Time-to-Complete-Trips/cpp-2187/main.cpp b/2001-2500/2187-Minimum-Time-to-Complete-Trips/cpp-2187/main.cpp new file mode 100644 index 00000000..92baa1f7 --- /dev/null +++ b/2001-2500/2187-Minimum-Time-to-Complete-Trips/cpp-2187/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-complete-trips/ +/// Author : liuyubobobo +/// Time : 2022-02-26 + +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Complexity: O(n * log(MAXN)) +/// Space Complexity: O(1) +class Solution { +public: + long long minimumTime(vector& time, int totalTrips) { + + long long l = 0, r = 1e14; + while(l < r){ + long long mid = (l + r) / 2ll; + if(ok(time, mid, totalTrips)) r = mid; + else l = mid + 1; + } + return l; + } + +private: + bool ok(const vector& time, long long mid, int totalTrips){ + long long res = 0; + for(int t: time){ + res += mid / t; + if(res >= totalTrips) break; + } + return res >= totalTrips; + } +}; + + +int main() { + + vector time1 = {1, 2, 3}; + cout << Solution().minimumTime(time1, 5) << endl; + // 3 + + vector time2 = {2}; + cout << Solution().minimumTime(time2, 1) << endl; + // 2 + + return 0; +} diff --git a/2001-2500/2188-Minimum-Time-to-Finish-the-Race/cpp-2188/CMakeLists.txt b/2001-2500/2188-Minimum-Time-to-Finish-the-Race/cpp-2188/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2188-Minimum-Time-to-Finish-the-Race/cpp-2188/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/2188-Minimum-Time-to-Finish-the-Race/cpp-2188/main.cpp b/2001-2500/2188-Minimum-Time-to-Finish-the-Race/cpp-2188/main.cpp new file mode 100644 index 00000000..2a80faa7 --- /dev/null +++ b/2001-2500/2188-Minimum-Time-to-Finish-the-Race/cpp-2188/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/minimum-time-to-finish-the-race/ +/// Author : liuyubobobo +/// Time : 2022-02-26 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n * log(MAX_TIME) + numLaps ^ 2) +/// Space Complexity: O(n + numLaps^2) +class Solution { +public: + int minimumFinishTime(vector>& tires, long long changeTime, long long numLaps) { + + int n = tires.size(); + sort(tires.begin(), tires.end()); + + long long L = (numLaps - 1ll) * changeTime + numLaps * tires[0][0]; + vector minT(numLaps + 1, LONG_LONG_MAX); + for(int i = 0; i < n; i ++){ + long long sum = 0, cur = tires[i][0]; + for(int j = 1; j <= numLaps && sum <= L; j ++){ + sum += cur; + cur *= tires[i][1]; + minT[j] = min(minT[j], sum); + } + } + + vector dp(numLaps + 1, -1); + return dfs(numLaps, minT, changeTime, dp); + } + +private: + long long dfs(int left, const vector& minT, long long changeTime, + vector& dp){ + + if(left == 0) return 0; + if(dp[left] != -1) return dp[left]; + + long long res = LONG_LONG_MAX; + for(int i = 1; i <= left && minT[i] != LONG_LONG_MAX; i ++){ + long long tres = minT[i] + (i == left ? 0 : changeTime) + dfs(left - i, minT, changeTime, dp); + res = min(res, tres); + } + return dp[left] = res; + } +}; + + +int main() { + + vector> tires1 = {{2, 3}, {3, 4}}; + int changeTime1 = 5, numLaps1 = 4; + cout << Solution().minimumFinishTime(tires1, changeTime1, numLaps1) << endl; + // 21 + + vector> tires2 = {{1, 10}, {2, 2}, {3, 4}}; + int changeTime2 = 6, numLaps2 = 5; + cout << Solution().minimumFinishTime(tires2, changeTime2, numLaps2) << endl; + // 25 + + return 0; +} diff --git a/2001-2500/2189-Number-of-Ways-to-Build-House-of-Cards/cpp-2189/CMakeLists.txt b/2001-2500/2189-Number-of-Ways-to-Build-House-of-Cards/cpp-2189/CMakeLists.txt new file mode 100644 index 00000000..29ccd364 --- /dev/null +++ b/2001-2500/2189-Number-of-Ways-to-Build-House-of-Cards/cpp-2189/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2189) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2189 main.cpp) diff --git a/2001-2500/2189-Number-of-Ways-to-Build-House-of-Cards/cpp-2189/main.cpp b/2001-2500/2189-Number-of-Ways-to-Build-House-of-Cards/cpp-2189/main.cpp new file mode 100644 index 00000000..5dbb8ed9 --- /dev/null +++ b/2001-2500/2189-Number-of-Ways-to-Build-House-of-Cards/cpp-2189/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-build-house-of-cards/ +/// Author : liuyubobobo +/// Time : 2022-03-04 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n^3) +/// Space Complexity: O(n^2) +class Solution { +public: + int houseOfCards(int n) { + + if(n < 2) return 0; + + int max_count = 1 + (n - 2) / 3 + !!((n - 2) % 3); + vector> dp(max_count + 1, vector(n + 1, -1)); + return dfs(max_count, n, dp); + } + +private: + int dfs(int max_count, int n, vector>& dp){ + + if(n == 0) return 1; + if(n == 1 || max_count == 0) return 0; + if(dp[max_count][n] != -1) return dp[max_count][n]; + + int res = dfs(0, n - 2, dp); + for(int cnt = 5; cnt <= n && 1 + (cnt - 2) / 3 <= max_count; cnt += 3) + res += dfs((cnt - 2) / 3, n - cnt, dp); + return dp[max_count][n] = res; + } +}; + + +int main() { + + cout << Solution().houseOfCards(16) << '\n'; + // 2 + + cout << Solution().houseOfCards(2) << '\n'; + // 1 + + cout << Solution().houseOfCards(4) << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2190-Most-Frequent-Number-Following-Key-In-an-Array/cpp-2190/CMakeLists.txt b/2001-2500/2190-Most-Frequent-Number-Following-Key-In-an-Array/cpp-2190/CMakeLists.txt new file mode 100644 index 00000000..7d086ba6 --- /dev/null +++ b/2001-2500/2190-Most-Frequent-Number-Following-Key-In-an-Array/cpp-2190/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2190) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2190 main.cpp) diff --git a/2001-2500/2190-Most-Frequent-Number-Following-Key-In-an-Array/cpp-2190/main.cpp b/2001-2500/2190-Most-Frequent-Number-Following-Key-In-an-Array/cpp-2190/main.cpp new file mode 100644 index 00000000..6f2bdf9a --- /dev/null +++ b/2001-2500/2190-Most-Frequent-Number-Following-Key-In-an-Array/cpp-2190/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/ +/// Author : liuyubobobo +/// Time : 2022-03-05 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int mostFrequent(vector& nums, int key) { + + map f; + for(int i = 0; i + 1 < nums.size(); i ++) + if(nums[i] == key) f[nums[i + 1]] ++; + + int max_cnt = 0, res; + for(const pair& p: f) + if(p.second > max_cnt) max_cnt = p.second, res = p.first; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2191-Sort-the-Jumbled-Numbers/cpp-2191/CMakeLists.txt b/2001-2500/2191-Sort-the-Jumbled-Numbers/cpp-2191/CMakeLists.txt new file mode 100644 index 00000000..9ea5dd80 --- /dev/null +++ b/2001-2500/2191-Sort-the-Jumbled-Numbers/cpp-2191/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2191) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2191 main.cpp) diff --git a/2001-2500/2191-Sort-the-Jumbled-Numbers/cpp-2191/main.cpp b/2001-2500/2191-Sort-the-Jumbled-Numbers/cpp-2191/main.cpp new file mode 100644 index 00000000..1345e6e8 --- /dev/null +++ b/2001-2500/2191-Sort-the-Jumbled-Numbers/cpp-2191/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/sort-the-jumbled-numbers/ +/// Author : liuyubobobo +/// Time : 2022-03-05 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(nlogn * log(num)) +/// Space Complexity: O(n) +class Solution { +public: + vector sortJumbled(vector& mapping, vector& nums) { + + int n = nums.size(); + vector> data(n); + for(int i = 0; i < n; i ++){ + data[i] = {0, i}; + + string s = to_string(nums[i]); + for(char& c: s) c = '0' + mapping[c - '0']; + data[i].first = atoi(s.c_str()); + } + + sort(data.begin(), data.end()); + + vector res(n); + for(int i = 0; i < n; i ++) res[i] = nums[data[i].second]; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2192-All-Ancestors-of-a-Node-in-a-Directed-Acyclic-Graph/cpp-2192/CMakeLists.txt b/2001-2500/2192-All-Ancestors-of-a-Node-in-a-Directed-Acyclic-Graph/cpp-2192/CMakeLists.txt new file mode 100644 index 00000000..1043e210 --- /dev/null +++ b/2001-2500/2192-All-Ancestors-of-a-Node-in-a-Directed-Acyclic-Graph/cpp-2192/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2192) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2192 main.cpp) diff --git a/2001-2500/2192-All-Ancestors-of-a-Node-in-a-Directed-Acyclic-Graph/cpp-2192/main.cpp b/2001-2500/2192-All-Ancestors-of-a-Node-in-a-Directed-Acyclic-Graph/cpp-2192/main.cpp new file mode 100644 index 00000000..87848623 --- /dev/null +++ b/2001-2500/2192-All-Ancestors-of-a-Node-in-a-Directed-Acyclic-Graph/cpp-2192/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph/ +/// Author : liuyubobobo +/// Time : 2022-03-05 + +#include +#include +#include +#include + +using namespace std; + + +/// Topological Sort + Bitset +/// Time Complexity: O(n + m) +/// Space Complexity: O(n + m) +class Solution { +public: + vector> getAncestors(int n, vector>& edges) { + + vector> g(n); + for(const vector& e: edges) + g[e[0]].push_back(e[1]); + + vector in_degrees(n, 0); + for(int u = 0; u < n; u ++) + for(int v: g[u]) in_degrees[v] ++; + + queue q; + for(int u = 0; u < n; u ++) + if(in_degrees[u] == 0) q.push(u); + vector> res(n); + + while(!q.empty()){ + int u = q.front(); q.pop(); + + for(int v: g[u]){ + res[v].set(u); + res[v] |= res[u]; + in_degrees[v] --; + if(in_degrees[v] == 0) q.push(v); + } + } + + vector> ret(n); + for(int i = 0; i < n; i ++) + for(int j = 0; j < n; j ++) + if(res[i].test(j)) ret[i].push_back(j); + return ret; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2193-Minimum-Number-of-Moves-to-Make-Palindrome/cpp-2193/CMakeLists.txt b/2001-2500/2193-Minimum-Number-of-Moves-to-Make-Palindrome/cpp-2193/CMakeLists.txt new file mode 100644 index 00000000..7cc7d493 --- /dev/null +++ b/2001-2500/2193-Minimum-Number-of-Moves-to-Make-Palindrome/cpp-2193/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2193) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2193 main.cpp) diff --git a/2001-2500/2193-Minimum-Number-of-Moves-to-Make-Palindrome/cpp-2193/main.cpp b/2001-2500/2193-Minimum-Number-of-Moves-to-Make-Palindrome/cpp-2193/main.cpp new file mode 100644 index 00000000..f3d12db4 --- /dev/null +++ b/2001-2500/2193-Minimum-Number-of-Moves-to-Make-Palindrome/cpp-2193/main.cpp @@ -0,0 +1,54 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/ +/// Author : liuyubobobo +/// Time : 2022-03-06 + +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int minMovesToMakePalindrome(string s) { + + return dfs(s, 0, s.size() - 1); + } + +private: + int dfs(string& s, int l, int r){ + + if(l >= r) return 0; + if(s[l] == s[r]) return dfs(s, l + 1, r - 1); + + int front_pos, tail_pos, front_swaps = 0, tail_swaps = 0; + for(front_pos = l; front_pos <= r && s[front_pos] != s[r]; front_pos ++, front_swaps ++); + for(tail_pos = r; tail_pos >= l && s[tail_pos] != s[l]; tail_pos --, tail_swaps ++); + + + if(front_swaps <= tail_swaps){ + for(int i = front_pos - 1; i >= l; i --) swap(s[i], s[i + 1]); + return front_swaps + dfs(s, l + 1, r - 1); + } + + for(int i = tail_pos + 1; i <= r; i ++) swap(s[i - 1], s[i]); + return tail_swaps + dfs(s, l + 1, r - 1); + } +}; + + +int main() { + + cout << Solution().minMovesToMakePalindrome("aabb") << endl; + // 2 + + cout << Solution().minMovesToMakePalindrome("letelt") << endl; + // 2 + + cout << Solution().minMovesToMakePalindrome("eqvvhtcsaaqtqesvvqch") << endl; + // 17 + + return 0; +} diff --git a/2001-2500/2194-Cells-in-a-Range-on-an-Excel-Sheet/cpp-2194/CMakeLists.txt b/2001-2500/2194-Cells-in-a-Range-on-an-Excel-Sheet/cpp-2194/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2194-Cells-in-a-Range-on-an-Excel-Sheet/cpp-2194/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/2194-Cells-in-a-Range-on-an-Excel-Sheet/cpp-2194/main.cpp b/2001-2500/2194-Cells-in-a-Range-on-an-Excel-Sheet/cpp-2194/main.cpp new file mode 100644 index 00000000..315f2d24 --- /dev/null +++ b/2001-2500/2194-Cells-in-a-Range-on-an-Excel-Sheet/cpp-2194/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/ +/// Author : liuyubobobo +/// Time : 2022-03-05 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: ((c2 - c1) * (r2 - r1) +/// Space Complexity: O(1) +class Solution { +public: + vector cellsInRange(string s) { + + char c1 = s[0], c2 = s[3]; + char r1 = s[1], r2 = s[4]; + + vector res; + for(char c = c1; c <= c2; c ++) + for(char r = r1; r <= r2; r ++){ + string t = ""; + t += c, t += r; + res.push_back(t); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2195-Append-K-Integers-With-Minimal-Sum/cpp-2195/CMakeLists.txt b/2001-2500/2195-Append-K-Integers-With-Minimal-Sum/cpp-2195/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2195-Append-K-Integers-With-Minimal-Sum/cpp-2195/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/2195-Append-K-Integers-With-Minimal-Sum/cpp-2195/main.cpp b/2001-2500/2195-Append-K-Integers-With-Minimal-Sum/cpp-2195/main.cpp new file mode 100644 index 00000000..831aa8f7 --- /dev/null +++ b/2001-2500/2195-Append-K-Integers-With-Minimal-Sum/cpp-2195/main.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/append-k-integers-with-minimal-sum/ +/// Author : liuyubobobo +/// Time : 2022-03-05 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + long long minimalKSum(vector& nums, int k) { + + nums.push_back(0); + nums.push_back(INT_MAX); + sort(nums.begin(), nums.end()); + + long long res = 0; + for(int i = 1; i < nums.size() && k; i ++){ + long long l = nums[i - 1] + 1, r = nums[i] - 1; + long long len = r - l + 1; + if(len <= 0) continue; + + if(len <= k){ + res += (l + r) * len / 2ll; + k -= len; + } + else{ + len = k, r = l + len - 1ll; + res += (l + r) * len / 2ll; + k = 0; + } + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2196-Create-Binary-Tree-From-Descriptions/cpp-2196/CMakeLists.txt b/2001-2500/2196-Create-Binary-Tree-From-Descriptions/cpp-2196/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2196-Create-Binary-Tree-From-Descriptions/cpp-2196/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/2196-Create-Binary-Tree-From-Descriptions/cpp-2196/main.cpp b/2001-2500/2196-Create-Binary-Tree-From-Descriptions/cpp-2196/main.cpp new file mode 100644 index 00000000..39d8714f --- /dev/null +++ b/2001-2500/2196-Create-Binary-Tree-From-Descriptions/cpp-2196/main.cpp @@ -0,0 +1,70 @@ +/// Source : https://leetcode.com/problems/create-binary-tree-from-descriptions/ +/// Author : liuyubobobo +/// Time : 2022-03-05 + +#include +#include +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n) +/// 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: + TreeNode* createBinaryTree(vector>& descriptions) { + + map value2node; + set is_child; + for(const vector& des: descriptions){ + int parent_value = des[0]; + TreeNode* parent_node = nullptr; + if(value2node.count(parent_value)) + parent_node = value2node[parent_value]; + else{ + parent_node = new TreeNode(parent_value); + value2node[parent_value] = parent_node; + } + + int child_value = des[1]; + TreeNode* child_node = nullptr; + if(value2node.count(child_value)) + child_node = value2node[child_value]; + else{ + child_node = new TreeNode(child_value); + value2node[child_value] = child_node; + } + + if(des[2]) parent_node->left = child_node; + else parent_node->right = child_node; + + is_child.insert(child_value); + } + + for(const vector& des: descriptions) + if(!is_child.count(des[0])) return value2node[des[0]]; + assert(false); + return nullptr; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2197-Replace-Non-Coprime-Numbers-in-Array/cpp-2197/CMakeLists.txt b/2001-2500/2197-Replace-Non-Coprime-Numbers-in-Array/cpp-2197/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2197-Replace-Non-Coprime-Numbers-in-Array/cpp-2197/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/2197-Replace-Non-Coprime-Numbers-in-Array/cpp-2197/main.cpp b/2001-2500/2197-Replace-Non-Coprime-Numbers-in-Array/cpp-2197/main.cpp new file mode 100644 index 00000000..7312570a --- /dev/null +++ b/2001-2500/2197-Replace-Non-Coprime-Numbers-in-Array/cpp-2197/main.cpp @@ -0,0 +1,62 @@ +/// Source : https://leetcode.com/problems/replace-non-coprime-numbers-in-array/ +/// Author : liuyubobobo +/// Time : 2022-03-05 + +#include +#include +#include + +using namespace std; + + +/// Stack +/// Time Complexity: O(nlogn) +/// Space Complexity:O(logn) +class Solution { +public: + vector replaceNonCoprimes(vector& nums) { + + vector res; + for(int num: nums){ + int x = num, g; + while(!res.empty() && (g = gcd(x, res.back())) != 1){ + res.back() *= x / g; + x = res.back(); + res.pop_back(); + } + + res.push_back(x); + } + 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); + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector nums1 = {6,4,3,2,7,6,2}; + print_vec(Solution().replaceNonCoprimes(nums1)); + // 12 7 6 + + vector nums2 = {2,2,1,1,3,3,3}; + print_vec(Solution().replaceNonCoprimes(nums2)); + // 2 1 1 3 + + vector nums3 = {287,41,49,287,899,23,23,20677,5,825}; + print_vec(Solution().replaceNonCoprimes(nums3)); + // 2009,20677,825 + + return 0; +} diff --git a/2001-2500/2198-Number-of-Single-Divisor-Triplets/cpp-2198/CMakeLists.txt b/2001-2500/2198-Number-of-Single-Divisor-Triplets/cpp-2198/CMakeLists.txt new file mode 100644 index 00000000..22b9be0b --- /dev/null +++ b/2001-2500/2198-Number-of-Single-Divisor-Triplets/cpp-2198/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2198) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2198 main.cpp) diff --git a/2001-2500/2198-Number-of-Single-Divisor-Triplets/cpp-2198/main.cpp b/2001-2500/2198-Number-of-Single-Divisor-Triplets/cpp-2198/main.cpp new file mode 100644 index 00000000..4c7565ae --- /dev/null +++ b/2001-2500/2198-Number-of-Single-Divisor-Triplets/cpp-2198/main.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/number-of-single-divisor-triplets/ +/// Author : liuyubobobo +/// Time : 2022-03-12 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(maxv^3) +/// Space Complexity: O(maxv) +class Solution { +public: + long long singleDivisorTriplet(vector& nums) { + + int maxv = 0; + vector f(101, 0); + for(int num: nums){ + f[num] ++; + maxv = max(maxv, num); + } + + long long res = 0ll; + for(int t1 = 1; t1 <= maxv; t1 ++) if(f[t1]) + for(int t2 = t1 + 1; t2 <= maxv; t2 ++) if(f[t2]) + for(int t3 = t2 + 1; t3 <= maxv; t3 ++) if(f[t3]){ + bool a = (t1 + t2 + t3) % t1; + bool b = (t1 + t2 + t3) % t2; + bool c = (t1 + t2 + t3) % t3; + if(a + b + c == 2) + res += f[t1] * f[t2] * f[t3] * 6; + } + + for(int t1 = 1; t1 <= maxv; t1 ++) if(f[t1]) + for(int t2 = t1 + 1; t2 <= maxv; t2 ++) if(f[t2]){ + if(f[t1] >= 2 && (t1 * 2 + t2) % t1 && (t1 * 2 + t2) % t2 == 0) + res += f[t1] * (f[t1] - 1) / 2 * f[t2] * 6; + if(f[t2] >= 2 && (t1 + t2 * 2) % t2 && (t1 + t2 * 2) % t1 == 0) + res += f[t2] * (f[t2] - 1) / 2 * f[t1] * 6; + } + return res; + } +}; + + +int main() { + + vector nums1 = {4,6,7,3,2}; + cout << Solution().singleDivisorTriplet(nums1) << '\n'; + // 12 + + vector nums2 = {1,2,2}; + cout << Solution().singleDivisorTriplet(nums2) << '\n'; + // 6 + + vector nums3 = {1,1,1}; + cout << Solution().singleDivisorTriplet(nums3) << '\n'; + // 0 + + vector nums4 = {71,2,88,93,15,88,69}; + cout << Solution().singleDivisorTriplet(nums4) << '\n'; + // 48 + + return 0; +} diff --git a/2001-2500/2200-Find-All-K-Distant-Indices-in-an-Array/cpp-2200/CMakeLists.txt b/2001-2500/2200-Find-All-K-Distant-Indices-in-an-Array/cpp-2200/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2200-Find-All-K-Distant-Indices-in-an-Array/cpp-2200/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/2200-Find-All-K-Distant-Indices-in-an-Array/cpp-2200/main.cpp b/2001-2500/2200-Find-All-K-Distant-Indices-in-an-Array/cpp-2200/main.cpp new file mode 100644 index 00000000..eaa5f1ac --- /dev/null +++ b/2001-2500/2200-Find-All-K-Distant-Indices-in-an-Array/cpp-2200/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/ +/// Author : liuyubobobo +/// Time : 2022-03-12 + +#include +#include +#include + +using namespace std; + + +/// BruteForce +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + vector findKDistantIndices(vector& nums, int key, int k) { + + int n = nums.size(); + vector res; + for(int i = 0; i < n; i ++) + for(int j = max(i - k, 0); j <= min(i + k, n - 1); j ++) + if(nums[j] == key){ + res.push_back(i); + break; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2201-Count-Artifacts-That-Can-Be-Extracted/cpp-2201/CMakeLists.txt b/2001-2500/2201-Count-Artifacts-That-Can-Be-Extracted/cpp-2201/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2201-Count-Artifacts-That-Can-Be-Extracted/cpp-2201/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/2201-Count-Artifacts-That-Can-Be-Extracted/cpp-2201/main.cpp b/2001-2500/2201-Count-Artifacts-That-Can-Be-Extracted/cpp-2201/main.cpp new file mode 100644 index 00000000..b082d924 --- /dev/null +++ b/2001-2500/2201-Count-Artifacts-That-Can-Be-Extracted/cpp-2201/main.cpp @@ -0,0 +1,41 @@ +/// Source : https://leetcode.com/problems/count-artifacts-that-can-be-extracted/ +/// Author : liuyubobobo +/// Time : 2022-03-12 + +#include +#include + +using namespace std; + + +/// 2D Presum +/// Time Complexity: O(n^2 + |dig| + |artifacts|) +/// Space Complexity: O(n^2) +class Solution { +public: + int digArtifacts(int n, vector>& artifacts, vector>& dig) { + + vector> g(n, vector(n, 1)); + for(const vector& p: dig) + g[p[0]][p[1]] = 0; + + 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] = g[i][j] + presum[i][j + 1] + presum[i + 1][j] - presum[i][j]; + + int res = 0; + for(const vector& p: artifacts){ + int x1 = p[0], y1 = p[1], x2 = p[2], y2 = p[3]; + int sum = presum[x2 + 1][y2 + 1] - presum[x1][y2 + 1] - presum[x2 + 1][y1] + presum[x1][y1]; + res += (sum == 0); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2202-Maximize-the-Topmost-Element-After-K-Moves/cpp-2202/CMakeLists.txt b/2001-2500/2202-Maximize-the-Topmost-Element-After-K-Moves/cpp-2202/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2202-Maximize-the-Topmost-Element-After-K-Moves/cpp-2202/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/2202-Maximize-the-Topmost-Element-After-K-Moves/cpp-2202/main.cpp b/2001-2500/2202-Maximize-the-Topmost-Element-After-K-Moves/cpp-2202/main.cpp new file mode 100644 index 00000000..b29a8c9e --- /dev/null +++ b/2001-2500/2202-Maximize-the-Topmost-Element-After-K-Moves/cpp-2202/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode.com/problems/maximize-the-topmost-element-after-k-moves/ +/// Author : liuyubobobo +/// Time : 2022-03-12 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int maximumTop(vector& nums, int k) { + + int n = nums.size(); + + if(k == 0) return n > 0 ? nums[0] : -1; + if(k == 1) return n == 1 ? -1 : nums[1]; + + int res = -1, left_op = k, can_add_max = -1; + for(int i = 0; i < min(k, n); i ++){ + left_op --; + + can_add_max = max(can_add_max, nums[i]); + + if(i + 1 < n && left_op % 2 == 0) + res = max(res, nums[i + 1]); + + if(left_op && (left_op % 2 || i)) + res = max(res, can_add_max); + } + return res; + } +}; + + +int main() { + + vector nums1 = {5,2,2,4,0,6}; + cout << Solution().maximumTop(nums1, 4) << '\n'; + // 5 + + vector nums2 = {2}; + cout << Solution().maximumTop(nums2, 1) << '\n'; + // -1 + + vector nums3 = {31,15,92,84,19,92,55}; + cout << Solution().maximumTop(nums3, 4) << '\n'; + // 92 + + vector nums4 = {55,39,99,57,82,48,39,91,82,68,86,4,64,38,73,40,51,66,58,44,55,6,100,6,49,91,34,87,42,22,77,74,31,67,15,99,57,21,61,79,93,98,29,9,34,51,89,67,83,49,2,17,14,29,73,7,41,64,93,83,21,89,66,100,38,45,27,31,25,16,78,32,74,21,48,99,77,65,8,47,93,67}; + cout << Solution().maximumTop(nums4, 40) << '\n'; + // 100 + + vector nums5 = {52,98,7,10,27,1,33,17,14,70,79,41,37,83,58,69,52,14,66,7,36,32,39,69,65,64,45,90,34,68,44,51,36,49,71,54,63,76,73,76,67,26,54,76,89,92,89,69,26,55,93,89,15,3,54,91,21,93,78,29,79,59,14,80,70,29,5,80,93,69,29,22,3,6,2,36,31,3,22,96,32,25,97,82,78,10,83,46,98,30,1,93,89}; + cout << Solution().maximumTop(nums5, 27) << '\n'; + // 98 + + vector nums6 = {91,98,17,79,15,55,47,86,4,5,17,79,68,60,60,31,72,85,25,77,8,78,40,96,76,69,95,2,42,87,48,72,45,25,40,60,21,91,32,79,2,87,80,97,82,94,69,43,18,19,21,36,44,81,99}; + cout << Solution().maximumTop(nums6, 2) << '\n'; + // 91 + + vector nums7 = {4,6,1,0,6,2,4}; + cout << Solution().maximumTop(nums7, 0) << '\n'; + // 4 + + return 0; +} diff --git a/2001-2500/2203-Minimum-Weighted-Subgraph-With-the-Required-Paths/cpp-2203/CMakeLists.txt b/2001-2500/2203-Minimum-Weighted-Subgraph-With-the-Required-Paths/cpp-2203/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2203-Minimum-Weighted-Subgraph-With-the-Required-Paths/cpp-2203/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/2203-Minimum-Weighted-Subgraph-With-the-Required-Paths/cpp-2203/main.cpp b/2001-2500/2203-Minimum-Weighted-Subgraph-With-the-Required-Paths/cpp-2203/main.cpp new file mode 100644 index 00000000..7db74345 --- /dev/null +++ b/2001-2500/2203-Minimum-Weighted-Subgraph-With-the-Required-Paths/cpp-2203/main.cpp @@ -0,0 +1,85 @@ +/// Source : https://leetcode.com/problems/minimum-weighted-subgraph-with-the-required-paths/ +/// Author : liuyubobobo +/// Time : 2022-03-12 + +#include +#include +#include +#include + +using namespace std; + + +/// Dijkstra +/// Time Complexity: O(nlogn) +/// Space Complexity: O(V + E) +class Solution { + +private: + long long INF = LONG_LONG_MAX / 3; + +public: + long long minimumWeight(int n, vector>& edges, int src1, int src2, int dest) { + + vector>> g(n), rg(n); + for(const vector& e: edges){ + int u = e[0], v = e[1]; + long long w = e[2]; + g[u].push_back({v, w}); + rg[v].push_back({u, w}); + } + + vector dis1 = dij(n, g, src1); + if(dis1[dest] >= INF) return -1; + + vector dis2 = dij(n, g, src2); + if(dis2[dest] >= INF) return -1; + + vector rdis = dij(n, rg, dest); + + long long res = rdis[src1] + rdis[src2]; + for(int i = 0; i < n; i ++) + res = min(res, rdis[i] + dis1[i] + dis2[i]); + return res; + } + +private: + vector dij(int n, const vector>>& g, int s){ + + 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(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; + } +}; + + +int main() { + + vector> edges1 = { + {0,2,2},{0,5,6},{1,0,3},{1,4,5},{2,1,1},{2,3,3},{2,3,4},{3,4,2},{4,5,1} + }; + cout << Solution().minimumWeight(6, edges1, 0, 1, 5) << '\n'; + // 9 + + return 0; +} diff --git a/2001-2500/2204-Distance-to-a-Cycle-in-Undirected-Graph/cpp-2204/CMakeLists.txt b/2001-2500/2204-Distance-to-a-Cycle-in-Undirected-Graph/cpp-2204/CMakeLists.txt new file mode 100644 index 00000000..2139f045 --- /dev/null +++ b/2001-2500/2204-Distance-to-a-Cycle-in-Undirected-Graph/cpp-2204/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2204) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2204 main.cpp) diff --git a/2001-2500/2204-Distance-to-a-Cycle-in-Undirected-Graph/cpp-2204/main.cpp b/2001-2500/2204-Distance-to-a-Cycle-in-Undirected-Graph/cpp-2204/main.cpp new file mode 100644 index 00000000..c38476de --- /dev/null +++ b/2001-2500/2204-Distance-to-a-Cycle-in-Undirected-Graph/cpp-2204/main.cpp @@ -0,0 +1,95 @@ +/// Source : https://leetcode.com/problems/distance-to-a-cycle-in-undirected-graph/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include +#include + +using namespace std; + + +/// Graph get cycle and BFS +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector distanceToCycle(int n, vector>& edges) { + + vector> g(n); + for(const vector& e: edges) + g[e[0]].push_back(e[1]), g[e[1]].push_back(e[0]); + + vector cycle = get_cycle(n, g); +// for(int e: cycle) cout << e << ' '; cout << '\n'; + + queue q; + vector res(n, -1); + for(int e: cycle){ + q.push(e); + res[e] = 0; + } + + while(!q.empty()){ + int u = q.front(); q.pop(); + + for(int v: g[u]){ + if(res[v] != -1) continue; + res[v] = res[u] + 1; + q.push(v); + } + } + return res; + } + +private: + vector get_cycle(int n, const vector>& g){ + + vector visited(n, false); + vector pre(n, -1); + vector cycle; + dfs(g, 0, -1, visited, pre, cycle); + return cycle; + } + + bool dfs(const vector>& g, int u, int p, + vector& visited, vector& pre, vector& cycle){ + + visited[u] = true; + pre[u] = p; + for(int v: g[u]){ + if(v == p) continue; + if(!visited[v]){ + if(dfs(g, v, u, visited, pre, cycle)) return true; + } + else{ + cycle.push_back(v); + int cur = u; + while(cur != v){ + cycle.push_back(cur); + cur = pre[cur]; + } + return true; + } + } + return false; + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector> edges1 = {{1,2},{2,3},{3,4},{4,1},{0,1},{5,2},{6,5}}; + print_vec(Solution().distanceToCycle(7, edges1)); + // 1 0 0 0 0 1 2 + + vector> edges2 = {{0,1},{1,2},{0,2},{2,6},{6,7},{6,8},{1,3},{3,4},{3,5}}; + print_vec(Solution().distanceToCycle(9, edges2)); + // 0,0,0,1,2,2,1,2,2 + + return 0; +} diff --git a/2001-2500/2206-Divide-Array-Into-Equal-Pairs/cpp-2206/CMakeLists.txt b/2001-2500/2206-Divide-Array-Into-Equal-Pairs/cpp-2206/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2206-Divide-Array-Into-Equal-Pairs/cpp-2206/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/2206-Divide-Array-Into-Equal-Pairs/cpp-2206/main.cpp b/2001-2500/2206-Divide-Array-Into-Equal-Pairs/cpp-2206/main.cpp new file mode 100644 index 00000000..3c694edc --- /dev/null +++ b/2001-2500/2206-Divide-Array-Into-Equal-Pairs/cpp-2206/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/divide-array-into-equal-pairs/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + bool divideArray(vector& nums) { + + unordered_map f; + for(int e: nums) f[e] ++; + + for(const pair& p: f) + if(p.second % 2) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2207-Maximize-Number-of-Subsequences-in-a-String/cpp-2207/CMakeLists.txt b/2001-2500/2207-Maximize-Number-of-Subsequences-in-a-String/cpp-2207/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2207-Maximize-Number-of-Subsequences-in-a-String/cpp-2207/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/2207-Maximize-Number-of-Subsequences-in-a-String/cpp-2207/main.cpp b/2001-2500/2207-Maximize-Number-of-Subsequences-in-a-String/cpp-2207/main.cpp new file mode 100644 index 00000000..55c2bf7f --- /dev/null +++ b/2001-2500/2207-Maximize-Number-of-Subsequences-in-a-String/cpp-2207/main.cpp @@ -0,0 +1,56 @@ +/// Source : https://leetcode.com/problems/maximize-number-of-subsequences-in-a-string/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long maximumSubsequenceCount(string text, string pattern) { + + string s1 = string(1, pattern[0]) + text; + long long res1 = get_res(s1, pattern[0], pattern[1]); + + string s2 = text + pattern[1]; + long long res2 = get_res(s2, pattern[0], pattern[1]); + + return max(res1, res2); + } + +private: + long long get_res(const string& s, char a, char b){ + + long long bnum = 0; + for(char c: s) bnum += (c == b); + + if(a == b) return bnum * (bnum - 1) / 2ll; + + long long res = 0, anum = 0; + for(char c: s) + if(c == a) anum ++, res += bnum; + else if(c == b) bnum --; + return res; + } +}; + + +int main() { + + cout << Solution().maximumSubsequenceCount("abdcdbc", "ac") << '\n'; + // 4 + + cout << Solution().maximumSubsequenceCount("aabb", "ab") << '\n'; + // 6 + + cout << Solution().maximumSubsequenceCount("mffiqmrvjmkfmbnaivajzecfdta", "hh") << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2208-Minimum-Operations-to-Halve-Array-Sum/cpp-2208/CMakeLists.txt b/2001-2500/2208-Minimum-Operations-to-Halve-Array-Sum/cpp-2208/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2208-Minimum-Operations-to-Halve-Array-Sum/cpp-2208/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/2208-Minimum-Operations-to-Halve-Array-Sum/cpp-2208/main.cpp b/2001-2500/2208-Minimum-Operations-to-Halve-Array-Sum/cpp-2208/main.cpp new file mode 100644 index 00000000..c0a08ccf --- /dev/null +++ b/2001-2500/2208-Minimum-Operations-to-Halve-Array-Sum/cpp-2208/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/minimum-operations-to-halve-array-sum/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include +#include +#include + +using namespace std; + + +/// Using Priority Queue +/// Time Complexity: O(xlogn) +/// Space Complexity: O(n) +class Solution { +public: + int halveArray(vector& nums) { + + long long sum = accumulate(nums.begin(), nums.end(), 0ll); + long double target = sum / 2.0; + + priority_queue pq; + for(int e: nums) pq.push(e); + + long double cur = 0; + int res = 0; + while(cur < target){ + long double x = pq.top(); + pq.pop(); + + cur += x / 2.0; + res ++; + + pq.push(x / 2.0); + } + return res; + } +}; + + +int main() { + + vector nums1 = {5, 19, 8, 1}; + cout << Solution().halveArray(nums1) << '\n'; + // 3 + + vector nums2 = {3, 8, 20}; + cout << Solution().halveArray(nums2) << '\n'; + // 3 + + return 0; +} diff --git a/2001-2500/2209-Minimum-White-Tiles-After-Covering-With-Carpets/cpp-2209/CMakeLists.txt b/2001-2500/2209-Minimum-White-Tiles-After-Covering-With-Carpets/cpp-2209/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2209-Minimum-White-Tiles-After-Covering-With-Carpets/cpp-2209/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/2209-Minimum-White-Tiles-After-Covering-With-Carpets/cpp-2209/main.cpp b/2001-2500/2209-Minimum-White-Tiles-After-Covering-With-Carpets/cpp-2209/main.cpp new file mode 100644 index 00000000..16952d61 --- /dev/null +++ b/2001-2500/2209-Minimum-White-Tiles-After-Covering-With-Carpets/cpp-2209/main.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/minimum-white-tiles-after-covering-with-carpets/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n^2) +/// Space Complexity: O(n^2) +class Solution { +public: + int minimumWhiteTiles(string floor, int numCarpets, int carpetLen) { + + int n = floor.size(); + vector presum(n + 1, 0); + for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] + (floor[i] == '1'); + + vector> dp(n, vector(numCarpets + 1, -1)); + return dfs(n, floor, presum, carpetLen, 0, numCarpets, dp); + } + +private: + int dfs(int n, const string& floor, const vector& presum, int len, + int index, int num, vector>& dp){ + + if(index == n) return 0; + if(num == 0) return presum[n] - presum[index]; + if(dp[index][num] != -1) return dp[index][num]; + + int r = min(index + len - 1, n - 1); + int res = dfs(n, floor, presum, len, r + 1, num - 1, dp); + + res = min(res, (floor[index] == '1') + dfs(n, floor, presum, len, index + 1, num, dp)); + return dp[index][num] = res; + } +}; + + +int main() { + + cout << Solution().minimumWhiteTiles("10110101", 2, 2) << '\n'; + // 2 + + cout << Solution().minimumWhiteTiles("11111", 2, 3) << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2210-Count-Hills-and-Valleys-in-an-Array/cpp-2210/CMakeLists.txt b/2001-2500/2210-Count-Hills-and-Valleys-in-an-Array/cpp-2210/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2210-Count-Hills-and-Valleys-in-an-Array/cpp-2210/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/2210-Count-Hills-and-Valleys-in-an-Array/cpp-2210/main.cpp b/2001-2500/2210-Count-Hills-and-Valleys-in-an-Array/cpp-2210/main.cpp new file mode 100644 index 00000000..d7123f1d --- /dev/null +++ b/2001-2500/2210-Count-Hills-and-Valleys-in-an-Array/cpp-2210/main.cpp @@ -0,0 +1,58 @@ +/// Source : https://leetcode.com/problems/count-hills-and-valleys-in-an-array/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int countHillValley(vector& nums) { + + int n = nums.size(), res = 0; + for(int start = 0, i = 1; i <= n; i ++){ + if(i == n || nums[i] != nums[start]){ + int left = -1, right = -1; + for(int j = start - 1; j >= 0; j --) + if(nums[j] != nums[start]){ + left = nums[j]; + break; + } + for(int j = start + 1; j < n; j ++) + if(nums[j] != nums[start]){ + right = nums[j]; + break; + } + if(left == -1 || right == -1){ + start = i; + continue; + } + if((left > nums[start] && right > nums[start]) || (left < nums[start] && right < nums[start])) + res ++; + + start = i; + } + } + return res; + } +}; + + +int main() { + + vector nums1 = {2, 4, 1, 1, 6, 5}; + cout << Solution().countHillValley(nums1) << '\n'; + // 3 + + vector nums2 = {6,6,5,5,4,1}; + cout << Solution().countHillValley(nums2) << '\n'; + // 0 + + return 0; +} diff --git a/2001-2500/2211-Count-Collisions-on-a-Road/cpp-2211/CMakeLists.txt b/2001-2500/2211-Count-Collisions-on-a-Road/cpp-2211/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2211-Count-Collisions-on-a-Road/cpp-2211/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/2211-Count-Collisions-on-a-Road/cpp-2211/main.cpp b/2001-2500/2211-Count-Collisions-on-a-Road/cpp-2211/main.cpp new file mode 100644 index 00000000..5ad1d8f6 --- /dev/null +++ b/2001-2500/2211-Count-Collisions-on-a-Road/cpp-2211/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/count-collisions-on-a-road/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include + +using namespace std; + + +/// Stack +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int countCollisions(string directions) { + + int res = 0; + string stack = ""; + for(char c: directions) + if(c == 'R') stack += c; + else if(c == 'L'){ + if(stack.empty()) continue; + else if(stack.back() == 'R'){ + res += 2, stack.pop_back(); + while(!stack.empty() && stack.back() == 'R') res ++, stack.pop_back(); + stack += 'S'; + } + else res ++; + } + else if(c == 'S'){ + if(stack.empty()) stack += 'S'; + else if(stack.back() == 'R'){ + while(!stack.empty() && stack.back() == 'R') res ++, stack.pop_back(); + stack += 'S'; + } + } + return res; + } +}; + + +int main() { + + cout << Solution().countCollisions("RLRSLL") << '\n'; + // 5 + + cout << Solution().countCollisions("LLRR") << '\n'; + // 0 + + cout << Solution().countCollisions("SSRSSRLLRSLLRSRSSRLRRRRLLRRLSSRR") << '\n'; + // 20 + + return 0; +} diff --git a/2001-2500/2212-Maximum-Points-in-an-Archery-Competition/cpp-2212/CMakeLists.txt b/2001-2500/2212-Maximum-Points-in-an-Archery-Competition/cpp-2212/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2212-Maximum-Points-in-an-Archery-Competition/cpp-2212/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/2212-Maximum-Points-in-an-Archery-Competition/cpp-2212/main.cpp b/2001-2500/2212-Maximum-Points-in-an-Archery-Competition/cpp-2212/main.cpp new file mode 100644 index 00000000..7f69550b --- /dev/null +++ b/2001-2500/2212-Maximum-Points-in-an-Archery-Competition/cpp-2212/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/maximum-points-in-an-archery-competition/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(2^12 * 12) +/// Space Complexity: O(12) +class Solution { +public: + vector maximumBobPoints(int numArrows, vector& aliceArrows) { + + int best_score = 0; + vector res(12, 0); + res[0] = numArrows; + + for(int state = 1; state < (1 << 12); state ++){ + vector tres(12, 0); + int tscore = 0; + for(int i = 0; i < 12; i ++) + if(state & (1 << i)){ + tres[i] = aliceArrows[i] + 1; + tscore += i; + } + if(tscore <= best_score) continue; + + int sum = accumulate(tres.begin(), tres.end(), 0); + if(sum > numArrows) continue; + + for(int i = 0; i < 12; i ++) + if(state & (1 << i)){ + tres[i] += (numArrows - sum); + break; + } + + best_score = tscore; + res = tres; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2213-Longest-Substring-of-One-Repeating-Character/cpp-2213/CMakeLists.txt b/2001-2500/2213-Longest-Substring-of-One-Repeating-Character/cpp-2213/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2213-Longest-Substring-of-One-Repeating-Character/cpp-2213/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/2213-Longest-Substring-of-One-Repeating-Character/cpp-2213/main.cpp b/2001-2500/2213-Longest-Substring-of-One-Repeating-Character/cpp-2213/main.cpp new file mode 100644 index 00000000..3bf795e3 --- /dev/null +++ b/2001-2500/2213-Longest-Substring-of-One-Repeating-Character/cpp-2213/main.cpp @@ -0,0 +1,111 @@ +/// Source : https://leetcode.com/problems/longest-substring-of-one-repeating-character/ +/// Author : liuyubobobo +/// Time : 2022-03-19 + +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// Using Tree Map and TreeSet to store segments +/// Time Complexity: O(nlogn + qlogn) +/// Space Complexity: O(n) +class Solution { +private: + map, char> segs; + multiset lens; + +public: + vector longestRepeating(string s, string queryCharacters, vector& queryIndices) { + + for(int start = 0, i = 1; i <= s.size(); i ++) + if(i == s.size() || s[i] != s[start]){ + segs[{start, i - 1}] = s[start]; + lens.insert(i - start); + start = i; + } + + vector res(queryCharacters.size()); + for(int q = 0; q < queryCharacters.size(); q ++){ + int index = queryIndices[q], qch = queryCharacters[q]; + + auto iter = segs.upper_bound({index, INT_MAX}); + iter --; + + int l = iter->first.first, r = iter->first.second; + char och = iter->second; + if(qch != och){ + + segs.erase(iter); + lens.erase(lens.find(r - l + 1)); + + if(l <= index - 1){ + segs[{l, index - 1}] = och; + lens.insert(index - 1 - l + 1); + } + if(index + 1 <= r){ + segs[{index + 1, r}] = och; + lens.insert(r - (index + 1) + 1); + } + insert(index, index, qch); + } + + res[q] = *lens.rbegin(); + } + return res; + } + +private: + void insert(int insert_l, int insert_r, char ch){ + + auto iter = segs.upper_bound({insert_l, INT_MAX}); + if(iter != segs.end() && iter->second == ch){ + int l = iter->first.first, r = iter->first.second; + segs.erase(iter); + lens.erase(lens.find(r - l + 1)); + + insert(insert_l, r, ch); + return; + } + + iter = segs.upper_bound({insert_l, INT_MAX}); + if(iter != segs.begin()){ + iter --; + if(iter->second == ch){ + int l = iter->first.first, r = iter->first.second; + segs.erase(iter); + lens.erase(lens.find(r - l + 1)); + + segs[{l, insert_r}] = ch; + lens.insert(insert_r - l + 1); + return; + } + } + + segs[{insert_l, insert_r}] = ch; + lens.insert(insert_r - insert_l + 1); + } +}; + + +void print_vec(const vector& v){ + for(int e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector q1 = {1, 3, 3}; + print_vec(Solution().longestRepeating("babacc", "bcb", q1)); + // 3 3 4 + + vector q2 = {2, 1}; + print_vec(Solution().longestRepeating("abyzz", "aa", q2)); + // 2 3 + + return 0; +} diff --git a/2001-2500/2214-Minimum-Health-to-Beat-Game/cpp-2214/CMakeLists.txt b/2001-2500/2214-Minimum-Health-to-Beat-Game/cpp-2214/CMakeLists.txt new file mode 100644 index 00000000..481c8260 --- /dev/null +++ b/2001-2500/2214-Minimum-Health-to-Beat-Game/cpp-2214/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2214) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2214 main.cpp) diff --git a/2001-2500/2214-Minimum-Health-to-Beat-Game/cpp-2214/main.cpp b/2001-2500/2214-Minimum-Health-to-Beat-Game/cpp-2214/main.cpp new file mode 100644 index 00000000..facb1c5f --- /dev/null +++ b/2001-2500/2214-Minimum-Health-to-Beat-Game/cpp-2214/main.cpp @@ -0,0 +1,37 @@ +/// Source : https://leetcode.com/problems/minimum-health-to-beat-game/ +/// Author : liuyubobobo +/// Time : 2022-03-24 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + long long minimumHealth(vector& damage, int armor) { + + int max_damage = *max_element(damage.begin(), damage.end()); + long long sum = accumulate(damage.begin(), damage.end(), 0ll); + + if(max_damage >= armor) sum -= armor; + else sum -= max_damage; + + return sum + 1; + } +}; + + +int main() { + + vector damage1 = {2, 7, 4, 3}; + cout << Solution().minimumHealth(damage1, 4) << '\n'; + // 13 + + return 0; +} diff --git a/2001-2500/2215-Find-the-Difference-of-Two-Arrays/cpp-2215/CMakeLists.txt b/2001-2500/2215-Find-the-Difference-of-Two-Arrays/cpp-2215/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2215-Find-the-Difference-of-Two-Arrays/cpp-2215/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/2215-Find-the-Difference-of-Two-Arrays/cpp-2215/main.cpp b/2001-2500/2215-Find-the-Difference-of-Two-Arrays/cpp-2215/main.cpp new file mode 100644 index 00000000..35434319 --- /dev/null +++ b/2001-2500/2215-Find-the-Difference-of-Two-Arrays/cpp-2215/main.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/find-the-difference-of-two-arrays/ +/// Author : liuyubobobo +/// Time : 2022-03-26 + +#include +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + vector> findDifference(vector& nums1, vector& nums2) { + + set res1; + for(int e: nums1) + if(find(nums2.begin(), nums2.end(), e) == nums2.end()) res1.insert(e); + + set res2; + for(int e: nums2) + if(find(nums1.begin(), nums1.end(), e) == nums1.end()) res2.insert(e); + + return {vector(res1.begin(), res1.end()), vector(res2.begin(), res2.end())}; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2216-Minimum-Deletions-to-Make-Array-Beautiful/cpp-2216/CMakeLists.txt b/2001-2500/2216-Minimum-Deletions-to-Make-Array-Beautiful/cpp-2216/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2216-Minimum-Deletions-to-Make-Array-Beautiful/cpp-2216/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/2216-Minimum-Deletions-to-Make-Array-Beautiful/cpp-2216/main.cpp b/2001-2500/2216-Minimum-Deletions-to-Make-Array-Beautiful/cpp-2216/main.cpp new file mode 100644 index 00000000..667df973 --- /dev/null +++ b/2001-2500/2216-Minimum-Deletions-to-Make-Array-Beautiful/cpp-2216/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/ +/// Author : liuyubobobo +/// Time : 2022-03-26 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int minDeletion(vector& nums) { + + vector v; + int res = 0; + for(int e: nums) + if(v.size() % 2 == 0) v.push_back(e); + else if(e == v.back()) res ++; + else v.push_back(e); + + if(v.size() % 2 == 1) res ++; + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/CMakeLists.txt b/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/CMakeLists.txt new file mode 100644 index 00000000..e85d4767 --- /dev/null +++ b/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) diff --git a/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/main.cpp b/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/main.cpp new file mode 100644 index 00000000..7e2d808b --- /dev/null +++ b/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + + +class Solution { + +public: + vector kthPalindrome(vector& queries, int intLength) { + + set data_set; + string s(intLength, ' '); + dfs(s, 0, intLength, data_set); + + vector data(data_set.begin(), data_set.end()); + vector res(queries.size(), -1); + for(int i = 0; i < queries.size(); i ++){ + int q = queries[i] - 1; + if(q < data.size()) res[i] = data[q]; + } + return res; + } + +private: + void dfs(string& s, int index, int len, set& data){ + + if(index > len - index - 1){ + data.insert(atoll(s.c_str())); + return; + } + + for(int d = (index == 0 ? 1 : 0); d <= 9; d ++){ + s[index] = s[len - index - 1] = (char)('0' + d); + dfs(s, index + 1, len, data); + } + } +}; + +int main() { + + vector queries1 = {97,902143467,332084633,15,139128663,347073005,167961442,32,15}; + Solution().kthPalindrome(queries1, 11); + + return 0; +} diff --git a/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/main2.cpp b/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/main2.cpp new file mode 100644 index 00000000..37def476 --- /dev/null +++ b/2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/main2.cpp @@ -0,0 +1,67 @@ +/// Source : https://leetcode.com/problems/find-palindrome-with-fixed-length/ +/// Author : liuyubobobo +/// Time : 2022-03-26 + +#include +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(intLength) for each query +/// Space Complexity: O(intLength) +class Solution { + +private: + vector pow10; + +public: + vector kthPalindrome(vector& queries, int intLength) { + + pow10 = {1ll}; + for(int i = 1; i <= 18; i ++) + pow10.push_back(pow10[i - 1] * 10ll); + + long long base = pow10[(intLength + 1) / 2 - 1]; + long long top = base * 10ll; + + vector res(queries.size(), -1); + for(int i = 0; i < queries.size(); i ++){ + int q = queries[i] - 1; + if(base + q >= top) continue; + + res[i] = get_palindrome(base + q, intLength); + } + return res; + } + +private: + long long get_palindrome(long long cur, int len){ + + if(len == 1) return cur; + + string s = to_string(cur); + if(len & 1) s.pop_back(); + reverse(s.begin(), s.end()); + long long second = atoll(s.c_str()); + return cur * pow10[s.size()] + second; + } +}; + + +void print_vec(const vector& v){ + for(long long e: v) cout << e << ' '; cout << '\n'; +} + +int main() { + + vector queries1 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,9,8}; + print_vec(Solution().kthPalindrome(queries1, 1)); + + vector queries2 = {97,902143467,332084633,15,139128663,347073005,167961442,32,15}; + print_vec(Solution().kthPalindrome(queries2, 11)); + + return 0; +} diff --git a/2001-2500/2218-Maximum-Value-of-K-Coins-From-Piles/cpp-2218/CMakeLists.txt b/2001-2500/2218-Maximum-Value-of-K-Coins-From-Piles/cpp-2218/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2218-Maximum-Value-of-K-Coins-From-Piles/cpp-2218/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/2218-Maximum-Value-of-K-Coins-From-Piles/cpp-2218/main.cpp b/2001-2500/2218-Maximum-Value-of-K-Coins-From-Piles/cpp-2218/main.cpp new file mode 100644 index 00000000..97dbba2a --- /dev/null +++ b/2001-2500/2218-Maximum-Value-of-K-Coins-From-Piles/cpp-2218/main.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/ +/// Author : liuyubobobo +/// Time : 2022-03-26 + +#include +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n * k) +/// Space Complexity: O(n * k) +class Solution { + +public: + int maxValueOfCoins(vector>& piles, int k) { + + int n = piles.size(); + vector> dp(n, vector(k + 1, INT_MIN)); + return dfs(n, piles, 0, k, dp); + } + +private: + int dfs(int n, const vector>& piles, + int index, int k, vector>& dp){ + + if(k == 0) return 0; + if(index == n) return INT_MIN / 2; + if(dp[index][k] != INT_MIN) return dp[index][k]; + + int res = dfs(n, piles, index + 1, k, dp); + int sum = 0, cnt = 0; + for(int e: piles[index]){ + sum += e; + cnt ++; + if(cnt > k) break; + res = max(res, sum + dfs(n, piles, index + 1, k - cnt, dp)); + } + return dp[index][k] = res; + } +}; + + +int main() { + + vector> piles1 = {{1, 100, 3}, {7, 8, 9}}; + cout << Solution().maxValueOfCoins(piles1, 2) << '\n'; + // 101 + + vector> piles2 = {{100}, {100}, {100}, {100}, {100}, {100}, {1,1,1,1,1,1,700}}; + cout << Solution().maxValueOfCoins(piles2, 7) << '\n'; + // 706 + + return 0; +} diff --git a/2001-2500/2219-Maximum-Sum-Score-of-Array/cpp-2219/CMakeLists.txt b/2001-2500/2219-Maximum-Sum-Score-of-Array/cpp-2219/CMakeLists.txt new file mode 100644 index 00000000..f9dfc97f --- /dev/null +++ b/2001-2500/2219-Maximum-Sum-Score-of-Array/cpp-2219/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2219) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2219 main.cpp) diff --git a/2001-2500/2219-Maximum-Sum-Score-of-Array/cpp-2219/main.cpp b/2001-2500/2219-Maximum-Sum-Score-of-Array/cpp-2219/main.cpp new file mode 100644 index 00000000..c7c0bd4a --- /dev/null +++ b/2001-2500/2219-Maximum-Sum-Score-of-Array/cpp-2219/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/maximum-sum-score-of-array/ +/// Author : liuyubobobo +/// Time : 2022-04-01 + +#include +#include + +using namespace std; + + +/// Linear Scan +/// Time Complexity: O(n) +/// Space Compelxity: O(1) +class Solution { +public: + long long maximumSumScore(vector& nums) { + + long long tail = 0; + for(int e: nums) tail += e; + + long long front = nums[0]; + long long res = max(front, tail); + for(int i = 1; i < nums.size(); i ++){ + front += nums[i]; + tail -= nums[i - 1]; + res = max(res, max(front, tail)); + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2220-Minimum-Bit-Flips-to-Convert-Number/cpp-2220/CMakeLists.txt b/2001-2500/2220-Minimum-Bit-Flips-to-Convert-Number/cpp-2220/CMakeLists.txt new file mode 100644 index 00000000..57079339 --- /dev/null +++ b/2001-2500/2220-Minimum-Bit-Flips-to-Convert-Number/cpp-2220/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2220) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2220 main.cpp) diff --git a/2001-2500/2220-Minimum-Bit-Flips-to-Convert-Number/cpp-2220/main.cpp b/2001-2500/2220-Minimum-Bit-Flips-to-Convert-Number/cpp-2220/main.cpp new file mode 100644 index 00000000..54fb7af4 --- /dev/null +++ b/2001-2500/2220-Minimum-Bit-Flips-to-Convert-Number/cpp-2220/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/minimum-bit-flips-to-convert-number/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include + +using namespace std; + + +/// Bitwise +/// Time Complexity: O(logn) +/// Space Complexity: O(1) +class Solution { +public: + int minBitFlips(int start, int goal) { + + int res = 0; + for(int b = 0; b <= 30; b ++) + res += ((start & (1 << b)) != (goal & (1 << b))); + return res; + } +}; + + +int main() { + + cout << Solution().minBitFlips(10, 7) << '\n'; + + return 0; +} diff --git a/2001-2500/2221-Find-Triangular-Sum-of-an-Array/cpp-2221/CMakeLists.txt b/2001-2500/2221-Find-Triangular-Sum-of-an-Array/cpp-2221/CMakeLists.txt new file mode 100644 index 00000000..170b4696 --- /dev/null +++ b/2001-2500/2221-Find-Triangular-Sum-of-an-Array/cpp-2221/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2221) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2221 main.cpp) diff --git a/2001-2500/2221-Find-Triangular-Sum-of-an-Array/cpp-2221/main.cpp b/2001-2500/2221-Find-Triangular-Sum-of-an-Array/cpp-2221/main.cpp new file mode 100644 index 00000000..fa56476f --- /dev/null +++ b/2001-2500/2221-Find-Triangular-Sum-of-an-Array/cpp-2221/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/find-triangular-sum-of-an-array/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int triangularSum(vector& nums) { + + vector t; + while(nums.size() > 1){ + for(int i = 1; i < nums.size(); i ++) + t.push_back((nums[i - 1] + nums[i]) % 10); + nums = t; + t.clear(); + } + return nums[0]; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2222-Number-of-Ways-to-Select-Buildings/cpp-2222/CMakeLists.txt b/2001-2500/2222-Number-of-Ways-to-Select-Buildings/cpp-2222/CMakeLists.txt new file mode 100644 index 00000000..94f43b63 --- /dev/null +++ b/2001-2500/2222-Number-of-Ways-to-Select-Buildings/cpp-2222/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2222) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2222 main.cpp) diff --git a/2001-2500/2222-Number-of-Ways-to-Select-Buildings/cpp-2222/main.cpp b/2001-2500/2222-Number-of-Ways-to-Select-Buildings/cpp-2222/main.cpp new file mode 100644 index 00000000..77cab765 --- /dev/null +++ b/2001-2500/2222-Number-of-Ways-to-Select-Buildings/cpp-2222/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/number-of-ways-to-select-buildings/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include + +using namespace std; + + +/// Memoization +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long numberOfWays(string s) { + + int n = s.size(); + vector>> dp(2, vector>(4, vector(n, -1))); + return dfs(n, s, 0, 3, 0, dp) + dfs(n, s, 1, 3, 0, dp); + } + +private: + long long dfs(int n, const string& s, int select, int left, int index, + vector>>& dp){ + + if(left == 0) return 1; + if(index == n) return 0; + if(dp[select][left][index] != -1) return dp[select][left][index]; + + long long res = dfs(n, s, select, left, index + 1, dp); + if(s[index] == (char)('0' + select)) + res += dfs(n, s, 1 - select, left - 1, index + 1, dp); + return dp[select][left][index] = res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2223-Sum-of-Scores-of-Built-Strings/cpp-2223/CMakeLists.txt b/2001-2500/2223-Sum-of-Scores-of-Built-Strings/cpp-2223/CMakeLists.txt new file mode 100644 index 00000000..19d320e4 --- /dev/null +++ b/2001-2500/2223-Sum-of-Scores-of-Built-Strings/cpp-2223/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2223) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2223 main.cpp) diff --git a/2001-2500/2223-Sum-of-Scores-of-Built-Strings/cpp-2223/main.cpp b/2001-2500/2223-Sum-of-Scores-of-Built-Strings/cpp-2223/main.cpp new file mode 100644 index 00000000..8c124251 --- /dev/null +++ b/2001-2500/2223-Sum-of-Scores-of-Built-Strings/cpp-2223/main.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/sum-of-scores-of-built-strings/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include + +using namespace std; + + +/// z-algo +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + long long sumScores(string s) { + + vector v = z_function(s); +// for(int e: v) cout << e << ' '; cout << '\n'; + + long long res = 0; + for(int e: v) res += e; + return res + (long long)s.size(); + } + +private: + vector z_function(string s) { + + int n = (int)s.length(); + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + if (i <= r && z[i - l] < r - i + 1) + z[i] = z[i - l]; + else{ + z[i] = max(0, r - i + 1); + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + } + + if (i + z[i] - 1 > r) + l = i, r = i + z[i] - 1; + } + return z; + } +}; + + +int main() { + + cout << Solution().sumScores("babab") << '\n'; + + return 0; +} diff --git a/2001-2500/2224-Minimum-Number-of-Operations-to-Convert-Time/cpp-2224/CMakeLists.txt b/2001-2500/2224-Minimum-Number-of-Operations-to-Convert-Time/cpp-2224/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2224-Minimum-Number-of-Operations-to-Convert-Time/cpp-2224/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/2224-Minimum-Number-of-Operations-to-Convert-Time/cpp-2224/main.cpp b/2001-2500/2224-Minimum-Number-of-Operations-to-Convert-Time/cpp-2224/main.cpp new file mode 100644 index 00000000..9e042700 --- /dev/null +++ b/2001-2500/2224-Minimum-Number-of-Operations-to-Convert-Time/cpp-2224/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(t) +/// Space Complexity: O(1) +class Solution { +public: + int convertTime(string current, string correct) { + + int a = get_time(current), b = get_time(correct); + int d = b - a; + + int res = 0; + while(d >= 60) d -= 60, res ++; + while(d >= 15) d -= 15, res ++; + while(d >= 5) d -= 5, res ++; + res += d; + return res; + } + +private: + int get_time(const string& t){ + + int h = atoi(t.substr(0, 2).c_str()); + int m = atoi(t.substr(3, 2).c_str()); + return h * 60 + m; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2225-Find-Players-With-Zero-or-One-Losses/cpp-2225/CMakeLists.txt b/2001-2500/2225-Find-Players-With-Zero-or-One-Losses/cpp-2225/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2225-Find-Players-With-Zero-or-One-Losses/cpp-2225/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/2225-Find-Players-With-Zero-or-One-Losses/cpp-2225/main.cpp b/2001-2500/2225-Find-Players-With-Zero-or-One-Losses/cpp-2225/main.cpp new file mode 100644 index 00000000..0d519bc8 --- /dev/null +++ b/2001-2500/2225-Find-Players-With-Zero-or-One-Losses/cpp-2225/main.cpp @@ -0,0 +1,45 @@ +/// Source : https://leetcode.com/problems/find-players-with-zero-or-one-losses/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include +#include +#include + +using namespace std; + + +/// Simulation, using map and set +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector> findWinners(vector>& matches) { + + set all_players; + + map losers; + for(const vector& match: matches){ + losers[match[1]] ++; + + all_players.insert(match[0]); + all_players.insert(match[1]); + } + + vector> res(2); + for(int player: all_players) + if(!losers.count(player)) res[0].push_back(player); + + for(const pair& p: losers) + if(p.second == 1) res[1].push_back(p.first); + + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2226-Maximum-Candies-Allocated-to-K-Children/cpp-2226/CMakeLists.txt b/2001-2500/2226-Maximum-Candies-Allocated-to-K-Children/cpp-2226/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2226-Maximum-Candies-Allocated-to-K-Children/cpp-2226/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/2226-Maximum-Candies-Allocated-to-K-Children/cpp-2226/main.cpp b/2001-2500/2226-Maximum-Candies-Allocated-to-K-Children/cpp-2226/main.cpp new file mode 100644 index 00000000..b88373e2 --- /dev/null +++ b/2001-2500/2226-Maximum-Candies-Allocated-to-K-Children/cpp-2226/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/find-players-with-zero-or-one-losses/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include +#include + +using namespace std; + + +/// Binary Search +/// Time Compelxity: O(nlog(sum)) +/// Space Complexity: O(1) +class Solution { +public: + int maximumCandies(vector& candies, long long k) { + + int n = candies.size(); + vector data(n); + for(int i = 0; i < n; i ++) data[i] = candies[i]; + + long long sum = accumulate(data.begin(), data.end(), 0ll); + + long long l = 0, r = sum / k; + while(l < r){ + long long mid = (l + r + 1) / 2; + if(ok(n, data, mid, k)) l = mid; + else r = mid - 1; + } + return l; + } + +private: + bool ok(int n, const vector& data, long long t, long long k){ + + long long num = 0; + for(long long e: data) num += e / t; + return num >= k; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/CMakeLists.txt b/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/CMakeLists.txt new file mode 100644 index 00000000..2552f642 --- /dev/null +++ b/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/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/2227-Encrypt-and-Decrypt-Strings/cpp-2227/main.cpp b/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/main.cpp new file mode 100644 index 00000000..e1c88a30 --- /dev/null +++ b/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/main.cpp @@ -0,0 +1,94 @@ +/// Source : https://leetcode.com/problems/encrypt-and-decrypt-strings/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include +#include + +using namespace std; + + +/// Using Trie +/// Time Complexity: init: O(|keys| + |values| + character_number_in_dictionary) +/// encrypt: O(|word1|) +/// decrypt: O(min(|word2|^26, all_nodes_in_trie)) +/// Space Compelxity: O(|keys| + |values| + character_number_in_dictionary) +class Encrypter { + +private: + class Node{ + + public: + vector next; + bool is_word; + + Node() : next(26, nullptr), is_word(false) {}; + }; + + vector char2index; + vector index2char; + vector index2values; + map> len2s2indexes; + Node* root; + +public: + Encrypter(vector& keys, vector& values, vector& dictionary) : + index2char(keys), char2index(256, -1), + index2values(values), root(new Node()){ + + for(int i = 0; i < keys.size(); i ++) + char2index[keys[i]] = i; + + for(int i = 0; i < values.size(); i ++) + len2s2indexes[values[i]].push_back(i); + + for(const string& s: dictionary) + insert(s); + } + + string encrypt(string word1) { + + string res = ""; + for(char c: word1) + res += index2values[char2index[c]]; + return res; + } + + int decrypt(string word2) { + return dfs(word2, 0, root); + } + +private: + int dfs(const string& s, int index, Node* node){ + + if(index == s.size()) return node->is_word; + + string len2s = s.substr(index, 2); + vector& v = len2s2indexes[len2s]; + + int res = 0; + for(int i: v) + if(i < index2char.size() && node->next[index2char[i] - 'a']) + res += dfs(s, index + 2, node->next[index2char[i] - 'a']); + return res; + } + + void insert(const string& word) { + + Node* cur = root; + for(char c: word){ + if(cur->next[c - 'a'] == nullptr) + cur->next[c - 'a'] = new Node(); + cur = cur->next[c - 'a']; + } + + cur->is_word = true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/main2.cpp b/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/main2.cpp new file mode 100644 index 00000000..792b8efb --- /dev/null +++ b/2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/main2.cpp @@ -0,0 +1,52 @@ +/// Source : https://leetcode.com/problems/encrypt-and-decrypt-strings/ +/// Author : liuyubobobo +/// Time : 2022-04-02 + +#include +#include +#include + +using namespace std; + + +/// Reverse Thinking, Store all encrypt result in dictionary +/// Time Complexity: init: O(|keys| + |values| + |dictionary|) +/// encrypt: O(|word1|) +/// decrypt: O(|word2|) +/// Space Compelxity: O(|keys| + |values| + |dictionary|) +class Encrypter { + +private: + vector char2index; + vector index2values; + map table; + +public: + Encrypter(vector& keys, vector& values, vector& dictionary) : + char2index(256, -1), index2values(values){ + + for(int i = 0; i < keys.size(); i ++) + char2index[keys[i]] = i; + + for(const string& s: dictionary) + table[encrypt(s)] ++; + } + + string encrypt(string word1) { + + string res = ""; + for(char c: word1) + res += index2values[char2index[c]]; + return res; + } + + int decrypt(string word2) { + return table[word2]; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2229-Check-if-an-Array-Is-Consecutive/cpp-2229/CMakeLists.txt b/2001-2500/2229-Check-if-an-Array-Is-Consecutive/cpp-2229/CMakeLists.txt new file mode 100644 index 00000000..4ff8d070 --- /dev/null +++ b/2001-2500/2229-Check-if-an-Array-Is-Consecutive/cpp-2229/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(cpp_2229) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(cpp_2229 main.cpp) diff --git a/2001-2500/2229-Check-if-an-Array-Is-Consecutive/cpp-2229/main.cpp b/2001-2500/2229-Check-if-an-Array-Is-Consecutive/cpp-2229/main.cpp new file mode 100644 index 00000000..dd812324 --- /dev/null +++ b/2001-2500/2229-Check-if-an-Array-Is-Consecutive/cpp-2229/main.cpp @@ -0,0 +1,30 @@ +/// Source : https://leetcode.com/problems/check-if-an-array-is-consecutive/ +/// Author : liuyubobobo +/// Time : 2022-04-08 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + bool isConsecutive(vector& nums) { + + sort(nums.begin(), nums.end()); + for(int i = 0; i < nums.size(); i ++) + if(nums[i] != nums[0] + i) return false; + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2231-Largest-Number-After-Digit-Swaps-by-Parity/cpp-2231/CMakeLists.txt b/2001-2500/2231-Largest-Number-After-Digit-Swaps-by-Parity/cpp-2231/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/2001-2500/2231-Largest-Number-After-Digit-Swaps-by-Parity/cpp-2231/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/2231-Largest-Number-After-Digit-Swaps-by-Parity/cpp-2231/main.cpp b/2001-2500/2231-Largest-Number-After-Digit-Swaps-by-Parity/cpp-2231/main.cpp new file mode 100644 index 00000000..a79ac97b --- /dev/null +++ b/2001-2500/2231-Largest-Number-After-Digit-Swaps-by-Parity/cpp-2231/main.cpp @@ -0,0 +1,48 @@ +/// Source : https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/ +/// Author : liuyubobobo +/// Time : 2022-04-10 + +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(logn * log(logn)) +/// Space Compelxity: O(logn) +class Solution { +public: + int largestInteger(int num) { + + vector even, odd; + vector is_even; + for(int i = 0; num; i ++){ + int d = num % 10; num /= 10; + if(d % 2){ + odd.push_back(d); + is_even.push_back(false); + } + else{ + even.push_back(d); + is_even.push_back(true); + } + } + + sort(even.begin(), even.end(), greater()); + sort(odd.begin(), odd.end(), greater()); + int even_p = 0, odd_p = 0, res = 0; + for(int i = is_even.size() - 1; i >= 0 ; i --){ + if(is_even[i]) res = res * 10 + even[even_p ++]; + else res = res * 10 + odd[odd_p ++]; + } + return res; + + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2232-Minimize-Result-by-Adding-Parentheses-to-Expression/cpp-2232/CMakeLists.txt b/2001-2500/2232-Minimize-Result-by-Adding-Parentheses-to-Expression/cpp-2232/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/2001-2500/2232-Minimize-Result-by-Adding-Parentheses-to-Expression/cpp-2232/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/2232-Minimize-Result-by-Adding-Parentheses-to-Expression/cpp-2232/main.cpp b/2001-2500/2232-Minimize-Result-by-Adding-Parentheses-to-Expression/cpp-2232/main.cpp new file mode 100644 index 00000000..61595415 --- /dev/null +++ b/2001-2500/2232-Minimize-Result-by-Adding-Parentheses-to-Expression/cpp-2232/main.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/ +/// Author : liuyubobobo +/// Time : 2022-04-10 + +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(|exp|^2) +/// Space Complexity: O(|exp|) +class Solution { +public: + string minimizeResult(string expression) { + + int add_pos = expression.find('+'); + string a = expression.substr(0, add_pos), b = expression.substr(add_pos + 1); + + long long best_exp_res = LONG_LONG_MAX; + string best_exp = ""; + for(int l_len = 0; l_len < a.size(); l_len ++) + for(int r_len = 1; r_len <= b.size(); r_len ++){ + long long l1 = l_len == 0 ? 1ll : atoll(a.substr(0, l_len).c_str()); + long long l2 = atoll(a.substr(l_len).c_str()); + long long r1 = atoll(b.substr(0, r_len).c_str()); + long long r2 = r_len == b.size() ? 1ll : atoll(b.substr(r_len).c_str()); + + long long tres = l1 * (l2 + r1) * r2; +// cout << l1 << ' ' << l2 << ' ' << r1 << ' ' << r2 << ' ' << tres << '\n'; + if(tres < best_exp_res){ + string exp = a.substr(0, l_len); + exp += "("; + exp += a.substr(l_len); + exp += "+"; + exp += b.substr(0, r_len); + exp += ")"; + exp += b.substr(r_len); + + best_exp = exp; + best_exp_res = tres; + } + } + return best_exp; + } +}; + + +int main() { + + cout << Solution().minimizeResult("247+38") << '\n'; + // 2(47+38) + + cout << Solution().minimizeResult("12+34") << '\n'; + // 1(2+3)4 + + cout << Solution().minimizeResult("999+999") << '\n'; + // (999+999) + + return 0; +} diff --git a/2001-2500/2233-Maximum-Product-After-K-Increments/cpp-2233/CMakeLists.txt b/2001-2500/2233-Maximum-Product-After-K-Increments/cpp-2233/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/2001-2500/2233-Maximum-Product-After-K-Increments/cpp-2233/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/2233-Maximum-Product-After-K-Increments/cpp-2233/main.cpp b/2001-2500/2233-Maximum-Product-After-K-Increments/cpp-2233/main.cpp new file mode 100644 index 00000000..9d5e6a44 --- /dev/null +++ b/2001-2500/2233-Maximum-Product-After-K-Increments/cpp-2233/main.cpp @@ -0,0 +1,42 @@ +/// Source : https://leetcode.com/problems/maximum-product-after-k-increments/ +/// Author : liuyubobobo +/// Time : 2022-04-10 + +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Time Complexity: O(klogn + nlogn) +/// Space Complexity: O(n) +class Solution { +public: + int maximumProduct(vector& nums, int k) { + + priority_queue, greater> pq; + for(int e: nums) pq.push(e); + + while(k --){ + int e = pq.top(); pq.pop(); + e ++; + pq.push(e); + } + + long long res = 1ll, MOD = 1e9 + 7; + while(!pq.empty()){ + int e = pq.top(); pq.pop(); + res *= e; + res %= MOD; + } + return res; + } +}; + + +int main() { + + return 0; +} diff --git a/2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/CMakeLists.txt b/2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/CMakeLists.txt new file mode 100644 index 00000000..9031340e --- /dev/null +++ b/2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/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/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/main.cpp b/2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/main.cpp new file mode 100644 index 00000000..67f3181b --- /dev/null +++ b/2001-2500/2234-Maximum-Total-Beauty-of-the-Gardens/cpp-2234/main.cpp @@ -0,0 +1,118 @@ +/// Source : https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/ +/// Author : liuyubobobo +/// Time : 2022-04-10 + +#include +#include +#include + +using namespace std; + + +/// Greedy +/// Time Compelxity: O(nlogn + nlogt + n) +/// Space Complexity: O(1) +class Solution { +public: + long long maximumBeauty(vector& flowers, + long long newFlowers, long long t, long long full, long long partial) { + + long long n = flowers.size(); + sort(flowers.begin(), flowers.end(), greater()); + + long long full_num = 0; + for(int i = 0; i < flowers.size(); i ++) + if(flowers[i] >= t) full_num ++; + else break; + + long long max_min_height = get_max_min_height(flowers, t - 1, newFlowers); + long long left = newFlowers; + for(int e: flowers) left -= max(max_min_height - e, 0ll); + + int index; + for(index = 0; index < n; index ++) + if(max_min_height - flowers[index] > 0) break; + // 从 index 的位置开始使用了 newFlowers + + long long res = f(full, partial, n, full_num, max_min_height); + for(int i = full_num; i < n; i ++){ + long long need2full = t - max((long long)flowers[i], max_min_height); + assert(need2full >= 0); + + index = max(index, i + 1); + if(left >= need2full){ + left -= need2full; + full_num ++; + res = max(res, f(full, partial, n, full_num, max_min_height)); + } + else{ + need2full -= left; + left = 0; + + while(need2full && index < n){ + assert(max_min_height >= flowers[index]); + long long cur = (long long)(max_min_height - flowers[index]) * (n - index); + if(need2full >= cur){ + need2full -= cur; + max_min_height = flowers[index]; + index ++; + } + else{ + long long remove_h = need2full / (n - index); + need2full -= remove_h * (n - index); + max_min_height -= remove_h; + if(need2full){ + max_min_height --; + left = n - index - need2full; + need2full = 0; + } + } + } + + if(need2full) break; + full_num ++; + res = max(res, f(full, partial, n, full_num, max_min_height)); + } + } + return res; + } + +private: + long long get_max_min_height(const vector& flowers, long long r, long long k){ + + long long l = min((long long)flowers.back(), r); + while(l < r){ + long long mid = (l + r + 1) / 2; + + long long sum = 0; + for(int e: flowers) + sum += max(mid - e, 0ll); + if(k >= sum) l = mid; + else r = mid - 1; + } + return l; + } + + long long f(long long full, long long partial, + long long n, long long full_num, long long max_min_height){ + return full * full_num + (full_num == n ? 0 : partial * max_min_height); + } +}; + + +int main() { + + vector flowers1 = {1, 3, 1, 1}; + cout << Solution().maximumBeauty(flowers1, 7, 6, 12, 1) << '\n'; + // 14 + + vector flowers2 = {2, 4, 5, 3}; + cout << Solution().maximumBeauty(flowers2, 10, 5, 2, 6) << '\n'; + // 30 + + vector flowers3 = {18, 16, 10, 10, 5}; + cout << Solution().maximumBeauty(flowers3, 10, 3, 15, 4) << '\n'; + // 75 + + return 0; +} 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/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/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/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/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/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/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/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..a3422f8f --- /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.21) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +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 new file mode 100644 index 00000000..86817c25 --- /dev/null +++ b/2001-2500/2243-Calculate-Digit-Sum-of-a-String/cpp-2243/main.cpp @@ -0,0 +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: + string digitSum(string s, int k) { + + while(s.size() > k){ + + string nexts = ""; + for(int i = 0; i < s.size(); i += k) + nexts += to_string(sum(s.substr(i, k))); + s = nexts; + } + return s; + } + +private: + int sum(const string& s){ + int res = 0; + for(int i = 0; i < s.size(); i ++) + res += (s[i] - '0'); + return res; + } +}; + + +int main() { + + return 0; +} 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/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; +} 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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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..f9de6da0 --- /dev/null +++ b/2001-2500/2290-Minimum-Obstacle-Removal-to-Reach-Corner/cpp-2290/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/ +/// Author : liuyubobobo +/// Time : 2022-05-29 + +#include +#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 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; + 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); + 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/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/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..bb71ae36 --- /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 main2.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/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; +} 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/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/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/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..73cfd5ad --- /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-18 + +#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/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/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..bb71ae36 --- /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 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 new file mode 100644 index 00000000..90682161 --- /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 - Knapback +/// 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/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; +} 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/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/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/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..0caaac65 --- /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 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 new file mode 100644 index 00000000..2fc7d43f --- /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 + Greedy +/// 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/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; +} 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/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/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/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/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/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/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/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/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/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/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/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/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..ae3cf74c --- /dev/null +++ b/2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/design-a-number-container-system/ +/// Author : liuyubobobo +/// Time : 2022-07-23 + +#include +#include +#include + +using namespace std; + + +/// Using Map +/// Time Complecity: O(logn) for every operation +/// Space Compelxity: O(n) +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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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..ff7b5e6a --- /dev/null +++ b/2001-2500/2414-Length-of-the-Longest-Alphabetical-Continuous-Substring/cpp-2414/main.cpp @@ -0,0 +1,30 @@ +/// 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) { + + 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/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/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/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/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..0de539e0 --- /dev/null +++ b/2001-2500/2418-Sort-the-People/cpp-2418/main.cpp @@ -0,0 +1,34 @@ +/// Source : https://leetcode.com/problems/sort-the-people/ +/// Author : liuyubobobo +/// Time : 2022-09-26 + +#include +#include +#include + +using namespace std; + + +/// Sorting +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +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/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..fdf64a77 --- /dev/null +++ b/2001-2500/2419-Longest-Subarray-With-Maximum-Bitwise-AND/cpp-2419/main.cpp @@ -0,0 +1,36 @@ +/// Source : https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/ +/// Author : liuyubobobo +/// Time : 2022-09-26 + +#include +#include +#include + +using namespace std; + + +/// Split +/// Time Complexity: O(n) +/// Space Complexity: O(1) +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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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; +} 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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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..92405cab --- /dev/null +++ b/2501-3000/2509-Cycle-Length-Queries-in-a-Tree/cpp-2509/main.cpp @@ -0,0 +1,59 @@ +/// Source : https://leetcode.com/problems/cycle-length-queries-in-a-tree/ +/// Author : liuyubobobo +/// Time : 2022-12-18 + +#include +#include +#include + +using namespace std; + + +/// Math +/// Time Complexity: O(qlogn) +/// Space Complexity: O(logn) +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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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..f98e2983 --- /dev/null +++ b/2501-3000/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/main.cpp @@ -0,0 +1,44 @@ +/// 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 f(n, 0); + + vector dp(n + 1, INT_MAX / 2); + dp[n] = 0; + for(int i = n - 1; i >= 0; i --){ + f.assign(n, 0); + 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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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..ec49765a --- /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 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 new file mode 100644 index 00000000..6506d0b0 --- /dev/null +++ b/2501-3000/2608-Shortest-Cycle-in-a-Graph/cpp-2608/main.cpp @@ -0,0 +1,66 @@ +/// 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); + + g[u].push_back(v), g[v].push_back(u); + } + 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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/Coding-Interviews/026/cpp-026/CMakeLists.txt b/Coding-Interviews/026/cpp-026/CMakeLists.txt new file mode 100644 index 00000000..e255bfaf --- /dev/null +++ b/Coding-Interviews/026/cpp-026/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(026) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(026 main.cpp) diff --git a/Coding-Interviews/026/cpp-026/main.cpp b/Coding-Interviews/026/cpp-026/main.cpp new file mode 100644 index 00000000..fc7f0624 --- /dev/null +++ b/Coding-Interviews/026/cpp-026/main.cpp @@ -0,0 +1,65 @@ +/// Source : https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/ +/// Author : liuyubobobo +/// Time : 2022-01-01 + +#include + +using namespace std; + + +/// DFS +/// Time Complexity: O(|A| * |B|) +/// Space Complexity: O(h) + +/// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} + TreeNode(int x, TreeNode* l, TreeNode* r): val(x), left(l), right(r){} +}; + +class Solution { +public: + bool isSubStructure(TreeNode* A, TreeNode* B) { + if (!B) return false; + return is_contain(A, B); + } + +private: + bool is_contain(TreeNode* A, TreeNode* B){ + + if (!A) return false; + + if (A->val == B->val && is_equal(A->left, B->left) && is_equal(A->right, B->right)) + return true; + + return is_contain(A->left, B) || is_contain (A->right, B); + } + + bool is_equal(TreeNode* A, TreeNode* B) { + + if(!B) return true; + if(!A) return false; + return A->val == B->val && is_equal(A->left, B->left) && is_equal(A->right, B->right); + } +}; + + +int main() { + + TreeNode* left_node1 = new TreeNode(0, new TreeNode(-4), new TreeNode(-3)); + TreeNode* right_node1 = new TreeNode(1); + TreeNode* B1 = new TreeNode(1, new TreeNode(-4), nullptr); + cout << Solution().isSubStructure(new TreeNode(1, left_node1, right_node1), B1) << endl; + // false + + TreeNode* left_node2 = new TreeNode(12, new TreeNode(8), new TreeNode(3)); + TreeNode* right_node2 = new TreeNode(6, new TreeNode(11), nullptr); + TreeNode* B2 = new TreeNode(10, new TreeNode(12, new TreeNode(8), nullptr), new TreeNode(6)); + cout << Solution().isSubStructure(new TreeNode(10, left_node2, right_node2), B2) << endl; + // true + + return 0; +} 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; +} 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; +} 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; +} 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; +} 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; +} 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; +} 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; +} 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; +} 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/LCP039/cpp-LCP039/CMakeLists.txt b/LC/LCP039/cpp-LCP039/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/LC/LCP039/cpp-LCP039/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) 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/LCP040/cpp-LCP040/CMakeLists.txt b/LC/LCP040/cpp-LCP040/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/LC/LCP040/cpp-LCP040/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) 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/LCP041/cpp-LCP041/CMakeLists.txt b/LC/LCP041/cpp-LCP041/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/LC/LCP041/cpp-LCP041/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) 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/LCP042/cpp-LCP042/CMakeLists.txt b/LC/LCP042/cpp-LCP042/CMakeLists.txt new file mode 100644 index 00000000..a0d16fe6 --- /dev/null +++ b/LC/LCP042/cpp-LCP042/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) 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/LCP044/cpp-LCP044/CMakeLists.txt b/LC/LCP044/cpp-LCP044/CMakeLists.txt new file mode 100644 index 00000000..2ae8397c --- /dev/null +++ b/LC/LCP044/cpp-LCP044/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main.cpp) 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/LCP045/cpp-LCP045/CMakeLists.txt b/LC/LCP045/cpp-LCP045/CMakeLists.txt new file mode 100644 index 00000000..3f98647f --- /dev/null +++ b/LC/LCP045/cpp-LCP045/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(B) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(B main.cpp) 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/LCP046/cpp-LCP046/CMakeLists.txt b/LC/LCP046/cpp-LCP046/CMakeLists.txt new file mode 100644 index 00000000..cad75f71 --- /dev/null +++ b/LC/LCP046/cpp-LCP046/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main.cpp) 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/LCP050/cpp-LCP050/CMakeLists.txt b/LC/LCP050/cpp-LCP050/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/LC/LCP050/cpp-LCP050/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/LCP050/cpp-LCP050/main.cpp b/LC/LCP050/cpp-LCP050/main.cpp new file mode 100644 index 00000000..e819803b --- /dev/null +++ b/LC/LCP050/cpp-LCP050/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/LC/LCP051/cpp-LCP051/CMakeLists.txt b/LC/LCP051/cpp-LCP051/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/LC/LCP051/cpp-LCP051/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/LCP051/cpp-LCP051/main.cpp b/LC/LCP051/cpp-LCP051/main.cpp new file mode 100644 index 00000000..e8156683 --- /dev/null +++ b/LC/LCP051/cpp-LCP051/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/LC/LCP052/cpp-LCP052/CMakeLists.txt b/LC/LCP052/cpp-LCP052/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/LC/LCP052/cpp-LCP052/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/LCP052/cpp-LCP052/main.cpp b/LC/LCP052/cpp-LCP052/main.cpp new file mode 100644 index 00000000..39162dce --- /dev/null +++ b/LC/LCP052/cpp-LCP052/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/LC/LCP055/cpp-LCP055/CMakeLists.txt b/LC/LCP055/cpp-LCP055/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/LC/LCP055/cpp-LCP055/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/LCP055/cpp-LCP055/main.cpp b/LC/LCP055/cpp-LCP055/main.cpp new file mode 100644 index 00000000..e78090e6 --- /dev/null +++ b/LC/LCP055/cpp-LCP055/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/LC/LCP056/cpp-LCP056/CMakeLists.txt b/LC/LCP056/cpp-LCP056/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/LC/LCP056/cpp-LCP056/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/LCP056/cpp-LCP056/main.cpp b/LC/LCP056/cpp-LCP056/main.cpp new file mode 100644 index 00000000..3cb2ed35 --- /dev/null +++ b/LC/LCP056/cpp-LCP056/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/LC/LCP057/cpp-LCP057/CMakeLists.txt b/LC/LCP057/cpp-LCP057/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/LC/LCP057/cpp-LCP057/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/LCP057/cpp-LCP057/main.cpp b/LC/LCP057/cpp-LCP057/main.cpp new file mode 100644 index 00000000..0e42445c --- /dev/null +++ b/LC/LCP057/cpp-LCP057/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/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/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/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 new file mode 100644 index 00000000..bd507912 --- /dev/null +++ b/LC/readme.md @@ -0,0 +1,72 @@ +## 力扣中文站比赛 + +| ID | Problem | Official
Solution | C++ | Java | Python | +| --- | --- | :---: | :---: | :---: | :---: | +| | | | | | | +| 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++](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/) | | | +| 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/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/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/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/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/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; +} 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/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/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/Others/2022spring-cnunionpay/A/CMakeLists.txt b/Others/2022spring-cnunionpay/A/CMakeLists.txt new file mode 100644 index 00000000..a3422f8f --- /dev/null +++ b/Others/2022spring-cnunionpay/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-cnunionpay/A/main.cpp b/Others/2022spring-cnunionpay/A/main.cpp new file mode 100644 index 00000000..d707b385 --- /dev/null +++ b/Others/2022spring-cnunionpay/A/main.cpp @@ -0,0 +1,58 @@ +/// Source : https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/D7rekZ/ +/// Author : liuyubobobo +/// Time : 2022-03-13 + +#include +#include + +using namespace std; + + +/// Greedy +/// Time Complexity: O(n) +/// Space Complexity: O(n) + +/// Definition for singly-linked list. +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +class Solution { +public: + bool isPalindrome(ListNode* head) { + + vector v; + ListNode* cur = head; + while(cur){ + v.push_back(cur->val); + cur = cur->next; + } + + int n = v.size(); + int l = 0, r = n - 1; + while(l < r && v[l] == v[r]) l ++, r --; + + if(l >= r) return true; + + return is_palindrome(v, l, r - 1) || is_palindrome(v, l + 1, r); + } + +private: + bool is_palindrome(const vector& v, int l, int r){ + + int i = l, j = r; + while(i < j){ + if(v[i] != v[j]) return false; + i ++, j --; + } + return true; + } +}; + + +int main() { + + return 0; +} diff --git a/Others/2022spring-cnunionpay/B/CMakeLists.txt b/Others/2022spring-cnunionpay/B/CMakeLists.txt new file mode 100644 index 00000000..0700646a --- /dev/null +++ b/Others/2022spring-cnunionpay/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-cnunionpay/B/main.cpp b/Others/2022spring-cnunionpay/B/main.cpp new file mode 100644 index 00000000..6183ce82 --- /dev/null +++ b/Others/2022spring-cnunionpay/B/main.cpp @@ -0,0 +1,72 @@ +/// Source : https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/kDPV0f/ +/// Author : liuyubobobo +/// Time : 2022-03-13 + +#include +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: init: O(1) +/// add: O(1) +/// remove: O(1) +/// consume: O(n) +/// Space Complexity: O(n) +class DiscountSystem { + +private: + vector> activity; // 0: priceLimit, 1: discount, 2: number, 3: userLimit + vector> user_in_act; // {useid, actid} -> freq + vector act_number; // actid -> number + int max_id = -1; + +public: + DiscountSystem() : + activity(1001, {-1, -1, -1, -1}), + user_in_act(1001, vector(1001, 0)), + act_number(1001, 0){} + + void addActivity(int actId, int priceLimit, int discount, int number, int userLimit) { + activity[actId] = {priceLimit, discount, number, userLimit}; + max_id = max(max_id, actId); + } + + void removeActivity(int actId) { + activity[actId] = {-1, -1, -1, -1}; + } + + int consume(int userId, int cost) { + + int best_act_id = -1, best_discount = -1; + for(int actid = 0; actid <= max_id; actid ++){ + if(activity[actid][0] == -1) continue; + if(cost < activity[actid][0]) continue; + if(user_in_act[userId][actid] >= activity[actid][3]) continue; + if(act_number[actid] >= activity[actid][2]) continue; + + if(activity[actid][1] > best_discount) + best_act_id = actid, best_discount = activity[actid][1]; + } + + if(best_act_id == -1) return cost; + + user_in_act[userId][best_act_id] ++; + act_number[best_act_id] ++; + return max(cost - best_discount, 0); + } +}; + + +int main() { + + DiscountSystem sys; + sys.addActivity(1, 10, 6, 3, 2); + sys.addActivity(2, 15, 8, 8, 2); + cout << sys.consume(101, 13) << '\n'; // 7 + sys.removeActivity(2); + + return 0; +} diff --git a/Others/2022spring-cnunionpay/C/CMakeLists.txt b/Others/2022spring-cnunionpay/C/CMakeLists.txt new file mode 100644 index 00000000..8accde5b --- /dev/null +++ b/Others/2022spring-cnunionpay/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-cnunionpay/C/main.cpp b/Others/2022spring-cnunionpay/C/main.cpp new file mode 100644 index 00000000..f22cdf7e --- /dev/null +++ b/Others/2022spring-cnunionpay/C/main.cpp @@ -0,0 +1,91 @@ +/// Source : https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/I4mOGz/ +/// Author : liuyubobobo +/// Time : 2022-03-13 + +#include +#include +#include +#include +#include +#include + +using namespace std; + + +/// Using PQ +/// Again, in Leetcode, unordered_map is faster than map +/// Time Complexity: O(nlogn) +/// Space Complexity: O(n) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int maxInvestment(vector& product, int limit) { + + unordered_map f; + for(int e: product) f[e] ++; + + priority_queue> pq; + for(const pair& p: f) + pq.push({p.first, p.second}); + pq.push({0, 0}); + + long long res = 0ll; + while(limit && !pq.empty()){ + + long long price = pq.top().first; + int cnt = pq.top().second; + pq.pop(); + + if(price == 0) break; + assert(!pq.empty()); + + long long next_price = pq.top().first; + assert(next_price < price); + + if(limit >= (price - next_price) * cnt){ + limit -= (price - next_price) * cnt; + res += (next_price + 1ll + price) * (price - next_price) / 2 % MOD * cnt % MOD; + res %= MOD; + + pair next = pq.top(); + pq.pop(); + next.second += cnt; + pq.push(next); + + continue; + } + else{ + if(limit / cnt){ + int next_price = price - limit / cnt + 1; + res += (next_price + price) * (price - next_price + 1) / 2 % MOD * cnt % MOD; + res %= MOD; + + price = next_price - 1; + } + if(limit % cnt){ + res += price * (limit % cnt) % MOD; + res %= MOD; + } + break; + } + } + return res % MOD; + } +}; + + +int main() { + + vector product1 = {4, 5, 3}; + cout << Solution().maxInvestment(product1, 8) << '\n'; + // 26 + + vector product2 = {2, 1, 3}; + cout << Solution().maxInvestment(product2, 20) << '\n'; + // 10 + + return 0; +} diff --git a/Others/2022spring-cnunionpay/D/CMakeLists.txt b/Others/2022spring-cnunionpay/D/CMakeLists.txt new file mode 100644 index 00000000..2552f642 --- /dev/null +++ b/Others/2022spring-cnunionpay/D/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/Others/2022spring-cnunionpay/D/main.cpp b/Others/2022spring-cnunionpay/D/main.cpp new file mode 100644 index 00000000..8a5223af --- /dev/null +++ b/Others/2022spring-cnunionpay/D/main.cpp @@ -0,0 +1,102 @@ +/// Source : https://leetcode-cn.com/contest/cnunionpay-2022spring/problems/lCh58I/ +/// Author : liuyubobobo +/// Time : 2022-03-13 + +#include +#include +#include + +using namespace std; + + +/// Using Hash and Map +/// It seems in Leetcode, unordered_map is faster than map in most cases +/// Especially when the test data is large +/// But unprdered_map is dangerouse in codeforces... +/// +/// Time Complexity: O(n * 2^4) +/// Space Complexity: O(n + max_skills^2) +class Solution { + +private: + const long long MOD = 1e9 + 7; + +public: + int coopDevelop(vector>& skills) { + + for(vector& skill: skills) + sort(skill.begin(), skill.end()); + sort(skills.begin(), skills.end(), + [](const vector& skill1, const vector& skill2){ + return skill1.size() < skill2.size(); + }); + + unordered_map f; + long long no = 0; + for(const vector& skill: skills){ + no += contains(f, skill); + f[get_hash(skill)] ++; + } + + long long n = skills.size(); + return (n * (n - 1) / 2 - no) % MOD; + } + +private: + long long contains(const unordered_map & f, + const vector& skills){ + + long long res = 0; + for(int i = 0; i < skills.size(); i ++){ + auto iter = f.find(get_hash({skills[i]})); + res += iter == f.end() ? 0 : iter->second; + } + + if(skills.size() == 1) return res; + + for(int i = 0; i < skills.size(); i ++) + for(int j = i + 1; j < skills.size(); j ++){ + auto iter = f.find(get_hash({skills[i], skills[j]})); + res += iter == f.end() ? 0 : iter->second; + } + + if(skills.size() == 2) return res; + + for(int i = 0; i < skills.size(); i ++) + for(int j = i + 1; j < skills.size(); j ++) + for(int k = j + 1; k < skills.size(); k ++){ + auto iter = f.find(get_hash({skills[i], skills[j], skills[k]})); + res += iter == f.end() ? 0 : iter->second; + } + + if(skills.size() == 3) return res; + + res += f.find(get_hash(skills)) != f.end(); + return res; + } + + long long get_hash(const vector& v){ + long long res = 0; + for(int e: v) + res = res * 1001ll + e; + return res; + } +}; + + +int main() { + + vector> skills1 = {{1, 2, 3}, {3}, {2, 4}}; + cout << Solution().coopDevelop(skills1) << '\n'; + // 2 + + vector> skills2 = {{3}, {6}}; + cout << Solution().coopDevelop(skills2) << '\n'; + // 1 + + vector> skills3 = {{2},{3,5,7},{2,3,5,6},{3,4,8},{2,6},{3,4,8},{3}}; + cout << Solution().coopDevelop(skills3) << '\n'; + // 13 + + return 0; +} diff --git a/Others/2022spring-cnunionpay/D/main2.cpp b/Others/2022spring-cnunionpay/D/main2.cpp new file mode 100644 index 00000000..ae6997c9 --- /dev/null +++ b/Others/2022spring-cnunionpay/D/main2.cpp @@ -0,0 +1,109 @@ +#include +#include +#include + +using namespace std; + + +/// Using Hash and Map +/// Precalc some hash to optimize +/// +/// It seems in Leetcode, unordered_map is faster than map in most cases +/// Especially when the test data is large +/// But unprdered_map is dangerouse in codeforces... +/// +/// Time Complexity: O(n * 2^4) +/// Space Complexity: O(n + max_skills^2) +class Solution { + +private: + const long long MOD = 1e9 + 7; + + vector h1, h4; + vector> h2; + +public: + Solution(): h1(1001), h2(1001, vector(1001)){ + + for(int i = 0; i <= 1000; i ++) h1[i] = i; + for(int i = 0; i <= 1000; i ++) + for(int j = 0; j <= 1000; j ++) h2[i][j] = i * 1001 + j; + } + + int coopDevelop(vector>& skills) { + + for(vector& skill: skills) + sort(skill.begin(), skill.end()); + sort(skills.begin(), skills.end(), + [](const vector& skill1, const vector& skill2){ + return skill1.size() < skill2.size(); + }); + + unordered_map f; + long long no = 0; + for(const vector& skill: skills){ + no += contains(f, skill); + f[get_hash(skill)] ++; + } + + long long n = skills.size(); + return (n * (n - 1) / 2 - no) % MOD; + } + +private: + long long contains(const unordered_map & f, + const vector& skills){ + + long long res = 0; + for(int i = 0; i < skills.size(); i ++){ + auto iter = f.find(h1[skills[i]]); + res += iter == f.end() ? 0 : iter->second; + } + + if(skills.size() == 1) return res; + + for(int i = 0; i < skills.size(); i ++) + for(int j = i + 1; j < skills.size(); j ++){ + auto iter = f.find(h2[skills[i]][skills[j]]); + res += iter == f.end() ? 0 : iter->second; + } + + if(skills.size() == 2) return res; + + for(int i = 0; i < skills.size(); i ++) + for(int j = i + 1; j < skills.size(); j ++) + for(int k = j + 1; k < skills.size(); k ++){ + auto iter = f.find(get_hash({skills[i], skills[j], skills[k]})); + res += iter == f.end() ? 0 : iter->second; + } + + if(skills.size() == 3) return res; + + res += f.find(get_hash(skills)) != f.end(); + return res; + } + + long long get_hash(const vector& v){ + long long res = 0; + for(int e: v) + res = res * 1001ll + e; + return res; + } +}; + +int main() { + + vector> skills1 = {{1, 2, 3}, {3}, {2, 4}}; + cout << Solution().coopDevelop(skills1) << '\n'; + // 2 + + vector> skills2 = {{3}, {6}}; + cout << Solution().coopDevelop(skills2) << '\n'; + // 1 + + vector> skills3 = {{2},{3,5,7},{2,3,5,6},{3,4,8},{2,6},{3,4,8},{3}}; + cout << Solution().coopDevelop(skills3) << '\n'; + // 13 + + return 0; +} 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 215c5a6a..a9dd2126 100644 --- a/readme.md +++ b/readme.md @@ -10,17 +10,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) 大家好,欢迎大家来到我的 **Leetcode 算法题解**代码仓。在这个代码仓中,近乎每一个问题都会使用多种方式进行解决,同时标注了简明的算法思想,时间复杂度和空间复杂度。所有问题都会使用C++进行解决,各别问题支持Java语言和Python语言。 -由于 Leetcode 如今已经问题量巨大,所以很多同学可能会刷起来毫无头绪。推荐大家可以使用 Leetcode 在2017年底推出的 [**Leetcode Explore**](https://leetcode.com/explore/),这个模块分门别类地整理了Leetcode上的问题,是一个很好的刷 Leetcode 的指引。Leetcode Explore模块上的题解,可以参考我的代码仓:[**Play Leetcode Explore**](https://github.com/liuyubobobo/Play-Leetcode-Explore) - - 如果对代码仓有任何问题,欢迎联系我:) -**个人网站**:[liuyubobobo.com](http://liuyubobobo.com) [废弃重整中...] - **电子邮件**:[liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) -**微博**: [刘宇波bobo http://weibo.com/liuyubobobo](http://weibo.com/liuyubobobo) - **知乎**: [刘宇波 http://www.zhihu.com/people/liuyubobobo](http://www.zhihu.com/people/liuyubobobo) **知乎专栏:**[是不是很酷 https://zhuanlan.zhihu.com/liuyubobobo](https://zhuanlan.zhihu.com/liuyubobobo) @@ -29,6 +22,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) ![QRCode](qrcode.png) +**知识星球(免费)** + +![zsxq](zsxq.jpg) + +
## 其他相关代码仓 @@ -42,9 +40,6 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) * [**《玩转图论算法》课程**](https://coding.imooc.com/class/370.html), 代码仓: [Play-with-Graph-Algorithms](https://github.com/liuyubobobo/Play-with-Graph-Algorithms) -* **LeetCode Explore 题解代码仓**:[Play Leetcode Explore](https://github.com/liuyubobobo/Play-Leetcode-Explore) (注:以C++实现为主) - * [Leetcode Explore](https://leetcode.com/explore/) 是 Leetcode 在2017年底上线的新模块,分门别类地整理了Leetcode上的问题。如果刷Leetcode一时不知从哪里下手,可以从Leetcode Explore为指引开始:) - * **LeetCode Database 题解代码仓**:[Play Leetcode Database](https://github.com/liuyubobobo/Play-Leetcode-Database/) @@ -57,15 +52,16 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 003 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/) | [solution](https://leetcode.com/problems/longest-substring-without-repeating-characters/solution/) | [C++](0001-0500/0003-Longest-Substring-Without-Repeating-Characters/cpp-0003/) | [Java](0001-0500/0003-Longest-Substring-Without-Repeating-Characters/java-0003/src/) | | | 004 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [solution](https://leetcode.com/problems/median-of-two-sorted-arrays/solution/) | [C++](0001-0500/0004-Median-of-Two-Sorted-Arrays/cpp-0004/) | [Java](0001-0500/0004-Median-of-Two-Sorted-Arrays/java-0004/) | | | 005 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [solution](https://leetcode.com/problems/longest-palindromic-substring/solution/) | [C++](0001-0500/0005-Longest-Palindromic-Substring/cpp-0005/) | | | -| | | | | | | +| 006 | [Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [solution](https://leetcode.com/problems/zigzag-conversion/solution/) | [C++](0001-0500/0006-Zigzag-Conversion/cpp-006/) | | | | 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/description/) | [solution](https://leetcode.com/problems/reverse-integer/solution/) | [C++](0001-0500/0007-Reverse-Integer/cpp-0007/) | | | +| 008 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [solution](https://leetcode.com/problems/string-to-integer-atoi/solution/) | [C++](0001-0500/0008-String-to-Integer/cpp-0008/) | | | | | | | | | | | 010 | [Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/) | [solution](https://leetcode.com/problems/regular-expression-matching/solution/) | [C++](0001-0500/0010-Regular-Expression-Matching/cpp-0010/) | | | | 011 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/) | [solution](https://leetcode.com/problems/container-with-most-water/solution/) | [C++](0001-0500/0011-Container-With-Most-Water/cpp-0011/) | | | | 012 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/) | [无] | [C++](0001-0500/0012-Integer-to-Roman/cpp-0012/) | | | | 013 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/) | [无] | [C++](0001-0500/0013-Roman-to-Integer/cpp-0013/) | | | | 014 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/) | [无] | [C++](0001-0500/0014-Longest-Common-Prefix/cpp-0014/) | | | -| 015 | [3Sum](https://leetcode.com/problems/3sum/description/) | [无] | [0001-0500/C++](0015-3Sum/cpp-0015/) | | | +| 015 | [3Sum](https://leetcode.com/problems/3sum/description/) | [无] | [C++](0001-0500/0015-3Sum/cpp-0015/) | | | | 016 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/description/) | [无] | [C++](0001-0500/0016-3Sum-Closest/cpp-0016/) | | | | 017 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) | [solution](https://leetcode.com/problems/letter-combinations-of-a-phone-number/solution/) | [C++](0001-0500/0017-Letter-Combinations-of-a-Phone-Number/cpp-0017/) | [Java](0001-0500/0017-Letter-Combinations-of-a-Phone-Number/java-0017/src/) | | | 018 | [4Sum](https://leetcode.com/problems/4sum/description/) | [无] | [C++](0001-0500/0018-4Sum/cpp-0018/) | | | @@ -80,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/) | | | @@ -93,6 +89,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 040 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/description/) | [无] | [C++](0001-0500/0040-Combination-Sum-II/cpp-0040/) | | | | 041 | [First Missing Positive](https://leetcode.com/problems/first-missing-positive/) | [solution](https://leetcode.com/problems/first-missing-positive/solution/) | [C++](0001-0500/0041-First-Missing-Positive/cpp-0041/) | | | | 042 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/) | [solution](https://leetcode.com/problems/trapping-rain-water/solution/) | [C++](0001-0500/0042-Trapping-Rain-Water/cpp-0042/) | | | +| 043 | [Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [solution](https://leetcode.com/problems/multiply-strings/solution/) | [C++](0001-0500/0043-Multiply-Strings/cpp-0043/) | | | | | | | | | | | 045 | [Jump Game II](https://leetcode.com/problems/jump-game-ii/) | [solution](https://leetcode.com/problems/jump-game-ii/solution/) | [C++](0001-0500/0045-Jump-Game-II/cpp-0045/) | | | | 046 | [Permutations](https://leetcode.com/problems/permutations/description/) | [solution](https://leetcode.com/problems/permutations/solution/)
[缺:排列算法整理] | [C++](0001-0500/0046-Permutations/cpp-0046/) | [Java](0001-0500/0046-Permutations/java-0046/src/) | | @@ -106,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/) | | | | | | | | | | @@ -219,12 +216,18 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 169 | [Majority Element](https://leetcode.com/problems/majority-element/description/) | [solution](https://leetcode.com/problems/majority-element/solution/) | [C++](0001-0500/0169-Majority-Element/cpp-0169/) | | [Python](0001-0500/0169-Majority-Element/py-0169/) | | 170 | [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/description/) | [无] | [C++](0001-0500/0170-Two-Sum-III-Data-structure-design/cpp-0170/) | | | | 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/description/) | [无] | [C++](0001-0500/0171-Excel-Sheet-Column-Number/cpp-0171/) | | | -| | | | | | | +| 172 | [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) | [solution](https://leetcode.com/problems/factorial-trailing-zeroes/solution/) | [C++](0001-0500/0172/Factorial-Trailing-Zeroes/cpp-0172/) | | | | 173 | [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/description/) | [无]
[缺:Morris遍历] | [C++](0001-0500/0173-Binary-Search-Tree-Iterator/cpp-0173/) | | | | 174 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) | [solution](https://leetcode.com/problems/dungeon-game/solution/) | [C++](0001-0500/0174-Dungeon-Game/cpp-0174/) | | | +| 175 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 176 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | | | | | | | | | 179 | [Largest Number](https://leetcode.com/problems/largest-number/) | [无] | [C++](0001-0500/0179-Largest-Number/cpp-0179/) | | | | | | | | | | +| 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/) | | | @@ -232,6 +235,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/) | - | - | - | - | +| 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/) | | @@ -263,7 +268,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/description/) | [solution](https://leetcode.com/problems/invert-binary-tree/solution/) | [C++](0001-0500/0226-Invert-Binary-Tree/cpp-0226/) | [Java](0001-0500/0226-Invert-Binary-Tree/java-0226/src/) | [Python](0001-0500/0226-Invert-Binary-Tree/py-0226/) | | 227 | [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/description/) | [无] | [C++](0001-0500/0227-Basic-Calculator-II/cpp-0227/) | | | | 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [solution](https://leetcode.com/problems/summary-ranges/solution/) | [C++](0001-0500/0228-Summary-Ranges/cpp-0228/) | | | -| | | | | | | +| 229 | [Majority Element II](https://leetcode.com/problems/majority-element-ii/) | [solution](https://leetcode.com/problems/majority-element-ii/solution/)
[缺:BM Voting] | [C++](0001-0500/0229-Majority-Element-II/cpp-0229/) | | | | 230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) | [solution](https://leetcode.com/problems/kth-smallest-element-in-a-bst/solution/)
[缺:非递归] | [C++](0001-0500/0230-Kth-Smallest-Element-in-a-BST/cpp-0230/) | | | | 231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [solution](https://leetcode.com/problems/power-of-two/solution/) | [C++](0001-0500/0231-Power-of-Two/cpp-0231/) | | | | 232 | [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/description/) | [solution](https://leetcode.com/problems/implement-queue-using-stacks/solution/) | [C++](0001-0500/0232-Implement-Queue-using-Stacks/cpp-0232/) | | | @@ -272,10 +277,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) | [solution](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/solution/) | [C++](0001-0500/0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/cpp-0235/) | [Java](0001-0500/0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/java-0235/src/) | | | 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) | [solution](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/)
[缺:非递归解法;LCA问题总结] | [C++](0001-0500/0236-Lowest-Common-Ancestor-of-a-Binary-Tree/cpp-0236/) | | | | 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/description/) | [solution](https://leetcode.com/problems/delete-node-in-a-linked-list/solution/) | [C++](0001-0500/0237-Delete-Node-in-a-Linked-List/cpp-0237/) | [Java](0001-0500/0237-Delete-Node-in-a-Linked-List/java-0237/src/) | | -| 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/) | | | [Python](0001-0500/0238-Product-of-Array-Except-Self/py-0238/) | +| 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/) | | | @@ -285,16 +290,16 @@ 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/) | | | | | | | | | | | 256 | [Paint House](https://leetcode.com/problems/paint-house/) | [无] | [C++](0001-0500/0256-Paint-House/cpp-0256/) | | | | 257 | [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/description/) | [solution](https://leetcode.com/problems/binary-tree-paths/solution/) | [C++](0001-0500/0257-Binary-Tree-Paths/cpp-0257/) | [Java](0001-0500/0257-Binary-Tree-Paths/java-0257/src/) | | -| | | | | | | +| 258 | [Add Digits](https://leetcode.com/problems/add-digits/) | [solution](https://leetcode.com/problems/add-digits/solution/) | [C++](0001-0500/0258-Add-Digits/cpp-0258/) | | | | 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/description/) | [solution](https://leetcode.com/problems/3sum-smaller/solution/) | [C++](0001-0500/0259-3Sum-Smaller/cpp-0259/) | | | -| | | | | | | +| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [solution](https://leetcode.com/problems/single-number-iii/solution/) | [C++](0001-0500/0260-Single-Number-III/cpp-0260/) | | | | 261 | [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/) | [C++](0001-0500/0261-Graph-Valid-Tree/cpp-0261/) | | | | | | | | | | | | 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [无] | [C++](0001-0500/0263-Ugly-Number/cpp-0263/) | | | @@ -312,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/) | | @@ -329,20 +335,22 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 296 | [Best Meeting Point](https://leetcode.com/problems/best-meeting-point/) | [solution](https://leetcode.com/problems/best-meeting-point/solution/) | [C++](0001-0500/0296-Best-Meeting-Point/cpp-0296/) | | | | 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/) | [solution](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/solution/) | [C++](0001-0500/0297-Serialize-and-Deserialize-Binary-Tree/cpp-0297/) | | | | 298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) | [solution](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/solution/) | [C++](0001-0500/0298-Binary-Tree-Longest-Consecutive-Sequence/cpp-0298/) | | | -| | | | | | | +| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [solution](https://leetcode.com/problems/bulls-and-cows/solution/) | [C++](0001-0500/0299-Bulls-and-Cows/cpp-0299/) | | | | 300 | [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/) | [solution](https://leetcode.com/problems/longest-increasing-subsequence/solution/) | [C++](0001-0500/0300-Longest-Increasing-Subsequence/cpp-0300/) | [Java](0001-0500/0300-Longest-Increasing-Subsequence/java-0300/src/) | | | 301 | [0301-Remove-Invalid-Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) | [solution](https://leetcode.com/problems/remove-invalid-parentheses/solution/)
[缺:DP+构建] | [C++](0001-0500/0301-Remove-Invalid-Parentheses/cpp-0301/) | | | | | | | | | | | 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/) | | | | 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/) | | | | 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/) | | | @@ -352,7 +360,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/) | | | | | | | | | | @@ -360,9 +368,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 329 | [Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) | [solution](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/solution/) | [C++](0001-0500/0329-Longest-Increasing-Path-in-a-Matrix/cpp-0329/) | | | | 330 | [Patching Array](https://leetcode.com/problems/patching-array/) | [无] | [C++](0001-0500/0330-Patching-Array/cpp-0330/) | | | | 331 | [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) | [solution](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/solution/) | [C++](0001-0500/0331-Verify-Preorder-Serialization-of-a-Binary-Tree/cpp-0331/) | | | +| 332 | [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | [solution](https://leetcode.com/problems/reconstruct-itinerary/solution/) | [C++](0001-0500/0332-Reconstruct-Itinerary/cpp-0332/) | | | | | | | | | | | 334 | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [solution](https://leetcode.com/problems/increasing-triplet-subsequence/solution/) | [C++](0001-0500/0334-Increasing-Triplet-Subsequence/cpp-0334/) | | | -| | | | | | | +| 335 | [Self Crossing](https://leetcode.com/problems/self-crossing/) | [无] | [C++](0001-0500/0335-Self-Crossing/cpp-0335/) | | | | 336 | [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | [solution](https://leetcode.com/problems/palindrome-pairs/solution/) | [C++](0001-0500/0336-Palindrome-Pairs/cpp-0336/)| | | | 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/description/) | [无] | [C++](0001-0500/0337-House-Robber-III/cpp-0337/) | | | | 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [solution](https://leetcode.com/problems/counting-bits/solution/) | [C++](0001-0500/0338-Counting-Bits/cpp-0338/) | | | @@ -383,21 +392,25 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 354 | [Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/) | [solution](https://leetcode.com/problems/russian-doll-envelopes/solution/) | [C++](0001-0500/0354-Russian-Doll-Envelopes/cpp-0354/) | | | | | | | | | | +| 357 | [Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/) | [无] | [C++](0001-0500/0357-Count-Numbers-with-Unique-Digits/cpp-0357/) | | | +| | | | | | | | 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/) | | | | 366 | [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) | [solution](https://leetcode.com/problems/find-leaves-of-binary-tree/solution/) | [C++](0001-0500/0366-Find-Leaves-of-Binary-Tree/cpp-0366/) | | | -| | | | | | | +| 367 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [solution](https://leetcode.com/problems/valid-perfect-square/solution/) | [C++](0001-0500/0367-Valid-Perfect-Square/cpp-0367/) | | | | 368 | [Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | [solution](https://leetcode.com/problems/largest-divisible-subset/solution/) | [C++](0001-0500/0368-Largest-Divisible-Subset/cpp-0368/) | | | | 369 | [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/) | [solution](https://leetcode.com/problems/plus-one-linked-list/solution/) | [C++](0001-0500/0369-Plus-One-Linked-List/cpp-0369/) | | | | 370 | [Range Addition](https://leetcode.com/problems/range-addition/description/) | | [C++](0001-0500/0370-Range-Addition/cpp-0370/) | | | | | | | | | | +| 372 | [Super Pow](https://leetcode.com/problems/super-pow/) | [无] | [C++](0001-0500/0372-Super-Pow/cpp-0372/) | | | | 373 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | [无] | [C++](0001-0500/0373-Find-K-Pairs-with-Smallest-Sums/cpp-0373/) | | | | 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/description/) | [solution](https://leetcode.com/problems/guess-number-higher-or-lower/solution/) | [C++](0001-0500/0374-Guess-Number-Higher-or-Lower/cpp-0374/) | | | -| | | | | | | +| 375 | [Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/) | [无] | [C++](0001-0500/0375-Guess-Number-Higher-or-Lower-II/cpp-0375/) | | | | 376 | [Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | [solution](https://leetcode.com/problems/wiggle-subsequence/solution/) | [C++](0001-0500/0376-Wiggle-Subsequence/cpp-0376/)
[缺:DP;O(1) 空间贪心] | | | | 377 | [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/description/) | [无] | [C++](0001-0500/0377-Combination-Sum-IV/cpp-0377/) | | | | 378 | [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [无] | [C++](0001-0500/0378-Kth-Smallest-Element-in-a-Sorted-Matrix/cpp-0378/) | | | @@ -407,7 +420,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/) | | | @@ -418,15 +431,18 @@ 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/) | | | -| | | | | | | +| 400 | [Nth Digit](https://leetcode.com/problems/nth-digit/) | [无] | [C++](0001-0500/0400-Nth-Digit/cpp-0400/) | | | | 401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [无] | [C++](0001-0500/0401-Binary-Watch/cpp-0401/) | | | | 402 | [Remove K Digits](https://leetcode.com/problems/remove-k-digits/) | [solution](https://leetcode.com/problems/remove-k-digits/solution/) [题解](https://leetcode-cn.com/problems/remove-k-digits/solution/yi-diao-kwei-shu-zi-by-leetcode/) | [C++](0001-0500/0402-Remove-K-Digits/cpp-0402/) | | | | 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/) | | | | | | | | | | @@ -435,103 +451,127 @@ 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/) | | | | 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/) | | | | | | | | | | | 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/) | | | -| | | | | | | -| 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/) | | | +| 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/) | | | +| 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/) | | | -| | | | | | | +| 440 | [K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/) | [无] [缺:更简单的实现] | [C++](0001-0500/0440-K-th-Smallest-in-Lexicographical-Order/cpp-0440/) | | | | 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/) | | | 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/) | | | | 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/) | | | -| | | | | | | +| 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/) | | | | 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/) | | | +| 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/) | | | | | | | | | | +| 472 | [Concatenated Words](https://leetcode.com/problems/concatenated-words/) | [无] | [C++](0001-0500/0472-Matchsticks-to-Square/cpp-0472/) | | | | 473 | [Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/description/) | [solution](https://leetcode.com/problems/matchsticks-to-square/solution/) | [C++](0001-0500/0473-Matchsticks-to-Square/cpp-0473/) | | | | 474 | [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/description/) | [solution](https://leetcode.com/problems/ones-and-zeroes/solution/) | [C++](0001-0500/0474-Ones-and-Zeroes/cpp-0474/) | | | -| | | | | | | +| 475 | [Heaters](https://leetcode.com/problems/heaters/) | [无] | [C++](0001-0500/0475-Heaters/cpp-0475/) | | | | 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/) | | | -| | | | | | | +| 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/) | | | | 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/) | | | | 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/) | | | -| | | | | | | +| 495 | [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/) | [solution](https://leetcode.com/problems/teemo-attacking/solution/) | [C++](0001-0500/0495-Teemo-Attacking/cpp-0495/) | | | | 496 | [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/) | [solution](https://leetcode.com/problems/next-greater-element-i/solution/) | [C++](0001-0500/0496-Next-Greater-Element-I/cpp-0496/) | | | | 497 | [Random Point in Non-overlapping Rectangles](https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/solution/) | [solution](https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/solution/) | [C++](0001-0500/0497-Random-Point-in-Non-overlapping-Rectangles/cpp-0497/) | | | | 498 | [Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/description/) | [无] | [C++](0001-0500/0498-Diagonal-Traverse/cpp-0498/) | | | | | | | | | | +| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row/) | [无] | [C++](0001-0500/0500-Keyboard-Row/cpp-0500/) | | | | 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/) | [无] | [C++](0501-1000/0501-Find-Mode-in-Binary-Search-Tree/cpp-0501/) | | | | 502 | [IPO](https://leetcode.com/problems/ipo/) | [solution](https://leetcode.com/problems/ipo/solution/) | [C++](0501-1000/0502-IPO/cpp-0502/) | | | | 503 | [Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/) | [solution](https://leetcode.com/problems/next-greater-element-ii/solution/) | [C++](0501-1000/0503-Next-Greater-Element-II/cpp-0503/) | | | -| | | | | | | +| 504 | [Base 7](https://leetcode.com/problems/base-7/) | [无] | [C++](0501-1000/0504-Base-7/cpp-0504/) | | | | 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/) | | | +| 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/) | | | | | | | | | | +| 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/) | | | | 519 | [Random Flip Matrix](https://leetcode.com/problems/random-flip-matrix/description/) | [solution](https://leetcode.com/problems/random-flip-matrix/solution/) | [C++](0501-1000/0519-Random-Flip-Matrix/cpp-0519/) | | | -| | | | | | | +| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital/) | [solution](https://leetcode.com/problems/detect-capital/solution/) | [C++](0501-1000/0520-Detect-Capital/cpp-0520/) | | | | 521 | [Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i/) | [solution](https://leetcode.com/problems/longest-uncommon-subsequence-i/solution/) | [C++](0501-1000/0521-Longest-Uncommon-Subsequence-I/cpp-0521/) | | | | 522 | [Longest Uncommon Subsequence II](https://leetcode.com/problems/longest-uncommon-subsequence-ii/) | [solution](https://leetcode.com/problems/longest-uncommon-subsequence-ii/solution/) | [C++](0501-1000/0522-Longest-Uncommon-Subsequence-II/cpp-0522/) | | | | 523 | [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [无] | [C++](0501-1000/0523-Continuous-Subarray-Sum/cpp-0523/) | | | | 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/) | | | 531 | [Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/) | [无] | [C++](0501-1000/0531-Lonely-Pixel-I/cpp-0531/) | | | -| | | | | | | +| 532 | [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [solution](https://leetcode.com/problems/k-diff-pairs-in-an-array/solution/) | [C++](0501-1000/0532-K-diff-Pairs-in-an-Array/cpp-0532/) | | | | 533 | [Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii/) | [无] | [C++](0501-1000/0533-Lonely-Pixel-II/cpp-0533/) | | | | | | | | | | | 536 | [Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/) | [solution](https://leetcode.com/problems/construct-binary-tree-from-string/solution/)
[缺:stack] | [C++](0501-1000/0536-Construct-Binary-Tree-from-String/cpp-0536/) | | | | 537 | [Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/) | [solution](https://leetcode.com/problems/complex-number-multiplication/solution/) | [C++](0501-1000/0537-Complex-Number-Multiplication/cpp-0537/) | | | | 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/) | [solution](https://leetcode.com/problems/convert-bst-to-greater-tree/solution/) | [C++](0501-1000/0538-Convert-BST-to-Greater-Tree/cpp-0538/) | | | -| | | | | | | +| 539 | [Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/) | [无] | [C++](0501-1000/0539-Convert-BST-to-Greater-Tree/cpp-0539/) | | | | 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [solution](https://leetcode.com/problems/single-element-in-a-sorted-array/solution/) | [C++](0501-1000/0540-Single-Element-in-a-Sorted-Array/cpp-0540/) | | | | 541 | [Reverse String II](https://leetcode.com/problems/reverse-string-ii/description/) | [solution](https://leetcode.com/problems/reverse-string-ii/solution/) | [C++](0501-1000/0541-Reverse-String-II/cpp-0541/) | | | | 542 | [01 Matrix](https://leetcode.com/problems/01-matrix/description/) | [solution](https://leetcode.com/problems/01-matrix/solution/) | [C++](0501-1000/0542-01-Matrix/cpp-0542/) | | | @@ -544,38 +584,46 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 551 | [Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i/) | [solution](https://leetcode.com/problems/student-attendance-record-i/solution/) | [C++](0501-1000/0551-Student-Attendance-Record-I/cpp-0551/) | | | | 552 | [Student Attendance Record II](https://leetcode.com/problems/student-attendance-record-ii/) | [solution](https://leetcode.com/problems/student-attendance-record-ii/solution/) | [C++](0501-1000/0552-Student-Attendance-Record-II/cpp-0552/) | | | -| | | | | | | +| 553 | [Optimal Division](https://leetcode.com/problems/optimal-division/) | [solution](https://leetcode.com/problems/optimal-division/solution/)
[缺:O(n) 解法] | [C++](0501-1000/0553-Optimal-Division/cpp-0553/) | | | | 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) | [solution](https://leetcode.com/problems/brick-wall/solution/) | [C++](0501-1000/0554-Brick-Wall/cpp-0554/) | | | | | | | | | | | 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/) | | | | | | | | | | | 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [solution](https://leetcode.com/problems/binary-tree-tilt/solution/) | [C++](0501-1000/0563-Binary-Tree-Tilt/cpp-0563/) | | | -| | | | | | | +| 564 | [Find the Closest Palindrome](https://leetcode.com/problems/find-the-closest-palindrome/) | [无] | [C++](0501-1000/0564-Find-the-Closest-Palindrome/cpp-0564/) | | | | 565 | [Array Nesting](https://leetcode.com/problems/array-nesting/) | [solution](https://leetcode.com/problems/array-nesting/solution/) | [C++](0501-1000/0565-Array-Nesting/cpp-0565/) | | | | 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/) | | | | | | | | | | | 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/) | | | +| 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/) | | | | 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/) | - | - | - | - | | | | | | | | | 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/) | | | @@ -583,17 +631,21 @@ 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/) | | | | | | | | | | | 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/) | | | | | | | | | | +| 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/) | | | @@ -601,32 +653,40 @@ 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/) | | | +| 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/) | | | | 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/) | | | | 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/) | | | +| 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/) | | | | | | | | | | +| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother/) | [无] | [C++](0501-1000/0658-Image-Smoother/cpp-0658/) | | | +| 662 | [Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/) | [solution](https://leetcode.com/problems/maximum-width-of-binary-tree/solution/) | [C++](0501-1000/0662-Maximum-Width-of-Binary-Tree/cpp-0662/) | | | +| | | | | | | | 664 | [Strange Printer](https://leetcode.com/problems/strange-printer/) | [solution](https://leetcode.com/problems/strange-printer/) | [C++](0501-1000/0664-Strange-Printer/cpp-0664/) | | | | 665 | [Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/) | [solution](https://leetcode.com/problems/non-decreasing-array/solution/) | [C++](0501-1000/0665-Non-decreasing-Array/cpp-0665/) | | | | 666 | [Path Sum IV](https://leetcode.com/problems/path-sum-iv/) | [solution](https://leetcode.com/problems/path-sum-iv/solution/) | [C++](0501-1000/0666-Path-Sum-IV/cpp-0666/) | | | | 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/) | | | @@ -636,16 +696,20 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 677 | [Map Sum Pairs](https://leetcode.com/problems/map-sum-pairs/description/) | [solution](https://leetcode.com/problems/map-sum-pairs/solution/) | [C++](0501-1000/0677-Map-Sum-Pairs/cpp-0677/) | | | | 678 | [Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/) | [solution](https://leetcode.com/problems/valid-parenthesis-string/solution/) | [C++](0501-1000/0678-Valid-Parenthesis-String/cpp-0678/) | | | | 679 | [24 Game](https://leetcode.com/problems/24-game/) | [solution](https://leetcode.com/problems/24-game/) | [C++](0501-1000/0679-24-Game/cpp-0679/) | | | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [无] | [C++](0501-1000/0680-Valid-Palindrome-II/cpp-0680/) | | | +| | | | | | | +| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [solution](https://leetcode.com/problems/baseball-game/solution/) | [C++](0501-1000/0682-Baseball-Game/cpp-0682/) | | | | | | | | | | | 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/description/) | [solution](https://leetcode.com/problems/redundant-connection/solution/) | [C++](0501-1000/0684-Redundant-Connection/cpp-0684/) | | | | 685 | [Redundant Connection II](https://leetcode.com/problems/redundant-connection-ii/description/) | [solution](https://leetcode.com/problems/redundant-connection-ii/solution/) | [C++](0501-1000/0685-Redundant-Connection-II/cpp-0685/) | | | -| | | | | | | +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) |[无] | [C++](0501-1000/0686-Repeated-String-Match/cpp-0686/) | | | | 687 | [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | [solution](https://leetcode.com/problems/longest-univalue-path/solution/) | [C++](0501-1000/0687-Longest-Univalue-Path/cpp-0687/) | | | -| | | | | | | +| 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/) | | | | 695 | [Max-Area-of-Island](https://leetcode.com/problems/max-area-of-island/description/) | | [C++](0501-1000/0695-Max-Area-of-Island/cpp-0695) | | | | 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/description/) | | [C++](0501-1000/0696-Count-Binary-Substrings/cpp-0696/) | | | @@ -671,18 +735,18 @@ 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/) | | | | 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/) | | | -| | | | | | | +| 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/) | | | @@ -695,15 +759,21 @@ 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/) | | | | | | | | | | +| 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/) | | | | | | | | | | -| 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/) | | | +| 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/) | | | | 764 | [Largest Plus Sign](https://leetcode.com/problems/largest-plus-sign/) | [C++](0501-1000/0764-Largest-Plus-Sign/cpp-0764/) | | | | | 765 | [Couples Holding Hands](https://leetcode.com/problems/couples-holding-hands/description/) | [solution](https://leetcode.com/problems/couples-holding-hands/solution/) | [C++](0501-1000/0765-Couples-Holding-Hands/cpp-0765/) | | | | 766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/description/) | [solution](https://leetcode.com/problems/toeplitz-matrix/solution/) | [C++](0501-1000/0766-Toeplitz-Matrix/cpp-0766/) | | | @@ -712,7 +782,9 @@ 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/) | | | | 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/) | | | @@ -731,7 +803,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 795 | [Number of Subarrays with Bounded Maximum](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/description/) | [solution](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/solution/) | [C++](0501-1000/0795-Number-of-Subarrays-with-Bounded-Maximum/cpp-0795/) | | | | 796 | [Rotate String](https://leetcode.com/problems/rotate-string/description/) | [solution](https://leetcode.com/problems/rotate-string/solution/)
[缺:Rolling Hash] | [C++](0501-1000/0796-Rotate-String/cpp-0796/) | | | | 797 | [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/description/) | [solution](https://leetcode.com/problems/all-paths-from-source-to-target/solution/) | [C++](0501-1000/0797-All-Paths-From-Source-to-Target/cpp-0797/) | | | -| | | | | | | +| 798 | [Smallest Rotation with Highest Score](https://leetcode.com/problems/smallest-rotation-with-highest-score/) | [solution](https://leetcode.com/problems/smallest-rotation-with-highest-score/solution/)
[缺:O(n) 解法] | [C++](0501-1000/0798-Smallest-Rotation-with-Highest-Score/cpp-0798/) | | | | 799 | [Champagne Tower](https://leetcode.com/problems/champagne-tower/description/) | [solution](https://leetcode.com/problems/champagne-tower/solution/) | [C++](0501-1000/0799-Champagne-Tower/cpp-0799/) | | | | 800 | [Similar RGB Color](https://leetcode.com/problems/similar-rgb-color/description/) | [solution](https://leetcode.com/problems/similar-rgb-color/solution/) | [C++](0501-1000/0800-Similar-RGB-Color/cpp-0800/) | | | | | | | | | | @@ -746,6 +818,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/) | | | | | | | | | | @@ -753,16 +826,22 @@ 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/) | | | | | | | | | | | 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/) | | | -| | | | | | | +| 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/) | | | | | | | | | | +| 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/) | | | @@ -772,11 +851,12 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [solution](https://leetcode.com/problems/backspace-string-compare/solution/) | [C++](0501-1000/0844-Backspace-String-Compare/cpp-0844/) | | | | | | | | | | +| 846 | [Hand of Straights](https://leetcode.com/problems/hand-of-straights/) | [无] | [C++](0501-1000/0846-Hand-of-Straights/cpp-0846/) | | | | 847 | [Shortest Path Visiting All Nodes](https://leetcode.com/problems/shortest-path-visiting-all-nodes/) | [无] | [C++](0501-1000/0847-Shortest-Path-Visiting-All-Nodes/cpp-0847/) | | | | 848 | [Shifting Letters](https://leetcode.com/problems/shifting-letters/) | [无] | [C++](0501-1000/0848-Shifting-Letters/cpp-0848/) | | | -| | | | | | | +| 849 | [Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | [无] | [C++](0501-1000/0849-Maximize-Distance-to-Closest-Person/cpp-0849/) | | | | 850 | [Rectangle Area II](https://leetcode.com/problems/rectangle-area-ii/) | [solution](https://leetcode.com/problems/rectangle-area-ii/solution/) | [C++](0501-1000/0850-Rectangle-Area-II/cpp-0850/) | | | -| | | | | | | +| 851 | [Loud and Rich](https://leetcode.com/problems/loud-and-rich/) | [solution](https://leetcode.com/problems/loud-and-rich/solution/) | [C++](0501-1000/0851-Loud-and-Rich/cpp-0851/) | | | | 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/description/) | [solution](https://leetcode.com/problems/peak-index-in-a-mountain-array/solution/) | [C++](0501-1000/0852-Peak-Index-in-a-Mountain-Array/cpp-0852/) | | | | 853 | [Car Fleet](https://leetcode.com/problems/car-fleet/description/) | [solution](https://leetcode.com/problems/car-fleet/solution/) | [C++](0501-1000/0853-Car-Fleet/cpp-0853/) | | | | 854 | [K-Similar Strings](https://leetcode.com/problems/k-similar-strings/description/) | [solution](https://leetcode.com/problems/k-similar-strings/solution/) | [C++](0501-1000/0854-K-Similar-Strings/cpp-0854/) | | | @@ -973,13 +1053,22 @@ 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/) | - | - | - | - | +| 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/) | | | | | | | | | | | 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/) | | | | | | | | | | +| 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/) | | | | | | | | | | | 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/) | | | @@ -989,8 +1078,10 @@ 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/) | | | | | | | | | | -| 1087 | [Brace Expansion](https://leetcode.com/problems/brace-expansion/) | [无] | [C++](1001-1500/1087-Brace-Expansion/cpp-1087/) | | | +| 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/) | | | @@ -1013,6 +1104,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/) | | | @@ -1034,12 +1126,14 @@ 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/) | | | | 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/) | | | @@ -1049,6 +1143,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/) | | | @@ -1063,6 +1158,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/) | | | @@ -1096,6 +1192,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/) | | | @@ -1118,7 +1215,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/) | | | @@ -1128,22 +1225,45 @@ 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/) | | | +| 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/) | | | +| | | | | | | +| 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/) | | | | 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/) | | | | 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/) | | | +| | | | | | | +| 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/) | | | +| | | | | | | | 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/) | | | @@ -1158,9 +1278,14 @@ 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/) | | | +| | | | | | | +| 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/) | | | -| | | | | | | +| 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/) | | | | 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/) | | | @@ -1327,15 +1452,31 @@ 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/) | | | +| 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/) | - | - | - | - | +| | | | | | | +| 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/) | | | | | | | | | | +| 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/) | | | | | | | | | | | 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/) | | | +| 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/) | | | @@ -1806,60 +1947,728 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 2043 | [Simple Bank System](https://leetcode.com/problems/simple-bank-system/) | [无] | [C++](2001-2500/2043-Simple-Bank-System/cpp-02043/) | | | | 2044 | [Count Number of Maximum Bitwise-OR Subsets](https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [无] | [C++](2001-2500/2044-Count-Number-of-Maximum-Bitwise-OR-Subsets/cpp-2044/) | | | | 2045 | [Second Minimum Time to Reach Destination](https://leetcode.com/problems/second-minimum-time-to-reach-destination/) | [无] | [C++](2001-2500/2045-Second-Minimum-Time-to-Reach-Destination/cpp-2045/) | | | -| 2046 | [The Time When the Network Becomes Idle](https://leetcode.com/problems/the-time-when-the-network-becomes-idle/) | [无] | [C++](2001-2500/2046-The-Time-When-the-Network-Becomes-Idle/cpp-2046/) | | | +| 2046 | [Sort Linked List Already Sorted Using Absolute Values](https://leetcode.com/problems/sort-linked-list-already-sorted-using-absolute-values/) | [无] | [C++](2001-2500/2046-Sort-Linked-List-Already-Sorted-Using-Absolute-Values/cpp-2046/) | | | +| 2047 | [Number of Valid Words in a Sentence](https://leetcode.com/problems/number-of-valid-words-in-a-sentence/) | [无] | [C++](2001-2500/2047-Number-of-Valid-Words-in-a-Sentence/cpp-2047/) | | | +| 2048 | [Next Greater Numerically Balanced Number](https://leetcode.com/problems/next-greater-numerically-balanced-number/) | [无] | [C++](2001-2500/2048-Next-Greater-Numerically-Balanced-Number/cpp-2048/) | | | +| 2049 | [Count Nodes With the Highest Score](https://leetcode.com/problems/count-nodes-with-the-highest-score/) | [无] | [C++](2001-2500/2049-Count-Nodes-With-the-Highest-Score/cpp-2049/) | | | +| 2050 | [Parallel Courses III](https://leetcode.com/problems/parallel-courses-iii/) | [无] | [C++](2001-2500/2050-Parallel-Courses-III/cpp-2050/) | | | +| 2051 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2052 | [Minimum Cost to Separate Sentence Into Rows](https://leetcode.com/problems/minimum-cost-to-separate-sentence-into-rows/) | [无] | [C++](2001-2500/2052-Minimum-Cost-to-Separate-Sentence-Into-Rows/cpp-2052/) | | | +| 2053 | [Kth Distinct String in an Array](https://leetcode.com/problems/kth-distinct-string-in-an-array/) | [无] | [C++](2001-2500/2053-Kth-Distinct-String-in-an-Array/cpp-2053/) | | | +| 2054 | [Two Best Non-Overlapping Events](https://leetcode.com/problems/two-best-non-overlapping-events/) | [无] | [C++](2001-2500/2054-Two-Best-Non-Overlapping-Events/cpp-2054/) | | | +| 2055 | [Plates Between Candles](https://leetcode.com/problems/plates-between-candles/) | [无] | [C++](2001-2500/2055-Plates-Between-Candles/cpp-2055/) | | | +| 2056 | [Number of Valid Move Combinations On Chessboard](https://leetcode.com/problems/number-of-valid-move-combinations-on-chessboard/) | [无] | [C++](2001-2500/2056-Number-of-Valid-Move-Combinations-On-Chessboard/cpp-2056/) | | | +| 2057 | [Smallest Index With Equal Value](https://leetcode.com/problems/smallest-index-with-equal-value/) | [无] | [C++](2001-2500/2057-Smallest-Index-With-Equal-Value/cpp-2057/) | | | +| 2058 | [Find the Minimum and Maximum Number of Nodes Between Critical Points](https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/) | [无] | [C++](2001-2500/2058-Find-the-Minimum-and-Maximum-Number-of-Nodes-Between-Critical-Points/cpp-2058/) | | | +| 2059 | [Minimum Operations to Convert Number](https://leetcode.com/problems/minimum-operations-to-convert-number/) | [无] | [C++](2001-2500/2059-Minimum-Operations-to-Convert-Number/cpp-2059/) | | | +| 2060 | [Check if an Original String Exists Given Two Encoded Strings](https://leetcode.com/problems/check-if-an-original-string-exists-given-two-encoded-strings/) | [无] | [C++](2001-2500/2060-Check-if-an-Original-String-Exists-Given-Two-Encoded-Strings/cpp-2060/) | | | +| 2061 | [Number of Spaces Cleaning Robot Cleaned](https://leetcode.com/problems/number-of-spaces-cleaning-robot-cleaned/) | [无] | [C++](2001-2500/2061-Number-of-Spaces-Cleaning-Robot-Cleaned/cpp-2061/) | | | +| 2062 | [Count Vowel Substrings of a String](https://leetcode.com/problems/count-vowel-substrings-of-a-string/) | [无] | [C++](2001-2500/2062-Count-Vowel-Substrings-of-a-String/cpp-2062/) | | | +| 2063 | [Vowels of All Substrings](https://leetcode.com/problems/vowels-of-all-substrings/) | [无] | [C++](2001-2500/2063-Vowels-of-All-Substrings/cpp-2063/) | | | +| 2064 | [Minimized Maximum of Products Distributed to Any Store](https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/) | [无] | [C++](2001-2500/2064-Minimized-Maximum-of-Products-Distributed-to-Any-Store/cpp-2064/) | | | +| 2065 | [Maximum Path Quality of a Graph](https://leetcode.com/problems/maximum-path-quality-of-a-graph/) | [无] | [C++](2001-2500/2065-Maximum-Path-Quality-of-a-Graph/cpp-2065/) | | | +| 2066 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2067 | [Number of Equal Count Substrings](https://leetcode.com/problems/number-of-equal-count-substrings/) | [无] | [C++](2001-2500/2067-Number-of-Equal-Count-Substrings/cpp-2067/) | | | +| 2068 | [Check Whether Two Strings are Almost Equivalent](https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent/) | [无] | [C++](2001-2500/2068-Check-Whether-Two-Strings-are-Almost-Equivalent/cpp-2068/) | | | +| 2069 | [Walking Robot Simulation II](https://leetcode.com/problems/walking-robot-simulation-ii/) | [无] | [C++](2001-2500/2069-Walking-Robot-Simulation-II/cpp-2069/) | | | +| 2070 | [Most Beautiful Item for Each Query](https://leetcode.com/problems/most-beautiful-item-for-each-query/) | [无] | [C++](2001-2500/2070-Most-Beautiful-Item-for-Each-Query/cpp-2070/) | | | +| 2071 | [Maximum Number of Tasks You Can Assign](https://leetcode.com/problems/maximum-number-of-tasks-you-can-assign/) | [无] | [C++](2001-2500/2071-Maximum-Number-of-Tasks-You-Can-Assign/cpp-2071/) | | | +| 2072 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2073 | [Time Needed to Buy Tickets](https://leetcode.com/problems/time-needed-to-buy-tickets/) | [无] | [C++](2001-2500/2073-Time-Needed-to-Buy-Tickets/cpp-2073/) | | | +| 2074 | [Reverse Nodes in Even Length Groups](https://leetcode.com/problems/reverse-nodes-in-even-length-groups/) | [无] | [C++](2001-2500/2074-Reverse-Nodes-in-Even-Length-Groups/cpp-2074/) | | | +| 2075 | [Decode the Slanted Ciphertext](https://leetcode.com/problems/decode-the-slanted-ciphertext/) | [无] | [C++](2001-2500/2075-Decode-the-Slanted-Ciphertext/cpp-2075/) | | | +| 2076 | [Process Restricted Friend Requests](https://leetcode.com/problems/process-restricted-friend-requests/) | [无] | [C++](2001-2500/2076-Process-Restricted-Friend-Requests/cpp-2076/) | | | +| 2077 | [Paths in Maze That Lead to Same Room](https://leetcode.com/problems/paths-in-maze-that-lead-to-same-room/) | [无] | [C++](2001-2500/2077-Paths-in-Maze-That-Lead-to-Same-Room/cpp-2077/) | | | +| 2078 | [Two Furthest Houses With Different Colors](https://leetcode.com/problems/two-furthest-houses-with-different-colors/) | [无] | [C++](2001-2500/2078-Two-Furthest-Houses-With-Different-Colors/cpp-2078/) | | | +| 2079 | [Watering Plants](https://leetcode.com/problems/watering-plants/) | [无] | [C++](2001-2500/2079-Watering-Plants/cpp-2079/) | | | +| 2080 | [Range Frequency Queries](https://leetcode.com/problems/range-frequency-queries/) | [无] | [C++](2001-2500/2080-Range-Frequency-Queries/cpp-2080/) | | | +| 2081 | [Sum of k-Mirror Numbers](https://leetcode.com/problems/sum-of-k-mirror-numbers/) | [无] | [C++](2001-2500/2081-Sum-of-k-Mirror-Numbers/cpp-2081/) | | | +| 2082 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2083 | [Substrings That Begin and End With the Same Letter](https://leetcode.com/problems/substrings-that-begin-and-end-with-the-same-letter/) | [无] | [C++](2001-2500/2083-Substrings-That-Begin-and-End-With-the-Same-Letter/cpp-2083/) | | | +| 2084 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2085 | [Count Common Words With One Occurrence](https://leetcode.com/problems/count-common-words-with-one-occurrence/) | [无] | [C++](2001-2500/2085-Count-Common-Words-With-One-Occurrence/cpp-2085/) | | | +| 2086 | [Minimum Number of Buckets Required to Collect Rainwater from Houses](https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/) | [无] | [C++](2001-2500/2086-Minimum-Number-of-Buckets-Required-to-Collect-Rainwater-from-Houses/cpp-2086/) | | | +| 2087 | [Minimum Cost Homecoming of a Robot in a Grid](https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/) | [无] | [C++](2001-2500/2087-Minimum-Cost-Homecoming-of-a-Robot-in-a-Grid/cpp-2087/) | | | +| 2088 | [Count Fertile Pyramids in a Land](https://leetcode.com/problems/count-fertile-pyramids-in-a-land/) | [无] | [C++](2001-2500/2088-Count-Fertile-Pyramids-in-a-Land/cpp-2088/) | | | +| 2089 | [Find Target Indices After Sorting Array](https://leetcode.com/problems/find-target-indices-after-sorting-array/) | [无] | [C++](2001-2500/2089-Find-Target-Indices-After-Sorting-Array/cpp-2089/) | | | +| 2090 | [K Radius Subarray Averages](https://leetcode.com/problems/k-radius-subarray-averages/) | [无] | [C++](2001-2500/2090-K-Radius-Subarray-Averages/cpp-2090/) | | | +| 2091 | [Removing Minimum and Maximum From Array](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/) | [无] | [C++](2001-2500/2091-Removing-Minimum-and-Maximum-From-Array/cpp-2091/) | | | +| 2092 | [Find All People With Secret](https://leetcode.com/problems/find-all-people-with-secret/) | [无] | [C++](2001-2500/2092-Find-All-People-With-Secret/cpp-2092/) | | | +| 2093 | [Minimum Cost to Reach City With Discounts](https://leetcode.com/problems/minimum-cost-to-reach-city-with-discounts/) | [无] | [C++](2001-2500/2093-Minimum-Cost-to-Reach-City-With-Discounts/cpp-2093/) | | | +| 2094 | [Finding 3-Digit Even Numbers](https://leetcode.com/problems/finding-3-digit-even-numbers/) | [无] | [C++](2001-2500/2094-Finding-3-Digit-Even-Numbers/cpp-2094/) | | | +| 2095 | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [无] | [C++](2001-2500/2095-Delete-the-Middle-Node-of-a-Linked-List/cpp-2095/) | | | +| 2096 | [Step-By-Step Directions From a Binary Tree Node to Another](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) | [无] | [C++](2001-2500/2096-Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/cpp-2096/) | | | +| 2097 | [Valid Arrangement of Pairs](https://leetcode.com/problems/valid-arrangement-of-pairs/) | [无] | [C++](2001-2500/2097-Valid-Arrangement-of-Pairs/cpp-2097/) | | | +| 2098 | [Subsequence of Size K With the Largest Even Sum](https://leetcode.com/problems/subsequence-of-size-k-with-the-largest-even-sum/) | [无] | [C++](2001-2500/2098-Subsequence-of-Size-K-With-the-Largest-Even-Sum/cpp-2098/) | | | +| 2099 | [Find Subsequence of Length K With the Largest Sum](https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/) | [无] | [C++](2001-2500/2099-Find-Subsequence-of-Length-K-With-the-Largest-Sum/cpp-2099/) | | | +| 2100 | [Find Good Days to Rob the Bank](https://leetcode.com/problems/find-good-days-to-rob-the-bank/) | [无] | [C++](2001-2500/2100-Find-Good-Days-to-Rob-the-Bank/cpp-2100/) | | | +| 2101 | [Detonate the Maximum Bombs](https://leetcode.com/problems/detonate-the-maximum-bombs/) | [无] | [C++](2001-2500/2101-Detonate-the-Maximum-Bombs/cpp-2101/) | | | +| 2102 | [Sequentially Ordinal Rank Tracker](https://leetcode.com/problems/sequentially-ordinal-rank-tracker/) | [无] | [C++](2001-2500/2102-Sequentially-Ordinal-Rank-Tracker/cpp-2102/) | | | +| 2103 | [Rings and Rods](https://leetcode.com/problems/rings-and-rods/) | [无] | [C++](2001-2500/2103-Rings-and-Rods/cpp-2103/) | | | +| 2104 | [Sum of Subarray Ranges](https://leetcode.com/problems/sum-of-subarray-ranges/) | [无] | [C++](2001-2500/2104-Sum-of-Subarray-Ranges/cpp-2104/) | | | +| 2105 | [Watering Plants II](https://leetcode.com/problems/watering-plants-ii/) | [无] | [C++](2001-2500/2105-Watering-Plants-II/cpp-2105/) | | | +| 2106 | [Maximum Fruits Harvested After at Most K Steps](https://leetcode.com/problems/maximum-fruits-harvested-after-at-most-k-steps/) | [无] | [C++](2001-2500/2106-Maximum-Fruits-Harvested-After-at-Most-K-Steps/cpp-2106/) | | | +| 2107 | [Number of Unique Flavors After Sharing K Candies](https://leetcode.com/problems/number-of-unique-flavors-after-sharing-k-candies/) | [无] | [C++](2001-2500/2107-Number-of-Unique-Flavors-After-Sharing-K-Candies/cpp-2107/) | | | +| 2108 | [Find First Palindromic String in the Array](https://leetcode.com/problems/find-first-palindromic-string-in-the-array/) | [无] | [C++](2001-2500/2108-Find-First-Palindromic-String-in-the-Array/cpp-2108/) | | | +| 2109 | [Adding Spaces to a String](https://leetcode.com/problems/adding-spaces-to-a-string/) | [无] | [C++](2001-2500/2109-Adding-Spaces-to-a-String/cpp-2109/) | | | +| 2110 | [Number of Smooth Descent Periods of a Stock](https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/) | [无] | [C++](2001-2500/2110-Number-of-Smooth-Descent-Periods-of-a-Stock/cpp-2110/) | | | +| 2111 | [Minimum Operations to Make the Array K-Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/) | [无] | [C++](2001-2500/2111-Minimum-Operations-to-Make-the-Array-K-Increasing/cpp-2111/) | | | +| 2112 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2113 | [Elements in Array After Removing and Replacing Elements](https://leetcode.com/problems/elements-in-array-after-removing-and-replacing-elements/) | [无] | [C++](2001-2500/2113-Elements-in-Array-After-Removing-and-Replacing-Elements/cpp-2113/) | | | +| 2114 | [Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/) | [无] | [C++](2001-2500/2114-Maximum-Number-of-Words-Found-in-Sentences/cpp-2114/) | | | +| 2115 | [Find All Possible Recipes from Given Supplies](https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/) | [无] | [C++](2001-2500/2115-Find-All-Possible-Recipes-from-Given-Supplies/cpp-2115/) | | | +| 2116 | [Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [无] | [C++](2001-2500/2116-Check-if-a-Parentheses-String-Can-Be-Valid/cpp-2116/) | | | +| 2117 | [Abbreviating the Product of a Range](https://leetcode.com/problems/abbreviating-the-product-of-a-range/) | [无] | [C++](2001-2500/2117-Abbreviating-the-Product-of-a-Range/cpp-2117/) | | | +| 2118 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2119 | [A Number After a Double Reversal](https://leetcode.com/problems/a-number-after-a-double-reversal/) | [无] | [C++](2001-2500/2119-A-Number-After-a-Double-Reversal/cpp-2119/) | | | +| 2120 | [Execution of All Suffix Instructions Staying in a Grid](https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/) | [无] | [C++](2001-2500/2120-Execution-of-All-Suffix-Instructions-Staying-in-a-Grid/cpp-2120/) | | | +| 2121 | [Intervals Between Identical Elements](https://leetcode.com/problems/intervals-between-identical-elements/) | [无] | [C++](2001-2500/2121-Intervals-Between-Identical-Elements/cpp-2121/) | | | +| 2122 | [Recover the Original Array](https://leetcode.com/problems/recover-the-original-array/) | [无] | [C++](2001-2500/2122-Recover-the-Original-Array/cpp-2122/) | | | +| 2123 | [Minimum Operations to Remove Adjacent Ones in Matrix](2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/) | [无] | [C++](2001-2500/2123-Minimum-Operations-to-Remove-Adjacent-Ones-in-Matrix/cpp-2123/) | | | +| 2124 | [Check if All A's Appears Before All B's](https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/) | [无] | [C++](2001-2500/2124-Check-if-All-As-Appears-Before-All-Bs/cpp-2124/) | | | +| 2125 | [Number of Laser Beams in a Bank](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/) | [无] | [C++](2001-2500/2125-Number-of-Laser-Beams-in-a-Bank/cpp-2125/) | | | +| 2126 | [Destroying Asteroids](https://leetcode.com/problems/destroying-asteroids/) | [无] | [C++](2001-2500/2126-Destroying-Asteroids/cpp-2126/) | | | +| 2127 | [Maximum Employees to Be Invited to a Meeting](https://leetcode.com/problems/maximum-employees-to-be-invited-to-a-meeting/) | [无] | [C++](2001-2500/2127-Maximum-Employees-to-Be-Invited-to-a-Meeting/cpp-2127/) | | | +| 2128 | [Remove All Ones With Row and Column Flips](https://leetcode.com/problems/remove-all-ones-with-row-and-column-flips/) | [C++](2001-2500/2128-Remove-All-Ones-With-Row-and-Column-Flips/cpp-2128/) | | | | +| 2129 | [Capitalize the Title](https://leetcode.com/problems/capitalize-the-title/) | [无] | [C++](2001-2500/2129-Capitalize-the-Title/cpp-2129/) | | | +| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [无] | [C++](2001-2500/2130-Maximum-Twin-Sum-of-a-Linked-List/cpp-2130/) | | | +| 2131 | [Longest Palindrome by Concatenating Two Letter Words](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/) | [无] | [C++](2001-2500/2131-Longest-Palindrome-by-Concatenating-Two-Letter-Words/cpp-2131/) | | | +| 2132 | [Stamping the Grid](https://leetcode.com/problems/stamping-the-grid/) | [缺:二维差分数组] | [C++](2001-2500/2132-Stamping-the-Grid/cpp-2132/) | | | +| 2133 | [Check if Every Row and Column Contains All Numbers](https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/) | [无] | [C++](2001-2500/2133-Check-if-Every-Row-and-Column-Contains-All-Numbers/cpp-2133/) | | | +| 2134 | [Minimum Swaps to Group All 1's Together II](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [无] | [C++](2001-2500/2134-Minimum-Swaps-to-Group-All-1s-Together-II/cpp-2134/) | | | +| 2135 | [Count Words Obtained After Adding a Letter](https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/) | [无] | [C++](2001-2500/2135-Count-Words-Obtained-After-Adding-a-Letter/cpp-2135/) | | | +| 2136 | [Earliest Possible Day of Full Bloom](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/) | [无] | [C++](2001-2500/2136-Earliest-Possible-Day-of-Full-Bloom/cpp-2136/) | | | +| 2137 | [Pour Water Between Buckets to Make Water Levels Equal](https://leetcode.com/problems/pour-water-between-buckets-to-make-water-levels-equal/) | [无] | [C++](2001-2500/2137-Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal/cpp-2137/) | | | +| 2138 | [Divide a String Into Groups of Size k](https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/) | [无] | [C++](2001-2500/2138-Divide-a-String-Into-Groups-of-Size-k/cpp-2138/) | | | +| 2139 | [Minimum Moves to Reach Target Score](https://leetcode.com/problems/minimum-moves-to-reach-target-score/) | [无] | [C++](2001-2500/2139-Minimum-Moves-to-Reach-Target-Score/cpp-2139/) | | | +| 2140 | [Solving Questions With Brainpower](https://leetcode.com/problems/solving-questions-with-brainpower/) | [无] | [C++](2001-2500/2140-Solving-Questions-With-Brainpower/cpp-2140/) | | | +| 2141 | [Maximum Running Time of N Computers](https://leetcode.com/problems/maximum-running-time-of-n-computers/) | [无] | [C++](2001-2500/2141-Maximum-Running-Time-of-N-Computers/cpp-2141/) | | | +| 2142 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2143 | [Choose Numbers From Two Arrays in Range](https://leetcode.com/problems/choose-numbers-from-two-arrays-in-range/) | [无] | [C++](2001-2500/2143-Choose-Numbers-From-Two-Arrays-in-Range/cpp-2143/) | | | +| 2144 | [Minimum Cost of Buying Candies With Discount](https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/) | [无] | [C++](2001-2500/2144-Minimum-Cost-of-Buying-Candies-With-Discount/cpp-2144/) | | | +| 2145 | [Count the Hidden Sequences](https://leetcode.com/problems/count-the-hidden-sequences/) | [无] | [C++](2001-2500/2145-Count-the-Hidden-Sequences/cpp-2145/) | | | +| 2146 | [K Highest Ranked Items Within a Price Range](https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/) | [无] | [C++](2001-2500/2146-K-Highest-Ranked-Items-Within-a-Price-Range/cpp-2146/) | | | +| 2147 | [Number of Ways to Divide a Long Corridor](https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/) | [无]
[缺:数学更简单的写法;dp] | [C++](2001-2500/2147-Number-of-Ways-to-Divide-a-Long-Corridor/cpp-2147/) | | | +| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/) | [无] | [C++](2001-2500/2148-Count-Elements-With-Strictly-Smaller-and-Greater-Elements/cpp-2148/) | | | +| 2149 | [Rearrange Array Elements by Sign](https://leetcode.com/problems/rearrange-array-elements-by-sign/) | [无] | [C++](2001-2500/2149-Rearrange-Array-Elements-by-Sign/cpp-2149/) | | | +| 2150 | [Find All Lonely Numbers in the Array](https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/) | [无] | [C++](2001-2500/2150-Find-All-Lonely-Numbers-in-the-Array/cpp-2150/) | | | +| 2151 | [Maximum Good People Based on Statements](https://leetcode.com/problems/maximum-good-people-based-on-statements/) | [无] | [C++](2001-2500/2151-Maximum-Good-People-Based-on-Statements/cpp-2151/) | | | +| 2152 | [Minimum Number of Lines to Cover Points](https://leetcode.com/problems/minimum-number-of-lines-to-cover-points/) | [无] | [C++](2001-2500/2152-Minimum-Number-of-Lines-to-Cover-Points/cpp-2152/) | | | +| 2153 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2154 | [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two/) | [无] | [C++](2001-2500/2154-Keep-Multiplying-Found-Values-by-Two/cpp-2154/) | | | +| 2155 | [All Divisions With the Highest Score of a Binary Array](https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/) | [无] | [C++](2001-2500/2155-All-Divisions-With-the-Highest-Score-of-a-Binary-Array/cpp-2155/) | | | +| 2156 | [Find Substring With Given Hash Value](https://leetcode.com/problems/find-substring-with-given-hash-value/) | [无] | [C++](2001-2500/2156-Find-Substring-With-Given-Hash-Value/cpp-2156/) | | | +| 2157 | [Groups of Strings](https://leetcode.com/problems/groups-of-strings/) | [无] | [C++](2001-2500/2157-Groups-of-Strings/cpp-2157/) | | | +| 2158 | [Amount of New Area Painted Each Day](https://leetcode.com/problems/amount-of-new-area-painted-each-day/) | [无] | [C++](2001-2500/2158-Amount-of-New-Area-Painted-Each-Day/cpp-2158/) | | | +| 2159 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2160 | [Minimum Sum of Four Digit Number After Splitting Digits](https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/) | [无] | [C++](2001-2500/2160-Minimum-Sum-of-Four-Digit-Number-After-Splitting-Digits/cpp-2160/) | | | +| 2161 | [Partition Array According to Given Pivot](https://leetcode.com/problems/partition-array-according-to-given-pivot/) | [无] | [C++](2001-2500/2161-Partition-Array-According-to-Given-Pivot/cpp-2161/) | | | +| 2162 | [Minimum Cost to Set Cooking Time](https://leetcode.com/problems/minimum-cost-to-set-cooking-time/) | [无] | [C++](2001-2500/2162-Minimum-Cost-to-Set-Cooking-Time/cpp-2162/) | | | +| 2163 | [Minimum Difference in Sums After Removal of Elements](https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/) | [无] | [C++](2001-2500/2163-Minimum-Difference-in-Sums-After-Removal-of-Elements/cpp-2163/) | | | +| 2164 | [Sort Even and Odd Indices Independently](https://leetcode.com/problems/sort-even-and-odd-indices-independently/) | [无] | [C++](2001-2500/2164-Sort-Even-and-Odd-Indices-Independently/cpp-2164/) | | | +| 2165 | [Smallest Value of the Rearranged Number](https://leetcode.com/problems/smallest-value-of-the-rearranged-number/) | [无] | [C++](2001-2500/2165-Smallest-Value-of-the-Rearranged-Number/cpp-2165/) | | | +| 2166 | [Design Bitset](https://leetcode.com/problems/design-bitset/) | [无] | [C++](2001-2500/2166-Design-Bitset/cpp-2166/) | | | +| 2167 | [Minimum Time to Remove All Cars Containing Illegal Goods](https://leetcode.com/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/) | [无] | [C++](2001-2500/2167-Minimum-Time-to-Remove-All-Cars-Containing-Illegal-Goods/cpp-2167/) | | | +| 2168 | [Unique Substrings With Equal Digit Frequency](https://leetcode.com/problems/unique-substrings-with-equal-digit-frequency/) | [无] | [C++](2001-2500/2168-Unique-Substrings-With-Equal-Digit-Frequency/cpp-2168/) | | | +| 2169 | [Count Operations to Obtain Zero](https://leetcode.com/problems/count-operations-to-obtain-zero/) | [无] | [C++](2001-2500/2169-Count-Operations-to-Obtain-Zero/cpp-2169/) | | | +| 2170 | [Minimum Operations to Make the Array Alternating](https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/) | [无] | [C++](2001-2500/2170-Minimum-Operations-to-Make-the-Array-Alternating/cpp-2170/) | | | +| 2171 | [Removing Minimum Number of Magic Beans](https://leetcode.com/problems/removing-minimum-number-of-magic-beans/) | [无] | [C++](2001-2500/2171-Removing-Minimum-Number-of-Magic-Beans/cpp-2171/) | | | +| 2172 | [Maximum AND Sum of Array](https://leetcode.com/problems/maximum-and-sum-of-array/) | [无] | [C++](2001-2500/2172-Maximum-AND-Sum-of-Array/cpp-2172/) | | | +| 2173 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2174 | [Remove All Ones With Row and Column Flips II](https://leetcode.com/problems/remove-all-ones-with-row-and-column-flips-ii/) | [无] | [C++](2001-2500/2174-Remove-All-Ones-With-Row-and-Column-Flips-II/cpp-2174/) | | | +| 2175 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2176 | [Count Equal and Divisible Pairs in an Array](https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/) | [无] | [C++](2001-2500/2176-Count-Equal-and-Divisible-Pairs-in-an-Array/cpp-2176/) | | | +| 2177 | [Find Three Consecutive Integers That Sum to a Given Number](https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/) | [无] | [C++](2001-2500/2177-Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/cpp-2177/) | | | +| 2178 | [Maximum Split of Positive Even Integers](https://leetcode.com/problems/maximum-split-of-positive-even-integers/) | [无] | [C++](2001-2500/2178-Maximum-Split-of-Positive-Even-Integers/cpp-2178/) | | | +| 2179 | [Count Good Triplets in an Array](https://leetcode.com/problems/count-good-triplets-in-an-array/) | [无] | [C++](2001-2500/2179-Count-Good-Triplets-in-an-Array/cpp-2179/) | | | +| 2180 | [Count Integers With Even Digit Sum](https://leetcode.com/problems/count-integers-with-even-digit-sum/) | [无] | [C++](2001-2500/2180-Count-Integers-With-Even-Digit-Sum/cpp-2180/) | | | +| 2181 | [Merge Nodes in Between Zeros](https://leetcode.com/problems/merge-nodes-in-between-zeros/) | [无] | [C++](2001-2500/2181-Merge-Nodes-in-Between-Zeros/cpp-2181/) | | | +| 2182 | [Construct String With Repeat Limit](https://leetcode.com/problems/construct-string-with-repeat-limit/) | [C++](2001-2500/2182-Construct-String-With-Repeat-Limit/cpp-2182/) | | | | +| 2183 | [Count Array Pairs Divisible by K](https://leetcode.com/problems/count-array-pairs-divisible-by-k/) | [无] | [C++](2001-2500/2183-Count-Array-Pairs-Divisible/cpp-2183/) | | | +| 2184 | [Number of Ways to Build Sturdy Brick Wall](https://leetcode.com/problems/number-of-ways-to-build-sturdy-brick-wall/) | [无] | [C++](2001-2500/2184-Number-of-Ways-to-Build-Sturdy-Brick-Wall/cpp-2184/) | | | +| 2185 | [Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | [无] | [C++](2001-2500/2185-Counting-Words-With-a-Given-Prefix/cpp-2185/) | | | +| 2186 | [Minimum Number of Steps to Make Two Strings Anagram II](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/) | [无] | [C++](2001-2500/2186-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram-II/cpp-2186/) | | | +| 2187 | [Minimum Time to Complete Trips](https://leetcode.com/problems/minimum-time-to-complete-trips/) | [无] | [C++](2001-2500/2187-Minimum-Time-to-Complete-Trips/cpp-2187/) | | | +| 2188 | [Minimum Time to Finish the Race](https://leetcode.com/problems/minimum-time-to-finish-the-race/) | [无] | [C++](2001-2500/2188-Minimum-Time-to-Finish-the-Race/cpp-2188/) | | | +| 2189 | [Number of Ways to Build House of Cards](https://leetcode.com/problems/number-of-ways-to-build-house-of-cards/) | [无] | [C++](2001-2500/2189-Number-of-Ways-to-Build-House-of-Cards/cpp-2189/) | | | +| 2190 | [Most Frequent Number Following Key In an Array](https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/) | [无] | [C++](2001-2500/2190-Most-Frequent-Number-Following-Key-In-an-Array/cpp-2190/) | | | +| 2191 | [Sort the Jumbled Numbers](https://leetcode.com/problems/sort-the-jumbled-numbers/) | [无] | [C++](2001-2500/2191-Sort-the-Jumbled-Numbers/cpp-2191/) | | | +| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph/) | [无] | [C++](2001-2500/2192-All-Ancestors-of-a-Node-in-a-Directed-Acyclic-Graph/cpp-2192/) | | | +| 2193 | [Minimum Number of Moves to Make Palindrome](https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/) | [无] | [C++](2001-2500/2193-Minimum-Number-of-Moves-to-Make-Palindrome/cpp-2193/) | | | +| 2194 | [Cells in a Range on an Excel Sheet](https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/) | [无] | [C++](2001-2500/2194-Cells-in-a-Range-on-an-Excel-Sheet/cpp-2194/) | | | +| 2195 | [Append K Integers With Minimal Sum](https://leetcode.com/problems/append-k-integers-with-minimal-sum/) | [无] | [C++](2001-2500/2195-Append-K-Integers-With-Minimal-Sum/cpp-2195/) | | | +| 2196 | [Create Binary Tree From Descriptions](https://leetcode.com/problems/create-binary-tree-from-descriptions/) | [无] | [C++](2001-2500/2196-Create-Binary-Tree-From-Descriptions/cpp-2196/) | | | +| 2197 | [Replace Non-Coprime Numbers in Array](https://leetcode.com/problems/replace-non-coprime-numbers-in-array/) | [无] | [C++](2001-2500/2197-Replace-Non-Coprime-Numbers-in-Array/cpp-2197/) | | | +| 2198 | [Number of Single Divisor Triplets](https://leetcode.com/problems/number-of-single-divisor-triplets/) | [无] | [C++](2001-2500/2198-Number-of-Single-Divisor-Triplets/cpp-2198/) | | | +| 2199 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2200 | [Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/) | [无] | [C++](2001-2500/2200-Find-All-K-Distant-Indices-in-an-Array/cpp-2200/) | | | +| 2201 | [Count Artifacts That Can Be Extracted](https://leetcode.com/problems/count-artifacts-that-can-be-extracted/) | [无] | [C++](2001-2500/2201-Count-Artifacts-That-Can-Be-Extracted/cpp-2201/) | | | +| 2202 | [Maximize the Topmost Element After K Moves](https://leetcode.com/problems/maximize-the-topmost-element-after-k-moves/) | [无] | [C++](2001-2500/2202-Maximize-the-Topmost-Element-After-K-Moves/cpp-2202/) | | | +| 2203 | [Minimum Weighted Subgraph With the Required Paths](https://leetcode.com/problems/minimum-weighted-subgraph-with-the-required-paths/) | [无] | [C++](2001-2500/2203-Minimum-Weighted-Subgraph-With-the-Required-Paths/cpp-2203/) | | | +| 2204 | [Distance to a Cycle in Undirected Graph](https://leetcode.com/problems/distance-to-a-cycle-in-undirected-graph/) | [无] | [C++](2001-2500/2204-Distance-to-a-Cycle-in-Undirected-Graph/cpp-2204/) | | | +| 2205 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2206 | [Divide Array Into Equal Pairs](https://leetcode.com/problems/divide-array-into-equal-pairs/) | [无] | [C++](2001-2500/2206-Divide-Array-Into-Equal-Pairs/cpp-2206/) | | | +| 2207 | [Maximize Number of Subsequences in a String](https://leetcode.com/problems/maximize-number-of-subsequences-in-a-string/) | [无] | [C++](2001-2500/2207-Maximize-Number-of-Subsequences-in-a-String/cpp-2207/) | | | +| 2208 | [Minimum Operations to Halve Array Sum](https://leetcode.com/problems/minimum-operations-to-halve-array-sum/) | [无] | [C++](2001-2500/2208-Minimum-Operations-to-Halve-Array-Sum/cpp-2208/) | | | +| 2209 | [Minimum White Tiles After Covering With Carpets](https://leetcode.com/problems/minimum-white-tiles-after-covering-with-carpets/) | [无] | [C++](2001-2500/2209-Minimum-White-Tiles-After-Covering-With-Carpets/cpp-2209/) | | | +| 2210 | [Count Hills and Valleys in an Array](https://leetcode.com/problems/count-hills-and-valleys-in-an-array/) | [无] | [C++](2001-2500/2210-Count-Hills-and-Valleys-in-an-Array/cpp-2210/) | | | +| 2211 | [Count Collisions on a Road](https://leetcode.com/problems/count-collisions-on-a-road/) | [无] | [C++](2001-2500/2211-Count-Collisions-on-a-Road/cpp-2211/) | | | +| 2212 | [Maximum Points in an Archery Competition](https://leetcode.com/problems/maximum-points-in-an-archery-competition/) | [无] | [C++](2001-2500/2212-Maximum-Points-in-an-Archery-Competition/cpp-2212/) | | | +| 2213 | [Longest Substring of One Repeating Character](https://leetcode.com/problems/longest-substring-of-one-repeating-character/) | [无] | [C++](2001-2500/2213-Longest-Substring-of-One-Repeating-Character/cpp-2213/) | | | +| 2214 | [Minimum Health to Beat Game](https://leetcode.com/problems/minimum-health-to-beat-game/) | [无] | [C++](2001-2500/2214-Minimum-Health-to-Beat-Game/cpp-2214/)| | | +| 2215 | [Find the Difference of Two Arrays](https://leetcode.com/problems/find-the-difference-of-two-arrays/) | [无] | [C++](2001-2500/2215-Find-the-Difference-of-Two-Arrays/cpp-2215/) | | | +| 2216 | [Minimum Deletions to Make Array Beautiful](https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/) | [无] | [C++](2001-2500/2216-Minimum-Deletions-to-Make-Array-Beautiful/cpp-2216/) | | | +| 2217 | [Find Palindrome With Fixed Length](https://leetcode.com/problems/find-palindrome-with-fixed-length/) | [无] | [C++](2001-2500/2217-Find-Palindrome-With-Fixed-Length/cpp-2217/) | | | +| 2218 | [Maximum Value of K Coins From Piles](https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/) | [无] | [C++](2001-2500/2218-Maximum-Value-of-K-Coins-From-Piles/cpp-2218/) | | | +| 2219 | [Maximum Sum Score of Array](https://leetcode.com/problems/maximum-sum-score-of-array/) | [无] | [C++](2001-2500/2219-Maximum-Sum-Score-of-Array/cpp-2219/) | | | +| 2220 | [Minimum Bit Flips to Convert Number](https://leetcode.com/problems/minimum-bit-flips-to-convert-number/) | [无] | [C++](2001-2500/2220-Minimum-Bit-Flips-to-Convert-Number/cpp-2220/) | | | +| 2221 | [Find Triangular Sum of an Array](https://leetcode.com/problems/find-triangular-sum-of-an-array/) | [无] | [C++](2001-2500/2221-Find-Triangular-Sum-of-an-Array/cpp-2221/) | | | +| 2222 | [Number of Ways to Select Buildings](https://leetcode.com/problems/number-of-ways-to-select-buildings/) | [无] | [C++](2001-2500/2222-Number-of-Ways-to-Select-Buildings/cpp-2222/) | | | +| 2223 | [Sum of Scores of Built Strings](https://leetcode.com/problems/sum-of-scores-of-built-strings/) | [无] | [C++](2001-2500/2223-Sum-of-Scores-of-Built-Strings/cpp-2223/) | | | +| 2224 | [Minimum Number of Operations to Convert Time](https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/) | [无] | [C++](2001-2500/2224-Minimum-Number-of-Operations-to-Convert-Time/cpp-2224/) | | | +| 2225 | [Find Players With Zero or One Losses](https://leetcode.com/problems/find-players-with-zero-or-one-losses/) | [无] | [C++](2001-2500/2225-Find-Players-With-Zero-or-One-Losses/cpp-2225/) | | | +| 2226 | [Maximum Candies Allocated to K Children](https://leetcode.com/problems/maximum-candies-allocated-to-k-children/) | [无] | [C++](2001-2500/2226-Maximum-Candies-Allocated-to-K-Children/cpp-2226/) | | | +| 2227 | [Encrypt and Decrypt Strings](https://leetcode.com/problems/encrypt-and-decrypt-strings/) | [无] | [C++](2001-2500/2227-Encrypt-and-Decrypt-Strings/cpp-2227/) | | | +| 2228 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2229 | [Check if an Array Is Consecutive](https://leetcode.com/problems/check-if-an-array-is-consecutive/) | [无] | [C++](2001-2500/2229-Check-if-an-Array-Is-Consecutive/cpp-2229/) | | | +| 2230 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - | +| 2231 | [Largest Number After Digit Swaps by Parity](https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/) | [无] | [C++](2001-2500/2231-Largest-Number-After-Digit-Swaps-by-Parity/cpp-2231/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| | | | | | | +| 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/) | | | +| 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++ 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/) | - | - | - | - | +| 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/) | | | +| 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/) | | | +| | | | | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| | | | | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| | | | | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | | | +| 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/) | - | - | - | - | +| 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/) | | | +| 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++](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++](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 | - | - | - | - | +| 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++](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++](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++](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++](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++](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++](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++](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++](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++](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 | - | - | - | - | +| 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++](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++](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 | - | - | - | - | +| 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++](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/) | | | | | | | | | | -## 力扣中文站比赛 - -| 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/) | | | -| | | | | | | - -## 其他 - -| ID | Problem | Official
Solution | C++ | Java | Python | -| --- | --- | :---: | :---: | :---: | :---: | -| 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/) | | | -| | | | | | | +### 力扣中文站比赛 [传送门](LC/) +### 力扣中文站其他比赛 [传送门](Others/) diff --git a/zsxq.jpg b/zsxq.jpg new file mode 100644 index 00000000..1004670d Binary files /dev/null and b/zsxq.jpg differ