forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #71
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1afb6eb
Update Readme.md
wisdompeak db731b4
Update Readme.md
wisdompeak 58610eb
Create 2366.Minimum-Replacements-to-Sort-the-Array_v2.cpp
wisdompeak 45d30b7
Create 2449.Minimum-Number-of-Operations-to-Make-Arrays-Similar.cpp
wisdompeak 77503de
Update 115.Distinct-Subsequences.cpp
wisdompeak 9efba58
Update Readme.md
wisdompeak b4ca3f7
Create Readme.md
wisdompeak fbd794e
Update 2449.Minimum-Number-of-Operations-to-Make-Arrays-Similar.cpp
wisdompeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
...Minimum-Replacements-to-Sort-the-Array/2366.Minimum-Replacements-to-Sort-the-Array_v2.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using LL = long long; | ||
class Solution { | ||
public: | ||
long long minimumReplacement(vector<int>& nums) | ||
{ | ||
LL ret = 0; | ||
for (int i = nums.size()-2; i>=0; i--) | ||
{ | ||
LL x = nums[i+1]; | ||
LL y = nums[i]; | ||
if (y<=x) continue; | ||
|
||
if (y%x==0) | ||
{ | ||
ret += y/x-1; | ||
nums[i] = x; | ||
} | ||
else | ||
{ | ||
int k = y/x+1; | ||
ret += y/x; | ||
nums[i] = y/k; | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
...tions-to-Make-Arrays-Similar/2449.Minimum-Number-of-Operations-to-Make-Arrays-Similar.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using LL = long long; | ||
class Solution { | ||
public: | ||
long long makeSimilar(vector<int>& nums, vector<int>& target) | ||
{ | ||
vector<LL>odd1, odd2, even1, even2; | ||
for (auto x: nums) | ||
{ | ||
if (x%2==0) | ||
even1.push_back(x); | ||
else | ||
odd1.push_back(x); | ||
} | ||
for (auto x: target) | ||
{ | ||
if (x%2==0) | ||
even2.push_back(x); | ||
else | ||
odd2.push_back(x); | ||
} | ||
|
||
return helper(even1, even2) + helper(odd1, odd2); | ||
} | ||
|
||
LL helper(vector<LL>&nums, vector<LL>&target) | ||
{ | ||
sort(target.begin(), target.end()); | ||
sort(nums.begin(), nums.end()); | ||
|
||
LL count = 0; | ||
for (int i=0; i<nums.size(); i++) | ||
if (nums[i] > target[i]) | ||
count += (nums[i]-target[i])/2; | ||
|
||
return count; | ||
} | ||
|
||
}; |
13 changes: 13 additions & 0 deletions
Greedy/2449.Minimum-Number-of-Operations-to-Make-Arrays-Similar/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
### 2449.Minimum-Number-of-Operations-to-Make-Arrays-Similar | ||
|
||
很显然题目的意思是,nums经过一系列操作之后,需要变成targets。于是nums和targets的数组元素之和必然相等,否则无法实现+2/-2的守恒。 | ||
|
||
另外,我们发现,偶数无论如何也无法操作成奇数,反之亦然。所以知道,需要将奇数偶数分开处理,即nums里的奇数需要多少操作转化为targets里的奇数,同理nums里的偶数需要多少操作否转化为targets里的偶数。 | ||
|
||
接下来,我们考虑只含有奇数的nums数组和只含有奇数的targets数组。很明显,我们必然会把nums[0]转化为targets[0],将nums[1]转化为targets[1],依次类推。这样能使得每对元素差的绝对值之和最小。简单的证明可以从两对开始研究。假设有`nums[i]<nums[j]`和`targets[x]<targets[y]`,那么无非三种情况 | ||
1. nums[i]<nums[j]<targets[x]<targets[y] | ||
2. nums[i]<targets[x]<nums[j]<targets[y] | ||
3. nums[i]<targets[x]<targets[y]<nums[j] | ||
画个线段图就发现,无论哪一种,都有`abs(nums[i]-targets[x])+ abs(nums[j]-targets[y]) <= abs(nums[i]-targets[y])+ abs(nums[j]-targets[x])`。将这个结论推广至任意两对元素之间的判断,就可以得出前面的结论。 | ||
|
||
有了这个策略之后,我们就可以计算出每对元素之间,需要多少次+的操作,或者多少次-的操作。但实际上我们只需要记录+的操作即可,因为题目保证最终全局(即包括了奇数数组和偶数数组)而言+和-的操作一定守恒。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.