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 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; + } +}; 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站而言没有意义。 diff --git a/Readme.md b/Readme.md index dfb0414aa..21ae6ac95 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+) @@ -128,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+) @@ -678,6 +680,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+) @@ -952,6 +955,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+) diff --git a/Recursion/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 new file mode 100644 index 000000000..447ca3eff --- /dev/null +++ b/Recursion/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& 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; + } +}; 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--; +```

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