这里有leetcode题集分类整理!!!
解题思路:
排序 + 贪心
贪心思路: 按照右边界 排序 , 迭代时,左边界超过右边界时更新右边界。
AC代码:
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if(points.empty()) {
return 0;
}
sort(points.begin(),points.end(),[](const vector<int> &u, const vector<int> &v){
return u[1]<v[1];
});
int pos = points[0][1];
int ans = 1;
for(const vector<int> &balloon:points) {
if(balloon[0] > pos) {
pos = balloon[1];
ans ++ ;
}
}
return ans ;
}
};