当前位置:   article > 正文

中等题 leetcode 452.用最少的箭射气球(贪心)_射气球c加加

射气球c加加

这里有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 ;
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/47753
推荐阅读
相关标签
  

闽ICP备14008679号