leetcode 452. Minimum Number of Arrows to Burst Balloons (除掉气球的最小箭数)

LeetCode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(贪心问题) LeetCode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球Description:Example:解答代码 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 start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.

Example:

Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).

题中数组代表气球摆放的直径范围,只考虑x坐标,不考虑y坐标
如[10, 16]代表气球的范围从10到16,一个范围内只需要一支arrow就能把该范围所有气球都burst掉
重叠区域也是只需要一支arrow,比如[1,6]和[2,8]在2~6的任一点放arrow,这两个区间可以同时burst掉

让找出最少的arrow可以把所有区间的气球都burst掉

思路:
这种区间重叠问题,首先想到的是按起始点排序或者终点排序

(1) 按起始点排序的话,需要注意右边界的问题
比如

[1, 100]
[3, 8]
[9, 11]

显然前两个区间一个arrow就能搞定,因为第二个区间起始点3<100
但是如果不更新右边界,还是100
那么第3个区间也能判断用一个arrow搞定,但是[3,8]和[9,11]是不重叠的,必须用2个arrow

所以在有重叠区域时,要把右边界设为较短的那一方
此思路和435题类似

注意在排序比较时可以用Integer.compare, 也可以用a[0] < b[0] ? -1 : 1,
就是不能用a[0] - b[0], 因为start和end的取值范围可以是2-31,相减整数会溢出。

//19ms
    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points,(a,b)->Integer.compare(a[0],b[0]));
        int res = 1;
        int end = points[0][1];

        for(int[] cur : points) {
            if(cur[0] <= end) {     
                end = Math.min(end, cur[1]);
            } else {
                res ++;
                end = cur[1];
            }
        }
        return res;

    }

(2) 按endpoint排序
因为start 一定 < end(题目要求),所以看两个区间(x1, x2) 和 (y1, y2)是否重叠,
取决于y1 是否 < x2。
x1是派不上用场的,因为不管y1在x1的左边还是右边,只要y1 <= x2,就一定重叠,y1 > x2就一定不重叠。
只有不重叠,arrow的个数才会+1.

重叠的话,endpoint要以短的那个为准,但是既然已经按endpoint排序,前面的end一定比后面的end小,所以不需要处理。

java:

//19ms
    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points,(a,b)->Integer.compare(a[1],b[1]));
        int res = 1;
        int end = points[0][1];

        for(int[] cur : points) {
            if(cur[0] > end) {
                res ++;
                end = cur[1];
            }
        }
        return res;
    }

c++

    int findMinArrowShots(vector<vector<int>>& points) {
        if (points.empty()) {
            return 0;
        }
        
        sort(points.begin(), points.end(), [&](vector<int>& a1, vector<int>& a2) {
           return a1[1] < a2[1]; 
        });
        
        int res = 1;
        int right = points.front()[1];
        
        for (const auto& point : points) {
            if (point[0] > right) {
                res ++;
                right = point[1];
            }
        }
        
        return res;
    }
