WEEK#2 Queue Reconstruction by H
![](https://img.haomeiwen.com/i6351528/9961fbc09227636f.png)
Problem Description
- Sort the queue by height(h), if height equal, sort by k, desendingly
- Insert all elements into the result queue by k
bool mycompare(pair<int,int> p1, 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>>& people) {
vector<pair<int,int>> Result;
//cout << people.size();
Result.resize(people.size());
sort(people.begin(), people.end(), mycompare);
for (auto person : people) {
int count = person.second;
//cout << count << endl;
auto it = Result.begin();
while (count--)
it++;
Result.insert(it, person);
}
Result.resize(people.size());
return Result;
}
};
本文标题:WEEK#2 Queue Reconstruction by H
本文链接:https://www.haomeiwen.com/subject/nqtjgxtx.html
网友评论