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..9479e7ffb --- /dev/null +++ b/Greedy/2457.Minimum-Addition-to-Make-Integer-Beautiful/2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + int DigitSum(long long x) + { + int sum = 0; + while (x>0) + { + sum += x%10; + x/=10; + } + return sum; + } + + long long makeIntegerBeautiful(long long n, int target) + { + string ret; + int carry = 0; + while (n> 0) + { + if (DigitSum(n) <= target) break; + + int cur = n%10; + int d; + if (cur != 0) + { + ret.push_back('0' + (10-cur)); + carry = 1; + } + else + { + ret.push_back('0'); + carry = 0; + } + + n = n/10 + carry; + } + + if(ret.empty()) return 0; + reverse(ret.begin(), ret.end()); + return stoll(ret); + } +}; 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. 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; 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; + } +}; 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]所对应的最后一个元素。 diff --git a/Readme.md b/Readme.md index 3d5b8e347..b943280c6 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) @@ -1148,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) @@ -1287,6 +1289,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) 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; + } +}; 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的递减性质。 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 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)

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