当前位置:网站首页>leetcode 406. Queue Reconstruction by Height(按身高重建队列)
leetcode 406. Queue Reconstruction by Height(按身高重建队列)
2022-07-01 11:53:00 【蓝羽飞鸟】
You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.
Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of the jth person in the queue (queue[0] is the person at the front of the queue).
Example 1:
Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Explanation:
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
Example 2:
Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
people数组里面的每一组是[身高,前面比我高或者和我一样高的有几个人]
按“前面比我高的有几个人”给数组排个序
思路:
首先明确一点,假设现在已经排好了序,前面比我高的有3个人,那么往前面加一个比我矮的是不会影响现在的顺序的。
但是前面加一个比我高的就会有影响,那前面比我高的就是4个人了。
所以,先把people按身高(people[i][0])降序排序,确保高的先排好,然后再把矮的加进去,就不会影响顺序。
people[i] = [hi, ki], ki表示前面有ki个人比当前第i个people高,
因为是按身高从高到低排的,那现有的前面的都是比当前people高的,所以直接把他insert到第ki个位置,
然后后面来的矮的insert到他前面也无所谓,不影响ki。
理解这点是关键。
那身高相同的两个人怎么排的,是先排前面的(ki较小的)还是后面的(ki较大的)?
试想,如果先排后面的,比如(5,4):身高为5的人,前面有4个比他高或者和他一样高的,
再来一个(5,2),要insert到第2个,在(5,4)的前面,那么好了,前面和他一样高的多了一个,(5,4)该变成(5,5)了,受到了影响。
所以,如果身高一样,先排ki较小的。
public int[][] reconstructQueue(int[][] people) {
int n = people.length;
Arrays.sort(people, (a, b)->(a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
List<int[]> res = new LinkedList<>();
for(int[] tmp : people) {
res.add(tmp[1], tmp);
}
return res.toArray(people);
}
边栏推荐
- Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
- Neo4j 中文开发者月刊 - 202206期
- 深入理解 grpc part1
- redis配置环境变量
- CAD如何設置標注小數比特
- Dataset partitioning script for classification tasks
- 2022/6/29学习总结
- openinstall:微信小程序跳转H5配置业务域名教程
- [Maui] add click events for label, image and other controls
- 2022/6/30学习总结
猜你喜欢
随机推荐
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
Dlhsoft Kanban, Kanban component of WPF
VScode快捷键(最全)[通俗易懂]
Wonderful! MarkBERT
华为HMS Core携手超图为三维GIS注入新动能
研发效能度量框架解读
241. Design priority for operational expressions: DFS application questions
Can I open an account today and buy stocks today? Is it safe to open an account online?
微信小程序开发 – 用户授权登陆「建议收藏」
Redis启动与库进入
[classic example] classic list questions @ list
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
Abbirb120 industrial robot mechanical zero position
Talk about the pessimistic strategy that triggers full GC?
Brief explanation of the working principle, usage scenarios and importance of fingerprint browser
耐克如何常年霸榜第一名?最新財報答案來了
耐克如何常年霸榜第一名?最新财报答案来了
Custom grpc plug-in
epoll介绍
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing








