题目9:
代码(首刷看解析 2024年2月17日):
class Solution {
private:const static bool cmp(vector<int>& a, vector<int>& b) {return a[0] < b[0];}
public:int findMinArrowShots(vector<vector<int>>& points) {if (points.size() == 0) return 0;/*将数组按首元素坐标升序排列*/sort(points.begin(), points.end(), cmp);int res = 1;int index = 0;for (int i = 0; i < points.size(); ++i) {/*如果某个数组的首元素的坐标大于之前数组的最短末尾元素坐标,则箭增加*/if (points[i][0] > points[index][1]) {res++;index = i;}if (points[i][1] <= points[index][1]) index = i;}return res;}
};