406. Queue Reconstruction by Height
- Total Accepted: 20635
- Total Submissions: 37676
- Difficulty: Medium
- Contributor: LeetCode
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.
题解:
bool comp(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(), comp);
vector< pair<int, int> > res;
for (int i=0; i<people.size(); i++)
res.insert(res.begin() + people[i].second, people[i]);
return res;
}