From 3fbfafd9f37150eccf96db50e47863e8d7a0e1d5 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 18:23:12 -0700 Subject: [PATCH 01/13] Update 2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp --- .../2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Math/2448.Minimum-Cost-to-Make-Array-Equal/2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp b/Math/2448.Minimum-Cost-to-Make-Array-Equal/2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp index 7277528f9..17616c4f0 100644 --- a/Math/2448.Minimum-Cost-to-Make-Array-Equal/2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp +++ b/Math/2448.Minimum-Cost-to-Make-Array-Equal/2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp @@ -17,7 +17,7 @@ class Solution { for (int i=0; i= totalCost/2) + if (curCost>= totalCost*1.0/2) { k = i; break; From 389bf2028e84079c29ad8d283fb799c8694d57b0 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 19:40:18 -0700 Subject: [PATCH 02/13] Create 907.Sum-of-Subarray-Minimums_v2.cpp --- .../907.Sum-of-Subarray-Minimums_v2.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Stack/907.Sum-of-Subarray-Minimums/907.Sum-of-Subarray-Minimums_v2.cpp diff --git a/Stack/907.Sum-of-Subarray-Minimums/907.Sum-of-Subarray-Minimums_v2.cpp b/Stack/907.Sum-of-Subarray-Minimums/907.Sum-of-Subarray-Minimums_v2.cpp new file mode 100644 index 000000000..802ac8300 --- /dev/null +++ b/Stack/907.Sum-of-Subarray-Minimums/907.Sum-of-Subarray-Minimums_v2.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int sumSubarrayMins(vector& arr) + { + int n = arr.size(); + vectornextSmaller(n, n); + vectorprevSmaller(n, -1); + + stackStack; + for (int i=0; i arr[i]) + { + nextSmaller[Stack.top()] = i; + Stack.pop(); + } + + if (!Stack.empty()) + prevSmaller[i] = Stack.top(); + Stack.push(i); + } + + + long ret = 0; + long M = 1e9+7; + for (int i=0; i Date: 2022年10月30日 19:41:41 -0700 Subject: [PATCH 03/13] Update Readme.md --- Stack/907.Sum-of-Subarray-Minimums/Readme.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Stack/907.Sum-of-Subarray-Minimums/Readme.md b/Stack/907.Sum-of-Subarray-Minimums/Readme.md index 500851280..6481a1ed5 100644 --- a/Stack/907.Sum-of-Subarray-Minimums/Readme.md +++ b/Stack/907.Sum-of-Subarray-Minimums/Readme.md @@ -8,5 +8,21 @@ 所以本题确切的说,是求每个元素的next smaller element,以及previous smaller or equal element. 另外,特别注意:如果一个数没有next smaller element,那么意味着它的左边界是可以到n;如果一个数没有prev smaller/equal element,那么意味着它的左边界是可以到-1. +Update: 事实上,`next smaller element`和`previous smaller or equal element`可以one-pass同时实现。 +```cpp + for (int i=0; i arr[i]) + { + nextSmaller[Stack.top()] = i; + Stack.pop(); + } -[Leetcode Link](https://leetcode.com/problems/sum-of-subarray-minimums) \ No newline at end of file + if (!Stack.empty()) + prevSmaller[i] = Stack.top(); + Stack.push(i); + } +``` + + +[Leetcode Link](https://leetcode.com/problems/sum-of-subarray-minimums) From d24a8b175230fba54bfbcae131f5f5f903df641c Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 20:00:53 -0700 Subject: [PATCH 04/13] Create 2453.Destroy-Sequential-Targets.cpp --- .../2453.Destroy-Sequential-Targets.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Others/2453.Destroy-Sequential-Targets/2453.Destroy-Sequential-Targets.cpp diff --git a/Others/2453.Destroy-Sequential-Targets/2453.Destroy-Sequential-Targets.cpp b/Others/2453.Destroy-Sequential-Targets/2453.Destroy-Sequential-Targets.cpp new file mode 100644 index 000000000..37384bb2f --- /dev/null +++ b/Others/2453.Destroy-Sequential-Targets/2453.Destroy-Sequential-Targets.cpp @@ -0,0 +1,32 @@ +using LL = long long; +class Solution { +public: + int destroyTargets(vector& nums, int space) + { + int len = 0; + int ret = 0; + + sort(nums.rbegin(), nums.rend()); + + unordered_mapdp; + + for (int i=0; i len) + { + ret = nums[i]; + len = dp[r]; + } + else if (dp[r] == len) + { + ret = nums[i]; + } + + } + + return ret; + } +}; From fd030478584f4afb25797f81cae05506780224d7 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 20:01:45 -0700 Subject: [PATCH 05/13] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 3d5b8e347..38c206e00 100644 --- a/Readme.md +++ b/Readme.md @@ -1287,6 +1287,7 @@ [2337.Move-Pieces-to-Obtain-a-String](https://github.com/wisdompeak/LeetCode/tree/master/Others/2337.Move-Pieces-to-Obtain-a-String) (aka. 777.Swap-Adjacent-in-LR-String) (M+) [2359.Find-Closest-Node-to-Given-Two-Nodes](https://github.com/wisdompeak/LeetCode/tree/master/Others/2359.Find-Closest-Node-to-Given-Two-Nodes) (M) [2380.Time-Needed-to-Rearrange-a-Binary-String](https://github.com/wisdompeak/LeetCode/tree/master/Others/2380.Time-Needed-to-Rearrange-a-Binary-String) (H) +[2453.Destroy-Sequential-Targets](https://github.com/wisdompeak/LeetCode/tree/master/Others/2453.Destroy-Sequential-Targets) (M) * ``结论转移`` [1685.Sum-of-Absolute-Differences-in-a-Sorted-Array](https://github.com/wisdompeak/LeetCode/tree/master/Others/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array) (M) [2121.Intervals-Between-Identical-Elements](https://github.com/wisdompeak/LeetCode/tree/master/Others/2121.Intervals-Between-Identical-Elements) (M) From 72e4e89298ab4f64cc43a8213068c4df2e899283 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 20:17:57 -0700 Subject: [PATCH 06/13] Create Readme.md --- Others/2453.Destroy-Sequential-Targets/Readme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Others/2453.Destroy-Sequential-Targets/Readme.md diff --git a/Others/2453.Destroy-Sequential-Targets/Readme.md b/Others/2453.Destroy-Sequential-Targets/Readme.md new file mode 100644 index 000000000..19c7839e8 --- /dev/null +++ b/Others/2453.Destroy-Sequential-Targets/Readme.md @@ -0,0 +1,5 @@ +### 2453.Destroy-Sequential-Targets + +很明显,能够构成序列的位置必然是间隔为space的等差数列。不同的等差数列之间仅仅区别于offset,这个offset就是关于space的余数。例如,space如果是3,那么就有三种等差数列{0,3,6,9...},{1,4,7,10...},{2,5,8,11...}。 + +我们将所有的位置逆序排列,对于任意nums[i],令`r = nums[i] % space`,那么说明此位置属于offset为r的序列上,就有`dp[r] += 1`. 最终我们返回最长的dp[r]所对应的最后一个元素。 From 2c2ef32b56f83dd171c1ea2e3ebb5846824aff4c Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 20:20:14 -0700 Subject: [PATCH 07/13] Create 2454.Next-Greater-Element-IV.cpp --- .../2454.Next-Greater-Element-IV.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Stack/2454.Next-Greater-Element-IV/2454.Next-Greater-Element-IV.cpp diff --git a/Stack/2454.Next-Greater-Element-IV/2454.Next-Greater-Element-IV.cpp b/Stack/2454.Next-Greater-Element-IV/2454.Next-Greater-Element-IV.cpp new file mode 100644 index 000000000..c9bc1b724 --- /dev/null +++ b/Stack/2454.Next-Greater-Element-IV/2454.Next-Greater-Element-IV.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector secondGreaterElement(vector& nums) + { + stackst1; + stackst2; + + vectorrets(nums.size(), -1); + + for (int i=0; itemp; + while (!st1.empty() && nums[st1.top()] < nums[i]) + { + temp.push_back(st1.top()); + st1.pop(); + } + + reverse(temp.begin(), temp.end()); + for (auto x: temp) + st2.push(x); + + st1.push(i); + } + + return rets; + } +}; From 3115ac58397dca3a7e1fbb2232611a186ab0afb2 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 20:20:37 -0700 Subject: [PATCH 08/13] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 38c206e00..64f2131de 100644 --- a/Readme.md +++ b/Readme.md @@ -359,6 +359,7 @@ [1950.Maximum-of-Minimum-Values-in-All-Subarrays](https://github.com/wisdompeak/LeetCode/tree/master/Stack/1950.Maximum-of-Minimum-Values-in-All-Subarrays) (H-) [1966.Binary-Searchable-Numbers-in-an-Unsorted-Array](https://github.com/wisdompeak/LeetCode/tree/master/Stack/1966.Binary-Searchable-Numbers-in-an-Unsorted-Array) (M+) [2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String](https://github.com/wisdompeak/LeetCode/tree/master/Stack/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String) (H-) +[2454.Next-Greater-Element-IV](https://github.com/wisdompeak/LeetCode/tree/master/Stack/2454.Next-Greater-Element-IV) (H-) * ``monotonic stack: other usages`` [962.Maximum-Width-Ramp](https://github.com/wisdompeak/LeetCode/tree/master/Stack/962.Maximum-Width-Ramp) (H) [1130.Minimum-Cost-Tree-From-Leaf-Values](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1130.Minimum-Cost-Tree-From-Leaf-Values) (H) From 4286e56b23e610dd086328ed1b327a8c2021c74b Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月30日 22:25:11 -0700 Subject: [PATCH 09/13] Create Readme.md --- Stack/2454.Next-Greater-Element-IV/Readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Stack/2454.Next-Greater-Element-IV/Readme.md diff --git a/Stack/2454.Next-Greater-Element-IV/Readme.md b/Stack/2454.Next-Greater-Element-IV/Readme.md new file mode 100644 index 000000000..4db7f0e98 --- /dev/null +++ b/Stack/2454.Next-Greater-Element-IV/Readme.md @@ -0,0 +1,9 @@ +### 2454.Next-Greater-Element-IV + +我们已经知道,常规的Next Greater Element可以用单调栈实现o(n)的解法。我们维护一个单调递减的栈,如果遇到新元素大于栈顶元素,就意味着栈顶元素遇到了next greater element,于是就可以退栈。 + +在此题里,栈顶元素遇到了next greater selement,并不意味着它就可以一劳永逸地舍弃。我们需要的是the second greater element,于是我们应该对这些元素进行标记,表示他们已经看到了一次next greater。当它们再次遇到greater element的时候,才能记录答案。 + +那么如何标记呢?如果把常规的单调栈记做stk1,那么我们可以把遇到过next greater的元素拿出来,放入另外一个单调栈里,记做stk2。每次新来一个元素nums[i],先看stk2的栈顶元素是否小于num[i],是的话就意味着这些栈顶元素遇到了the second greater element,就可以记录答案并退栈了。接下来看stk1的栈顶元素是否小于nums[i],同理,是的话就意味着这些栈顶元素遇到过了next greater element,并将其移至stk2中。 + +这里要注意一定,将stk1的元素移至stk2的过程中,是否会干扰stk2的单调顺序?是不会的。stk2经过退栈之后,栈顶元素一定是大于nums[i]的;而从stk1转移至stk2的这些元素都是小于nums[i]的,所以我们可以放心将转移的元素都堆在stk1的栈顶,依然能保持stk2的递减性质。 From 93ef9c0b63c0fd58b03ddc1207fe5d6795cefbae Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月31日 00:32:38 -0700 Subject: [PATCH 10/13] Create 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp --- ...mum-Addition-to-Make-Integer-Beautiful.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp diff --git a/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp b/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp new file mode 100644 index 000000000..aa4ed56b2 --- /dev/null +++ b/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + int getSum(long long x) + { + int sum = 0; + while (x>0) + { + sum += x%10; + x/=10; + } + return sum; + } + + long long makeIntegerBeautiful(long long n, int target) + { + int len = to_string(n).size(); + string ret; + + int carry = 0; + for (int i=0; i Date: 2022年10月31日 00:33:10 -0700 Subject: [PATCH 11/13] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 64f2131de..b943280c6 100644 --- a/Readme.md +++ b/Readme.md @@ -1149,6 +1149,7 @@ [2366.Minimum-Replacements-to-Sort-the-Array](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2366.Minimum-Replacements-to-Sort-the-Array) (H-) [2371.Minimize-Maximum-Value-in-a-Grid](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2371.Minimize-Maximum-Value-in-a-Grid) (M+) [2449.Minimum-Number-of-Operations-to-Make-Arrays-Similar](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2449.Minimum-Number-of-Operations-to-Make-Arrays-Similar) (M+) +[2457.Minimum-Addition-to-Make-Integer-Beautiful](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful) (M) * ``DI Sequence`` [942.DI-String-Match](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/942.DI-String-Match) (M) [484.Find-Permutation](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/484.Find-Permutation) (M) From d2994af0cb88781bf3ed39628b3eb9d918b79129 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月31日 00:42:30 -0700 Subject: [PATCH 12/13] Create Readme.md --- .../Readme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/Readme.md diff --git a/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/Readme.md b/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/Readme.md new file mode 100644 index 000000000..03ec1ea1a --- /dev/null +++ b/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/Readme.md @@ -0,0 +1,5 @@ +### 2457.Minimum-Addition-to-Make-Integer-Beautiful + +很明显,想要以最小的代价来降低digit sum,必然是从低位往高位,逐个加上一个"互补"的数字,使得将该位"清零"。即原数的某位上是2的话,你必然补上8,使得digit sum能够降低2. + +这里特别需要注意的是进位。例如原数是232,你补上一个8之后,你下一个考虑的十位数其实是4而不是3. From ad0e85c854cfbe3d161271059b0c1947c9517e48 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年10月31日 00:46:59 -0700 Subject: [PATCH 13/13] Update 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp --- ...2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp b/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp index aa4ed56b2..9479e7ffb 100644 --- a/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp +++ b/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp @@ -1,6 +1,6 @@ class Solution { public: - int getSum(long long x) + int DigitSum(long long x) { int sum = 0; while (x>0) @@ -13,13 +13,11 @@ class Solution { long long makeIntegerBeautiful(long long n, int target) { - int len = to_string(n).size(); - string ret; - + string ret; int carry = 0; - for (int i=0; i 0) { - if (getSum(n) <= target) break; + if (DigitSum(n) <= target) break; int cur = n%10; int d;

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