[leetcode]406. Queue Reconstruction by Height
Analysis
中午吃啥?—— [啊啊啊啊 paper结果真的要出来了,心塞]
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.
虽然知道要先排序,但是排完序之后还是有点懵,参考了大神的做法:
https://blog.csdn.net/wyh476901857/article/details/70495661
Implement
class Solution {
public:
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
vector<pair<int, int>> res;
sort(people.begin(), people.end(), cmp);
for(auto p:people){
res.insert(res.begin()+p.second, p);
}
return res;
}
static bool cmp(pair<int, int> a, pair<int, int> b){
if(a.first == b.first)
return a.second<b.second;
return a.first>b.first;
}
};