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 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]]
#include <iostream>
#include <vector>
using namespace std;
bool compare(const pair<int, int>& p1, const pair<int, int>& p2)
{
return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second);
}
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
sort(people.begin(), people.end(), compare);
//按照高度h进行降序排序,如果高度相同就按高于的人数升序排序
//排序后结果[7,0] [7,1] [6,1] [5,0] [5,2] [4,4]
//每一pair都小于等于前面的每一个pair
vector<pair<int, int>> res;
for (auto& p : people)
res.insert(res.begin() + p.second, p);
//根据前面有高于的数目插入相应位置
//如[6,1]插入到位置1,前面只有[7,0],所以满足了[6,1]要求
//[5,0]插入到位置0,满足[5,0]要求
//[5,2]插入到位置2,前面有[5,0] [7,0],满足要求
//……
return res;
}
//下面是测试案例
int main()
{
vector<pair<int, int> > pp;
pp.push_back(make_pair<int,int>(7,0));
pp.push_back(make_pair<int,int>(4,4));
pp.push_back(make_pair<int,int>(7,1));
pp.push_back(make_pair<int,int>(5,0));
pp.push_back(make_pair<int,int>(6,1));
pp.push_back(make_pair<int,int>(5,2));
for(auto iter=pp.begin();iter!=pp.end();iter++)
{
cout<<"["<<iter->first<<","<<iter->second<<"]"<<" ";
}
vector<pair<int, int>> res = reconstructQueue(pp);
cout << endl << "-------------------------------" << endl;
for(auto iter=res.begin();iter!=res.end();iter++)
{
cout<<"["<<iter->first<<","<<iter->second<<"]"<<" ";
}
cout << endl;
return 0;
}
输出:
[7,0] [4,4] [7,1] [5,0] [6,1] [5,2]
[5,0] [7,0] [5,2] [6,1] [4,4] [7,1]
LeetCode406. Queue Reconstruction by Height Add to List的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 57.Queue Reconstruction by Height(按身高重建对列)
Level: Medium 题目描述: Suppose you have a random list of people standing in a queue. Each person is d ...
- 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Ubuntu安装“启动引导器”的设备选哪一项,选默认还是选/boot分区?
如果你要用Ubuntu的引导器代替Windows的引导器,就选 /dev/sda. 如果你要保留Windows的引导器,就选 /boot分区,但这样一来,装完Ubuntu重启后,只能启动Windows ...
- hibernate CascadeType属性
CascadeType.PERSIST 只有A类新增时,会级联B对象新增.若B对象在数据库存(跟新)在则抛异常(让B变为持久态) : 级联保存,当调用了Persist() 方法,会级联保存相应的数据 ...
- Kubernetes ServiceAccount的配置
开始配置Kubernetes集群的时候为了少出问题,都是在apiserver配置中去掉ServiceAccount采用非安全连接的方式,但在后面配置FEK日志的过程中,很多时候绕不开这个安全机制,但因 ...
- JS中的import和require区别
1.import xx from yy的方式是静态编译,即编译时加载,要写在文件的最上头,但是import()函数可以实现动态加载,写在任何地方 2.require是动态加载,即运行时加载,理论上可以 ...
- supervisord的环境变量的设置
在python中引用jar包,使用java的功能,需要使用三方包:pyjnius from jnius import autoclass 需要java环境,因此需要设置JAVA_HOME环境变量. 但 ...
- camera主观测试经验分享.ppt33页
http://max.book118.com/html/2016/0802/50061502.shtm http://www.docin.com/p-1408441708.html
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面如何自动运行,不让用户干涉,设置起始界面
右击视图管理器,添加一个TargetVisualization 在起始视图中点击右边的按钮,然后选择一个HMI作为起始HMI 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: h ...
- Java 多线程之 synchronized 和 volatile 的比較
概述 在做多线程并发处理时,常常须要对资源进行可见性訪问和相互排斥同步操作.有时候,我们可能从前辈那里得知我们须要对资源进行 volatile 或是 synchronized 关键字修饰处理.但是,我 ...
- 【android相关】【问题解决】R.java文件丢失
在进行android开发过程中,有时候,我们会遇到gen文件中R.java丢失的现象.重新build,或者clean工程,close并重新打开Project,但有时也没解决. 这可能是由于不小心把xm ...
- 小伙子又乱码了吧-Java字符编码原理总结
前提 配合前面阅读的I/O和NIO的资料,现在总结一下关于字符集和乱码问题的原理和解决方案.参考资料: 码表ASCII Unicode GBK UTF-8 字符编码笔记ASCII,Unicode和UT ...