Leetcode 452. 用最少数量的箭引爆气球

class Solution {
public:
    int findMinArrowShots(vector<pair<int, int>>& points) {
        if(points.size()==0)
            return 0;

        sort(points.begin(), points.end(), [](const auto a, const auto b){
                                                                            return a.first < b.first;
                                                                         });
        int short_num = 1;
        int short_beg = points[0].first;
        int short_end = points[0].second;
        for(int i=1; i<points.size(); ++i)
        {
            if(points[i].first <= short_end)
            {
                short_beg = points[i].first;
                if(short_end > points[i].second)
                    short_end = points[i].second;
            }
            else
            {
                ++short_num;
                short_beg = points[i].first;
                short_end = points[i].second;
            }
        }
        return short_num;
    }
};

原文地址:https://www.cnblogs.com/randyniu/p/9334861.html

时间: 2024-08-09 20:19:17

Leetcode 452. 用最少数量的箭引爆气球的相关文章

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

452. 用最少数量的箭引爆气球 题目描述 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面内最多存在104个气球. 一支弓箭可以沿着x轴从不同点完全垂直地射出.在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被引爆.可以射出的弓箭的数量没有限制. 弓箭一

LeetCode | 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球【Python】

LeetCode 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球[Medium][Python][区间贪心] Problem LeetCode There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of

力扣第452题 用最少数量的箭引爆气球

力扣第452题 用最少数量的箭引爆气球 class Solution { public: int findMinArrowShots(vector<vector<int>>& points) { int len = points.size(); if (len == 0) { return 0; } sort(points.begin(), points.end(), [](const vector<int> &pl1, const vector<

[LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

LeetCode:用最少的箭引爆气球【452】

LeetCode:用最少的箭引爆气球[452] 题目描述 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面内最多存在104个气球. 一支弓箭可以沿着x轴从不同点完全垂直地射出.在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足  xstart ≤ x ≤ xend,则该气球会被引爆.可以射出的弓箭的数量没

数字问题-LeetCode 452、453、454、455、456、459(KMP算法)

LeetCode # 452 453 454 455 456 459 1 编程题 [LeetCode #452]用最少数量的箭引爆气球 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面内最多存在104个气球. 一支弓箭可以沿着x轴从不同点完全垂直地射出.在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足

[Leetcode 452] 最少需要射出多少支箭Minimum Number of Arrows to Burst Balloons 贪心 重载

[题目] There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates

Leetcode 200.岛屿的数量 - DFS、BFS

Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7MB,99.7% / 92% public: /** * row,col: 坐标,以0开始. * 用row和col,而不是x,y. 否则容易写成grid[x][y],顺序不对!! */ void dfs_set_zero(vector<vector<char>>& grid, i

[LeetCode] 452 Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s