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 ...
随机推荐
- int和Integer,String和String(包装类)
1.int和Integer的值如果是一样的,则是在内存中开辟相同的内存空间 2.但是String和String(包装类)是不一样的 代码演示: int a=1; Integer b = new Int ...
- 协同过滤中的Grey Sheep问题
寒神解释:某些用户的倾向性和品味没有一致性,比较散.因此在协同过滤这种算法里,没办法和某个group有很高的相似/一致度,推荐会失效. 我理解是寻找邻居时候计算得到的相似度和其他用户相似度都非常小,或 ...
- 集群Cluster介绍
来源:http://www.ibm.com/developerworks/cn/linux/cluster/lw-clustering.html简单的说,集群(cluster)就是一组计算机,它们作为 ...
- 【JVM】Myecplise自带的JVM大小调整,用于Junit等测试时使用
一般在使用Junit或者一个工具类的main方法执行时,在Myecplise中运行,并不会占用多大的堆空间.如果出现OutofMemory错误,调整MyEcplise自带的JVM大小. 在Myecpl ...
- LinkedIn是如何优化Kafka的
作者 张卫滨 发布于 2015年9月21日 转载 在LinkedIn的数据基础设施中,Kafka是核心支柱之一.来自LinkedIn的工程师曾经就Kaf ...
- solr6.6 高级搜索Facet
1.介绍 facet分面查询是solr中以导航为目的的查询,在用户查询的结果上根据分类增加了count信息,然后用户根据count信息做进一步实现渐进式精确搜索. 什么字段适合用facet呢? fa ...
- Yii2系列教程:安装及Hello World
http://www.yiiframework.com/ 安装Yii2 打算从头开始,所以,连安装Yii2也稍微写一点吧.安装Yii2最好的方式就是使用composer: composer globa ...
- Node.js meitulu图片批量下载爬虫1.03版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
- Sql中存在斜杠“/”怎么办?
比如下面的语句 select concat(name,'/',description) from table1 这样的语句在数据库访问工具中执行没问题,到java中就报错. 解决办法也很简单,用单引号 ...
- QQ和微信凶猛成长的背后:腾讯网络基础架构的这些年
本文来自腾讯资深架构师杨志华的分享. 1.前言 也许没有多少人记得2004年发生的事情.但对于老腾讯来说,14年前的那个日子,2004年6月16日永远难以忘怀.这一天,QQ诞生5年后的腾讯在香港联交所 ...