diff --git a/README.md b/README.md index 7187e5f4bec1382d91b9633f4a7232138f538bf3..c8ae209fe3820a9675970180e6b9e6aebf8a4f0d 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,12 @@ [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) + [322. 零钱兑换](https://leetcode-cn.com/problems/coin-change) [312. 戳气球](https://leetcode-cn.com/problems/burst-balloons) @@ -53,8 +59,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) @@ -89,6 +93,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) @@ -99,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/)「二分查找」 @@ -163,6 +171,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」 @@ -173,6 +183,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/) @@ -187,6 +201,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/)「小顶堆」 @@ -197,10 +213,14 @@ [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)「单调栈」 +[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/) @@ -235,6 +255,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/) @@ -268,6 +290,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)「中序遍历」 @@ -358,7 +382,11 @@ [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) + [54. 螺旋矩阵](https://leetcode-cn.com/problems/spiral-matrix) [59. 螺旋矩阵 II](https://leetcode-cn.com/problems/spiral-matrix-ii) diff --git a/include/stl.h b/include/stl.h index c4af3ffb04408374429cd6d4c18b9603cfab05f6..f5dbc1b72f4ccdfb7beb7e4f0198be2ad1356a8d 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 4e5270707c790dae1b1e4e1921fab441aba5d173..a2ab4707ec335e52bfd74b69f6c35aab2928ea94 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 0000000000000000000000000000000000000000..4411c97197894c426d0bc46736021a32fc1e0562 --- /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 0000000000000000000000000000000000000000..13eec29db831de71776528f0d6649f02b6d9f104 --- /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 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 0000000000000000000000000000000000000000..75161da0b38e865a90b0584788c301d67f636c04 --- /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 0000000000000000000000000000000000000000..094002690e3d981929f7f83e6bad9273d7d734bf --- /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 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 0000000000000000000000000000000000000000..6b983922ebed6c384ae06194ab02bd5d00e2a037 --- /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 0000000000000000000000000000000000000000..8385c802230c08f9dd5b4af9bf16047db3eb3a3d --- /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 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 0000000000000000000000000000000000000000..7da907125f1eee1db9a6311c93d14081a8a671d6 --- /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 0000000000000000000000000000000000000000..57042e372c987e37d4e47957b31c9629421a79db --- /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> 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 0000000000000000000000000000000000000000..f3c2fdac5c05c1e559a7e58675eb9ca8c6d6e4d1 --- /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> 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 0000000000000000000000000000000000000000..0ab67b84350286d9f7e5f8c952da6daf7dee67f1 --- /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> 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 0000000000000000000000000000000000000000..1c63b0b1ffecb274958fd1fc85005e1d29a93bfb --- /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 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 0000000000000000000000000000000000000000..baff39d3f1db5f8dc9ffe2c5142041db9817f7e1 --- /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 0000000000000000000000000000000000000000..5a9bb0cc10c50f6f070cc51e517fd201740789fe --- /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 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 0000000000000000000000000000000000000000..55abde7888c10195bac69d01b6557583c4172ecf --- /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 0000000000000000000000000000000000000000..726f2e6c15bedcba04ca1332f93ee5e1f93edaba --- /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 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 0000000000000000000000000000000000000000..8c8a1eabc001b255878b6a122b5a059719eca740 --- /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 0000000000000000000000000000000000000000..4bea20e1c8cd965651d813540a12d69ef7f38708 --- /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 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 0000000000000000000000000000000000000000..4b626615e3ed1a5eea5531ecb438ed5df853158b --- /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 0000000000000000000000000000000000000000..9c521ea51fd21e4f6710727f939b8ac81d5d442d --- /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 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 0000000000000000000000000000000000000000..340b095e310aeec129a09812358f2961632713d8 --- /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 0000000000000000000000000000000000000000..da033f55fe57e72ad7ee743d6fd154c42770890b --- /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> 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 0000000000000000000000000000000000000000..a248f8f5e3931f1737de10df21a0f87f9a7bf781 --- /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> 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 0000000000000000000000000000000000000000..89f78e5e593cf70df922f8bc195c389dc98b4058 --- /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