From 6a0706390b6d7f0967ea8a9e293eed7265452bd7 Mon Sep 17 00:00:00 2001 From: sudo Date: Mon, 1 Nov 2021 23:01:29 +0800 Subject: [PATCH 01/14] =?UTF-8?q?LC-406.=20=E6=A0=B9=E6=8D=AE=E8=BA=AB?= =?UTF-8?q?=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 18 ++++++++++++++++++ .../solution.h" | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 "questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/main.cpp" create mode 100644 "questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/solution.h" diff --git a/README.md b/README.md index 7187e5f..56f19c3 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ [152. 乘积最大子数组](https://leetcode-cn.com/problems/maximum-product-subarray) ## 排序 +[406. 根据身高重建队列](https://leetcode-cn.com/problems/queue-reconstruction-by-height) + [148. 排序链表](https://leetcode-cn.com/problems/sort-list)「归并排序」 [5864. 游戏中弱角色的数量](https://leetcode-cn.com/problems/the-number-of-weak-characters-in-the-game) diff --git "a/questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/main.cpp" "b/questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/main.cpp" new file mode 100644 index 0000000..6b98392 --- /dev/null +++ "b/questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]\"],[\"[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + vector> result=s->reconstructQueue(arg0); + string resultabc =serializeIntegerArrArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/solution.h" "b/questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/solution.h" new file mode 100644 index 0000000..8385c80 --- /dev/null +++ "b/questions/406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227/solution.h" @@ -0,0 +1,19 @@ +// @algorithm @lc id=406 lang=cpp +// @title queue-reconstruction-by-height + + +// @test([[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]])=[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] +// @test([[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]])=[[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]] +class Solution { +public: + vector> reconstructQueue(vector>& people) { + sort(people.begin(), people.end(), + [](const vector&a, const vector&b){ + return a[0]>b[0] || a[0]==b[0]&&a[1] > que; + for(auto & p : people) + que.insert(que.begin()+p[1], p); + return que; + } +}; \ No newline at end of file -- Gitee From a1e67871bc517539556cc4d88b088c09f1b8b198 Mon Sep 17 00:00:00 2001 From: sudo Date: Mon, 1 Nov 2021 23:46:11 +0800 Subject: [PATCH 02/14] =?UTF-8?q?LC-416.=20=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 18 ++++++++++ .../solution.h" | 36 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 "questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/main.cpp" create mode 100644 "questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/solution.h" diff --git a/README.md b/README.md index 56f19c3..b77db88 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging) ## 动态规则 +[416. 分割等和子集](https://leetcode-cn.com/problems/partition-equal-subset-sum) + [322. 零钱兑换](https://leetcode-cn.com/problems/coin-change) [312. 戳气球](https://leetcode-cn.com/problems/burst-balloons) diff --git "a/questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/main.cpp" "b/questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/main.cpp" new file mode 100644 index 0000000..2105942 --- /dev/null +++ "b/questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,5,11,5]\"],[\"[1,2,3,5]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + bool result=s->canPartition(arg0); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/solution.h" "b/questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/solution.h" new file mode 100644 index 0000000..f3c2fda --- /dev/null +++ "b/questions/416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206/solution.h" @@ -0,0 +1,36 @@ +// @algorithm @lc id=416 lang=cpp +// @title partition-equal-subset-sum + + +#define print(...) +// @test([1,5,11,5])=true +// @test([1,2,3,5])=false +class Solution { +public: + bool canPartition(vector& nums) { + print("nums=", nums, "\n") + int n = nums.size(); + if(n<2) return false; + int target = accumulate(nums.begin(), nums.end(), 0); + print("sum:", target, "\n") + if(target%2) return false; + target /= 2; + int maxElem = *max_element(nums.begin(), nums.end()); + print("maxElem:", maxElem, "\n") + if(target dp(target+1, false); + dp[0] = dp[nums[0]] = true; + for(int i=1; i np(target+1, false); + np[0] = true; + int j=1; + for(; j Date: Thu, 4 Nov 2021 23:09:49 +0800 Subject: [PATCH 03/14] =?UTF-8?q?LC-407.=20=E6=8E=A5=E9=9B=A8=E6=B0=B4=20I?= =?UTF-8?q?I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + .../main.cpp" | 18 +++++++++ .../solution.h" | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 "questions/407.\346\216\245\351\233\250\346\260\264 II/main.cpp" create mode 100644 "questions/407.\346\216\245\351\233\250\346\260\264 II/solution.h" diff --git a/README.md b/README.md index b77db88..30e349f 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,8 @@ [42. 接雨水](https://leetcode-cn.com/problems/trapping-rain-water)「单调栈」 +[407. 接雨水 II](https://leetcode-cn.com/problems/trapping-rain-water-ii)「单调栈」 + [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) [20. 有效的括号](https://leetcode-cn.com/problems/valid-parentheses/) diff --git "a/questions/407.\346\216\245\351\233\250\346\260\264 II/main.cpp" "b/questions/407.\346\216\245\351\233\250\346\260\264 II/main.cpp" new file mode 100644 index 0000000..7da9071 --- /dev/null +++ "b/questions/407.\346\216\245\351\233\250\346\260\264 II/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]\"],[\"[[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]\"],[\"[[1,3,3,1,3,2],[3,2,1,3,2,3],[3,3,3,2,3,1]]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + int result=s->trapRainWater(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/407.\346\216\245\351\233\250\346\260\264 II/solution.h" "b/questions/407.\346\216\245\351\233\250\346\260\264 II/solution.h" new file mode 100644 index 0000000..57042e3 --- /dev/null +++ "b/questions/407.\346\216\245\351\233\250\346\260\264 II/solution.h" @@ -0,0 +1,40 @@ +// @algorithm @lc id=407 lang=cpp +// @title trapping-rain-water-ii + + +// @test([[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]])=4 +// @test([[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]])=10 +// @test([[1,3,3,1,3,2],[3,2,1,3,2,3],[3,3,3,2,3,1]])=4 +using pii = pair; +class Solution { +public: + int trapRainWater(vector>& heightMap) { + if(heightMap.size()<=2 || heightMap[0].size()<=2) return 0; + int m = heightMap.size(), n = heightMap[0].size(); + vector> via(m, vector(n, false)); + priority_queue, greater> water; + for(int i=0; i Date: Thu, 4 Nov 2021 23:36:43 +0800 Subject: [PATCH 04/14] =?UTF-8?q?LC-438.=20=E6=89=BE=E5=88=B0=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E4=B8=AD=E6=89=80=E6=9C=89=E5=AD=97=E6=AF=8D?= =?UTF-8?q?=E5=BC=82=E4=BD=8D=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 19 ++++++++++++++ .../solution.h" | 26 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 "questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/main.cpp" create mode 100644 "questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/solution.h" diff --git a/README.md b/README.md index 30e349f..a0a07da 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,8 @@ [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression)「双指针」 ## 滑动窗口 +[438. 找到字符串中所有字母异位词](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string)「数组key」 + [76. 最小覆盖子串](https://leetcode-cn.com/problems/minimum-window-substring) [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) diff --git "a/questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/main.cpp" "b/questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/main.cpp" new file mode 100644 index 0000000..7cb1cd2 --- /dev/null +++ "b/questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"cbaebabacd\\\"\",\"\\\"abc\\\"\"],[\"\\\"abab\\\"\",\"\\\"ab\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + string arg1 = parseString(args[1]); + vector result=s->findAnagrams(arg0,arg1); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/solution.h" "b/questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/solution.h" new file mode 100644 index 0000000..0ab67b8 --- /dev/null +++ "b/questions/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/solution.h" @@ -0,0 +1,26 @@ +// @algorithm @lc id=438 lang=cpp +// @title find-all-anagrams-in-a-string + + +// @test("cbaebabacd","abc")=[0,6] +// @test("abab","ab")=[0,1,2] +class Solution { +public: + vector findAnagrams(string s, string p) { + int n = s.length(), m = p.length(); + vector as; + if(n sc(26,0), pc(26,0); + for(int i=0; i Date: Sat, 6 Nov 2021 23:55:44 +0800 Subject: [PATCH 05/14] =?UTF-8?q?LC-494.=20=E7=9B=AE=E6=A0=87=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 19 ++++++++++++++ .../solution.h" | 26 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 "questions/494.\347\233\256\346\240\207\345\222\214/main.cpp" create mode 100644 "questions/494.\347\233\256\346\240\207\345\222\214/solution.h" diff --git a/README.md b/README.md index a0a07da..b882137 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging) ## 动态规则 +[494. 目标和](https://leetcode-cn.com/problems/target-sum) + [416. 分割等和子集](https://leetcode-cn.com/problems/partition-equal-subset-sum) [322. 零钱兑换](https://leetcode-cn.com/problems/coin-change) diff --git "a/questions/494.\347\233\256\346\240\207\345\222\214/main.cpp" "b/questions/494.\347\233\256\346\240\207\345\222\214/main.cpp" new file mode 100644 index 0000000..ad4a07a --- /dev/null +++ "b/questions/494.\347\233\256\346\240\207\345\222\214/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,1,1,1,1]\",\"3\"],[\"[1]\",\"1\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->findTargetSumWays(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/494.\347\233\256\346\240\207\345\222\214/solution.h" "b/questions/494.\347\233\256\346\240\207\345\222\214/solution.h" new file mode 100644 index 0000000..1c63b0b --- /dev/null +++ "b/questions/494.\347\233\256\346\240\207\345\222\214/solution.h" @@ -0,0 +1,26 @@ +// @algorithm @lc id=494 lang=cpp +// @title target-sum + +#define print(...) +// @test([1,1,1,1,1],3)=5 +// @test([1],1)=1 +class Solution { +public: + int findTargetSumWays(vector& nums, int target) { + print("\nnums=", nums, "\n") + int sum = accumulate(nums.begin(), nums.end(), 0); + print("sum:", sum, "\n") + int neg = sum - target; + if(neg<0 || neg%2) return 0; + neg >>= 1; + print("neg:", neg, "\n") + unordered_map dp; + dp[0] = 1; + for(auto & a : nums){ + for(int t=neg; t>=a; t--) + dp[t] += dp[t-a]; + } + print("dp:", dp, "\n") + return dp[neg]; + } +}; \ No newline at end of file -- Gitee From 6490a76dbdd3003de96e2d801dd5aa94ebe8b6f5 Mon Sep 17 00:00:00 2001 From: sudo Date: Sun, 7 Nov 2021 20:52:31 +0800 Subject: [PATCH 06/14] =?UTF-8?q?LC-538.=20=E6=8A=8A=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA=E7=B4=AF?= =?UTF-8?q?=E5=8A=A0=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 18 ++++++++++ .../solution.h" | 34 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 "questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/main.cpp" create mode 100644 "questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/solution.h" diff --git a/README.md b/README.md index b882137..039ce92 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,8 @@ [138. 复制带随机指针的链表](https://leetcode-cn.com/problems/copy-list-with-random-pointer) ## 树 +[538. 把二叉搜索树转换为累加树](https://leetcode-cn.com/problems/convert-bst-to-greater-tree) + [337. 打家劫舍 III](https://leetcode-cn.com/problems/house-robber-iii) [230. 二叉搜索树中第K小的元素](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst)「中序遍历」 diff --git "a/questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/main.cpp" "b/questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/main.cpp" new file mode 100644 index 0000000..baff39d --- /dev/null +++ "b/questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]\"],[\"[0,null,1]\"],[\"[1,0,2]\"],[\"[3,2,4,1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + TreeNode * arg0 = parseTreeNode(args[0]); + TreeNode * result=s->convertBST(arg0); + string resultabc =serializeTreeNode(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/solution.h" "b/questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/solution.h" new file mode 100644 index 0000000..5a9bb0c --- /dev/null +++ "b/questions/538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/solution.h" @@ -0,0 +1,34 @@ +// @algorithm @lc id=538 lang=cpp +// @title convert-bst-to-greater-tree + + +#define print(...) +// @test([4,1,6,0,2,5,7,null,null,null,3,null,null,null,8])=[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] +// @test([0,null,1])=[1,null,1] +// @test([1,0,2])=[3,3,2] +// @test([3,2,4,1])=[7,9,4,10] +class Solution { +public: + TreeNode* convertBST(TreeNode* root) { + function convert = [&](TreeNode *node, int sum){ + if(node==NULL) return sum; + print("val=", node->val, ", sum=", sum, "\n") + node->val += convert(node->right, sum); + print("newVal=", node->val, "\n") + return convert(node->left, node->val); + }; + convert(root, 0); + return root; + } +}; +/** + * 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) {} + * }; + */ \ No newline at end of file -- Gitee From f405d98cfd0d15acfaae872ae2b53e37389fb311 Mon Sep 17 00:00:00 2001 From: sudo Date: Sun, 7 Nov 2021 21:40:24 +0800 Subject: [PATCH 07/14] =?UTF-8?q?LC-560.=20=E5=92=8C=E4=B8=BA=20K=20?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 19 +++++++++++++++++ .../solution.h" | 21 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 "questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/main.cpp" create mode 100644 "questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/solution.h" diff --git a/README.md b/README.md index 039ce92..e3e046f 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,8 @@ [1. 两数之和](https://leetcode-cn.com/problems/two-sum/)「互补 Key」 +[560. 和为 K 的子数组](https://leetcode-cn.com/problems/subarray-sum-equals-k)「前缀和, 互补 Key」 + [49. 字母异位词分组](https://leetcode-cn.com/problems/group-anagrams/) 同 [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) ## 位运算 [136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number)「异或 b^a^b=a」 diff --git "a/questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/main.cpp" "b/questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/main.cpp" new file mode 100644 index 0000000..55abde7 --- /dev/null +++ "b/questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,1,1]\",\"2\"],[\"[1,2,3]\",\"3\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->subarraySum(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/solution.h" "b/questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/solution.h" new file mode 100644 index 0000000..726f2e6 --- /dev/null +++ "b/questions/560.\345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204/solution.h" @@ -0,0 +1,21 @@ +// @algorithm @lc id=560 lang=cpp +// @title subarray-sum-equals-k + + +// @test([1,1,1],2)=2 +// @test([1,2,3],3)=2 +class Solution { +public: + int subarraySum(vector& nums, int k) { + unordered_map sumCnt; + sumCnt[0] = 1; + int preSum = 0, ans = 0; + for(auto & i : nums){ + preSum += i; + if(sumCnt.count(preSum-k)) + ans += sumCnt[preSum-k]; + sumCnt[preSum]++; + } + return ans; + } +}; \ No newline at end of file -- Gitee From 579fbc09f88af609423f4d1729a91b9ea8ace9ff Mon Sep 17 00:00:00 2001 From: sudo Date: Mon, 8 Nov 2021 21:36:39 +0800 Subject: [PATCH 08/14] =?UTF-8?q?LC-647.=20=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++-- .../main.cpp" | 18 ++++++++++++++++++ .../solution.h" | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 "questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/main.cpp" create mode 100644 "questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/solution.h" diff --git a/README.md b/README.md index e3e046f..3ad8aab 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,6 @@ [600. 不含连续1的非负整数](https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones) -[5. 最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/) - [10. 正则表达式匹配](https://leetcode-cn.com/problems/regular-expression-matching/) [44. 通配符匹配](https://leetcode-cn.com/problems/wildcard-matching) @@ -181,6 +179,10 @@ [5851. 找出不同的二进制字符串](https://leetcode-cn.com/problems/find-unique-binary-string) ## 字符串 +[647. 回文子串](https://leetcode-cn.com/problems/palindromic-substrings)「中心扩展」 + +[5. 最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/)「DP」 + [6. Z 字形变换](https://leetcode-cn.com/problems/zigzag-conversion/) [8. 字符串转换整数 (atoi)](https://leetcode-cn.com/problems/string-to-integer-atoi/) diff --git "a/questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/main.cpp" "b/questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/main.cpp" new file mode 100644 index 0000000..340b095 --- /dev/null +++ "b/questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"abc\\\"\"],[\"\\\"aaa\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + int result=s->countSubstrings(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/solution.h" "b/questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/solution.h" new file mode 100644 index 0000000..da033f5 --- /dev/null +++ "b/questions/647.\345\233\236\346\226\207\345\255\220\344\270\262/solution.h" @@ -0,0 +1,18 @@ +// @algorithm @lc id=647 lang=cpp +// @title palindromic-substrings + + +// @test("abc")=3 +// @test("aaa")=6 +class Solution { +public: + int countSubstrings(string s) { + int n = s.length(), m = 0; + for(int i=0; i<=2*n-2; i++){ + int L = i/2, R = L+i%2; + while(0<=L && R Date: Mon, 8 Nov 2021 23:18:15 +0800 Subject: [PATCH 09/14] =?UTF-8?q?LC-621.=20=E4=BB=BB=E5=8A=A1=E8=B0=83?= =?UTF-8?q?=E5=BA=A6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 21 +++++++++++++++++++ .../solution.h" | 16 ++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 "questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/main.cpp" create mode 100644 "questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/solution.h" diff --git a/README.md b/README.md index 3ad8aab..9684adf 100644 --- a/README.md +++ b/README.md @@ -375,6 +375,8 @@ ## 几何 [223. 矩形面积](https://leetcode-cn.com/problems/rectangle-area) ## 模拟 +[621. 任务调度器](https://leetcode-cn.com/problems/task-scheduler) + [54. 螺旋矩阵](https://leetcode-cn.com/problems/spiral-matrix) [59. 螺旋矩阵 II](https://leetcode-cn.com/problems/spiral-matrix-ii) diff --git "a/questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/main.cpp" "b/questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/main.cpp" new file mode 100644 index 0000000..8c8a1ea --- /dev/null +++ "b/questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/main.cpp" @@ -0,0 +1,21 @@ +#include "stl.h" +#include "solution.h" + +int main(int argc, char *argv[]){ + struct{ + vector tasks; + int n; + }tests[]={ + { {'A','A','A','B','B','B'}, 2 }, // 8 + { {'A','A','A','B','B','B'}, 0 }, // 6 + { {'A','A','A','A','A','A','B','C','D','E','F','G'}, 2 }, // 16 + }; + Solution sln; + for(auto & ts : tests){ + print("\ntasks=", ts.tasks, "\nn=", ts.n, "\n") + print("ans:", sln.leastInterval(ts.tasks, ts.n), "\n") + } + + print("\n------ main exit ------\n") + return 0; +} diff --git "a/questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/solution.h" "b/questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/solution.h" new file mode 100644 index 0000000..4bea20e --- /dev/null +++ "b/questions/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250/solution.h" @@ -0,0 +1,16 @@ +// @algorithm @lc id=621 lang=cpp +// @title task-scheduler + +class Solution { +public: + int leastInterval(vector& tasks, int n) { + unordered_map execTimes; + for(auto & t : tasks) + execTimes[t]++; + int maxTimes = max_element(execTimes.begin(), execTimes.end(), + [](const auto&a, const auto&b){return a.secondsecond; + int maxCount = accumulate(execTimes.begin(), execTimes.end(), 0, + [=](int acc, const auto&a){ return acc + (a.second==maxTimes);}); + return max( (int)tasks.size(), (maxTimes-1)*(n+1)+maxCount ); + } +}; \ No newline at end of file -- Gitee From 6dbe4c286e51cf43d3722094c30967e3095397e9 Mon Sep 17 00:00:00 2001 From: sudo Date: Thu, 11 Nov 2021 22:10:12 +0800 Subject: [PATCH 10/14] =?UTF-8?q?LC-629.=20K=E4=B8=AA=E9=80=86=E5=BA=8F?= =?UTF-8?q?=E5=AF=B9=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 19 +++++++++++++++ .../solution.h" | 24 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 "questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/main.cpp" create mode 100644 "questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/solution.h" diff --git a/README.md b/README.md index 9684adf..871cfdb 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging) ## 动态规则 +[629. K个逆序对数组](https://leetcode-cn.com/problems/k-inverse-pairs-array) + [494. 目标和](https://leetcode-cn.com/problems/target-sum) [416. 分割等和子集](https://leetcode-cn.com/problems/partition-equal-subset-sum) diff --git "a/questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/main.cpp" "b/questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/main.cpp" new file mode 100644 index 0000000..4b62661 --- /dev/null +++ "b/questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"3\",\"0\"],[\"3\",\"1\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->kInversePairs(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/solution.h" "b/questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/solution.h" new file mode 100644 index 0000000..9c521ea --- /dev/null +++ "b/questions/629.K\344\270\252\351\200\206\345\272\217\345\257\271\346\225\260\347\273\204/solution.h" @@ -0,0 +1,24 @@ +// @algorithm @lc id=629 lang=cpp +// @title k-inverse-pairs-array + + +// @test(3,0)=1 +// @test(3,1)=2 +#define mod 1000'000'007 +class Solution { +public: + int kInversePairs(int n, int k) { + vector> dp(2, vector(k+1, 0)); + dp[0][0] = 1; + for(int i=1; i<=n; i++){ + for(int j=0; j<=k; j++){ + auto & cur = dp[i&1]; + auto & pre = dp[1-(i&1)]; + cur[j] = (j<1?0:cur[j-1]) - (j= mod) cur[j] -= mod; + else if(cur[j]<0) cur[j] += mod; + } + } + return dp[n&1][k]; + } +}; \ No newline at end of file -- Gitee From 050d6cf235993e759756f907f0c83d7615810801 Mon Sep 17 00:00:00 2001 From: sudo Date: Mon, 15 Nov 2021 23:03:41 +0800 Subject: [PATCH 11/14] =?UTF-8?q?LC-739.=20=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 19 ++++++++++++++++ .../solution.h" | 22 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 "questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/main.cpp" create mode 100644 "questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/solution.h" diff --git a/README.md b/README.md index 871cfdb..f695dae 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,8 @@ [218. 天际线问题](https://leetcode-cn.com/problems/the-skyline-problem/)「大顶堆」 ## 栈 +[739. 每日温度](https://leetcode-cn.com/problems/daily-temperatures) + [84. 柱状图中最大的矩形](https://leetcode-cn.com/problems/largest-rectangle-in-histogram)「单调栈」[85. 最大矩形](https://leetcode-cn.com/problems/maximal-rectangle) [42. 接雨水](https://leetcode-cn.com/problems/trapping-rain-water)「单调栈」 diff --git "a/questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/main.cpp" "b/questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/main.cpp" new file mode 100644 index 0000000..5bf554e --- /dev/null +++ "b/questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "stack" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[73,74,75,71,69,72,76,73]\"],[\"[30,40,50,60]\"],[\"[30,60,90]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + vector result=s->dailyTemperatures(arg0); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/solution.h" "b/questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/solution.h" new file mode 100644 index 0000000..a248f8f --- /dev/null +++ "b/questions/739.\346\257\217\346\227\245\346\270\251\345\272\246/solution.h" @@ -0,0 +1,22 @@ +// @algorithm @lc id=739 lang=cpp +// @title daily-temperatures + +// @test([73,74,75,71,69,72,76,73])=[1,1,4,2,1,1,0,0] +// @test([30,40,50,60])=[1,1,1,0] +// @test([30,60,90])=[1,1,0] +class Solution { +public: + vector dailyTemperatures(vector& temperatures) { + stack stk; + auto & t = temperatures; + vector ans(t.size(), 0); + for(int i=0; i Date: Wed, 17 Nov 2021 00:44:55 +0800 Subject: [PATCH 12/14] =?UTF-8?q?LC-391.=20=E5=AE=8C=E7=BE=8E=E7=9F=A9?= =?UTF-8?q?=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + .../main.cpp" | 18 ++++++++ .../solution.h" | 42 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 "questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/main.cpp" create mode 100644 "questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/solution.h" diff --git a/README.md b/README.md index f695dae..808b2b2 100644 --- a/README.md +++ b/README.md @@ -378,6 +378,8 @@ [1894. 找到需要补充粉笔的学生编号](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk)「前缀和」 ## 几何 [223. 矩形面积](https://leetcode-cn.com/problems/rectangle-area) + +[391. 完美矩形](https://leetcode-cn.com/problems/perfect-rectangle) ## 模拟 [621. 任务调度器](https://leetcode-cn.com/problems/task-scheduler) diff --git "a/questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/main.cpp" "b/questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/main.cpp" new file mode 100644 index 0000000..75161da --- /dev/null +++ "b/questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]\"],[\"[[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]\"],[\"[[1,1,3,3],[3,1,4,2],[1,3,2,4],[3,2,4,4]]\"],[\"[[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + bool result=s->isRectangleCover(arg0); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/solution.h" "b/questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/solution.h" new file mode 100644 index 0000000..0940026 --- /dev/null +++ "b/questions/391.\345\256\214\347\276\216\347\237\251\345\275\242/solution.h" @@ -0,0 +1,42 @@ +// @algorithm @lc id=391 lang=cpp +// @title perfect-rectangle + + +// #define print(...) +// @test([[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]])=true +// @test([[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]])=false +// @test([[1,1,3,3],[3,1,4,2],[1,3,2,4],[3,2,4,4]])=false +// @test([[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]])=false +using pt = uint64_t; +#define PT(x,y) ( (pt(uint32_t(x))<<32) | uint32_t(y) ) +#define PTS(rc) multimap({ {rc[0],rc[1]}, {rc[0],rc[3]}, {rc[2],rc[1]}, {rc[2],rc[3]} }) +class Solution { +public: + bool isRectangleCover(vector>& rectangles) { + print("\nrects=", rectangles, "\n") + unordered_map ptn; + vector out(rectangles[0]); + int64_t sumArea = 0; + for(auto & rc : rectangles){ + for(int i=0; i<2; i++) out[i] = min(out[i], rc[i]); + for(int i=2; i<4; i++) out[i] = max(out[i], rc[i]); + sumArea += int64_t(rc[2]-rc[0])*(rc[3]-rc[1]); + for(auto &[x,y] : PTS(rc)) + ptn[ PT(x,y) ]++; + } + if(sumArea != int64_t(out[2]-out[0])*(out[3]-out[1])) + return false; + for(auto &[x,y] : PTS(out)){ + print("out<", x, ",", y, ">=", ptn[PT(x,y)], "\n") + if(!ptn.count(PT(x,y))) return false; + } + for(auto &[x,y] : PTS(out)) + ptn.erase(PT(x,y)); + for(auto &[p,n] : ptn){ + print("ptn<", p>>32, ",", p&0xFFFF, ">=", n, "\n") + if(n!=2 && n!=4) + return false; + } + return true; + } +}; \ No newline at end of file -- Gitee From 99a6786f85b87b86e2df2ff9723e971c2bb6582c Mon Sep 17 00:00:00 2001 From: sudo Date: Mon, 29 Nov 2021 23:07:09 +0800 Subject: [PATCH 13/14] =?UTF-8?q?LC-786.=20=E7=AC=AC=20K=20=E4=B8=AA?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=9A=84=E7=B4=A0=E6=95=B0=E5=88=86=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../main.cpp" | 19 +++++++++++++++ .../solution.h" | 24 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 "questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/main.cpp" create mode 100644 "questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/solution.h" diff --git a/README.md b/README.md index 808b2b2..37f10f9 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,8 @@ [1818. 绝对差值和](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/)「二分查找」 ## 堆 +[786. 第 K 个最小的素数分数](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction)「小顶堆」 + [347. 前 K 个高频元素](https://leetcode-cn.com/problems/top-k-frequent-elements)「小顶堆」 [253. 会议室 II](https://leetcode-cn.com/problems/meeting-rooms-ii/)「小顶堆」 diff --git "a/questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/main.cpp" "b/questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/main.cpp" new file mode 100644 index 0000000..af990ed --- /dev/null +++ "b/questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,2,3,5]\",\"3\"],[\"[1,7]\",\"1\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int arg1 = parseInteger(args[1]); + vector result=s->kthSmallestPrimeFraction(arg0,arg1); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/solution.h" "b/questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/solution.h" new file mode 100644 index 0000000..89f78e5 --- /dev/null +++ "b/questions/786.\347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/solution.h" @@ -0,0 +1,24 @@ +// @algorithm @lc id=802 lang=cpp +// @title k-th-smallest-prime-fraction + + +// @test([1,2,3,5],3)=[2,5] +// @test([1,7],1)=[1,7] +using pii = pair; +class Solution { +public: + vector kthSmallestPrimeFraction(vector& arr, int k) { + int n = arr.size(); + auto cmp = [&](const pii&x, const pii&y){ + return arr[x.first] * arr[y.second] > arr[y.first] * arr[x.second]; + }; + priority_queue, decltype(cmp)> pq(cmp); + for(int i=1; i Date: Sat, 29 Jan 2022 19:52:36 +0800 Subject: [PATCH 14/14] =?UTF-8?q?LC-1610.=20=E5=8F=AF=E8=A7=81=E7=82=B9?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + include/stl.h | 1 + include/virtualio.h | 2 - .../main.cpp" | 20 +++++++++ .../solution.h" | 43 +++++++++++++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 "questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/main.cpp" create mode 100644 "questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/solution.h" diff --git a/README.md b/README.md index 37f10f9..c8ae209 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,8 @@ [274. H 指数](https://leetcode-cn.com/problems/h-index/)「计数(桶)排序」 ## 查找 +[1610. 可见点的最大数目](https://leetcode-cn.com/problems/maximum-number-of-visible-points)「极角,二分查找」 + [352. 将数据流变为多个不相交区间](https://leetcode-cn.com/problems/data-stream-as-disjoint-intervals)「map二分查找」 [981. 基于时间的键值存储](https://leetcode-cn.com/problems/time-based-key-value-store/)「二分查找」 diff --git a/include/stl.h b/include/stl.h index c4af3ff..f5dbc1b 100644 --- a/include/stl.h +++ b/include/stl.h @@ -7,6 +7,7 @@ #include #include "virtualio.h" using namespace std; +#define print(...) print_any(__VA_ARGS__); template void vv_conv(const vector> & from, vector> & to, diff --git a/include/virtualio.h b/include/virtualio.h index 4e52707..a2ab470 100644 --- a/include/virtualio.h +++ b/include/virtualio.h @@ -10,8 +10,6 @@ #include #include -#define print(...) print_any(__VA_ARGS__); - template struct Array{ const T & Ref; diff --git "a/questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/main.cpp" "b/questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/main.cpp" new file mode 100644 index 0000000..4411c97 --- /dev/null +++ "b/questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/main.cpp" @@ -0,0 +1,20 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[2,1],[2,2],[3,3]]\",\"90\",\"[1,1]\"],[\"[[2,1],[2,2],[3,4],[1,1]]\",\"90\",\"[1,1]\"],[\"[[1,0],[2,1]]\",\"13\",\"[1,1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + int arg1 = parseInteger(args[1]); + vector arg2 = parseIntegerArr(args[2]); + int result=s->visiblePoints(arg0,arg1,arg2); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/solution.h" "b/questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/solution.h" new file mode 100644 index 0000000..13eec29 --- /dev/null +++ "b/questions/1610.\345\217\257\350\247\201\347\202\271\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/solution.h" @@ -0,0 +1,43 @@ +// @algorithm @lc id=1733 lang=cpp +// @title maximum-number-of-visible-points + + +#define print(...) +// @test([[2,1],[2,2],[3,3]],90,[1,1])=3 +// @test([[2,1],[2,2],[3,4],[1,1]],90,[1,1])=4 +// @test([[1,0],[2,1]],13,[1,1])=1 +class Solution { +public: + int visiblePoints(vector>& points, int angle, vector& location) { + int sameCnt = 0; + vector polarDegrees; + for (auto & point : points) { + if (point[0] == location[0] && point[1] == location[1]) { + sameCnt++; + continue; + } + double degree = atan2(point[1] - location[1], point[0] - location[0]); + polarDegrees.emplace_back(degree); + } + sort(polarDegrees.begin(), polarDegrees.end()); + + int m = polarDegrees.size(); + for (int i = 0; i < m; ++i) { + polarDegrees.emplace_back(polarDegrees[i] + 2 * M_PI); + } + + int maxCnt = 0; + double degree = angle * M_PI / 180; + for (int i = 0; i < m; ++i) { + auto it = upper_bound(polarDegrees.begin() + i, polarDegrees.end(), polarDegrees[i] + degree); + int curr = it - polarDegrees.begin() - i; + maxCnt = max(maxCnt, curr); + } + return maxCnt + sameCnt; + } +}; + +/*作者:LeetCode-Solution +链接:https://leetcode-cn.com/problems/maximum-number-of-visible-points/solution/you-xiao-ke-jian-dian-de-zui-da-shu-mu-b-r1qz/ +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/ \ No newline at end of file -- Gitee