forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #82
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
13 commits
Select commit
Hold shift + click to select a range
7a658c0
Create 2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K.cpp
wisdompeak 48c9b62
Update Readme.md
wisdompeak 800a37b
Create Readme.md
wisdompeak 3cf1ef8
Create 2361.Minimum-Costs-Using-the-Train-Line.cpp
wisdompeak f17627c
Update Readme.md
wisdompeak d3bcbc9
Create Readme.md
wisdompeak a86f29f
Create 2378.Choose-Edges-to-Maximize-Score-in-a-Tree.cpp
wisdompeak e593e62
Rename Dynamic_Programming/2378.Choose-Edges-to-Maximize-Score-in-a-T...
wisdompeak 9f79e25
Update Readme.md
wisdompeak b086302
Create Readme.md
wisdompeak 89ba3ef
Create 2387.Median-of-a-Row-Wise-Sorted-Matrix.cpp
wisdompeak 80abf21
Update Readme.md
wisdompeak ea974b6
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
25 changes: 25 additions & 0 deletions
...earch/2387.Median-of-a-Row-Wise-Sorted-Matrix/2387.Median-of-a-Row-Wise-Sorted-Matrix.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,25 @@ | ||
class Solution { | ||
public: | ||
int matrixMedian(vector<vector<int>>& grid) | ||
{ | ||
int m = grid.size(); | ||
int n = grid[0].size(); | ||
int k = (m*n+1)/2; | ||
|
||
int left = 0, right = INT_MAX; | ||
while (left < right) | ||
{ | ||
int mid = left+(right-left)/2; | ||
int count = 0; | ||
for (int i=0; i<m; i++) | ||
count += upper_bound(grid[i].begin(), grid[i].end(), mid) - grid[i].begin(); | ||
if (count < k) | ||
left = mid + 1; | ||
else | ||
right = mid; | ||
} | ||
|
||
return left; | ||
|
||
} | ||
}; |
9 changes: 9 additions & 0 deletions
Binary_Search/2387.Median-of-a-Row-Wise-Sorted-Matrix/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,9 @@ | ||
### 2387.Median-of-a-Row-Wise-Sorted-Matrix | ||
|
||
令`k=(m*n+1)/2`,本题就是求矩阵里的从小到大的第k个元素。本质和`215.Kth-Largest-Element-in-an-Array`相同,只不过需要将各行`smallerOrEqual(mid)`的结果累加起来得到count。 | ||
```cpp | ||
if (count < k) | ||
left = mid+1; | ||
else | ||
right = mid; | ||
``` |
26 changes: 26 additions & 0 deletions
...mming/2361.Minimum-Costs-Using-the-Train-Line/2361.Minimum-Costs-Using-the-Train-Line.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,26 @@ | ||
using LL = long long; | ||
class Solution { | ||
LL dp[100005][2]; | ||
public: | ||
vector<long long> minimumCosts(vector<int>& regular, vector<int>& express, int expressCost) | ||
{ | ||
int n = regular.size(); | ||
regular.insert(regular.begin(), 0); | ||
express.insert(express.begin(), 0); | ||
|
||
dp[0][0] = 0; | ||
dp[0][1] = expressCost; | ||
|
||
vector<LL>rets; | ||
|
||
for (int i=1; i<=n; i++) | ||
{ | ||
dp[i][0] = min(dp[i-1][0] + regular[i], dp[i-1][1] + regular[i]); | ||
dp[i][1] = min(dp[i-1][1] + express[i], dp[i-1][0] + expressCost + express[i]); | ||
|
||
rets.push_back(min(dp[i][0], dp[i][1])); | ||
} | ||
|
||
return rets; | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
Dynamic_Programming/2361.Minimum-Costs-Using-the-Train-Line/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,8 @@ | ||
### 2361.Minimum-Costs-Using-the-Train-Line | ||
|
||
很明显,状态变量dp[i][0]表示到达第i个车站的regular所需要的最小代价,dp[i][1]表示到达第i个车站的express所需要的最小代价。于是有转移方程: | ||
```cpp | ||
dp[i][0] = min(dp[i-1][0] + regular[i], dp[i-1][1] + regular[i]); | ||
dp[i][1] = min(dp[i-1][1] + express[i], dp[i-1][0] + expressCost + express[i]); | ||
``` | ||
注意我们不需要考虑dp[i][0]与dp[i][1]之间的转移。这是因为,我们如果想要从dp[i][0]转移到dp[i][1],其目的一定只是为了后续得到dp[i+1][1]。单独从第i站的角度来看,只要到了regular或express都算达成了任务,两者间的跳转对于第i站而言没有意义。 |
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
52 changes: 52 additions & 0 deletions
...hoose-Edges-to-Maximize-Score-in-a-Tree/2378.Choose-Edges-to-Maximize-Score-in-a-Tree.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,52 @@ | ||
using LL = long long; | ||
class Solution { | ||
vector<pair<int,LL>>children[100005]; | ||
LL memo[100005][2]; | ||
public: | ||
long long maxScore(vector<vector<int>>& edges) | ||
{ | ||
int n = edges.size(); | ||
int root = -1; | ||
for (int i=0; i<n; i++) | ||
{ | ||
if (edges[i][0]==-1) | ||
{ | ||
root = i; | ||
continue; | ||
} | ||
int par = edges[i][0]; | ||
int weight = edges[i][1]; | ||
children[par].push_back({i, weight}); | ||
} | ||
|
||
return dfs(root, 0); | ||
} | ||
|
||
LL dfs(int node, int status) | ||
{ | ||
if (memo[node][status]!=0) | ||
return memo[node][status]; | ||
|
||
if (status == 0) | ||
{ | ||
LL sum = 0; | ||
for (auto& [child, weight]: children[node]) | ||
sum += dfs(child, 0); | ||
|
||
LL maxSum = sum; | ||
for (auto& [child, weight]: children[node]) | ||
maxSum = max(maxSum, sum - dfs(child, 0) + dfs(child, 1) + weight); | ||
|
||
memo[node][0] = maxSum; | ||
return maxSum; | ||
} | ||
else | ||
{ | ||
LL sum = 0; | ||
for (auto& [child, weight]: children[node]) | ||
sum += dfs(child, 0); | ||
memo[node][1] = sum; | ||
return sum; | ||
} | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
Recursion/2378.Choose-Edges-to-Maximize-Score-in-a-Tree/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,11 @@ | ||
### 2378.Choose-Edges-to-Maximize-Score-in-a-Tree | ||
|
||
我们不难发现,如果从parent到node的edge被选中的话,那么node到它所有children的edge都不能选中。如果从parent到node的edge不被选中的话,那么node到它所有children的edge里只能最多选一条。 | ||
|
||
所以我们定义`dfs(node, 1)`表示从parent到node的edge被选中,那么以node为根的子树的最大收益。于是有 | ||
```cpp | ||
dfs(node, 1) = sum{dfs(child, 0)}; | ||
``` | ||
同理,定义`dfs(node, 0)`表示从parent到node的edge不被选中,那么以node为根的子树的最大收益。因为我们允许有一条node的子边被选中,所以需要遍历这个选择。为了便于计算,我们先求出`Sum = sum{dfs(child, 0)}`,那么对于任何一个child,如果它的边被选中的话,则整棵树的最大收益就是`Sum - dfs(child, 0) + dfs(child, 1) + weight`. 我们遍历child,再返回最大的作为`dfs(node,1)`. | ||
|
||
显然,此题需要记忆化来避免重复计算。 |
31 changes: 31 additions & 0 deletions
...Distinct-Subarrays-With-Length-K/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K.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,31 @@ | ||
using LL = long long; | ||
class Solution { | ||
public: | ||
long long maximumSubarraySum(vector<int>& nums, int k) | ||
{ | ||
LL ret = 0; | ||
unordered_map<int,int>Map; | ||
int count = 0; | ||
LL sum = 0; | ||
for (int i=0; i<nums.size(); i++) | ||
{ | ||
Map[nums[i]]++; | ||
if (Map[nums[i]]==1) | ||
count++; | ||
sum += nums[i]; | ||
|
||
if (i>=k-1) | ||
{ | ||
if (count == k) | ||
ret = max(ret, sum); | ||
|
||
Map[nums[i-k+1]]--; | ||
sum -= nums[i-k+1]; | ||
if (Map[nums[i-k+1]]==0) | ||
count--; | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
Two_Pointers/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/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,17 @@ | ||
### 2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K | ||
|
||
非常普通的固定长度的滑窗。 | ||
|
||
用一个HashMap来记录每个number出现的次数。用count来表示滑窗内的distinct number的数量。count的变动依据如下: | ||
1. 当新加入一个数字时 | ||
```cpp | ||
Map[num]++; | ||
if (Map[num]==1) | ||
count++; | ||
``` | ||
2. 当移出一个数字时 | ||
```cpp | ||
Map[num]--; | ||
if (Map[num]==0) | ||
count--; | ||
``` |
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.