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

Commit f118d91

Browse files
Create Readme.md
1 parent 6deccd4 commit f118d91

File tree

1 file changed

+7
-0
lines changed
  • Greedy/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level

1 file changed

+7
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### 2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level
2+
3+
将属于同一个level的数字都收集起来。然后用Indexing sort的方法去贪心地交换。
4+
5+
比如说,对于一个乱序的nums数组,我们可以提前知道每个数字的期望位置rank[nums[i]]。我们就从前往后查看每一个位置,如果当前的`rank[nums[i]]!=i`,那么就把nums[i]与位于rank[nums[i]]的数字交换。这样的交换可以持续多次,直至我们在i这个位置上迎来期望的数字。
6+
7+
为什么一定能够迎来期望的数字呢?因为每一次交换,我们都把一个数字送到了它应该在的位置。一旦把n-1个数字都放到了它们对应期望的位置,那么i这个位置的数字一定也已经安排到了期望的数字。

0 commit comments

Comments
(0)

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