452. 用最少数量的箭引爆气球

image.png
若当前气球和上一气球重叠,就更新当前气球的最小右边界

  1. points[i][1] = min(points[i][1],points[i-1][1]);

若不重叠,箭的数量加一

  1. class Solution {
  2. public:
  3. static bool cmp(vector<int>& a,vector<int>& b)
  4. {
  5. return a[0]<b[0];
  6. }
  7. int findMinArrowShots(vector<vector<int>>& points) {
  8. int result = 1;
  9. sort(points.begin(),points.end(),cmp);
  10. for(int i=1;i<points.size();i++)
  11. {
  12. if(points[i][0]>points[i-1][1])
  13. result++;
  14. else
  15. points[i][1] = min(points[i][1],points[i-1][1]);
  16. }
  17. return result;
  18. }
  19. };