forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #87
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
10 commits
Select commit
Hold shift + click to select a range
fc2d1a6
Create 2467.Most-Profitable-Path-in-a-Tree.cpp
wisdompeak 168eff5
Update Readme.md
wisdompeak ab29523
Create Readme.md
wisdompeak d827130
Update Readme.md
wisdompeak 950bc19
Update 2467.Most-Profitable-Path-in-a-Tree.cpp
wisdompeak 22164d9
Update 2467.Most-Profitable-Path-in-a-Tree.cpp
wisdompeak feeb9ff
Create 2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Lev...
wisdompeak 380b4f0
Update Readme.md
wisdompeak 6deccd4
Update 2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Lev...
wisdompeak f118d91
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
50 changes: 50 additions & 0 deletions
...Binary-Tree-by-Level/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level.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,50 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
vector<int>level[100005]; | ||
int maxDepth = 0; | ||
public: | ||
int minimumOperations(TreeNode* root) | ||
{ | ||
dfs(root, 0); | ||
int count = 0; | ||
for (int t=0; t<=maxDepth; t++) | ||
{ | ||
auto& nums = level[t]; | ||
auto sorted = nums; | ||
sort(sorted.begin(), sorted.end()); | ||
unordered_map<int,int>rank; | ||
for (int i=0; i<sorted.size(); i++) | ||
rank[sorted[i]] = i; | ||
|
||
int n = nums.size(); | ||
for (int i=0; i<n; i++) | ||
{ | ||
while (rank[nums[i]] != i) | ||
{ | ||
swap(nums[i], level[t][rank[nums[i]]]); | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
void dfs(TreeNode* node, int depth) | ||
{ | ||
if (node==NULL) return; | ||
maxDepth = max(maxDepth, depth); | ||
level[depth].push_back(node->val); | ||
dfs(node->left, depth+1); | ||
dfs(node->right, depth+1); | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/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,7 @@ | ||
### 2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level | ||
|
||
将属于同一个level的数字都收集起来。然后用Indexing sort的方法去贪心地交换。 | ||
|
||
比如说,对于一个乱序的nums数组,我们可以提前知道每个数字的期望位置rank[nums[i]]。我们就从前往后查看每一个位置,如果当前的`rank[nums[i]]!=i`,那么就把nums[i]与位于rank[nums[i]]的数字交换。这样的交换可以持续多次,直至我们在i这个位置上迎来期望的数字。 | ||
|
||
为什么一定能够迎来期望的数字呢?因为每一次交换,我们都把一个数字送到了它应该在的位置。一旦把n-1个数字都放到了它们对应期望的位置,那么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
68 changes: 68 additions & 0 deletions
Tree/2467.Most-Profitable-Path-in-a-Tree/2467.Most-Profitable-Path-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,68 @@ | ||
class Solution { | ||
int b[100005]; | ||
vector<int>next[100005]; | ||
int ret = INT_MIN/2; | ||
int bob; | ||
vector<int>amount; | ||
public: | ||
int mostProfitablePath(vector<vector<int>>& edges, int bob, vector<int>& amount) | ||
{ | ||
this->bob = bob; | ||
this->amount = amount; | ||
|
||
int n = amount.size(); | ||
for (int i=0; i<n; i++) | ||
b[i] = INT_MAX/2; | ||
|
||
for (auto& edge: edges) | ||
{ | ||
int a = edge[0], b = edge[1]; | ||
next[a].push_back(b); | ||
next[b].push_back(a); | ||
} | ||
|
||
dfs(0, -1, 0); | ||
|
||
dfs2(0, -1, 0, 0); | ||
return ret; | ||
} | ||
|
||
void dfs(int cur, int parent, int step) | ||
{ | ||
if (cur==bob) | ||
{ | ||
b[cur] = 0; | ||
return; | ||
} | ||
|
||
int toBob = INT_MAX/2; | ||
for (int nxt: next[cur]) | ||
{ | ||
if (nxt==parent) continue; | ||
dfs(nxt, cur, step+1); | ||
toBob = min(toBob, b[nxt]+1); | ||
} | ||
b[cur] = toBob; | ||
return; | ||
} | ||
|
||
void dfs2(int cur, int parent, int step, int score) | ||
{ | ||
if (step == b[cur]) | ||
score += amount[cur]/2; | ||
else if (step < b[cur]) | ||
score += amount[cur]; | ||
|
||
if (next[cur].size()==1 && next[cur][0]==parent) | ||
{ | ||
ret = max(ret, score); | ||
return; | ||
} | ||
|
||
for (int nxt: next[cur]) | ||
{ | ||
if (nxt==parent) continue; | ||
dfs2(nxt, cur, step+1, score); | ||
} | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
Tree/2467.Most-Profitable-Path-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,7 @@ | ||
### 2467.Most-Profitable-Path-in-a-Tree | ||
|
||
两次遍历全树。第一次遍历求得每个点到bob所在节点的距离(但只限bob节点往上的部分)。第二次遍历求每个点与root的距离。 | ||
|
||
对于每个节点而言,如果前者大于后者,则对于Alice而言没有收益。如果前者小于后者,则Alice可以拿取该节点的价值。基于这个原则,第二次dfs的时候可以求得收益最大的一条从root到leaf的路径。 | ||
|
||
通过本题,需要掌握不需要建rooted tree的dfs方法,即`dfs(cur, parent)`。当`next[cur]!=parent`时,可以继续递归`dfs(next[cur], cur)`。 |
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.