当前位置:网站首页>406. 根据身高重建队列
406. 根据身高重建队列
2022-06-12 17:04:00 【zzu菜】
406. 根据身高重建队列
假设有打乱顺序的一群人站成一个队列,数组 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]]
思考
我们考虑到People有两个维度high(身高)、pre(前面的人数)
我们肯定不能两个维度一起考虑,以至于顾头不顾尾。
我们这里首先考虑high
对people执行排序规则:
- high大的在前面
- high相同,pre小的在前面
排序完成前:[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
排序完成后: [[7,0], [7,1], [6,1], [5,0], [5,2],[4,4]]
我们为什么这样考虑?
为什么身高高的在前面呢?
我们可以遍历排序完成后的people数组,进行插入操作,插入的位置为pre。
因为身高高的在前面,所以后面的插入对前面已经插入好的没有影响。
插入的过程:
- 插入[7,0]:[[7,0]]
- 插入[7,1]:[[7,0],[7,1]]
- 插入[6,1]:[[7,0],[6,1],[7,1]]
- 插入[5,0]:[[5,0],[7,0],[6,1],[7,1]]
- 插入[5,2]:[[5,0],[7,0],[5,2],[6,1],[7,1]]
- 插入[4,4]:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
代码实现
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o2[0]==o1[0]){
return o1[1]-o2[1];
}
return o2[0]-o1[0];
}
});
ArrayList<int[]> list=new ArrayList<>();
for (int i=0;i<people.length;i++){
int postion=people[i][1];
list.add(postion,people[i]);
}
return list.toArray(new int[people.length][people[0].length]);
}
边栏推荐
- To understand Devops, you must read these ten books!
- Gerrit触发Jenkins SonarQube扫描
- Idea displays services on the console to uniformly manage all jetty services,
- 2022-2028 global press dehydrator industry research and trend analysis report
- su直接切换到超级管理员模式,这样很多报错都可以避免了
- R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置ylab参数设置Y轴的轴标签文本(y axis)
- Is the securities account opened by qiniu safe? Is it legal?
- Modify the configuration of the router connected to your computer. The website is 192.168.1.1
- Go的变量
- 1723. 完成所有工作的最短时间
猜你喜欢

JVM内存模型与本地内存

Gerrit+2 triggers Jenkins task

Kill program errors in the cradle with spotbugs

Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现

Cicada mother talks to rainbow couple: 1.3 billion goods a year, from e-commerce beginners to super goods anchor

Google browser debugging skills

A variety of Qt development methods, which do you choose?

Extract the new Chinese cross modal benchmark zero from 5billion pictures and texts, and Qihoo 360's new pre training framework surpasses many SOTAS

Uniapp壁纸小程序源码/双端微信抖音小程序源码

叶子分享站PHP源码下载
随机推荐
Download PHP source code of leaf sharing station
Microsoft Office MSDT Code Execution Vulnerability (cve-2022-30190) vulnerability recurrence
The significance of writing technology blog
Add static route
Extract the new Chinese cross modal benchmark zero from 5billion pictures and texts, and Qihoo 360's new pre training framework surpasses many SOTAS
Schrodinger's Japanese learning applet source code
Modify the configuration of the router connected to your computer. The website is 192.168.1.1
Information outline recording tool: omnioutliner 5 Pro Chinese version
R语言计算data.table在一个分组变量的值固定的情况下另外一个分组变量下指定数值变量的均值
The R language uses the aggregate The plot function visualizes the summary statistical information of each subset (visualization is based on the probability value and its 95% confidence interval of th
Structural requirement analysis of software engineering student information management system
2080虚拟机登录命令
js 使用Rsa 加密 解密
(五)输出和输出
goland变成中文版了怎么修改回英文版
Idea displays services on the console to uniformly manage all jetty services,
The R language uses the pyramid function of epidisplay package to visualize the pyramid graph and the pyramid graph based on the existing summary data (table data)
redis. clients. jedis. exceptions. JedisConnectionException: Could not get a resource from the pool
薛定谔的日语学习小程序源码
R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容