原题目:https://leetcode-cn.com/problems/queue-reconstruction-by-height/
思路:
这题使用到贪心算法,
具体的贪心策略为:按身高从大到小依次插入,因为后面插入的元素肯定不大于前面的元素,所以插入的位置就是people[i][1]所指示的前面比他高或相同身高的人数。
代码:
class Solution {
public:
static bool cmp(vector<int> a,vector<int> b){
if(a[0]!=b[0]) return a[0] > b[0];
return a[1] < b[1];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(),people.end(),cmp);
vector<vector<int>> res;
for(int i=0;i<people.size();i++){
res.insert(res.begin()+people[i][1],people[i]);
}
return res;
}
};