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 最少箭数爆气球的更多相关文章

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

  2. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  3. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

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

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  5. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  7. 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

    [抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...

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

  9. 【leetcode】452. Minimum Number of Arrows to Burst Balloons

    题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...

随机推荐

  1. 如何有效使用Project(2)——进度计划的执行与监控

    继上次的的<编制进度计划.保存基准>继续讲解如何对计划进行执行和监控. 计划执行即:反馈实际进度.反馈工作消耗(本文只考虑工时,不考虑成本).提出计划变更请求.如果你的企业实施了专门的PM ...

  2. c++读写matlab中.mat数据

    前言:在进行图形图像处理时,经常会用到matlab进行算法的仿真验证,然后再移植到别的语言中.有时会涉及到数据的交互,比如直接读取matlab的.mat类型数据,或者是将c++中的数组存为.mat,为 ...

  3. js 做留言提交

    如下是留言提交源码 功能:点击按钮或点击enter键可以提交内容扩展:ctrlKey\shiftKey\altKye 可以将如上三个参数与oEvent.keyCode == 13 进行与操作可以得到 ...

  4. javascript之随机密码[必包含大写,小写,数字]

    js取两个数字之间的随机数: parseInt(Math.random()*(上限-下限+1)+下限) 如:取1-10之间的随机数   parseInt(Math.random()*(10-1+1)+ ...

  5. GoCN每日新闻(2019-10-21)

    GoCN每日新闻(2019-10-21) GoCN每日新闻(2019-10-21)   1. 使用 Golang, RabbitMQ 和 Protobuf 构建高效的微服务 https://mediu ...

  6. Pytest权威教程(官方教程翻译)

    Pytest权威教程01-安装及入门 Pytest权威教程02-Pytest 使用及调用方法 Pytest权威教程03-原有TestSuite的执行方法 Pytest权威教程04-断言的编写和报告 P ...

  7. QML学习(四)——<Text显示>

    文本显示是界面开发必不可少的内容,在Qt Quick模块中提供了 Text 项目来进行文本的显示,其中可以使用 font 属性组对文本字体进行设置.这一篇我们来看看它们的具体使用. 使用字体 就像前面 ...

  8. Mac版微信无法安装之始末

    前言 Mac版微信安装不了...纠结了一周时间 ̄□ ̄||... 今天终于可以登录了(虽然还是没有安装到电脑上,但可以使用了) 因为之前也查了很多,有人遇到,但是没有可以解决我这个问题的方法, 浪费了很 ...

  9. tf.matmul()报错expected scalar type Float but found Double

    tf.matmul(a,b)将矩阵a乘以矩阵b,生成a * b,这里的a,b要有相同的数据类型,否则会因为数据类型不匹配而出错. 如果出错,请看是前后分别是什么类型的,然后把数据类型进行转换.

  10. HAProxy+Keepalived高可用负载均衡

    一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...