406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。
注意:
总人数少于1100人。
示例
输入:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
输出:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
详见:https://leetcode.com/problems/queue-reconstruction-by-height/description/
C++:
class Solution {
public:
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
sort(people.begin(), people.end(), [](const pair<int,int> &a, const pair<int, int> &b){
return a.first > b.first || (a.first == b.first && a.second < b.second);
});
for (int i = 0; i < people.size(); i++)
{
auto p = people[i];
if (p.second != i)
{
people.erase(people.begin() + i);
people.insert(people.begin() + p.second, p);
}
}
return people;
}
};
参考:https://www.cnblogs.com/grandyang/p/5928417.html
406 Queue Reconstruction by Height 根据身高重建队列的更多相关文章
- [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] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- 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 ...
- 406. Queue Reconstruction by Height
一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...
- 【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为 ...
- [leetcode] 406. Queue Reconstruction by Height (medium)
原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...
随机推荐
- ubuntu14.04 配置网络
ubuntu14.04 配置网络的练习 本文参考的资料: https://blog.csdn.net/liu782726344/article/details/52912797. 感谢作者的分享! 打 ...
- ****HTML模板资源汇总
站长素材: http://sc.chinaz.com/tag_moban/HTML.html wordpress模板: http://www.cssmoban.com/wpthemes/ http:/ ...
- [bzoj1207][HNOI2004]打鼹鼠_动态规划
打鼹鼠 bzoj-1207 HNOI-2004 题目大意:题目链接. 注释:略. 想法: $dp_i$表示打到了第$i$个鼹鼠时最多打了多少个鼹鼠. $O(n)$转移即可. 总时间复杂度$O(n^2) ...
- [bzoj3527][Zjoi2014]力_FFT
力 bzoj-3527 Zjoi-2014 题目大意:给定长度为$n$的$q$序列,定义$F_i=\sum\limits_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum\lim ...
- POJ——T 2976 Dropping tests
http://poj.org/problem?id=2976 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13861 ...
- Systemtap工具查看系统资源使用
http://www.xbwolf.com/507 http://blog.csdn.net/kafeiflynn/article/details/6429976 安装步骤yum install sy ...
- 异 形 卵 709 南阳oj
http://acm.nyist.net/JudgeOnline/problem.php? pid=709 异 形 卵 时间限制:1000 ms | 内存限制:65535 KB 难度: 描写叙述 ...
- R语言pdf输出中文乱码处理
1.使用基础包.使用函数pdf()输出 在使用pdf()函数时,要输出中文,仅仅有一种字体可选. 样例: pdf("chinese.pdf",family="GB1&qu ...
- B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)
B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...
- HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...