题目:406. 根据身高重建队列
思路
贪心法;
本题涉及到2种选择因素:h
和k
;
优先考虑h
,再考虑k
;
如果在你的脑子里,这2个变量搅在一起就完蛋了 w(゚Д゚)w
代码
// 有2种因素要考虑时, 先考虑一种,另一种不管
class Solution {
static bool compare(vector<int> a, vector<int> b)
{if(a[0] == b[0]){return a[1] < b[1];}return a[0] > b[0];
}public:vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {vector<vector<int>> queue;int i, index;sort(people.begin(), people.end(), compare);for(i = 0; i < people.size(); i++){index = people[i][1];queue.insert(queue.begin() + index, people[i]);}return queue;}
};