leetcode 406. Queue Reconstruction by Height 人群高度排序
来源:互联网 发布:保湿水推荐 知乎 编辑:程序博客网 时间:2024/05/29 02:03
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>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <bitset>using namespace std;bool cmp(const pair<int, int>& p1, const pair<int, int>& p2){ return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second);}class Solution {public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& peo) { sort(peo.begin(),peo.end(),cmp); vector<pair<int, int>> res; for (auto a : peo) res.insert(res.begin()+a.second,a); return res; }};
阅读全文
0 0
- leetcode 406. Queue Reconstruction by Height 人群高度排序
- LeetCode 406. Queue Reconstruction by Height【H排序,c++11】
- LeetCode—406. Queue Reconstruction by Height
- [LeetCode]406.Queue Reconstruction by Height
- LeetCode 406. Queue Reconstruction by Height
- [LeetCode] 406. Queue Reconstruction by Height
- Leetcode 406. Queue Reconstruction by Height
- LeetCode 406. Queue Reconstruction by Height
- LeetCode笔记:406. Queue Reconstruction by Height
- 【LeetCode】 406. Queue Reconstruction by Height
- [leetcode]406. Queue Reconstruction by Height
- [Leetcode] 406. Queue Reconstruction by Height
- LeetCode 406. Queue Reconstruction by Height
- Leetcode 406. Queue Reconstruction by Height
- [LeetCode]406. Queue Reconstruction by Height
- Leetcode-406. Queue Reconstruction by Height
- LeetCode OJ 406. Queue Reconstruction by Height
- 【LeetCode】406. Queue Reconstruction by Height
- hbase regionLocator 获取region过程
- 最好的6个Go语言Web框架
- 关于 xftp工具 远程连接虚拟机 失败>>>>>>>>Sftp和ftp 区别、工作原理等(汇总)
- 台阶问题(1or2)
- 读《批判性思维》续
- leetcode 406. Queue Reconstruction by Height 人群高度排序
- C++命名空间 namespace的作用和使用解析
- 一个32岁入门的70后程序员给我的启示
- 表达式转换(25 分)(栈的应用)
- 使用logstash输出到elasticsearch中文乱码的解决方案
- 十个Chatbot框架介绍
- 课程小结——SURF特征提取
- 选择某种Map集合保存学号从1到15的学员的学号(键)和姓名(值)
- 《算法笔记》C++ 进制转换