当前位置:网站首页>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);
}
边栏推荐
- Test case writing specification in unittest framework and how to run test cases
- 流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码
- kafuka学习之路(一)kafuka安装和简单使用
- Software project management 9.2 Software project configuration management process
- About keil compiler, "file has been changed outside the editor, reload?" Solutions for
- Epoll introduction
- How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
- CPU 上下文切换的机制和类型 (CPU Context Switch)
- Comment Cao définit la décimale de dimension
- 印象深刻的bug汇总(持续更新)
猜你喜欢
随机推荐
邻接矩阵无向图(一) - 基本概念与C语言
Golang des-cbc
Vscode shortcut key (the most complete) [easy to understand]
Dlhsoft Kanban, Kanban component of WPF
Force button homepage introduction animation
Building external modules
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
Wechat applet development - user authorization to log in to "suggestions collection"
Learning summary on June 29, 2022
Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ
openinstall:微信小程序跳转H5配置业务域名教程
2022/6/30学习总结
Mechanism and type of CPU context switch
How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
Openinstall: wechat applet jump to H5 configuration service domain name tutorial
Are the consequences of securities account cancellation safe
Introduction to unittest framework and the first demo
Acly and metabolic diseases
Value/string in redis



![[MCU] [nixie tube] nixie tube display](/img/5e/9e14302b4e4f5e03601392ac90479d.png)




