Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 13 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
3fbfafd
Update 2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp
wisdompeak Oct 31, 2022
389bf20
Create 907.Sum-of-Subarray-Minimums_v2.cpp
wisdompeak Oct 31, 2022
2b247e3
Update Readme.md
wisdompeak Oct 31, 2022
d24a8b1
Create 2453.Destroy-Sequential-Targets.cpp
wisdompeak Oct 31, 2022
fd03047
Update Readme.md
wisdompeak Oct 31, 2022
72e4e89
Create Readme.md
wisdompeak Oct 31, 2022
2c2ef32
Create 2454.Next-Greater-Element-IV.cpp
wisdompeak Oct 31, 2022
3115ac5
Update Readme.md
wisdompeak Oct 31, 2022
4286e56
Create Readme.md
wisdompeak Oct 31, 2022
93ef9c0
Create 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp
wisdompeak Oct 31, 2022
f912ebf
Update Readme.md
wisdompeak Oct 31, 2022
d2994af
Create Readme.md
wisdompeak Oct 31, 2022
ad0e85c
Update 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp
wisdompeak Oct 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### 2457.Minimum-Addition-to-Make-Integer-Beautiful

很明显,想要以最小的代价来降低digit sum,必然是从低位往高位,逐个加上一个"互补"的数字,使得将该位"清零"。即原数的某位上是2的话,你必然补上8,使得digit sum能够降低2.

这里特别需要注意的是进位。例如原数是232,你补上一个8之后,你下一个考虑的十位数其实是4而不是3.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Solution {
for (int i=0; i<arr.size(); i++)
{
curCost += arr[i].second;
if (curCost >= totalCost/2)
if (curCost >= totalCost*1.0/2)
{
k = i;
break;
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using LL = long long;
class Solution {
public:
int destroyTargets(vector<int>& nums, int space)
{
int len = 0;
int ret = 0;

sort(nums.rbegin(), nums.rend());

unordered_map<int,int>dp;

for (int i=0; i<nums.size(); i++)
{
int r = nums[i]%space;
dp[r] = dp[r] + 1;

if (dp[r] > len)
{
ret = nums[i];
len = dp[r];
}
else if (dp[r] == len)
{
ret = nums[i];
}

}

return ret;
}
};
5 changes: 5 additions & 0 deletions Others/2453.Destroy-Sequential-Targets/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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]所对应的最后一个元素。
3 changes: 3 additions & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
vector<int> secondGreaterElement(vector<int>& nums)
{
stack<int>st1;
stack<int>st2;

vector<int>rets(nums.size(), -1);

for (int i=0; i<nums.size(); i++)
{
while (!st2.empty() && nums[st2.top()] < nums[i])
{
rets[st2.top()] = nums[i];
st2.pop();
}

vector<int>temp;
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;
}
};
9 changes: 9 additions & 0 deletions Stack/2454.Next-Greater-Element-IV/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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的递减性质。
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
int sumSubarrayMins(vector<int>& arr)
{
int n = arr.size();
vector<int>nextSmaller(n, n);
vector<int>prevSmaller(n, -1);

stack<int>Stack;
for (int i=0; i<n; i++)
{
while (!Stack.empty() && arr[Stack.top()] > 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<n; i++)
{
int a = prevSmaller[i];
int b = nextSmaller[i];
long num = arr[i]*(i-a) % M *(b-i) % M;
ret = (ret + num) % M;
}
return ret;
}
};
18 changes: 17 additions & 1 deletion Stack/907.Sum-of-Subarray-Minimums/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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<n; i++)
{
while (!Stack.empty() && arr[Stack.top()] > arr[i])
{
nextSmaller[Stack.top()] = i;
Stack.pop();
}

[Leetcode Link](https://leetcode.com/problems/sum-of-subarray-minimums)
if (!Stack.empty())
prevSmaller[i] = Stack.top();
Stack.push(i);
}
```


[Leetcode Link](https://leetcode.com/problems/sum-of-subarray-minimums)

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