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. spring mvc 第四天【注解实现springmvc 配合使用Exception Resolver 的配置】

    Tips:这里使用具体springmvc的异常处理只是拿一个简单的案例来说明问题,并不做实用,如有需求可做更改: 这里演示的仅是能够实现service验证的一种方案,如有更好的请留言我会努力学习!! ...

  2. 解决Linux系统下程序找不到动态库的方法

    思路:一般来说,通过make命令已经将程序依赖的动态库编译出来了,通过make install命令已经将动态库安装到系统的某个路径下.找没找到动态库就看这个路径是否包含在系统默认搜索动态库的路径中,如 ...

  3. 关于rem的计算顺序

    /*响应式字体*//* * 字体响应式 * 屏幕>640px时,html字体大小 * 屏幕<640px时,html字体根据屏幕做出相应  * */(function(doc,win){   ...

  4. 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)

    Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...

  5. mysql学习(1)-linux操作系统源码包安装

    背景: CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.22. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server ...

  6. Java线程池入门

    序 为什么要用线程池?什么情况下才会用到线程池? 并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间. 因此 ...

  7. onethink和thinkphp3.2学习

    thinkphp发布3.2版本之后,也发布了一个简单的内容管理系统onthink,这样有助于理解thinkphp3.2的使用: 一.首先最关键的一点是thinkphp3.2中加入了命名空间的使用 什么 ...

  8. 解决Visual Studio 2010闪退问题

    许多人都会面临这样的问题,vs2010闪退,明明程序执行成功,明明没有错误,缺闪了一下结束. 闪退问题主要是缓存的问题,通过在程序末尾,即main函数后增加 getchar(); getchar(); ...

  9. javascript:history.go()和History.back()的区别(转载)

    javascript:history.go()和History.back()的区别       <input type=button value=刷新 onclick="window. ...

  10. easyui datagrid加载json

      服务端: string pseries = context.Request["ajaxSearch"].ToString().Trim(); var jsonMap = new ...