From 7a658c053dd257d3257a080a6a98ed305057aa9f Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 6 Nov 2022 23:37:47 -0800 Subject: [PATCH 01/13] Create 2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K.cpp --- ...um-of-Distinct-Subarrays-With-Length-K.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K.cpp diff --git a/Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K.cpp b/Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K.cpp new file mode 100644 index 000000000..296089a64 --- /dev/null +++ b/Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K.cpp @@ -0,0 +1,31 @@ +using LL = long long; +class Solution { +public: + long long maximumSubarraySum(vector& nums, int k) + { + LL ret = 0; + unordered_mapMap; + int count = 0; + LL sum = 0; + for (int i=0; i=k-1) + { + if (count == k) + ret = max(ret, sum); + + Map[nums[i-k+1]]--; + sum -= nums[i-k+1]; + if (Map[nums[i-k+1]]==0) + count--; + } + } + + return ret; + } +}; From 48c9b62c393a53ba27f63ef5512ddc27ed798443 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 6 Nov 2022 23:38:09 -0800 Subject: [PATCH 02/13] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index dfb0414aa..1f5a5b9d1 100644 --- a/Readme.md +++ b/Readme.md @@ -53,6 +53,7 @@ [159.Longest-Substring-with-At-Most-Two-Distinct-Characters](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/159.Longest-Substring-with-At-Most-Two-Distinct-Characters)(H-) [340.Longest-Substring-with-At-Most-K-Distinct-Characters](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/340.Longest-Substring-with-At-Most-K-Distinct-Characters) (H) [992.Subarrays-with-K-Different-Integers](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/992.Subarrays-with-K-Different-Integers) (H-) +[2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K) (M) * ``Two pointers for two seuqences`` [986.Interval-List-Intersections](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/986.Interval-List-Intersections) (M) [1229.Meeting-Scheduler](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1229.Meeting-Scheduler) (M+) From 800a37b8d8d0d44defb508cfb1205c17eadf8169 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 6 Nov 2022 23:42:19 -0800 Subject: [PATCH 03/13] Create Readme.md --- .../Readme.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Readme.md diff --git a/Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Readme.md b/Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Readme.md new file mode 100644 index 000000000..01afc5d50 --- /dev/null +++ b/Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Readme.md @@ -0,0 +1,17 @@ +### 2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K + +非常普通的固定长度的滑窗。 + +用一个HashMap来记录每个number出现的次数。用count来表示滑窗内的distinct number的数量。count的变动依据如下: +1. 当新加入一个数字时 +```cpp +Map[num]++; +if (Map[num]==1) + count++; +``` +2. 当移出一个数字时 +```cpp +Map[num]--; +if (Map[num]==0) + count--; +``` From 3cf1ef819f241fb2dd7891f90d6fe3f8972759ba Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 6 Nov 2022 23:53:42 -0800 Subject: [PATCH 04/13] Create 2361.Minimum-Costs-Using-the-Train-Line.cpp --- ...361.Minimum-Costs-Using-the-Train-Line.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/2361.Minimum-Costs-Using-the-Train-Line.cpp diff --git a/Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/2361.Minimum-Costs-Using-the-Train-Line.cpp b/Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/2361.Minimum-Costs-Using-the-Train-Line.cpp new file mode 100644 index 000000000..e29f45d2d --- /dev/null +++ b/Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/2361.Minimum-Costs-Using-the-Train-Line.cpp @@ -0,0 +1,26 @@ +using LL = long long; +class Solution { + LL dp[100005][2]; +public: + vector minimumCosts(vector& regular, vector& express, int expressCost) + { + int n = regular.size(); + regular.insert(regular.begin(), 0); + express.insert(express.begin(), 0); + + dp[0][0] = 0; + dp[0][1] = expressCost; + + vectorrets; + + for (int i=1; i<=n; i++) + { + dp[i][0] = min(dp[i-1][0] + regular[i], dp[i-1][1] + regular[i]); + dp[i][1] = min(dp[i-1][1] + express[i], dp[i-1][0] + expressCost + express[i]); + + rets.push_back(min(dp[i][0], dp[i][1])); + } + + return rets; + } +}; From f17627cc150eef7bf9e41b3ff8568a8a86186481 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 6 Nov 2022 23:54:05 -0800 Subject: [PATCH 05/13] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 1f5a5b9d1..60d79ab60 100644 --- a/Readme.md +++ b/Readme.md @@ -679,6 +679,7 @@ [2036.Maximum-Alternating-Subarray-Sum](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2036.Maximum-Alternating-Subarray-Sum) (M+) [2143.Choose-Numbers-From-Two-Arrays-in-Range](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2143.Choose-Numbers-From-Two-Arrays-in-Range) (H) [2318.Number-of-Distinct-Roll-Sequences](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2318.Number-of-Distinct-Roll-Sequences) (H-) +[2361.Minimum-Costs-Using-the-Train-Line](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line) (M+) * ``基本型 II`` [368.Largest-Divisible-Subset](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/368.Largest-Divisible-Subset) (M+) [300.Longest-Increasing-Subsequence](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/300.Longest-Increasing-Subsequence) (M+) From d3bcbc90215edeed8efe5465069e2b2ccaa47f88 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 6 Nov 2022 23:59:27 -0800 Subject: [PATCH 06/13] Create Readme.md --- .../2361.Minimum-Costs-Using-the-Train-Line/Readme.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/Readme.md diff --git a/Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/Readme.md b/Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/Readme.md new file mode 100644 index 000000000..de0a1a977 --- /dev/null +++ b/Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/Readme.md @@ -0,0 +1,8 @@ +### 2361.Minimum-Costs-Using-the-Train-Line + +很明显,状态变量dp[i][0]表示到达第i个车站的regular所需要的最小代价,dp[i][1]表示到达第i个车站的express所需要的最小代价。于是有转移方程: +```cpp +dp[i][0] = min(dp[i-1][0] + regular[i], dp[i-1][1] + regular[i]); +dp[i][1] = min(dp[i-1][1] + express[i], dp[i-1][0] + expressCost + express[i]); +``` +注意我们不需要考虑dp[i][0]与dp[i][1]之间的转移。这是因为,我们如果想要从dp[i][0]转移到dp[i][1],其目的一定只是为了后续得到dp[i+1][1]。单独从第i站的角度来看,只要到了regular或express都算达成了任务,两者间的跳转对于第i站而言没有意义。 From a86f29f9d819feee953ac5e65a5391fdc87034e9 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 7 Nov 2022 00:16:07 -0800 Subject: [PATCH 07/13] Create 2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp --- ...oose-Edges-to-Maximize-Score-in-a-Tree.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp diff --git a/Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp b/Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp new file mode 100644 index 000000000..447ca3eff --- /dev/null +++ b/Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp @@ -0,0 +1,52 @@ +using LL = long long; +class Solution { + vector>children[100005]; + LL memo[100005][2]; +public: + long long maxScore(vector>& edges) + { + int n = edges.size(); + int root = -1; + for (int i=0; i Date: Mon, 7 Nov 2022 00:17:08 -0800 Subject: [PATCH 08/13] Rename Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp to Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp --- .../2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Dynamic_Programming => Recursion}/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp (100%) diff --git a/Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp b/Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp similarity index 100% rename from Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp rename to Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp From 9f79e25c98afd4a5fc7ff9ec4f770b69dfc232af Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 7 Nov 2022 00:17:36 -0800 Subject: [PATCH 09/13] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 60d79ab60..2e178da5e 100644 --- a/Readme.md +++ b/Readme.md @@ -954,6 +954,7 @@ [133.Clone-Graph](https://github.com/wisdompeak/LeetCode/tree/master/Recursion/133.Clone-Graph) (M+) [213.House-Robber-II](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/213.House-Robber-II) (H-) [337.House-Robber-III](https://github.com/wisdompeak/LeetCode/tree/master/Recursion/337.House-Robber-III) (M+) +[2378.Choose-Edges-to-Maximize-Score-in-a-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree) (H-) [390.Elimination-Game](https://github.com/wisdompeak/LeetCode/tree/master/Recursion/390.Elimination-Game) (H) [395.Longest-Substring-with-At-Least-K-Repeating-Characters](https://github.com/wisdompeak/LeetCode/tree/master/Recursion/395.Longest-Substring-with-At-Least-K-Repeating-Characters) (H) [397.Integer-Replacement](https://github.com/wisdompeak/LeetCode/tree/master/Recursion/397.Integer-Replacement) (M+) From b086302fee90371c094114101fc44bba96535dc5 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 7 Nov 2022 00:24:59 -0800 Subject: [PATCH 10/13] Create Readme.md --- .../Readme.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/Readme.md diff --git a/Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/Readme.md b/Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/Readme.md new file mode 100644 index 000000000..457b2c703 --- /dev/null +++ b/Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/Readme.md @@ -0,0 +1,11 @@ +### 2378.Choose-Edges-to-Maximize-Score-in-a-Tree + +我们不难发现,如果从parent到node的edge被选中的话,那么node到它所有children的edge都不能选中。如果从parent到node的edge不被选中的话,那么node到它所有children的edge里只能最多选一条。 + +所以我们定义`dfs(node, 1)`表示从parent到node的edge被选中,那么以node为根的子树的最大收益。于是有 +```cpp +dfs(node, 1) = sum{dfs(child, 0)}; +``` +同理,定义`dfs(node, 0)`表示从parent到node的edge不被选中,那么以node为根的子树的最大收益。因为我们允许有一条node的子边被选中,所以需要遍历这个选择。为了便于计算,我们先求出`Sum = sum{dfs(child, 0)}`,那么对于任何一个child,如果它的边被选中的话,则整棵树的最大收益就是`Sum - dfs(child, 0) + dfs(child, 1) + weight`. 我们遍历child,再返回最大的作为`dfs(node,1)`. + +显然,此题需要记忆化来避免重复计算。 From 89ba3efc69342be6053b18e1ec75ee64885a65ba Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 7 Nov 2022 01:09:59 -0800 Subject: [PATCH 11/13] Create 2387.Median-of-a-Row-Wise-Sorted-Matrix.cpp --- ...387.Median-of-a-Row-Wise-Sorted-Matrix.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/2387.Median-of-a-Row-Wise-Sorted-Matrix.cpp diff --git a/Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/2387.Median-of-a-Row-Wise-Sorted-Matrix.cpp b/Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/2387.Median-of-a-Row-Wise-Sorted-Matrix.cpp new file mode 100644 index 000000000..211ed8d22 --- /dev/null +++ b/Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/2387.Median-of-a-Row-Wise-Sorted-Matrix.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int matrixMedian(vector>& grid) + { + int m = grid.size(); + int n = grid[0].size(); + int k = (m*n+1)/2; + + int left = 0, right = INT_MAX; + while (left < right) + { + int mid = left+(right-left)/2; + int count = 0; + for (int i=0; i Date: Mon, 7 Nov 2022 01:10:41 -0800 Subject: [PATCH 12/13] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 2e178da5e..21ae6ac95 100644 --- a/Readme.md +++ b/Readme.md @@ -129,6 +129,7 @@ [793.Preimage-Size-of-Factorial-Zeroes-Function](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/793.Preimage-Size-of-Factorial-Zeroes-Function) (H-) [1201.Ugly-Number-III](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/1201.Ugly-Number-III) (H-) [1539.Kth-Missing-Positive-Number](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/1539.Kth-Missing-Positive-Number) (H-) +[2387.Median-of-a-Row-Wise-Sorted-Matrix](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix) (H-) #### [Hash Map](https://github.com/wisdompeak/LeetCode/tree/master/Hash) [049.Group-Anagrams](https://github.com/wisdompeak/LeetCode/tree/master/Hash/049.Group-Anagrams) (M+) From ea974b6a1a59a3d163b8ad6d3c18a86cdfaae3e0 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 7 Nov 2022 01:12:55 -0800 Subject: [PATCH 13/13] Create Readme.md --- .../2387.Median-of-a-Row-Wise-Sorted-Matrix/Readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/Readme.md diff --git a/Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/Readme.md b/Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/Readme.md new file mode 100644 index 000000000..4c42ebded --- /dev/null +++ b/Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/Readme.md @@ -0,0 +1,9 @@ +### 2387.Median-of-a-Row-Wise-Sorted-Matrix + +令`k=(m*n+1)/2`,本题就是求矩阵里的从小到大的第k个元素。本质和`215.Kth-Largest-Element-in-an-Array`相同,只不过需要将各行`smallerOrEqual(mid)`的结果累加起来得到count。 +```cpp +if (count < k) + left = mid+1; +else + right = mid; +```

AltStyle によって変換されたページ (->オリジナル) /