452. 用最少数量的箭引爆气球 - 力扣(LeetCode)
类似题:
LeetCode第 646 题:最长对数链(C++)_zj-CSDN博客
leetcode 第 435 题:无重叠子区间(C++)_zj-CSDN博客
重叠不重叠反着来就可以了,所以寻找最长的不重叠子区间个数就行了:如果最多有 n 个不重叠子区间,那么就至少需要 n 个箭穿透所有区间。
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if(points.empty()) return 0;
sort(points.begin(), points.end(), [](const vector<int> &a, const vector<int> &b){
return a[1] < b[1] || (a[1] == b[1] && a[0] < b[0]);
});
int r = points[0][1];
int cnt = 1;
for(int i = 1; i < points.size(); ++i){
if(points[i][0] > r){
++cnt;
r = points[i][1];
}
}
return cnt;
}
};