[力扣题解]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;
    }
};
相关推荐
鹿角片ljp6 小时前
LeetCode 46. 全排列|吃透回溯
算法·leetcode·职场和发展
鼎艺创新科技7 小时前
不依赖 UE/Unity:我们如何从零搭建一套国产三维 GIS 渲染引擎
人工智能·算法·unity·游戏引擎·三维电子沙盘
.道阻且长.9 小时前
11.LeetCode算法习题讲解--滑动窗口--将x减到0的最小操作数
算法·leetcode·职场和发展
wenyq79 小时前
LeetCode 2460. Apply Operations to an Array
算法·leetcode
.格子衫.10 小时前
032动态规划之区间DP——算法备赛
算法·动态规划
青 春 记 忆10 小时前
LeetCode 142. 环形链表 II|Python 解法详解
python·leetcode·链表
小欣加油11 小时前
leetcode3069 将元素分配到两个数组中I
数据结构·c++·算法·leetcode·职场和发展
不会代码的小猴11 小时前
7. JSON
开发语言·c++·笔记·qt·算法·json
怪奇云呼军12 小时前
知识库也会注入指令?闪电智能VoiceAgent 如何防住 Prompt Injection
人工智能·python·算法·云计算·音视频
阿里云大数据AI技术12 小时前
基于 EMR Serverless Ray 实现 Qwen 模型批量推理实践
人工智能·算法·agent