# 【LeetCode】 406. Queue Reconstruction by Height
## Description
> 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.
> 假設有隨機的人群在列隊中。每個人有一對整數`(h, k)`去描述他,`h`是這個人的身高,`k`是他前面有多少人比他高。寫一個演算法重新排序這個列隊。
> 提示:
> 描述人的數字小於 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]]
```
## Solution
* 先讓人按照身高遞減排序。
* 接著從頭跑一次列隊再重新排序一次,這次他前面有幾個人比他高就將他安插在哪個位子。
* 因為列隊已經按照身高排序,後面的一定都比較矮,不會因為後面的人加入導致前面的人不符合`k`的規則。
### Code
```C++=1
bool myCompare(vector<int> a, vector<int> b)
{
return (a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]));
}
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> ans;
sort(people.begin(), people.end(), myCompare);
for(int i = 0; i < people.size(); i++)
ans.insert(ans.begin() + people[i][1], people[i]);
return ans;
}
};
```
###### tags: `LeetCode` `C++`