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 ...
随机推荐
- Android Studio生成APK自动追加版本号、自定义apk名称、指定签名证书文件
你也可以查看我的其他同类文章,也会让你有一定的收货! 生成APK自动追加版本号 可自动区分debug和release,并追加版本号: 打开 build.gradle 在 android 节点中插入下面 ...
- mongodb聚合(转)
聚合 是泛指各种可以处理批量记录并返回计算结果的操作.MongoDB提供了丰富的聚合操作,用于对数据集执行计算操作.在 mongod 实例上执行聚合操作可以大大简化应用的代码,并降低对资源的消耗. 聚 ...
- unity render pipeline
post process v2 GUI temp8->TaregtPool0->temp8 tem8 temp8->backbu ...
- Hibernate 参数匹配查询
第一种: public User validate(String userName, String password) { String hql = "from User where use ...
- gcc编译错误:DSO missing from command line
在用gcc 编译连接的时候,可能会遇到类似以下的错误: /usr/bin/ld: test_desktop_utils-test-desktop-utils.o: undefined referenc ...
- [Linux]屏幕输出控制
专门的术语叫做ANSI Escape sequences(ANSI Escape codes),题目并不恰当,与其说是屏幕输出控制,不如说是通过bash在兼容VT100的终端上进行输出. 主要有以下类 ...
- N的阶层(王道)
题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可能包括多组数据,对于每一组输入数据,输出N的阶乘 样例输入: 4 5 15 样例输出: ...
- dubbo发布webservice服务
dubbo发布webservice服务 学习了:https://blog.csdn.net/zhangyunpengchang/article/details/51567127 https://blo ...
- IE 下 input 不响应 change 事件的处理
很多时候,我们都需要通过 input 来上传文件,通过 change 事件获取用户上传的文件,然后做一些额外的处理,最后上传到服务器. 可是事情往往就是没有那么美好.是的,IE 下 input 在选择 ...
- Sql中存在斜杠“/”怎么办?
比如下面的语句 select concat(name,'/',description) from table1 这样的语句在数据库访问工具中执行没问题,到java中就报错. 解决办法也很简单,用单引号 ...