当前位置:网站首页>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][]);
}
}
边栏推荐
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- After reading these 12 interview questions, the new media operation post is yours
- 侯捷STL标准库和泛型编程
- When I use MySQL CDC, there are 100 million pieces of data in the source table. In the full volume phase, when I synchronize 10 million, I stop, and then pass
- 从开发转测试:我从零开始,一干就是6年的自动化测试历程
- PostgreSQL queries [table field type] and [all series] in the library
- [cloud computing] several mistakes that enterprises need to avoid after going to the cloud
- JS inheritance method
- Principle of line of sight tracking and explanation of the paper
- Design for failure常见的12种设计思想
猜你喜欢

站在大佬的肩膀上,你可以看的更远

478-82(56、128、718、129)

SQL injection - pre Foundation

从开发转测试:我从零开始,一干就是6年的自动化测试历程

网络层的IP协议

Smartbi of smart smart smart software completed the c-round financing and accelerated the domestic Bi into the intelligent era

NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ

Image batch processing | necessary skills

Customer first | domestic Bi leader, smart software completes round C financing

Smart software completed round C financing, making Bi truly "inclusive"
随机推荐
Quickly build a gateway service, dynamic routing and authentication process, and watch the second meeting (including the flow chart)
The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
Two dimensional array and operation
TXT文本文件存储
Go waitgroup and defer
How to obtain the subordinate / annotation information of KEGG channel
Go interface Foundation
NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ
Among China's top ten national snacks, it is actually the first
Go panic and recover
Image batch processing | necessary skills
10、学习MySQL LIKE 子句
谷歌 Material Design 的文本框为什么没人用?
MDM data quality application description
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
VS2015使用dumpbin 查看库的导出函数符号
实现批量数据增强 | keras ImageDataGenerator使用
JSON file storage
说透缓存一致性与内存屏障
CSV文件存储