[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 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的更多相关文章
- [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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 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 ...
- 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球
在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...
随机推荐
- jquery选择器 之 获取父级元素、同级元素、子元素
jquery选择器 之 获取父级元素.同级元素.子元素 一.获取父级元素 1. parent([expr]): 获取指定元素的所有父级元素 <div id="par_div" ...
- C#中线程对控件的访问
Control类提供了一个Invoke方法来给子线程访问主线程的控件,它的原型是酱紫的: object.Control.Invoke(Delegate method); object.Control. ...
- Python3利用BeautifulSoup4抓取站点小说全文的代码
再写一个用BeautifulSoup抓站的工具,体会BeautifulSoup的强大. 根据小说索引页获取小说全部章节内容并在本地整合为小说全文.不过不是智能的,不同的站点对代码需要做相应的修改. # ...
- Lucene教程(转)
Lucene教程 1 lucene简介1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿来 ...
- 【总结】清除webbrowser cookie/session的6种方法
下面是我测试下来的6种清除webbrowser中cookie的6种方法: //方法一:调用 wininet.dll清除cookie (推荐) SuppressWininetBehavior(); // ...
- linux操作系统-脚本入门
背景:在使用linux时,经常会写一些linux命令片段,比较麻烦,有经验的程序员会把 这些碎片式的命令写成shell脚本 1.重启tomcat脚本 #!/bin/sh #kill tomcat pi ...
- jQuery.cookie.js插件了解及使用方法
jquery.cookie.js插件实现浏览器的cookie存储,该插件是基于jquery开发,方便cookie使用. jquerycookie.js的下载地址 http://plugins.jque ...
- Twitter的分布式自增ID算法snowflake (Java版)
概述 分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种 ...
- Winform主窗体设计
主窗体顶部为菜单按钮,子窗体内嵌入Panel显示 界面如下: 第二步,主窗体离不开的几个方法 1,点击菜单功能,加载子窗体 private void btnOpenForm_Click(object ...
- Oracle直方图的详细解析
yuanwen:http://blog.csdn.net/javacoffe/article/details/5578206 Oracle直方图解析 一. 何谓直方图: 直方图是一种统计学上的工 ...