[力扣题解]452. 用最少数量的箭引爆气球

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

思路

贪心法
希望尽可能射爆叠在一起的气球;
以气球的左边界进行升序排序,再从左到右遍历,遇到有重叠的气球,则让当前气球的有边界与上一个气球的右边界对齐(min操作);

代码

class Solution {
private:
    static bool compare(vector<int> a, vector<int> b)
    {
        return a[0] < b[0];
    }

public:
    int findMinArrowShots(vector<vector<int>>& points) {
        int result = 1, i;
        if(points.size() == 1)
        {
            return 1;
        }
        sort(points.begin(), points.end(), compare);
        for(i = 1; i < points.size(); i++)
        {
            if(points[i][0] > points[i-1][1])
            {
                result++;
            }
            else
            {
                points[i][1] = min(points[i][1], points[i-1][1]);
            }
        }

        return result;
    }
};

相关推荐

  1. [题解]452. 数量引爆气球

    2024-05-14 12:54:06       13 阅读
  2. 452. 数量引爆气球

    2024-05-14 12:54:06       21 阅读
  3. 【LeetCode-452数量引爆气球(贪心)

    2024-05-14 12:54:06       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-14 12:54:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-14 12:54:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-14 12:54:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-14 12:54:06       18 阅读

热门阅读

  1. 算法训练营day37

    2024-05-14 12:54:06       11 阅读
  2. Hive的条件判断

    2024-05-14 12:54:06       10 阅读
  3. Spring new对象时注解失效

    2024-05-14 12:54:06       10 阅读
  4. [开发] Oh My Zsh 安装pnpm插件

    2024-05-14 12:54:06       13 阅读
  5. content-type之multipart/form-data和application/json比较

    2024-05-14 12:54:06       11 阅读
  6. 2、MySQL总结

    2024-05-14 12:54:06       12 阅读
  7. Prompt提示词的技巧

    2024-05-14 12:54:06       10 阅读
  8. 删除有序数组中的重复项

    2024-05-14 12:54:06       11 阅读
  9. 从零学算法994

    2024-05-14 12:54:06       9 阅读
  10. C++|内存管理(2)

    2024-05-14 12:54:06       10 阅读