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 #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
pull merged 8 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
View file Open in desktop
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
View file Open in desktop
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(或更小),返回失败。否则返回成功。

通过二分搜值的讨论,我们可以很容易求出满足条件的最小值。

注意本题一定有解(什么都不做就可以返回数组最大值),因此二分搜值的收链解就是最优解。
4 changes: 2 additions & 2 deletions Dynamic_Programming/416.Partition-Equal-Subset-Sum/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
这就是01背包问题的基本思想。如果dp的空间大小合理,那么我们就可以来解决之前DFS所无法处理的复杂度。基本的模板如下:
```
for (auto x: nums) // 遍历物品
for (auto s= 0 to sum/2) // 遍历容量
for (auto s= sum/2 to 0) // 遍历容量
if dp'[s-x] = true
dp[s] = true // 如果考察x之前,已经能够凑出s-x,那么加上x这个数字就一定能凑出和为x的subset。
```

此外还有另外一种dp的写法
```
for (auto x: nums) // 遍历物品
for (auto s= 0 to sum/2) // 遍历容量
for (auto s= sum/2 to 0) // 遍历容量
if dp'[s] = true
dp[s+x] = true // 如果考察x之前,已经能够凑出s,那么加上x这个数字就一定能凑出和为s+x的subset。
```
Expand Down
View file Open in desktop
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
View file Open in desktop
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)`,所以可以将区间的乘积转化为"指数"区间的求和,再求一次幂即可。
2 changes: 2 additions & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
[2137.Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2137.Pour-Water-Between-Buckets-to-Make-Water-Levels-Equal) (M)
[2141.Maximum-Running-Time-of-N-Computers](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2141.Maximum-Running-Time-of-N-Computers) (M+)
[2226.Maximum-Candies-Allocated-to-K-Children](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2226.Maximum-Candies-Allocated-to-K-Children) (M)
[2439.Minimize-Maximum-of-Array](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2439.Minimize-Maximum-of-Array) (H-)
* ``Find K-th Element``
[215.Kth-Largest-Element-in-an-Array](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/215.Kth-Largest-Element-in-an-Array) (M)
[287.Find-the-Duplicate-Number](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/287.Find-the-Duplicate-Number) (H-)
Expand Down Expand Up @@ -1334,6 +1335,7 @@
[1906.Minimum-Absolute-Difference-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Others/1906.Minimum-Absolute-Difference-Queries) (M+)
[2245.Maximum-Trailing-Zeros-in-a-Cornered-Path](https://github.com/wisdompeak/LeetCode/tree/master/Others/2245.Maximum-Trailing-Zeros-in-a-Cornered-Path) (M)
[2281.Sum-of-Total-Strength-of-Wizards](https://github.com/wisdompeak/LeetCode/tree/master/Others/2281.Sum-of-Total-Strength-of-Wizards) (H)
[2438.Range-Product-Queries-of-Powers](https://github.com/wisdompeak/LeetCode/tree/master/Others/2438.Range-Product-Queries-of-Powers) (M+)
* ``2D Presum``
1314.Matrix-Block-Sum (M)
[1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/1292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold) (H-)
Expand Down

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