406.根据身高重建队列
思路:
先按照身高由大到小排序,如果身高相同,比较人数(由小到大);
按照人数重构数组,将节点插入到合适的位置
class Solution {
private:
static bool compareByK(vector<int>& lhs, vector<int>& rhs) {
if(lhs[0] == rhs[0]) return lhs[1] < rhs[1];
return lhs[0] > rhs[0];
}
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), compareByK);
list<vector<int>> queue;
for(int i = 0; i < people.size(); ++i) {
int position = people[i][1];
list<vector<int>>::iterator it = queue.begin();
while(position--) it++;
queue.insert(it, people[i]);
}
return vector<vector<int>>(queue.begin(), queue.end());
}
};
452.用最少数量的箭引爆气球
class Solution {
private:
static bool compare(vector<int>& lhs, vector<int>& rhs) {
return lhs[0] < rhs[0];
}
public:
int findMinArrowShots(vector<vector<int>>& points) {
int result = 1;
sort(points.begin(), points.end(), compare);
for(int i = 0; i < points.size(); ++i) {
if(i > 0 && points[i - 1][1] < points[i][0]) result++;
else if (i > 0) points[i][1] = min(points[i][1], points[i - 1][1]);
}
return result;
}
};
标签:return,people,rhs,随想录,406,vector,lhs,第三十六,points
From: https://www.cnblogs.com/cscpp/p/18244328