diff --git a/Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level.cpp b/Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level.cpp new file mode 100644 index 000000000..1708f0a3f --- /dev/null +++ b/Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level.cpp @@ -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 { + vectorlevel[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_maprank; + for (int i=0; ival); + dfs(node->left, depth+1); + dfs(node->right, depth+1); + } +}; diff --git a/Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Readme.md b/Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Readme.md new file mode 100644 index 000000000..62d16b8f3 --- /dev/null +++ b/Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Readme.md @@ -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这个位置的数字一定也已经安排到了期望的数字。 diff --git a/Readme.md b/Readme.md index d0fba82b9..509d25d92 100644 --- a/Readme.md +++ b/Readme.md @@ -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) @@ -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+) diff --git a/Tree/2467.Most-Profitable-Path-in-a-Tree/2467.Most-Profitable-Path-in-a-Tree.cpp b/Tree/2467.Most-Profitable-Path-in-a-Tree/2467.Most-Profitable-Path-in-a-Tree.cpp new file mode 100644 index 000000000..25e5f7de9 --- /dev/null +++ b/Tree/2467.Most-Profitable-Path-in-a-Tree/2467.Most-Profitable-Path-in-a-Tree.cpp @@ -0,0 +1,68 @@ +class Solution { + int b[100005]; + vectornext[100005]; + int ret = INT_MIN/2; + int bob; + vectoramount; +public: + int mostProfitablePath(vector>& edges, int bob, vector& amount) + { + this->bob = bob; + this->amount = amount; + + int n = amount.size(); + for (int i=0; i

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