当前位置:网站首页>LeetCode_406_根据身高重建队列
LeetCode_406_根据身高重建队列
2022-07-28 08:14:00 【Fitz1318】
题目链接
题目描述
假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。
请你重新构造并返回输入数组 people 所表示的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队列前面的人)。
示例 1:
输入:people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
输出:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
解释:
编号为 0 的人身高为 5 ,没有身高更高或者相同的人排在他前面。
编号为 1 的人身高为 7 ,没有身高更高或者相同的人排在他前面。
编号为 2 的人身高为 5 ,有 2 个身高更高或者相同的人排在他前面,即编号为 0 和 1 的人。
编号为 3 的人身高为 6 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。
编号为 4 的人身高为 4 ,有 4 个身高更高或者相同的人排在他前面,即编号为 0、1、2、3 的人。
编号为 5 的人身高为 7 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。
因此 [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] 是重新构造后的队列。
示例 2:
输入:people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
输出:[[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
提示:
1 <= people.length <= 20000 <= hi <= 10^60 <= ki < people.length- 题目数据确保队列可以被重建
解题思路
贪心法
- 按照身高来排序,身高相同的按照k从小到大来排序
AC代码
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a, b) -> {
if (a[0] == b[0]) {
return a[1] - b[1];
}
return b[0] - a[0];
});
LinkedList<int[]> ans = new LinkedList<>();
for (int[] p : people) {
ans.add(p[1], p);
}
return ans.toArray(new int[people.length][]);
}
}
边栏推荐
- I am a 27 year old technical manager, whose income is too high, and my heart is in a panic
- CSV file storage
- Detailed explanation of switch link aggregation [Huawei ENSP]
- PostgreSQL queries [table field type] and [all series] in the library
- 12 common design ideas of design for failure
- Kubernetes cluster configuration serviceaccount
- Do you know the five minute rule and the ten byte rule?
- Argocd Web UI loading is slow? A trick to teach you to solve
- Sentry log management system installation and use tutorial
- Why is the text box of Google material design not used?
猜你喜欢

Dapp安全总结与典型安全事件分析

象棋机器人夹伤7岁男孩手指,软件测试工程师的锅?我笑了。。。
![Detailed explanation of switch link aggregation [Huawei ENSP]](/img/c2/f9797fe8b17a418466b60cc3dc50a1.png)
Detailed explanation of switch link aggregation [Huawei ENSP]

公众号简介

Mongodb (compare relational database, cloud database, common command line, tutorial)

VS2015使用dumpbin 查看库的导出函数符号

台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】

SQL injection - pre Foundation

c语言数组指针和指针数组辨析,浅析内存泄漏

Marketing play is changeable, and understanding the rules is the key!
随机推荐
完善的交叉编译环境记录 peta 生成的shell 脚本
推荐一个摆脱变量名纠结的神器和批量修改文件名方法
Implementation principle of golang synergy
为什么 ThreadLocal 可以做到线程隔离?
You're not still using xshell, are you? This open source terminal tool is yyds!
修改虚拟机IP地址
Distributed system architecture theory and components
Go channel
Post it notes -- 45 {packaging of the uniapp component picker, for data transmission and processing -- Based on the from custom packaging that will be released later}
1w5 words to introduce those technical solutions of distributed system in detail
Data analysis interview question summary
MDM数据质量应用说明
谷歌 Material Design 的文本框为什么没人用?
[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture
Kubernetes technology and Architecture (VII)
Network interface network crystal head RJ45, Poe interface definition line sequence
Sentinel
Learn to draw with nature communications -- complex violin drawing
Solution: indexerror: index 13 is out of bounds for dimension 0 with size 13
Among China's top ten national snacks, it is actually the first