|
1 | 1 | [#0406-queue-reconstruction-by-height]
|
2 |
| -= 406. Queue Reconstruction by Height |
| 2 | += 406. 根据身高重建队列 |
3 | 3 |
|
4 |
| -{leetcode}/problems/queue-reconstruction-by-height/[LeetCode - Queue Reconstruction by Height^] |
| 4 | +https://leetcode.cn/problems/queue-reconstruction-by-height/[LeetCode - 406. 根据身高重建队列 ^] |
5 | 5 |
|
6 |
| -Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers `(h, k)`, where `h` is the height of the person and `k` is the number of people in front of this person who have a height greater than or equal to `h`. Write an algorithm to reconstruct the queue. |
| 6 | +假设有打乱顺序的一群人站成一个队列,数组 `people` 表示队列中一些人的属性(不一定按顺序)。每个 `people[i] = [h~i~, k~i~]` 表示第 `i` 个人的身高为 `h~i~` ,前面 *正好* 有 `k~i~` 个身高大于或等于 `h~i~` 的人。 |
7 | 7 |
|
8 |
| -*Note:* |
| 8 | +请你重新构造并返回输入数组 `people` 所表示的队列。返回的队列应该格式化为数组 `queue` ,其中 `queue[j] = [h~j~, k~j~]` 是队列中第 `j` 个人的属性(`queue[0]` 是排在队列前面的人)。 |
9 | 9 |
|
10 |
| -The number of people is less than 1,100. |
| 10 | +*示例 1:* |
11 | 11 |
|
| 12 | +.... |
| 13 | +输入:people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] |
| 14 | +输出:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] |
| 15 | +解释: |
| 16 | +编号为 0 的人身高为 5 ,没有身高更高或者相同的人排在他前面。 |
| 17 | +编号为 1 的人身高为 7 ,没有身高更高或者相同的人排在他前面。 |
| 18 | +编号为 2 的人身高为 5 ,有 2 个身高更高或者相同的人排在他前面,即编号为 0 和 1 的人。 |
| 19 | +编号为 3 的人身高为 6 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。 |
| 20 | +编号为 4 的人身高为 4 ,有 4 个身高更高或者相同的人排在他前面,即编号为 0、1、2、3 的人。 |
| 21 | +编号为 5 的人身高为 7 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。 |
| 22 | +因此 [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] 是重新构造后的队列。 |
| 23 | +.... |
12 | 24 |
|
13 |
| -.Example |
14 |
| ----- |
15 |
| -Input: |
16 |
| -[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] |
| 25 | +*示例 2:* |
17 | 26 |
|
18 |
| -Output: |
19 |
| -[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] |
20 |
| ----- |
| 27 | +.... |
| 28 | +输入:people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]] |
| 29 | +输出:[[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]] |
| 30 | +.... |
21 | 31 |
|
22 |
| -这道题有点不明觉厉。 |
23 | 32 |
|
24 |
| -== 参考资料 |
| 33 | +*提示:* |
25 | 34 |
|
26 |
| -. https://leetcode-cn.com/problems/queue-reconstruction-by-height/solution/gen-ju-shen-gao-zhong-jian-dui-lie-by-leetcode/[根据身高重建队列 - 根据身高重建队列 - 力扣(LeetCode)^] |
| 35 | +* `+1 <= people.length <= 2000+` |
| 36 | +* `0 \<= h~i~ \<= 10^6^` |
| 37 | +* `0 \<= k~i~ < people.length` |
| 38 | +* 题目数据确保队列可以被重建 |
27 | 39 |
|
28 |
| -Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers `(h, k)`, where `h` is the height of the person and `k` is the number of people in front of this person who have a height greater than or equal to `h`. Write an algorithm to reconstruct the queue. |
29 | 40 |
|
30 |
| -*Note:* |
| 41 | +== 思路分析 |
31 | 42 |
|
| 43 | +套路:一般这种数对,还涉及排序的,根据第一个元素正向排序,根据第二个元素反向排序,或者根据第一个元素反向排序,根据第二个元素正向排序,往往能够简化解题过程。 |
32 | 44 |
|
33 |
| -The number of people is less than 1,100. |
34 |
| - |
| 45 | +image::images/0406-10.png[{image_attr}] |
35 | 46 |
|
36 |
| -*Example* |
| 47 | +image::images/0406-11.png[{image_attr}] |
37 | 48 |
|
38 |
| -[subs="verbatim,quotes,macros"] |
39 |
| ----- |
40 |
| -Input: |
41 |
| -[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] |
| 49 | +image::images/0406-12.png[{image_attr}] |
42 | 50 |
|
43 |
| -Output: |
44 |
| -[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] |
45 |
| ----- |
| 51 | +image::images/0406-13.png[{image_attr}] |
| 52 | + |
| 53 | +image::images/0406-14.png[{image_attr}] |
| 54 | + |
| 55 | +image::images/0406-15.png[{image_attr}] |
| 56 | + |
| 57 | +image::images/0406-16.png[{image_attr}] |
46 | 58 |
|
47 |
| - |
| 59 | +image::images/0406-17.png[{image_attr}] |
48 | 60 |
|
| 61 | +image::images/0406-18.png[{image_attr}] |
49 | 62 |
|
50 | 63 |
|
51 | 64 | [[src-0406]]
|
| 65 | +[tabs] |
| 66 | +==== |
| 67 | +一刷:: |
| 68 | ++ |
| 69 | +-- |
52 | 70 | [{java_src_attr}]
|
53 | 71 | ----
|
54 | 72 | include::{sourcedir}/_0406_QueueReconstructionByHeight.java[tag=answer]
|
55 | 73 | ----
|
| 74 | +-- |
| 75 | +
|
| 76 | +二刷:: |
| 77 | ++ |
| 78 | +-- |
| 79 | +[{java_src_attr}] |
| 80 | +---- |
| 81 | +include::{sourcedir}/_0406_QueueReconstructionByHeight_2.java[tag=answer] |
| 82 | +---- |
| 83 | +-- |
| 84 | +==== |
| 85 | + |
| 86 | + |
| 87 | +== 参考资料 |
56 | 88 |
|
| 89 | +. https://leetcode.cn/problems/queue-reconstruction-by-height/solutions/486493/xian-pai-xu-zai-cha-dui-dong-hua-yan-shi-suan-fa-g/?envType=problem-list-v2&envId=9UZ9R3lB[406. 根据身高重建队列 - 【先排序,再插队】动画演示算法过程,有点小套路^] |
| 90 | +. https://leetcode.cn/problems/queue-reconstruction-by-height/solutions/486337/406du-shuo-shi-tan-xin-na-yao-wei-shi-yao-yong-tan/?envType=problem-list-v2&envId=9UZ9R3lB[406. 根据身高重建队列 - 「代码随想录」带你学透贪心算法!^] |
| 91 | +. https://leetcode.cn/problems/queue-reconstruction-by-height/solutions/486066/gen-ju-shen-gao-zhong-jian-dui-lie-by-leetcode-sol/?envType=problem-list-v2&envId=9UZ9R3lB[406. 根据身高重建队列 - 官方题解^] |
0 commit comments