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 ...
随机推荐
- SSH学习——Spring基础
1.理解什么是Spring框架? spring是J2EE应用程序框架,是轻量级的IOC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框 ...
- Redis编程实践【pub/sub】
原文:http://shift-alt-ctrl.iteye.com/blog/1867454 Redis或许已经在很多企业开始推广并试水,本文也根据个人的实践,简单描述一下Redis在实际开发过程中 ...
- Android Studio打包:“APP_NAME" IS NOT TRANSLATED IN ZH, ZH_CN……..解决办法
开始用Android Studio更新到2.0稳定版,调试的时候没啥问题,在打包的时候出现了"app_name" is not translated in zh, zh_CN….. ...
- WebLogic Server 关键优化指标
昨天给客户做巡检,又将整个WebLogic Server的优化过程走了一遍,记录下来给大家参考. 1.JVM优化 查看 $ps –ef | grep java /opt/java1.5/bin/jav ...
- Dedecms getip()的漏洞利用
flyh4t在非安全发布了dedecms getip()的注射漏洞,漏洞本身的成因没什么好说的老掉牙的X-Forwarded-For的问题,我想这个漏洞很多人都找到了,不过这个漏洞的利用有个地方还是可 ...
- MAC 使用技巧及常用软件备忘
公司转向MAC快一年, 换了MAC PRO半年时间,MAC这东西除了颜值和性能,软件真是不如WINDOWS啊,不是没有,只是好多都收费! 先介绍几个跨平台的. WIN+MAC 通用: 浏览器: CHR ...
- Yahoo 股票数据抓取
Yahoo提供日线级别的历史数据,包括国内外各交易所的数据,可以写爬虫抓取数据做简单的分析. api基本格式如下: http://ichart.yahoo.com/table.csv?s=<st ...
- @value
通过name来获取bean对象. 这一点eclipse就没有intelliJ好,拉到方法内部上面就不现实方法名了,这里是上面调用的方法的内部. 处理内部属性: 然后就在这里抛出异常了: 我们打个deb ...
- grep怎样匹配tab键
grep怎样匹配tab键 学习了:https://blog.csdn.net/qixinkui/article/details/2746433 1 grep -P '/t'; 2 awk '//t/' ...
- android 开发者的个人博客集
1. http://stormzhang.com/posts.html //不少的好的工具与建议