406. Queue Reconstruction by Height
最后更新
二刷
15-Jan-2016
排座问题,记得以前是H难度的,现在改成M了。。
总结一句话,先按身高排,高的在前面;身高一样的情况下,K小的在前面。
BB半天说白了一句话,H大的在顶端,H一样的K小的在顶端。
根据这句话设立PQ的判断规则就行了。。
这样排的原因是因为出QUEUE的时候,后出的因为H小,不会影响前面的人。 而H一样的时候,虽然会影响前面同身高的,但是肯定K小的放前面,因为他能容忍比自己>=h的人更少。
最后的问题就是返还是list of int[],所以得找个办法记录PERSON在原先队列的位置= =麻烦了不少。
中间数组和LIST来回换很容易把自己弄晕= =||,有了这个做法也不想看别的做法了,拿到一个OFFER之后,我已经是一条咸鱼了。
说好的拿到这个OFFER决不负自己呢?
Time: O(NlogN)
Space: O(N)
public class Solution {
public class Person {
int height;
int k;
int index;
public Person(int h, int k, int i) {
this.height = h;
this.k = k;
this.index = i;
}
}
public int[][] reconstructQueue(int[][] people) {
PriorityQueue<Person> pq = new PriorityQueue<>(new Comparator<Person>() {
public int compare(Person a, Person b) {
if (a.height != b.height) {
return Integer.compare(b.height, a.height);
} else {
return Integer.compare(a.k, b.k);
}
}
});
for (int i = 0; i < people.length; i++) {
pq.offer(new Person(people[i][0], people[i][1], i));
}
// contain index of each person in the input array.
List<Integer> refList = new ArrayList<>();
while (!pq.isEmpty()) {
Person tempPerson = pq.poll();
refList.add(tempPerson.k, tempPerson.index);
}
int[][] res = new int[people.length][2];
for (int i = 0; i < refList.size(); i++) {
int pos = refList.get(i);
res[i][0] = people[pos][0];
res[i][1] = people[pos][1];
}
return res;
}
}
一刷
25-Sep-2016
一开始backtrack,设计了很多剪枝情况,还是TLE了

