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).
解法: 把“气球”看成是一个线段,实际上一个“箭”的坐标就是一个点,要尽可能的被多个线段覆盖到。对于线段先进行排序,然后遍历一遍。
注意遍历的时候,要要更新边界值,当下一个线段的起始值大于边界值的时候,表示要新增一个“箭”。
public class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length == 0){
return 0;
}else if (points.length == 1) {
return 1;
} else {
Arrays.sort(points, (o1, o2) -> o1[0] - o2[0]);
int count = 1;
int upperBound = points[0][1];
for (int i = 0; i < points.length - 1; ) {
while ( i < points.length && points[i][0]<= upperBound ){
if (points[i][1] < upperBound){
upperBound = points[i][1];
}
i++;
}
if (i == points.length){
break;
}else {
count++;
upperBound = points[i][1];
}
}
return count;
}
}
}
												

[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. 452. 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, pr ...

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

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

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

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

    在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...

随机推荐

  1. Android 学习 (一)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...

  2. 抽象和封装_JAVA_OOP

    很久没做笔记了,没有以前的刚开始学习软件时候的热情了.包括几年前U盘损坏,数据丢失,通过数据恢复,也只是找回一些零星的碎片. 现在就抽时间把以前的技术笔记找回来,这十条记录在电脑上显示的最后修改日期为 ...

  3. 关于tesseract-ocr3的训练和使用

    众所周知,这是一个出色的字符识别软件.这个开源项目可以在http://code.google.com/p/tesseract-ocr/downloads/list下载. 在使用时,建议使用3而不要使用 ...

  4. myeclipse 手动安装 lombok

    1. 将 lombok.jar 复制到 myeclipse.ini / eclipse.ini 所在的文件夹目录下    2. 打开 eclipse.ini / myeclipse.ini,在最后面插 ...

  5. 关于js原型继承

    js的每个类都有一个prototype对象 访问对象的属性时,会先访问到对象自身是否有定义这个属性 如果没有定义,就会去访问对象所属类型的prototype对象是否有此属性 原型继承就是把类型的pro ...

  6. python打印服务器所有进程

    #有时候我们需要查看服务器上所有进程,来判断哪些进程是否已经称为僵尸进程#!/usr/local/bin/python3.5 import psutil for i in psutil.pids(): ...

  7. Google统计

    <!-- Google Analytics --><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i ...

  8. MINA系列学习-IoBuffer

    在阅读IoBuffer源码之前,我们先看Mina对IoBuffer的描述:A byte buffer used by MINA applications. This is a replacement ...

  9. C/C++入门基础---指针(2)

    5,数组指针的不同含义 int a[5][10]; printf(%d, %d, %d\n", a, a+1, &a+1);  //1310392,1310432,1310592 a ...

  10. Spring环境搭建之:导入jar包、配置文件名称及放置位置

    Spring环境搭建之:导入jar包.配置文件名称及放置位置 现在项目开发中spring框架应用的还是比较多的,自己用的还不太熟练,每次用的时候总配置半天,总有些配置弄错,就找个时间总结以下,方便以后 ...