当前位置:网站首页>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);
}
边栏推荐
- 今天开户今天能买股票吗?在线开户是很安全么?
- I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
- About keil compiler, "file has been changed outside the editor, reload?" Solutions for
- redis中value/String
- [MCU] [nixie tube] nixie tube display
- Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
- Unity XLua 协程封装
- Why does the JVM heap memory exceed 32g and pointer compression fail?
- redis配置环境变量
- Golang根据参数引入相应配置文件的实现方式
猜你喜欢

自组织是管理者和成员的双向奔赴

Adjacency matrix undirected graph (I) - basic concepts and C language

GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021

Matrix of numpy

ABBIRB120工业机器人机械零点位置

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

Abbirb120 industrial robot mechanical zero position
![[Maui] add click events for label, image and other controls](/img/d6/7ac9632681c970ed99c9e4d3934ddc.jpg)
[Maui] add click events for label, image and other controls

On recursion and Fibonacci sequence

Learning summary on June 30, 2022
随机推荐
印象深刻的bug汇总(持续更新)
对于mvvm和mvc的理解
Impressive bug summary (continuously updated)
小米手机解BL锁教程
Can solo be accessed through IPv6?
Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
Vscode shortcut key (the most complete) [easy to understand]
Unity XLua 协程封装
How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
自定义 grpc 插件
Openinstall: wechat applet jump to H5 configuration service domain name tutorial
图的理论基础
Value/set in redis
ES6 promise Usage Summary
Golang des-cbc
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Understanding of MVVM and MVC
Value/string in redis
耐克如何常年霸榜第一名?最新財報答案來了
activity工作流引擎