Skip to content

Commit 95d13e1

Browse files
committed
二刷406
1 parent a94e080 commit 95d13e1

File tree

14 files changed

+100
-29
lines changed

14 files changed

+100
-29
lines changed

docs/0000-27-greedy.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ image::images/greedy-01.png[{image_attr}]
2525
. xref:0376-wiggle-subsequence.adoc[376. Wiggle Subsequence]
2626
. xref:0397-integer-replacement.adoc[397. Integer Replacement]
2727
. xref:0402-remove-k-digits.adoc[402. Remove K Digits]
28+
. xref:0406-queue-reconstruction-by-height.adoc[406. Queue Reconstruction by Height]
2829
. xref:0409-longest-palindrome.adoc[409. Longest Palindrome]
2930
. xref:0410-split-array-largest-sum.adoc[410. Split Array Largest Sum]
3031
. xref:0420-strong-password-checker.adoc[420. Strong Password Checker]
Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,91 @@
11
[#0406-queue-reconstruction-by-height]
2-
= 406. Queue Reconstruction by Height
2+
= 406. 根据身高重建队列
33

4-
{leetcode}/problems/queue-reconstruction-by-height/[LeetCode - Queue Reconstruction by Height^]
4+
https://leetcode.cn/problems/queue-reconstruction-by-height/[LeetCode - 406. 根据身高重建队列 ^]
55

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~` 的人。
77

8-
*Note:*
8+
请你重新构造并返回输入数组 `people` 所表示的队列。返回的队列应该格式化为数组 `queue` ,其中 `queue[j] = [h~j~, k~j~]` 是队列中第 `j` 个人的属性(`queue[0]` 是排在队列前面的人)。
99

10-
The number of people is less than 1,100.
10+
*示例 1:*
1111

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+
....
1224

13-
.Example
14-
----
15-
Input:
16-
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
25+
*示例 2:*
1726

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+
....
2131

22-
这道题有点不明觉厉。
2332

24-
== 参考资料
33+
*提示:*
2534

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+
* 题目数据确保队列可以被重建
2739
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.
2940
30-
*Note:*
41+
== 思路分析
3142

43+
套路:一般这种数对,还涉及排序的,根据第一个元素正向排序,根据第二个元素反向排序,或者根据第一个元素反向排序,根据第二个元素正向排序,往往能够简化解题过程。
3244

33-
The number of people is less than 1,100.
34-
45+
image::images/0406-10.png[{image_attr}]
3546

36-
*Example*
47+
image::images/0406-11.png[{image_attr}]
3748

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}]
4250

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}]
4658

47-
59+
image::images/0406-17.png[{image_attr}]
4860

61+
image::images/0406-18.png[{image_attr}]
4962

5063

5164
[[src-0406]]
65+
[tabs]
66+
====
67+
一刷::
68+
+
69+
--
5270
[{java_src_attr}]
5371
----
5472
include::{sourcedir}/_0406_QueueReconstructionByHeight.java[tag=answer]
5573
----
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+
== 参考资料
5688

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. 根据身高重建队列 - 官方题解^]

docs/images/0406-10.png

76.2 KB
Loading

docs/images/0406-11.png

50.8 KB
Loading

docs/images/0406-12.png

27.2 KB
Loading

docs/images/0406-13.png

27.8 KB
Loading

docs/images/0406-14.png

29.9 KB
Loading

docs/images/0406-15.png

30.3 KB
Loading

docs/images/0406-16.png

31.6 KB
Loading

docs/images/0406-17.png

30.6 KB
Loading

0 commit comments

Comments
 (0)