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 是这 ...
随机推荐
- CSS 导航栏
实例: 导航栏 Home News Articles Forum Contact About 导航栏 熟练使用导航栏,对于任何网站都非常重要. 使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜 ...
- MYSQL 不排序
mysql: SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509) ORDER BY INSTR(',443,419,43 ...
- C++静态成员函数访问非静态成员的几种方法
大家都知道C++中类的成员函数默认都提供了this指针,在非静态成员函数中当你调用函数的时候,编译器都会“自动”帮你把这个this指针加到函数形参里去.当然在C++灵活性下面,类还具备了静态成员和静态 ...
- 使用Node.js作为后台进行爬虫
看了一遍又一遍Node.js但是没过多久就又忘了,总想找点东西来练练手,就发现B站首页搜索框旁边的GIF图特别有意思,想着是不是可以写一个小Node.js项目把这些图全部扒下来,于是带着复习.预习与探 ...
- php5.4安装ecshopphp5.4问题及解决
includes/cls_template.php line422 将 $tag_sel = array_shift(explode(" ", $tag)); 这句话拆开为两句. $tag_exp = ...
- GoogleCode新手教程
GoogleCode页面介绍 Project Home 首先显示的是project home,页面左边的是这个项目的介绍,右边的License是说明使用的是什么开源协议,Labels是标签的意思,就是 ...
- ng-html 报 不安全 警告解决办法
app.filter('to_trusted',['$sce',function($sce){ return function(text){ return $sce.trustAsHtml(text) ...
- Android ART简介
一. Android ART简介 Android DEX/ODEX/OAT文件
- prepare—Article【准备篇】之SSH_tool#PuTTY
第一:下载PuTTY: url : http://www.openssh.com/ 下载界面: 安装后: 详解以上命令 ① ② PuTTYgen is a key generator. It ...
- Sql Server2000,2005,2008各版本主要区别
Emerson回来之后,在过程中遇到的一些问题,再次做一些整理,包括本篇的Sql Server各版本之间的区别和另一篇数据库函数. (博文内容来自网络) 数据类型 SQL Server 2008 数据 ...