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值从小到大排,如果k值相同则h值从大到小排。然后再遍历排序过后的数组,进行按值插入。看过别人的分析后,更好的方式是先根据h值从大到小排,如果h值相同则k值从小到大排。然后将数组按顺序插入到ArrayList中,经过第二种排序方式的数组,当前即将被插入的数组的k值就是它在list中应有的位置。这是因为数组先按照h值从大到小排序过,因此已经在list中的数组的h值都是大于或者等于当前数组的h值的。代码如下:

Queue Reconstruction by Height的更多相关文章

  1. sort学习 - LeetCode #406 Queue Reconstruction by Height

    用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...

  2. LN : leetcode 406 Queue Reconstruction by Height

    lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...

  3. LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46

    406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...

  4. 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 ...

  5. [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 ...

  6. 406. Queue Reconstruction by Height

    一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...

  7. LeetCode_406. Queue Reconstruction by Height解题思路

    题目如下: Suppose you have a random list of people standing in a queue. Each person is described by a pa ...

  8. [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 ...

  9. LeetCode406. Queue Reconstruction by Height Add to List

    Description Suppose you have a random list of people standing in a queue. Each person is described b ...

随机推荐

  1. iOS中的数据持久化方式

    iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data. 1.属性列表 涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults ...

  2. css学习笔记 7

    background-position属性值为百分比的时候,第一个百分比表示水平方向的距离,第二个表示垂直方向上的距离. text-indent的主要作用是为段落设置首行缩进,只能应用于块级元素.该属 ...

  3. url的内容及格式

    url的内容及结构: url格式:

  4. (42) Aeroo 模板实战

    用writer设计一个采购单的模板 我用的是libreoffice 5.2.x 对于这个表格是通过工具栏上的插入指定的表格行和列完成,然后排版 对于单号po00001 这这样插入的 这样就完成一个订单 ...

  5. IOS开发常见错误整理

    1.Cannot create an NSPersistentStoreCoordinator with a nil model 这是在执行到这段代码时引发的: - (void)viewDidLoad ...

  6. 《精通C#》十四章-.NET程序集入门

    在书中,这一章节的开头说的是自定义命名空间和使用命名空间,在以我目前有限的经验来说,程序集就是一个类库经过编译之后,所生成的一个在引用命名空间,进而使用该文件中已经定义好的字段,属性以及方法的文件,以 ...

  7. mongodb的一些小总结

    mongodb的安装,官网下载想要的版本,可视化工具mongovue(注意不支持mongodb3.0以上的版本) 下载mis安装,解压后bin,... 1.配置环境变量,将H:\mongodb\mon ...

  8. uva 10801(最短路)

    题目大意: 有一层不超过100层的大楼, 有n个电梯,它们的速度都不同. 而且每个电梯只能到达指定的那些楼层,而且它们都有各自的速度(即上升一层或下降一层所用的时间). 如果一个人在某层走出电梯,要换 ...

  9. web.config连接字符串的一些总结

    阅读目录: DS01:数据库连接字符串的两种写法 DS02:数据库连接字符串的内容 DS01:数据库连接字符串的两种写法 1.连接字符串的两种写法: <configuration>   & ...

  10. 总结js中数据类型的bool值及其比较

    首先需要知道的是,js中有6个值为false,分别是: 0, '', null, undefined, NaN 和 false, 其他(包括{}, [], Infinity)为true. 可以使用Bo ...