[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 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 xendbursts 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).
给一堆气球,用区间[start,end]来表示气球大小,会有重叠区间。箭从某一个位置发射,只要区间包含这个点的就可以被射中。求用最少的箭数将所有的气球打爆。
解法:贪婪算法Greedy,先给区间排序,遍历区间,第一个区间时,先加1箭,记录区间的end,然后比较后面的区间的start,如果start <= end,说明两个区间有重合,箭从重合区间发射就可以同时打爆这两个气球。重新记录这两个区间里end小的值,在和后面的气球比较。如果start > end,说明没有重合区间,得在发射1箭,箭数加1。遍历结束就能得到所需的最少箭数。
Java:
public class Solution {
public int findMinArrowShots(int[][] points) {
if(points==null || points.length==0) return 0; Arrays.sort(points, ( x , y) -> x[0] == y[0] ? x[1] - y[1] : x[0] - y[0]);
int count = 1;
int arrowLimit = points[0][1];
//贪心法,基于上一个箭,记录当前能够射穿的所有
for(int i = 1;i < points.length;i++) {
if(points[i][0] <= arrowLimit) {
arrowLimit = Math.min(arrowLimit, points[i][1]);
} else {
count++;
arrowLimit = points[i][1];
}
}
return count;
}
}
Python:
class Solution(object):
def findMinArrowShots(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
if not points:
return 0 points.sort() result = 0
i = 0
while i < len(points):
j = i + 1
right_bound = points[i][1]
while j < len(points) and points[j][0] <= right_bound:
right_bound = min(right_bound, points[j][1])
j += 1
result += 1
i = j
return result
C++:
class Solution {
public:
int findMinArrowShots(vector<pair<int, int>>& points) {
if (points.empty()) {
return 0;
} sort(points.begin(), points.end()); int result = 0;
for (int i = 0; i < points.size(); ++i) {
int j = i + 1;
int right_bound = points[i].second;
while (j < points.size() && points[j].first <= right_bound) {
right_bound = min(right_bound, points[j].second);
++j;
}
++result;
i = j - 1;
}
return result;
}
};
All LeetCode Questions List 题目汇总
[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 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 452. Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- [LC] 452. Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 【leetcode】452. Minimum Number of Arrows to Burst Balloons
题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...
随机推荐
- C# 退出应用程序的几种方法
Application.Exit();//好像只在主线程可以起作用,而且当有线程,或是阻塞方法的情况下,很容易失灵 this.Close();//只是关闭当前窗体. Application.ExitT ...
- CodeForces - 24D :Broken robot (DP+三对角矩阵高斯消元 随机)
pro:给定N*M的矩阵,以及初始玩家位置. 规定玩家每次会等概率的向左走,向右走,向下走,原地不动,问走到最后一行的期望.保留4位小数. sol:可以列出方程,高斯消元即可,发现是三角矩阵,O(N* ...
- 36氪首发 | 掘金RPA百亿新蓝海,弘玑Cyclone获DCM、源码千万美元A轮融资
https://36kr.com/p/5213638?ktm_source=feed 在“没风口”的2019年,RPA算是一个“小风口”了. 36氪了解到,近日国内数家RPA公司已完成或即将完成融资, ...
- 基于Helm和Operator的K8S应用管理
https://blog.csdn.net/RancherLabs/article/details/79483013 大家好,今天我们分享的内容是基于Helm和Operator的K8S应用管理. 我们 ...
- ReactiveX 学习笔记(30)操作符辨析
RxJava: merge/concat/switch RxJS: merge/concat/switch/exhaust RxSwift: merge/concat/switchLatest mer ...
- Spring框架的JDBC模板技术和事物
Spring框架的JDBC模板技术 技术分析之Spring框架的JDBC模板技术概述 1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 ...
- MySQL入门篇之mysqldump备份和恢复
一.备份单个数据库 1.备份命令:mysqldump MySQL数据库自带的一个很好用的备份命令.是逻辑备份,导出 的是SQL语句.也就是把数据从MySQL库中以逻辑的SQL语句的形式直接输出或生成备 ...
- Comet OJ - Contest #14 转转的数据结构题 珂朵莉树+树状数组
题目链接: 题意:有两个操作 操作1:给出n个操作,将区间为l到r的数字改为x 操作2:给出q个操作,输出进行了操作1中的第x到x+y-1操作后的结果 解法: 把询问离线,按照r从小到大排序 每次询问 ...
- 全局异常捕获处理-@ControllerAdvice+@HandleException
涂涂影院管理系统这个demo中有个异常管理的标签,用于捕获 涂涂影院APP用户异常信息 ,有小伙伴好奇,排除APP,后台端的是如何处理全局异常的,故项目中的实际应用已记之. 关于目前的异常处理 在使用 ...
- 正确创建本地C++发布构建PDBS
在调试版本中遇到的一个问题是编译本地的C++应用程序.例如,许多局部变量消失了,因为代码生成器没有将它们放在堆栈上,而是将它们放在寄存器中,就像在调试生成中发生的那样.此外,release积极地构建对 ...