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 #15

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 44 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
19ce837
Create 2322.Minimum-Score-After-Removals-on-a-Tree.cpp
wisdompeak Jun 27, 2022
d145aa5
Update Readme.md
wisdompeak Jun 27, 2022
092df5a
Update 2311.Longest-Binary-Subsequence-Less-Than-or-Equal-to-K_v1.cpp
wisdompeak Jun 27, 2022
07fb2b5
Update Readme.md
wisdompeak Jun 27, 2022
e74d781
Update 327.Count-of-Range-Sum.cpp
wisdompeak Jun 27, 2022
de74fd8
Update 327.Count-of-Range-Sum.cpp
wisdompeak Jun 27, 2022
9f284b5
Update Readme.md
wisdompeak Jun 27, 2022
9e8fe66
Update Readme.md
wisdompeak Jun 27, 2022
96355ed
Update Readme.md
wisdompeak Jun 27, 2022
fe1779d
Update 315.Count-of-Smaller-Numbers-After-Self.cpp
wisdompeak Jun 27, 2022
a5504ee
Update 1649.Create-Sorted-Array-through-Instructions_DivideConque.cpp
wisdompeak Jun 27, 2022
c30a5cc
Update 2141.Maximum-Running-Time-of-N-Computers.cpp
wisdompeak Jun 27, 2022
814052e
Create Readme.md
wisdompeak Jun 27, 2022
df6a1de
Create 2321.Maximum-Score-Of-Spliced-Array.cpp
wisdompeak Jun 27, 2022
ebee723
Update Readme.md
wisdompeak Jun 27, 2022
289068f
Update 2322.Minimum-Score-After-Removals-on-a-Tree.cpp
wisdompeak Jun 27, 2022
72d51bc
Create Readme.md
wisdompeak Jun 27, 2022
fdf2f4c
Create 2320.Count-Number-of-Ways-to-Place-Houses.cpp
wisdompeak Jun 27, 2022
8069772
Update Readme.md
wisdompeak Jun 27, 2022
8f62433
Create 2320.Count-Number-of-Ways-to-Place-Houses_v2.cpp
wisdompeak Jun 27, 2022
89cbb34
Update and rename 2320.Count-Number-of-Ways-to-Place-Houses.cpp to 23...
wisdompeak Jun 27, 2022
1e355d5
Create 2320.Count-Number-of-Ways-to-Place-Houses_v3.cpp
wisdompeak Jun 27, 2022
7919316
Create Readme.md
wisdompeak Jun 27, 2022
0c1c944
Create 2318.Number-of-Distinct-Roll-Sequences.cpp
wisdompeak Jun 28, 2022
b50deba
Update Readme.md
wisdompeak Jun 28, 2022
06aa0b0
Create Readme.md
wisdompeak Jun 28, 2022
d586a6b
Update 2318.Number-of-Distinct-Roll-Sequences.cpp
wisdompeak Jun 29, 2022
b72aba5
Update 1268.Search-Suggestions-System_v1.cpp
wisdompeak Jun 30, 2022
7d9a6fe
Update Readme.md
wisdompeak Jun 30, 2022
397844f
Create 1102.Path-With-Maximum-Minimum-Value.cpp_v2.cpp
wisdompeak Jul 2, 2022
d038b71
Update Readme.md
wisdompeak Jul 2, 2022
7a5ee83
Update 1102.Path-With-Maximum-Minimum-Value.cpp_v2.cpp
wisdompeak Jul 2, 2022
d05b324
Update 1102.Path-With-Maximum-Minimum-Value.cpp
wisdompeak Jul 2, 2022
8012977
Rename 1102.Path-With-Maximum-Minimum-Value.cpp to 1102.Path-With-Max...
wisdompeak Jul 2, 2022
cdaf272
Rename 1102.Path-With-Maximum-Minimum-Value.cpp_v2.cpp to 1102.Path-W...
wisdompeak Jul 2, 2022
c1173a7
Update Readme.md
wisdompeak Jul 2, 2022
e0ae1fb
Update Readme.md
wisdompeak Jul 2, 2022
407df52
Update Readme.md
wisdompeak Jul 2, 2022
aee6d6d
Update Readme.md
wisdompeak Jul 2, 2022
afa8f7c
Update Readme.md
wisdompeak Jul 2, 2022
adbf372
Update Readme.md
wisdompeak Jul 2, 2022
cdce66c
Update Readme.md
wisdompeak Jul 2, 2022
a1c4767
Update 1388.Pizza-With-3n-Slices.cpp
wisdompeak Jul 2, 2022
56d6d6b
Update Readme.md
wisdompeak Jul 2, 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
@@ -1,41 +1,31 @@
class Solution {
public:
int maximumMinimumPath(vector<vector<int>>& A)
int maximumMinimumPath(vector<vector<int>>& grid)
{
int left = INT_MAX;
int right = INT_MIN;
int M = A.size();
int N = A[0].size();
for (int i=0; i<M; i++)
for (int j=0; j<N; j++)
{
left = min(left,A[i][j]);
right = max(right,A[i][j]);
}

int left = 0, right = 1e9;
while (left<right)
{
int mid = right-(right-left)/2;

if (check(A,mid))
if (check(grid,mid))
left = mid;
else
right = mid-1;
}
return left;
}

bool check(vector<vector<int>> A, int K)
bool check(vector<vector<int>> grid, int K)
{
if (A[0][0]<K) return false;
if (grid[0][0]<K) return false;
auto dir = vector<pair<int,int>>({{1,0},{-1,0},{0,1},{0,-1}});

int M = A.size();
int N = A[0].size();
int M = grid.size(), N = grid[0].size();

queue<pair<int,int>>q;
q.push({0,0});
A[0][0] = -1;
vector<vector<int>>visited(M, vector<int>(N));
visited[0][0] = 1;

while (q.size()>0)
{
Expand All @@ -47,19 +37,16 @@ class Solution {
{
int i = x+dir[k].first;
int j = y+dir[k].second;
if (i<0||i>=M||j<0||j>=N)
continue;
if (A[i][j]==-1)
continue;
if (A[i][j]<K) continue;
if (i<0||i>=M||j<0||j>=N) continue;
if (visited[i][j]) continue;
if (grid[i][j]<K) continue;
if (i==M-1 && j==N-1) return true;
A[i][j]=-1;
visited[i][j] = 1;

q.push({i,j});
}
}

return false;

}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AI3 = array<int,3>;
class Solution {
vector<pair<int,int>>dir = {{1,0},{-1,0},{0,1},{0,-1}};
public:
int maximumMinimumPath(vector<vector<int>>& grid)
{
int M = grid.size();
int N = grid[0].size();
priority_queue<AI3>pq;

pq.push({grid[0][0], 0,0});
vector<vector<int>>visited(M, vector<int>(N,0));
vector<vector<int>>rets(M, vector<int>(N,0));

while (pq.size()>0)
{
auto [d, x, y] = pq.top();
pq.pop();
if (visited[x][y]==1) continue;
rets[x][y] = d;
visited[x][y] = 1;
if (x==M-1 && y==N-1)
return d;

for (int k=0; k<4; k++)
{
int i = x+dir[k].first;
int j = y+dir[k].second;
if (i<0||i>=M||j<0||j>=N)
continue;
pq.push({min(d, grid[i][j]), i, j});
}
}

return -1;
}
};
16 changes: 12 additions & 4 deletions Binary_Search/1102.Path-With-Maximum-Minimum-Value/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
### 1102.Path-With-Maximum-Minimum-Value

此题是二分法的非常精彩的应用.
#### 解法1:二分搜索

我们想,如果这个maximum score是x,那么意味着存在一条路径,里面不能有任何小于x的格子.因此,如果给定x,我们尝试用BFS的方法从左上角走到右下角.如果能抵达,说明至少存在一条成功的路,他们所有的元素都不会小于x,而且x还可能有提升的空间.相反,如果不能走到,说明从左上到右下被一些列小于x的各自给阻断了,因此我们对于maximum score的预期必须下调,至少得小于x.
我们想,如果这个maximum score是x,那么意味着存在一条路径,里面不能有任何小于x的格子因此,如果给定x,我们尝试用BFS的方法从左上角走到右下角,约定不能经过任何小于x的格子。如果能抵达,说明至少存在一条成功的路,其沿途元素都不会小于x,故x是一个可行解,我们可以调高对maximum score的预期。相反,如果不能走到,说明从左上到右下肯定会被一系列小于x的格子给阻断了,因此我们对于maximum score的预期必须下调,至少得小于x.

所以二分的策略就非常清楚了:
```
```cpp
while (left<right)
{
int mid = right-(right-left)/2;
Expand All @@ -17,5 +17,13 @@
```
因为我们在收缩范围的时候,永远是将可行解放在闭区间[left,right]内,不可行解排除在闭区间外.所以当left和right收敛的时候,一定是一个可行解,直接返回left即可.

#### 解法2:Dijkstra
此题还可以用Dijkstra来解决,不过这是一个与常规Dijkstra问题完全对偶的场景。

[Leetcode Link](https://leetcode.com/problems/path-with-maximum-minimum-value)
Dijkstra通常用在求最短路径的问题,在非负边权的图里,经过的节点越多,往往意味着路径会更长。所以在小顶堆的PQ中越早弹出来的节点,对应的就是该节点的最短路径。

本题反其道而行之,求的是一个"最长路径"的问题。但这道题定义的"路径长度"不是元素和,而是沿途元素里面的最小值。所以经过的格子越多,"路径长度"反而会越小。所以结合大顶堆(而不是通常的小顶堆),也能够将这道题解出来。

注:此题和[1631](https://github.com/wisdompeak/LeetCode/tree/master/Union_Find/1631.Path-With-Minimum-Effort)非常相似。

[Leetcode Link](https://leetcode.com/problems/path-with-maximum-minimum-value)
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
using LL = long long;
class Solution {
class Solution {
public:
long long maxRunTime(int n, vector<int>& batteries)
{
LL left = 0, right = LLONG_MAX/2;
// sort(batteries.rbegin(), batteries.rend());

LL left = 0, right = LLONG_MAX/n;
while (left < right)
{
LL mid = right-(right-left)/2;
if (checkOK(mid, batteries, n))
left = mid;
if (checkOK(mid, n, batteries))
left = mid;
else
right = mid-1;
right = mid-1;
}
return left;
return left;
}

bool checkOK(LL T, vector<int>&nums, int n)
bool checkOK(LL T, LL n, vector<int>& batteries)
{
int count = 0;
LL cur = 0;
for (int i=0; i<nums.size(); i++)
LL sum = 0;
for (auto x: batteries)
{
cur+=min((LL)nums[i], T);
while (cur >= T)
{
count++;
cur-=T;
}
if (count >= n)
sum += min((LL)x, T);
if (sum >= T*n)
return true;
}
return false;
}
};

Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,36 @@ class Solution {
numSmaller[i] += iter-(sorted+a);
}

int i=a, j=mid+1, p = 0;
while (i<=mid && j<=b)
{
if (sorted[i]<=sorted[j])
{
temp[p] = sorted[i];
i++;
}
else
{
temp[p] = sorted[j];
j++;
}
p++;
}
while (i<=mid)
{
temp[p] = sorted[i];
i++;
p++;
}
while (j<=b)
{
temp[p] = sorted[j];
j++;
p++;
}
for (int i=0; i<b-a+1; i++)
sorted[a+i] = temp[i];
inplace_merge(sorted+a, sorted+mid+1, sorted+b+1);

// int i=a, j=mid+1, p = 0;
// while (i<=mid && j<=b)
// {
// if (sorted[i]<=sorted[j])
// {
// temp[p] = sorted[i];
// i++;
// }
// else
// {
// temp[p] = sorted[j];
// j++;
// }
// p++;
// }
// while (i<=mid)
// {
// temp[p] = sorted[i];
// i++;
// p++;
// }
// while (j<=b)
// {
// temp[p] = sorted[j];
// j++;
// p++;
// }
// for (int i=0; i<b-a+1; i++)
// sorted[a+i] = temp[i];
}
};


// X X 3 X 3 Y Y Y
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Solution {
}

// 将两段已经有序的数组段start~mid,mid+1~end合起来排序。
// 如果写归并排序的code会更快一些。这里就偷懒了,直接用sort函数。
sort(sortedNums.begin()+start,sortedNums.begin()+end+1);
inplace_merge(sortedNums.begin()+start, sortedNums.begin()+mid+1, sortedNums.begin()+end+1);
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

最后注意,本题需要三个数组,nums, sortedNums, count。原来的数据存在nums, 归并排序后的数组存在sortedNums, count[i]对应的是nums[i]的 number of smaller elements to the right.

补充:inplace_merge(iter1, iter2, iter3)可以实现[iter1,iter2)和[iter2,iter3)两段区间的归并排序(前提是两段各自有序)。

[Leetcode Link](https://leetcode.com/problems/count-of-smaller-numbers-after-self)

[Leetcode Link](https://leetcode.com/problems/count-of-smaller-numbers-after-self)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution {
int result;
long temp[10001];
long temp[100005];
public:
int countRangeSum(vector<int>& nums, int lower, int upper)
{
Expand All @@ -26,34 +26,36 @@ class Solution {
result+=p2-p1;
}

int i=a, j=mid+1, p = 0;
while (i<=mid && j<=b)
{
if (nums[i]<=nums[j])
{
temp[p] = nums[i];
i++;
}
else
{
temp[p] = nums[j];
j++;
}
p++;
}
while (i<=mid)
{
temp[p] = nums[i];
i++;
p++;
}
while (j<=b)
{
temp[p] = nums[j];
j++;
p++;
}
for (int i=0; i<b-a+1; i++)
nums[a+i] = temp[i];
inplace_merge(nums.begin()+a, nums.begin()+mid+1, nums.begin()+b+1);

// int i=a, j=mid+1, p = 0;
// while (i<=mid && j<=b)
// {
// if (nums[i]<=nums[j])
// {
// temp[p] = nums[i];
// i++;
// }
// else
// {
// temp[p] = nums[j];
// j++;
// }
// p++;
// }
// while (i<=mid)
// {
// temp[p] = nums[i];
// i++;
// p++;
// }
// while (j<=b)
// {
// temp[p] = nums[j];
// j++;
// p++;
// }
// for (int i=0; i<b-a+1; i++)
// nums[a+i] = temp[i];
}
};
3 changes: 2 additions & 1 deletion Divide_Conquer/327.Count-of-Range-Sum/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@

本题的另一个训练点就是对C++的STL里lower_bound的考察。如何写自定义比较函数是关键。我们需要在右序列中找到下限的位置,希望找到的位置在原序列中是大于等于sums[i]+lower的,所以自定义函数里要写a<b。另一方面,我们需要在右序列中找到上限的位置,希望找到的位置在原序列中是大于sums[i]+upper的,所以自定义函数里要写a<=b。最终pos2-pos1表示原序列里处于[sums[i]+lower,sums[i]+upper]闭区间的个数。

补充:```inplace_merge(iter1, iter2, iter3)```可以实现```[iter1,iter2)```和```[iter2,iter3)```两段区间的归并排序(前提是两段各自有序)。

[Leetcode Link](https://leetcode.com/problems/count-of-range-sum)
[Leetcode Link](https://leetcode.com/problems/count-of-range-sum)
3 changes: 2 additions & 1 deletion Divide_Conquer/493.Reverse-Pairs/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

另外,两个有序数组的归并排序操作,代码要熟练掌握。

补充:inplace_merge(iter1, iter2, iter3)可以实现[iter1,iter2)和[iter2,iter3)两段区间的归并排序(前提是两段各自有序)。

[Leetcode Link](https://leetcode.com/problems/reverse-pairs)
[Leetcode Link](https://leetcode.com/problems/reverse-pairs)
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ class Solution {
}

int helper(int st, int en, int k, vector<int>& slices)
{
vector<int>f(k+1,0); // f[i]: the maximum gain by the current round if we take i slices, and we do take the current slice.
vector<int>g(k+1,0); // g[i]: the maximum gain by the current round if we take i slices, and we do NOT take the current slice.
{
vector<int>dp0(k+1);
vector<int>dp1(k+1);

for (int i=st; i<=en; i++)
for (int j=min(k,i-st+1); j>=1; j--)
{
g[j] = max(g[j], f[j]);
f[j] = g[j-1] + slices[i];
dp0[j] = max(dp0[j], dp1[j]);
dp1[j] = dp0[j-1] + slices[i];
}

return max(f[k], g[k]);
return max(dp0[k], dp1[k]);
}
};
Loading

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