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. BZOJ4542: [Hnoi2016]大数

    Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...

  2. Poj 3250 单调栈

    1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...

  3. Emoji表情符号录入MySQL数据库报错

    版本一: 1,查看tomcat后台日志,核心报错信息如下:   Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\ ...

  4. RSA_RSA算法原理(二)

    上一次,我介绍了一些数论知识. 有了这些知识,我们就可以看懂RSA算法.这是目前地球上最重要的加密算法. 六.密钥生成的步骤 我们通过一个例子,来理解RSA算法.假设爱丽丝要与鲍勃进行加密通信,她该怎 ...

  5. osg学习示例之遇到问题四骨骼动画编译osgCal

    osg学习示例之遇到问题四骨骼动画编译osgCal 转自:http://blog.csdn.net/wuwangrun/article/details/8239451 今天学到书<OpenSce ...

  6. 如何在weka中连接数据库(转)

    相关准备: Weka.mysql已安装 MYSQL Driver for JDBC 1.进入weka的安装目录 1)新建文件夹lib和文件夹weka,然后将mysql-connector-java-5 ...

  7. Struts的文件下载功能实现代码

    Action: package com.tengfeiyang.action; import java.io.File; import java.io.FileInputStream; import ...

  8. 关于http协议的理解

    一.状态码 1.200:请求成功. 2.302:浏览器进行重定向. 3.304:资源已使用,即有缓存. 4.404:请求失败,请求的资源未在服务器上发现. 5.500:服务器端发生错误. 二.php获 ...

  9. JAVA中序列化和反序列化

    一般程序在运行时,产生对象,这些对象随着程序的停止运行而消失(java回收机制)但如果我们想把某些对象(因为是对象,所以有各自不同的特性)保存下来,在程序终止运行后,这些对象仍然存在,可以在程序再次运 ...

  10. oracle结构与安全

    从宏观上来看:oracle数据库服务器主要有数据库和实例组成. 在orale数据库服务器中,首先会产生一个实例,通过实例访问一个数据库. 一个实例对应着一个数据库. oracle 数据库在逻辑上是按层 ...