Queue Reconstruction by Height
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的更多相关文章
- 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 ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
- 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] 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的做法和剪 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- [阅读笔记]Software optimization resources
http://www.agner.org/optimize/#manuals 阅读笔记Optimizing software in C++ 7. The efficiency of differe ...
- SSAS 部署失败 总结
今天部署微软官方的SSAS实例AdventureWorks Multidimensional Models SQL Server 2012到本地SQL SERVER数据库,报了好几个错误.总结一下给大 ...
- ==与equals的区别
==比较两个对象在内存里是不是同一个对象,就是说在内存里的存储位置一致.两个String对象存储的值是一样的,但有可能在内存里存储在不同的地方 . ==比较的是引用而equals方法比较的是内容.pu ...
- How to Make Terrains in Tiled Map Editor
Published July 13th, 2015 by Stephen Gygi How to Make Terrains in Tiled Map Editor http://www.binary ...
- c#基础3
Console.WriteLine("屏幕显示的内容"); Console.Write("屏幕显示的内容"); 两者区别是:Console.WriteLine( ...
- shell中创建mysql库和执行sql脚本
以前执行oracle脚本都是放到plsql中执行 mysql 脚本执行: (1).先创建一个worlddb库 (2).导入sql脚本: 这就ok啦,哈哈.
- 269. Alien Dictionary 另类字典 *HARD*
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- C#winform调整控件的位置
现在有三个控件并排放置 第二个控件有隐藏功能 隐藏后第一个控件和第三个控件的距离要缩小,于是就要改变第三个控件的位置 尝试用Location.X属性去设置,但是被告知此非变量 于是只能另外想办法 搜到 ...
- nodeType的意思
nodeType是用来获得当前节点对象的类型.nodeType 属性可返回节点的类型.元素element 1 属性attr 2 文本text 3 注释comments 8 ...
- json数组转数组对象
import java.io.IOException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import com. ...