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 #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
pull merged 10 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Nov 14, 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
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);
}
};
View file Open in desktop
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这个位置的数字一定也已经安排到了期望的数字。
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 @@ -240,6 +240,7 @@
[2313.Minimum-Flips-in-Binary-Tree-to-Get-Result](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2313.Minimum-Flips-in-Binary-Tree-to-Get-Result) (H)
[2322.Minimum-Score-After-Removals-on-a-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2322.Minimum-Score-After-Removals-on-a-Tree) (H-)
[2458.Height-of-Binary-Tree-After-Subtree-Removal-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2458.Height-of-Binary-Tree-After-Subtree-Removal-Queries) (M+)
[2467.Most-Profitable-Path-in-a-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2467.Most-Profitable-Path-in-a-Tree) (M+)
* ``Path in a tree``
[543.Diameter-of-Binary-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Tree/543.Diameter-of-Binary-Tree) (M)
[124.Binary-Tree-Maximum-Path-Sum](https://github.com/wisdompeak/LeetCode/tree/master/Tree/124.Binary-Tree-Maximum-Path-Sum) (M)
Expand Down Expand Up @@ -1222,6 +1223,7 @@
[442.Find-All-Duplicates-in-an-Array](https://github.com/wisdompeak/LeetCode/blob/master/Greedy/442.Find-All-Duplicates-in-an-Array) (M)
[448.Find-All-Numbers-Disappeared-in-an-Array](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/448.Find-All-Numbers-Disappeared-in-an-Array) (M)
[645.Set-Mismatch](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/645.Set-Mismatch) (M)
[2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level) (M+)
* ``Parenthesis``
[032.Longest-Valid-Parentheses](https://github.com/wisdompeak/LeetCode/tree/master/Stack/032.Longest-Valid-Parentheses) (H)
[921.Minimum-Add-to-Make-Parentheses-Valid](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/921.Minimum-Add-to-Make-Parentheses-Valid) (M+)
Expand Down
View file Open in desktop
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
View file Open in desktop
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)`。

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