leetcode 452. Minimum Number of Arrows to Burst Balloons | 452. 用最少数量的箭引爆气球(左程云:最大线段重合问题) 题目 https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/ 题解 重叠区间问题可以总结为在坐标轴上若干个位置为 (start(i),end(i)) 的区间,要求求解这些区间中有多少个不重叠区间,或者合并重叠的区间。 对于重叠区间问题,海外版评论区有人总结了模板,见 A Concise Template for “Overlapping Interval Problem” 该问题分两类:第一类求重叠区间个数(lee 阅读详情

相关推荐

Leetcode 452. Minimum Number of Arrows to Burst Balloons(python+cpp)

Leetcode 452. Minimum Number of Arrows to Burst Balloons题目解析python代码C++版本 题目 解析 Leetcode官方solution这边对贪心策略有一段话挺经典 这道题目跟435本质上是一样的,只不过435是让你算要去掉多少个气球,而这边是算要用多少支箭,具体流程如下: 将intervals按照结尾排序 将第一个interval...

qq_37821701的博客 434

452. Minimum Number of Arrows to Burst Balloons

452.Minimum Number of Arrows to Burst Balloons 题目: There are some 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'.

同销万古愁的博客 172

告别浏览器崩溃!EasyPlayer.js长时间播放FLV视频内存泄漏排查指南

本文针对EasyPlayer.js长时间播放FLV视频导致浏览器内存泄漏和崩溃的问题,提供了一套系统性排查与解决方案。核心策略包括将视频编码从H.265转为H.264以降低解码压力,以及将传输协议从HTTP-FLV切换至WebRTC以优化资源管理。文章还详细介绍了播放器实例生命周期管理、浏览器开发者工具监控等精细化排查手段,帮助开发者实现稳定、长时间的多路视频播放。

weixin_29229055的博客 190

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

题目来源 minimum-number-of-arrows-to-burst-balloons 题目描述 class Solution { public: int findMinArrowShots(vector<vector<int>>& points) { } }; 题目解析 问题:要求当前位置下(不许排序),要把所有的箭头引爆,需要使用多少箭 遍历所有数组,从第一个气球开始,如果可以引爆其他气球,就引爆他们 这个问题就转换为求重叠区间有

OceanStar的博客 494

利用Matlab绘制非线性系统相图:从理论到实践

本文详细介绍了如何利用Matlab进行非线性系统的相图绘制与相平面分析。从理论基础的状态空间转换出发,通过无阻尼线性谐振子和范德波尔振荡器等实例,手把手演示了使用ode45求解器绘制相图、分析平衡点与极限环的完整流程,并分享了方向场绘制、参数分岔探索及常见错误解决方案等高级技巧,为动力学系统分析与控制设计提供了直观强大的可视化工具。

yhn456789的博客 918

iPhone免越狱Git服务器搭建教程-iSH

iPhone免越狱Git服务器搭建教程

jts666的博客 4439

Leetcode 452. 用最少数量的箭引爆气球(贪心)——python

前几天在补充数据方面的业务知识,有四五天没刷leetcode了,有点生疏的感觉。 1. 题目描述 链接:https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons 在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以y坐标并不重要,因此只要知道开...

missTu~的博客 559

西工大noj100题python2024第一季1-10

【代码】西工大noj100题python2024第一季1-10。

weixin_70523540的博客 1820

LeetCode 452. Minimum Number of Arrows to Burst Balloons(用最少数量的箭引爆气球)

题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都被刺破。求解最小的投飞镖次数使所有气球都被刺破。 如果最多有 n 个不重叠的区间,那么就至少需要 n 个箭头穿透所有区间 箭头如果碰到气球的边界气球也会爆炸,所以说相当于区间的边界触碰也算重叠: public int findMinArrowShots(int[][] points) { ...

肉丸不肉 523

博图Advanced实战:VB脚本实现HMI画面智能弹出(附完整代码解析)

本文详细介绍了在西门子博图Advanced平台中利用VB脚本实现HMI画面智能弹出的实战方法。通过动态计算坐标、分辨率自适应方案和完整代码解析,帮助工程师解决不同显示环境下的定位难题,提升工业自动化界面的交互体验。

weixin_29041767的博客 349

【DEIM创新改进】全网首发、独家特征融合创新改进篇 | CVPR2025 | 引入RLAB残差线性注意力特征融合块,有效融合并强调多尺度特征,有效减少信息丢失,助力DEIM目标检测有效涨点

本文提出了一种改进DEIM网络模型的新方法RLAB(残差线性注意力特征融合模块),用于医学图像生物标志物分割。RLAB模块通过多尺度残差精炼和注意力机制,有效整合编码器跳跃连接与解码器特征图,提升特征对齐和关键区域关注能力。实验表明,该方法在MoNuSeg、DSB等四个医学图像数据集上显著优于现有方法,最高提升4.03%的绝对性能。文章详细介绍了RLAB的网络结构、核心代码实现方式,并提供了将模块集成到DEIM模型的具体步骤。

Ai缝合怪、助力小伙伴高效发论文 357

452. Minimum Number of Arrows to Burst Balloons

452. Minimum Number of Arrows to Burst Balloons方法1: greedy There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates o...

wilzxu的博客 252

【Windows Server 2019】活动目录 (Active Directory) ——理论

博文介绍了活动目录 (Active Directory) 的定义,作用以及与 DNS 的关系。同时也介绍了AD的组织结构,包括域控制器,子域,对象,属性,容器,组织单位,域间信任等术语的解释,实验拓扑图。

VMware, Windows, Linux, Cisco, Container, Kubernetes, Kali 7758

LeetCode452. Minimum Number of Arrows to Burst Balloons

【代码】LeetCode452. Minimum Number of Arrows to Burst Balloons。

叶卡捷琳堡的博客 1678

【Unity-UGUI控件全面解析】| Layout自动布局组件详解

UGUI中有个自动布局组件Auto Layout,主要包含下面几个模块(点击可进入官方文档下查看描述): 自动布局 布局元素 (Layout Element) 水平布局组 (Horizontal Layout Group) 垂直布局组 (Vertical Layout Group) 网格布局组 (Grid Layout Group) 内容大小适配器 (Content Size Fitter) 自动布局系统 基于布局元素和布局控制器的概念。一个布局元素是具有矩形变换以及其他可选组件的游戏对象。布局元素对自身

努力前行,总会成为自己心中的那道光 2万+

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【小白爬Leetcod452】3.5 用最少数量的箭引爆气球 Minimum Number of Arrows to Burst Balloons

【小白爬Leetcod452】3.5 用最少数量的箭引爆气球 Minimum Number of Arrows to Burst Balloons题目discription中文描述尝试一 解答错误...尝试二 先排序气球...尝试三 用vector作甚... Leetcode 452 medium\color{#FF6347}{medium}medium 点击进入原题链接:Leetcode 452 用最少数量的箭引爆气球 Minimum Number of Arrows to Burst Balloo

weixin_41604040的博客 416

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Leetcode 452. Minimum Number of Arrows to Burst Balloons

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 ...

fengran_mark的博客 136

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

Minimum Number of Arrows to Burst Balloons

Leetcode-Algorithm-Divide and Conquer-452

huangjx36的博客 326

leetcode - 452. Minimum Number of Arrows to Burst Balloons

算法系列博客之Greedy Greedy 贪心算法是一种非常优美的算法,不过贪心算法本身的可行性很多时候会受到一些局限。但是一旦能够找到一种可行的贪心策略,问题的解决将会变得非常高效,因为通常情况下,贪心算法的复杂度是O(n)本篇博客将运用这种思想来解决leetcode上452号问题 问题描述:There are a number of spherical balloons spread in

Quiteen的博客 463

【LeetCode】452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(Medium)(JAVA)

【LeetCode】452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(Medium)(JAVA) 题目地址: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/ 题目描述: There are some spherical balloons spread in two-dimensional space. For each balloon, p

qq_16927853的博客 200

LeetCode 452. Minimum Number of Arrows to Burst Balloons

原题目:https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/ 思路: 首先对数组按照右端点进行排序,每次从右端点射出,就可达到贪心策略。如果起点大于了射箭的位置,数目加一,箭的位置设置为当前气球的右端点。 代码: class Solution { public: int findMinArrowShots(vector<vector<int>>& ..

庾信平生最萧瑟 215

LeetCode452. Minimum Number of Arrows to Burst Balloons题解

有若干个气球,给出了它们的直径两个端点的坐标(X轴),我们的工作是需要用一支箭来戳破气球。例如两个气球的直径分别是(1,6)和(2,8),那么我们就可以在x=6的坐标处发射一支箭从而将它们一起戳破。求解的问题是:给出一系列气球直径的坐标之后,最少能用多少支箭就可以戳破它们。

SYSU_LBY的博客 1318

Leetcode 452 Minimum Number of Arrows to Burst Balloons

Leetcode 452 Minimum Number of Arrows to Burst Balloons题目思路和代码 题目 There are some 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-coor

weixin_43796689的博客 146

LeetCode 题解之 452. Minimum Number of Arrows to Burst Balloons

452. Minimum Number of Arrows to Burst Balloons 题目描述和难度 题目描述: 在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,...

sdf57的博客 348

LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons

【代码】LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons。

Made in Code 1333

[Leetcode力扣 452] Minimum Number of Arrows to Burst Balloons

一、题目描述 给一堆二维闭区间表示气球,问最少用几支箭能把所有气球射爆 二、解法:区间贪心

NiuYutao的博客 243
上一篇: leetcode 860. Lemonade Change
下一篇: leetcode 630. Course Schedule III(课程表III)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址