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的更多相关文章

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

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

  2. LN : leetcode 406 Queue Reconstruction by Height

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

  3. LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46

    406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...

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

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

  6. 406. Queue Reconstruction by Height

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

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

  8. 57.Queue Reconstruction by Height(按身高重建对列)

    Level:   Medium 题目描述: Suppose you have a random list of people standing in a queue. Each person is d ...

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

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

随机推荐

  1. AppCompatActivity与toolbar的结合

    原文:http://www.51itong.net/android-activity-appcompatactivity-toolbar-15750.html 另外一个博客:Android 5.x T ...

  2. Linux下使用xargs将多行文本转换成一行并用tr实现逗号隔开

    准备: cat test.txt 示例: cat test.txt | xargs 可以看出得到的字符串为空格隔开的. 再把上面的字符串用逗号隔开,可以使用tr命令进行空格的替换 cat test.t ...

  3. 如何让Adobe reader 记住上次pdf文档打开位置?

    菜单栏: Edit --> Preferences --> Documents --> 勾选 “Restore last view settings where reopening ...

  4. 推荐一些不错的开源免费易上手的web前端框架

    1. bui 2.Semantic UI 3.oniui

  5. Chrome DevTools 代码覆盖率功能详解

    共 1812 字,读完需 3 分钟.工欲善其事必先利其器,前端周刊本周起每周会加餐 1 篇工具技巧,里面辅以动图,让大家看完就能学会,并上手使用.本文会介绍 Chrome Canary 新增的代码覆盖 ...

  6. 微信开发之消息接收与回复--weixin-java-tools

    一.前言 在上一篇文章<微信开发之如何使用开发工具--weixin-java-tools>中我给各位介绍了weixin-java-tools,并且介绍了如何使用weixin-java-to ...

  7. ASP.NET Core 1.0基础之日志

    过年出去玩了一圈,回来继续翻译.前两天偷懒没有翻译,只是转了两篇C# 7计划中的新features,大家还是很支持的.现在继续完善这个系列. 来源https://docs.asp.net/en/lat ...

  8. windows SFC(System File Checker) 命令的使用

    SFC(System File Checker)可以扫描所有受保护的系统文件的完整性,并使用正确的 Microsoft 版本替换. 步骤:点击开始,输入cmd: 右键,以管理员身份运行 输入sfc/s ...

  9. WebLogic MBean Monitor

    weblogic server提供了一个dashboard让我们对mbean进行图形化的展现和分析,地址是 http://localhost:7001/console/dashboard 但有时候总是 ...

  10. 【Docker】MySQL容器因为内存限制启动失败?

    参考资料: https://github.com/docker-library/mysql/issues/3 Improving MySQL's default configuration:http: ...