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的做法和剪 ...
随机推荐
- ERROR in Template execution failed: ReferenceError: htmlwebpackPlugin is not defined
ejs文件配置如下: <!DOCTYPE html> <html lang="zh-CN"> <head> <title>webpa ...
- C#学习大纲
一.C#: 1.进制转换 2.vs界面内容 熟悉软件 3.数据类型 :引用类型 值类型 4.变量 (存储数据)一个变量存储一个数据 5.类型转换 6.运算符:算数运 ...
- C/C++字符串和其他类型转换
C语言中string char int类型转换 转载自:http://blog.sina.com.cn/s/blog_63041bb801016b4x.html ,char型数字转换为int型 &qu ...
- ansible笔记(二)--配置文件详解
配置文件ansible.cfg约有350行语句,大多数为注释行默认配置项.该文件遵循INI格式,分为如下几类配置.(1)[defaults] [defaults] # inventory = /etc ...
- OpenCV图像数据字节对齐
目录 1. IplImage的data字段,是char*类型,是4字节对齐. 2. 手动创建的Mat通常是没有字节对齐的 3. 从IplImage转过来的Mat,是字节对齐的 4. 总结 图像数据是否 ...
- 固定内网ip的方法
ip最后一位找一个不常用的,比如200之后的,ping不通它就用它. 子网掩码,默认网关保持和原来的一样. DNS要填公司的,网上查的不能用,因为他们是互联网上的.主备:XXXXXX/XX (之前填的 ...
- max_length 属性
错误:漏掉了 max_length 属性 ERRORS:users.UserProfile.image: (fields.E210) Cannot use ImageField because Pi ...
- node-解压版 安装配置测试
一.下载node压缩包 地址:https://nodejs.org/en/download/ 二.解压下载的压缩包,在文件根目录新增两个文件夹: node_cache:缓存文件位置 node_gl ...
- ceph-状态监测-脚本
http://www.tang-lei.com/2018/06/05/ceph-%E7%8A%B6%E6%80%81%E7%9B%91%E6%B5%8B-%E8%84%9A%E6%9C%AC/ 为了能 ...
- JS中关于构造函数、原型链、prototype、constructor、instanceof、__proto__属性
在Javascript不存在类(Class)的概念,javascript中不是基于类的,而是通过构造函数(constructor)和原型链(prototype chains)实现的.但是在ES6中引入 ...