[力扣题解]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;
    }
};
相关推荐
CV-Climber1 小时前
检索技术的实际应用
人工智能·算法
hhzz2 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_3 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
TsingtaoAI3 小时前
3D高斯泼溅技术发展及其在具身智能领域的应用综述
人工智能·算法·ai·具身智能·高斯泼溅
atunet3 小时前
关于图算法中的连通分量检测与最小割问题7
算法
心运软件4 小时前
银行客户流失预测(Python 完整实战)
算法
邪神与厨二病4 小时前
牛客周赛 Round 153
python·算法
qizayaoshuap6 小时前
# ❌ 井字棋 — 鸿蒙ArkTS Minimax AI算法与博弈系统设计
人工智能·算法·华为·harmonyos
皓月斯语6 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet6 小时前
树状结构在查询优化中的作用与实现细节7
算法