406. 根据身高重建队列
思路
先按照身高从大到小排列, 然后再按顺序把前面有多少个插入,因为最前面一定是高的,所以不影响后面排布顺序
代码
class Solution {
static bool cmp (vector<int>& a, vector<int>& b){
if (a[0] == b[0]) return a[1] < b[1];
else return a[0] > b[0];
}
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), cmp);
list<vector<int>> res;
for (int i = 0;i < people.size(); i ++){
auto it = res.begin();
for (int j = 0; j < people[i][1]; j ++) it ++;
res.insert(it, people[i]);
}
return vector<vector<int>> (res.begin(), res.end());
}
};
452. 用最少数量的箭引爆气球
思路
看重合区间,如果本次和之前有重合,去尾端最小值,如果没重合就是重新开始一轮
代码
class Solution {
static bool cmp(vector<int>& a, vector<int>& b){
if (a[0] == b[0]) return a[1] < b[1];
return a[0] < b[0];
}
public:
int findMinArrowShots(vector<vector<int>>& points) {
int res = 1;
sort(points.begin(), points.end(), cmp);
int end = points[0][1];
// cout << points[0][1] << endl;
for (int i = 0; i < points.size(); i ++){
// cout << end << endl;
if (end >= points[i][0]){
end = min (end, points[i][1]);
continue;
}
else {
res ++;
end = points[i][1];
}
}
return res;
}
};