[力扣题解]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;
    }
};
相关推荐
执风挽^13 分钟前
Python基础编程题2
开发语言·python·算法·visual studio code
Z9fish23 分钟前
sse哈工大C语言编程练习20
c语言·开发语言·算法
晓131328 分钟前
第六章 【C语言篇:结构体&位运算】 结构体、位运算全面解析
c语言·算法
iAkuya34 分钟前
(leetcode)力扣100 61分割回文串(回溯,动归)
算法·leetcode·职场和发展
梵刹古音37 分钟前
【C语言】 指针与数据结构操作
c语言·数据结构·算法
VT.馒头42 分钟前
【力扣】2695. 包装数组
前端·javascript·算法·leetcode·职场和发展·typescript
CoderCodingNo2 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
大闲在人2 小时前
7. 供应链与制造过程术语:“周期时间”
算法·供应链管理·智能制造·工业工程
小熳芋2 小时前
443. 压缩字符串-python-双指针
算法
Charlie_lll3 小时前
力扣解题-移动零
后端·算法·leetcode