forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #66
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
f54cc50
Update Readme.md
xymabinogi 6cd5087
Create 2438.Range-Product-Queries-of-Powers.cpp
wisdompeak 42a9351
Update Readme.md
wisdompeak 3bcfba9
Merge pull request #69 from xymabinogi/patch-3
wisdompeak 52727bc
Create Readme.md
wisdompeak cb2bafd
Create 2439.Minimize-Maximum-of-Array.cpp
wisdompeak 1daa5c9
Update Readme.md
wisdompeak 67f77b8
Create Readme.md
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
35 changes: 35 additions & 0 deletions
Binary_Search/2439.Minimize-Maximum-of-Array/2439.Minimize-Maximum-of-Array.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,35 @@ | ||
class Solution { | ||
public: | ||
int minimizeArrayValue(vector<int>& nums) | ||
{ | ||
int left = nums[0], right = 1e9; | ||
while (left < right) | ||
{ | ||
int mid = left+(right-left)/2; | ||
|
||
long long buff = 0; | ||
int flag = true; | ||
for (int i=0; i<nums.size(); i++) | ||
{ | ||
int x = nums[i]; | ||
if (x > mid) | ||
buff -= (x-mid); | ||
else | ||
buff += (mid-x); | ||
if (buff < 0) | ||
{ | ||
flag = false; | ||
break; | ||
} | ||
} | ||
|
||
if (flag) | ||
right = mid; | ||
else | ||
left = mid+1; | ||
} | ||
|
||
return left; | ||
|
||
} | ||
}; |
15 changes: 15 additions & 0 deletions
Binary_Search/2439.Minimize-Maximum-of-Array/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,15 @@ | ||
### 2439.Minimize-Maximum-of-Array | ||
|
||
本题的大体思路就是,如果有一个数字特别大,那么我们希望它能与之前的数字们一起"抹匀",来尽量减少最大值。由于更靠前的数字,只能与更少数量的伙伴一起"抹匀",所以最终呈现出来的最优解形态,应该是piece-wise constant的递减数列。这个数列的第一个元素的大小,就是最终答案。 | ||
|
||
那么这个数字最小是什么呢?并不太容易直接求出来。但是如果我们猜测一个,是容易判断它是否成立的。 | ||
|
||
我们首先猜测最终答案是x。明显,这个x必然不会小于nums[0],因为nums[0]没有机会向前分摊数值。如果x>nums[0],那么这意味着后面的元素有机会向nums[0]分摊一些数值,我们记做"缓冲值":`buff = x-nums[0]`. | ||
|
||
接下来我们看nums[1]。如果`nums[1]>x`,那么不得不让它往前分摊数值,最多能够分摊多少呢?显然就是buff。于是如果`buff> nums[1]-x`,那么就OK,同时`buff-=nums[1]-x`;否则就直接返回失败。反之,如果`nums[1]<x`,同样意味着后面的元素有机会往前分摊数值,这部分分摊可以均匀承担在nums[0]与nums[1]上,所以`buff+= x-nums[1]`。 | ||
|
||
由此可见,我们需要做的就是不停的比较x与nums[i],根据孰大孰小和增加或者减少buffer。当buffer降为0以下的时候,说明无法实现前面若干个元素"抹匀"成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
35 changes: 35 additions & 0 deletions
Others/2438.Range-Product-Queries-of-Powers/2438.Range-Product-Queries-of-Powers.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,35 @@ | ||
class Solution { | ||
public: | ||
vector<int> productQueries(int n, vector<vector<int>>& queries) | ||
{ | ||
vector<int>powers; | ||
for (int i=0; i<32; i++) | ||
{ | ||
if (n%2!=0) | ||
powers.push_back(i); | ||
n/=2; | ||
if (n==0) break; | ||
} | ||
|
||
vector<int>presum(powers.size()); | ||
for (int i=0; i<powers.size(); i++) | ||
{ | ||
if (i==0) presum[i] = powers[i]; | ||
else presum[i] = presum[i-1] + powers[i]; | ||
} | ||
|
||
vector<long>twos(32*32,1); | ||
long M = 1e9+7; | ||
for (int i=1; i<32*32; i++) | ||
twos[i] = twos[i-1] * 2 % M; | ||
|
||
vector<int>rets; | ||
for (auto& query : queries) | ||
{ | ||
int l = query[0], r = query[1]; | ||
int diff = presum[r] - (l==0?0:presum[l-1]); | ||
rets.push_back(twos[diff]); | ||
} | ||
return rets; | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
Others/2438.Range-Product-Queries-of-Powers/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,5 @@ | ||
### 2438.Range-Product-Queries-of-Powers | ||
|
||
对于区间乘积,虽然我们可以借鉴区间求和的思路,采用前缀积相除的方法。但是本题涉及到对大数取模,应该注意到`(a/b) mod M != (a mod M) / (b mod M)`,而引入逆元的话,又显得比较繁琐。所以这道题的切入点应该在别处。 | ||
|
||
因为所有相乘的元素都是2的幂,显然我们知道这个性质,`2^a * 2^b = 2^(a+b)`,所以可以将区间的乘积转化为"指数"区间的求和,再求一次幂即可。 |
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.