假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(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 根据身高重建队列的更多相关文章

  1. [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 ...

  2. [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 ...

  3. sort学习 - LeetCode #406 Queue Reconstruction by Height

    用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...

  4. LN : leetcode 406 Queue Reconstruction by Height

    lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...

  5. 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 ...

  6. 406. Queue Reconstruction by Height

    一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...

  7. 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  9. [leetcode] 406. Queue Reconstruction by Height (medium)

    原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...

随机推荐

  1. msp430入门学习12

    msp430的定时器--Timer_A(定时器A) msp430入门学习

  2. 1sting 大数 递推

    You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or lea ...

  3. mysql常用jar包

    连接Mysql数据库: 常用的连接池有两种 DBCP连接池 C3P0连接池 Apache的commons组件 -- DBCP连接池: commons-dbutils-1.4.jar 封装并简化了JDB ...

  4. APPLE STORE

    直接在设置中,使用查看APPLE ID是无法更改的,现在必须要有所在区域的信用卡信息,支付方式无法像以前一样选择“无”. 查询后发现,有人说icloud3.0,即这个旧版的可以进行更改,于是下载. 但 ...

  5. tomcat会自动解压webapps目录下的war包

    如图,把war包放到tomcat的webapps目录,会被自动解压

  6. UIButton和UISlider

    UIButton 主要功能:按钮控件,主要用于与用户操作进行交互 经常使用属性及方法 系统内建的按钮类型 UIButtonTypeCustom UIButtonTypeSystem UIButtonT ...

  7. C#如何使用右下角托盘图标notifyIcon

    1 拖放一个NotifyIcon控件,并设置图标,还有显示的文字   2 双击这个控件,即当最小化了主窗体,然后双击这个右下角图标的时候,要显示主窗体(大部分程序的用户体验都是这样干的,比如QQ,双击 ...

  8. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  9. 对json的爱恨情仇

    本文回想了对json的爱恨情仇. C++有风险,使用需慎重. 本文相关代码在:http://download.csdn.net/detail/baihacker/7862785 当中的測试数据不在里面 ...

  10. 【bzoj1042】[HAOI2008]硬币购物

    首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案. 为了避免重复的方案被转移,所以我们以硬币种类为第一层循环,这样阶段性的增加硬币. 一定要注意这个第一层循环要 ...