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
原创粉丝点击