。。后来用PQ做的。
其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了。。
PQ的做法和剪枝的判断标准是一样的,只不过PQ不是遍历,比兼职纯遍历要快。
想象一下排座位,h是身高,k是某个小朋友最多允许几个人挡住他(貌似不是很恰当)。
对于单个小朋友来说,只有比他高才会挡住他,所以我们先按K的标准排最高的一批小朋友,这样一来后面批次的小朋友不会影响自己,因为永远不可能挡住自己。
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
[7,0]和[7,1]先排好。
同一批次里,K小的在前面,这个很好理解。。
第二批小朋友是身高为6的,[6,1],他要保证只有1个人挡住他,他坐到刚才2个人之间就行了。
[7,0] [6,1] [7,1]
第三批是身高为5的,[5,0]和[5,2],[5,0]想近距离观察美女老师,而目前在坐的所有人都比他高,他就跑去第一排了。至于[5,2],性欲不那么旺盛,被2个人挡住也无所谓,于是坐到2个人后面。
[5,0] [7,0] [6,1] [7,1]
[5,0] [7,0] [5,2] [6,1] [7,1]
然后是最后一个小朋友[4,4],他可能人比较耿直,正人君子,上课的时候心无旁骛,不需要看美女老师,所以前面4个人无所谓,他跑到4个人后面
[5,0] [7,0] [5,2] [6,1] [4,4] [7,1]
BB半天说白了一句话,H大的在顶端,H一样的K小的在顶端。
根据这句话设立PQ的判断规则就行了。。
这样排的原因是因为出QUEUE的时候,后出的因为H小,不会影响前面的人。 而H一样的时候,虽然会影响前面同身高的,但是肯定K小的放前面,因为他能容忍比自己>=h的人更少。
public class Solution {
public class People
{
public int h;
public int k;
public int i; //index of this people in people[][]
public People(int h, int k, int i)
{
this.h = h;
this.k = k;
this.i = i;
}
}
public class compare implements Comparator<People>
{
public int compare(People p1, People p2)
{
if(p1.h!=p2.h) return p2.h-p1.h;
else return p1.k-p2.k;
}
}
public int[][] reconstructQueue(int[][] people)
{
if(people.length == 0) return new int[0][0];
PriorityQueue<People> pq1 = new PriorityQueue(new compare());
for(int i = 0; i < people.length;i++)
{
pq1.add(new People(people[i][0],people[i][1],i));
}
//index of people in people[][]
List<Integer> l = new ArrayList<Integer>();
while(!pq1.isEmpty())
{
People p = pq1.poll();
l.add(p.k,p.i);
}
int[][] res = new int[people.length][2];
for(int i = 0; i < l.size();i++)
{
res[i][0] = people[l.get(i)][0];
res[i][1] = people[l.get(i)][1];
}
return res;
}
}
406. 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 ...
- 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 根据高度重建队列
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 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode] 406. Queue Reconstruction by Height
https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...
- 406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...
- [leetcode] 406. Queue Reconstruction by Height (medium)
原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
随机推荐
- 检查.gitignore语法
每次配置git的时候,要写gitignore文件,你知道你的.gitignore匹配那些文件吗? 看看.gitignore放过了哪些文件: git ls-files -ocm --exclude-st ...
- 三种C#.net生成静态页面的方法
ASP.NET生成静态页面方法主要有三种 第一种方法:向服务器的动态页面发送请求,获取页面的html代码.这种方法缺点显而易见:速度慢.另外如果请求的动态页面有验证控件的话,返回的html页面却无 ...
- c#写个基础的Socket通讯
晚上想写点东西,想想把我刚来公司学的Sockt通讯写上来吧.要写的简单易懂点,新人们可以借鉴下哦,用控制台写. 先得说说Socket,与TCP/UDP啥关系,一直讲什么Socket通讯,TCP通讯,都 ...
- 新增的output元素 progress元素 meter元素 keygen元素
结果图 <output>是双标签 name:定义对象的唯一属性 for:定义输出域相关的一个或多个元素. form:定义所属的一个至多个表单. progress和meter一般和JS一起使 ...
- js学习--DOM操作详解大全 前奏(认识DOM)
一 . 节点属性 DOM 是树型结构,相应的,可以通过一些节点属性来遍历节点树: 方法 说明 nodeName 节点名称,相当于tagName.属性节点返回属性名,文本节点返回#text.nodeNa ...
- Chromium网页Frame Tree创建过程分析
Chromium在加载一个网页之前,需要在Browser进程创建一个Frame Tree.Browser进程为网页创建了Frame Tree之后,再请求Render进程加载其内容.Frame ...
- Android中WebView的JavaScript代码和本地代码交互的三种方式
一.Android中WebView的漏洞分析最近在开发过程中遇到一个问题,就是WebView使用的时候,还是需要解决之前系统(4.2之前)导致的一个漏洞,虽然现在这个系统版本用户很少了,但是也不能忽视 ...
- window.frameElement属性
比如有一个iframe的src是xxx.htm frameElement的作用就是在xxx.htm中获得这个引用它的iframe objet 这样你就可以在xxx.htm改变iframe的大小,或是边 ...
- [Linux]Ubuntu下如何将普通用户提升到root权限
转至:http://jingyan.baidu.com/album/6181c3e0780131152ef153ff.html?picindex=0&qq-pf-to=pcqq.c2c 在u ...
- PHP的类自动加载机制
在PHP开发过程中,如果希望从外部引入一个class,通常会使用include和require方法,去把定义这个class的文件包含进来. 这个在小规模开发的时候,没什么大问题.但在大型的开发项目中, ...