57.Queue Reconstruction by Height(按身高重建对列)
Level:
Medium
题目描述:
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k)
, where h
is the height of the person and k
is the number of people in front of this person who have a height greater than or equal to h
. Write an algorithm to reconstruct the queue.
Note:
The number of people is less than 1,100.
Example
Input:
[[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]]
思路分析:
换种思考方式,题目里面说了,每个位置的 k 的值,等于在这个位置之前 h 比它大的位置的数量。如果要向一个队列中插入一个人,要判断的就是插入的位置。那么如果可以确定,当前队列中的所有人的 h 值都比待插入的这个人的 h 值大,那么这个人的 k 值就是他应该插入的位置。并且可以确定,如果插入的是一个 h 值小于当前队列中所有人的 h 值的人,那么无论他的位置在哪,都不会影响当前队列中所有人的 k 值,即不会破坏当前队列的正确性。
再看本题,我们只需要将这个队列按照 h 从高到低排序,然后依次插入到一个新的队列,这样就能保证新插入的人的 h 值始终小于(或等于)当前队列中所有人的 h 值,满足了上面的条件。
再有一个问题,如果两个人的 h 值相同的时候该如何排序?如一个队列[[7,1],[7,0]],因为题目中已经指出了:每个人的 k 值,是在他的前面所有 h 值大于(或等于)他的 h 的人的个数,那么对于两个 h 值相同的人,他们的 k 值肯定是不同的,并且能够确定,k 值大的人应该排在后面
代码:
public class Solution{
public int [][]reconstructQueue(int [][]people){
//按照h从高到低进行排序
Arrays.sort(people,new Comparator<int []>(){
@Override
public int compare(int[]o1,int []o2){
return o1[0]==o2[0]?o1[1]-o2[1]:o2[0]-o1[0];//将数组按照身高进行高到低排序,如果高度形同,则按k由小到大排序
}
});
List<int[]>res=new ArrayList<>();
for(int []p:people){
res.add(p[1],p);
}
return res.toArray(people); //转换成二维数组
}
}
57.Queue Reconstruction by Height(按身高重建对列)的更多相关文章
- 406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- [LeetCode] 406. Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- LC 406. Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- LN : leetcode 406 Queue Reconstruction by Height
lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...
- [Swift]LeetCode406. 根据身高重建队列 | Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- 406. Queue Reconstruction by Height
一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...
随机推荐
- UIWindow与UIView
UIView与UIWindow * 一般应用程序只有一个UIWindow对象.所有的控件都是在UIWindow上展现的.每个UIView对象都有一个window属性,表示当前view显示在哪个窗体上. ...
- Centos上Docker的安装及加速
#环境 :内核的版本必须大于3.10 #安装docker yum install epel-release -y yum install docker-ce ##安装docker-ce #配置文件 d ...
- 八皇后问题 -- python面向对象解法
# [8*8棋盘八皇后问题] class Queen: def __init__(self, row, col): self.row = row self.col = col self.pos = ( ...
- bzoj2560 串珠子 状压DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2560 题解 大概是这类关于无向图的联通性计数的套路了. 一开始我想的是这样的,考虑容斥,那么就 ...
- python输出转义字符
转义字符在字符串中不代表自己,比如\n代表回车,不代表\n字符,那我想输入转义字符本身呢? 答:在字符串前面加个r 如print(“aa\nbb”) 会输出aa bb 如print(r"aa ...
- SpringBoot---监控与管理actuator
1.概述 SpringBoot在Start POMS中提供了一个特殊依赖模块spring-boot-starter-actuator: 引入spring-boot-starter-actuator模块 ...
- keras及神经网络,以简单实例入门
由浅入深,深入浅出.还给你reference了很多,如果你想要更多. 迄今为止看到最棒的,最值得follow的入门tutorial: https://realpython.com/python-ker ...
- MySQL/RDS数据如何同步到MaxCompute之实践讲解
摘要:大数据计算服务(MaxCompute,原名ODPS)是阿里云提供的一种快速.完全托管的EB级数据仓库解决方案.本文章中阿里云MaxCompute公有云技术支持人员刘力夺通过一个实验向大家介绍了阿 ...
- Git的安装及配置
1.Git Git 是一个开源的分布式版本管理工具,可以在你电脑不联网的情况下,只在本地使用的一个版本管理工具,其作用就是可以让你更好的管理你的程序.在你每次的修改代码并提交后,Git 都会将这些记录 ...
- truncate与delete删除数据的区别