当前位置:网站首页>leetcode 406. Queue Reconstruction by Height
leetcode 406. Queue Reconstruction by Height
2022-07-31 07:09:00 【okokabcd】
一、题目大意
标签: 贪心
https://leetcode.cn/problems/queue-reconstruction-by-height
假设有打乱顺序的一群人站成一个队列,数组 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 <= 2000
- 0 <= hi <= 106
- 0 <= ki < people.length
- 题目数据确保队列可以被重建
二、解题思路
This question needs to be understood first,一个无序数组,数组中每个元素有两个属性,一个表示身高,One indicates that there are several people in front of him that are as tall or higher than him.Now let's sort this array correctly by its properties.
思路:~~创建一个list,Traverse the array sorted by height,Insert the second property as a positionlist中.~~将people数组排序,再通过一个变量cnt与krelationship to move the element forward to the correct position,The way to move is by swapping positions with the previous element each time.
三、解题方法
3.1 Java实现
public class Solution {
public int[][] reconstructQueue(int[][] people) {
// 先对peopleSort by height
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
for (int i = 1; i < people.length; i++) {
int cnt = 0;
for (int j = 0; j < i; j++) {
if (cnt == people[i][1]) {
int[] t = people[i];
for (int k = i - 1; k >= j; k--) {
people[k+1] = people[k];
}
people[j] = t;
break;
}
cnt++;
}
}
return people;
}
}
四、总结小记
- 2022/7/30 7penultimate day of the month,It has rained heavily for the past two days
边栏推荐
- 【云原生】-Docker容器迁移Oracle到MySQL
- Project exercise - memorandum (add, delete, modify, check)
- leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
- Dart入门
- Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
- Database Principles Homework 2 — JMU
- 服务器硬件及RAID配置实战
- Debian 10 dhcp 服务配置
- LeetCode brush # 376 # Medium - swing sequence
- 使用powerDesigner反向工程生成Entity
猜你喜欢
随机推荐
Redux state management
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
数据库原理作业3 — JMU
DirectExchange交换机简单入门demo
postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
Skywalking安装部署
银河麒麟服务器v10 sp1安装.net6
TypeScript基本类型
【云原生】3.3 Kubernetes 中间件部署实战
浅析伪类和伪元素
【云原生】-Docker容器迁移Oracle到MySQL
二叉树的还原(反序列化)
【Star项目】小帽飞机大战(八)
运行 npm 会弹出询问 “你要如何打开这个文件?“
使用powerDesigner反向工程生成Entity
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
Install and use uView
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
Oracle 日期函数相关
(border-box)盒子模型w3c、IE的区别









