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

我的Greedy+Heap做法:

Array按start moment升序sort,Heap里按end moment升序sort,一旦到Heap里结束时间最早的气球的end,就要扔arrow,这时在heap里的气球都会被引爆

 public class Solution {
public int findMinArrowShots(int[][] points) {
if (points==null || points.length==0 || points[0].length==0) return 0;
if (points[0][0]==Integer.MIN_VALUE && points[0][1]==Integer.MAX_VALUE) return 1;
int res = 0;
Arrays.sort(points, new Comparator<int[]>() {
public int compare(int[] a1, int[] a2) {
return a1[0]!=a2[0]? a1[0]-a2[0] : a1[1]-a2[1];
}
});
PriorityQueue<int[]> queue = new PriorityQueue<int[]>(1, new Comparator<int[]>() {
public int compare(int[] a1, int[] a2) {
return a1[1]-a2[1];
}
}); for (int[] point : points) {
if (queue.isEmpty() || point[0] <= queue.peek()[1]) {
queue.offer(point);
}
else if (point[0] > queue.peek()[1]) {
res++;
queue.clear();
queue.offer(point);
}
}
if (!queue.isEmpty()) res++;
return res;
}
}

改进:因为一旦气球爆了的话,Heap里面的元素要全部清空,不需要知道Heap里面次小值是什么,所以其实不需要Heap, 维护一个最小值即可

 public int findMinArrowShots(int[][] points) {
if(points==null || points.length==0 || points[0].length==0) return 0;
Arrays.sort(points, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0]==b[0]) return a[1]-b[1];
else return a[0]-b[0];
}
}); int minArrows = 1;
int arrowLimit = points[0][1];
for(int i=1;i<points.length;i++) {
int[] baloon = points[i];
if(baloon[0]<=arrowLimit) {
arrowLimit=Math.min(arrowLimit, baloon[1]);
} else {
minArrows++;
arrowLimit=baloon[1];
}
}
return minArrows;
}

Leetcode: Minimum Number of Arrows to Burst Balloons的更多相关文章

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

  6. [Leetcode 452] 最少需要射出多少支箭Minimum Number of Arrows to Burst Balloons 贪心 重载

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

  7. [Swift]LeetCode452. 用最少数量的箭引爆气球 | Minimum Number of Arrows to Burst Balloons

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

  8. 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. 452. Minimum Number of Arrows to Burst Balloons

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

随机推荐

  1. tornado 学习笔记2 Python web主流框架

    2.1 Django 官方网址:https://www.djangoproject.com/ 简介:Django is a high-level Python Web framework that e ...

  2. 通过MongoDB的samus驱动实现基本数据操作

    一.MongoDB的驱动 MongoDB支持多种语言的驱动: 在此我们只介绍 C# 的驱动.仅C#驱动都有很多种,每种驱动的形式大致相同,但是细节各有千秋,因此代码不能通用.比较常用的是官方驱动和sa ...

  3. Jquery超简单遮罩层实现代码

    看了很多代码,下面跟大家分享一下我认为最简单的遮罩层实现方式: 1.样式如下设置: CSS代码: <style type="text/css"> .mask { pos ...

  4. velocityjs 动画库 比jquery默认的animate强

    神坑记录: 1.transform: translate3d(80%,0,0); 无法作为参数,必须修改为这种:translateX: 0% 官方文档 http://velocityjs.org/ g ...

  5. C# - 时间格式

    如果是字符串,需要先转化为DateTime格式 DateTime ExDate = DateTime.Parse(dt.Rows[]["HKMonth"].ToNotNullStr ...

  6. BizTalk 开发系列(四十一) BizTalk 2010 BAM 安装手记

    使用64位系统可以支持更大的内存,现在服务器基本上都使用64位系统.微软从Windows Server 2008 R2开始服务器版的操作系统也只支持64位了,不过对于像BizTalk这种“繁杂的东西” ...

  7. GROUP BY 與 Null 值

    若群組資料行包含了 Null 值,該資料列將變成結果中的一個群組.若群組資料行內包含了多個 Null 值,Null 值將放入單一群組內.此行為定義於 SQL-2003 標準之中. Product 資料 ...

  8. C# Color

    一.创建一个Color对象: Color c=Color.FromKnownColor(KnownColor.colorname); 二.四种同样颜色的不同方式: Color c1=Color.Fro ...

  9. ul+li标签制作表格

    table标签制作表格代码繁琐,且不方便后期代码维护. li标签加上css的浮动样式可以制作多种样式的表格. 代码如下: <ul id="ttttt" style=" ...

  10. 拒绝IE8-,CSS3 transform rotate旋转动画效果(支持IE9+/chrome/firefox)

    <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...