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的做法和剪 ...
随机推荐
- vue iview分页
距离上次博客更新已经快一个月了,期间也有想法在空闲的时候更新几篇博文. 燃鹅,最近懒癌作祟,丢掉的东西越来越多,再不遏止的话就真成癌了. 趁着刚看完一篇心灵鸡汤,让打满鸡血的我总结下前段时间用到的iv ...
- JSONP面试
jQuery 的 JSONP的原理是动态创建一个 script 标签,利用src 发送请求,获取数据 回调函数的键名叫做 callback 跟ajax没有关系 JSONP:主要是利用 script标 ...
- Opencv识别图中人脸
#!/usr/bin/python #coding=utf-8 # 识别图片中的人脸 import face_recognition jobs_image = face_recognition.loa ...
- TensorFlow学习——入门篇
本文主要通过一个简单的 Demo 介绍 TensorFlow 初级 API 的使用方法,因为自己也是初学者,因此本文的目的主要是引导刚接触 TensorFlow 或者 机器学习的同学,能够从第一步开始 ...
- Spring STS 开发IDE下载安装
https://spring.io/tools https://spring.io/tools3/sts/all/ Spring STS,全称是SpringSource Tool Suite ,是由s ...
- Deepin学习笔记
更改更新源 1)sudo vim /etc/apt/sources.list 2)sudo apt-get update 3) 镜像源 http://mirrors.aliyun.com/deepi ...
- java的任务
1.完善现有的日志记录系统,对异常进行处理和记录 2.基于需求实现账号信息录入接口
- vmwre虚拟机配置
mware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NAT(网络地址转换模式).Host-Only(仅主机模式).打开vmware虚拟机,我们可以在选项栏的“编辑”下的“虚 ...
- 使用stylelint进行Vue项目样式检查
stylelint是一个强大的现代 CSS 检测器,可以让开发者在样式表中遵循一致的约定和避免错误.拥有超过170条的规则,包括捕捉错误.最佳实践.控制可以使用的语言特性和强制代码风格规范.它支持最新 ...
- Python的"random"函数的使用(一)
random.randrange(1,10) 随机产生0~7之间的整数,不包含7. random.sample(range(100), 5) 随机从range(100)中产生5个数,放入一个list. ...