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

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

思路

贪心法

希望尽可能射爆叠在一起的气球;

以气球的左边界进行升序排序,再从左到右遍历,遇到有重叠的气球,则让当前气球的有边界与上一个气球的右边界对齐(min操作);

代码

cpp 复制代码
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;
    }
};
相关推荐
Frostnova丶7 小时前
【算法笔记】数学知识
笔记·算法
吴可可1237 小时前
AutoCAD 2016与2014二次开发关键差异
算法
雨白8 小时前
哈希:以时间换空间的算法实战
算法
San813_LDD10 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x1387028595710 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta199811 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油11 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero12 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称12 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划