[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坐标就足够了.开始坐标总是小于结束坐标.平面 ...
随机推荐
- 新版react踩坑总结
使用es6语法与原本es5语法几个有区别的地方 1.React.creatClass与React.Component var Component = React.createClass({ rende ...
- mysql查询表的数据大小
在需要备份数据库里面的数据时,我们需要知道数据库占用了多少磁盘大小,可以通过一些sql语句查询到整个数据库的容量,也可以单独查看表所占容量. 1.要查询表所占的容量,就是把表的数据和索引加起来就可以了 ...
- sizeof(结构体) = ?
关于结构体大小怎样计算的文章,我想网上一搜到处都有人总结,本人之所以在此基础上还要发表这样的文章是想用最简单的计算方法来总结前人给出的结论,以致我们在以后在对结构体相关编程中不会陷入字节对齐的陷阱中. ...
- vs快捷方式
项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添加 ...
- JVM内存区域介绍
学习JVM第一个要了解的就是JVM的内存区域. Java虚拟机在运行时会从操作系统内存中划分一部分出来作为JVM内存,而JVM内存又划分为以下几个区域: 大体上可以分为两种: 线程共享数据区 该类型的 ...
- 用CSS3在手机上写弹出框,遮盖层
html: 在页面头部要写 <title>网上预约</title> <link href="../App_Themes/default/css/header.c ...
- 统计黑ip的脚本
#!/bin/bash > ] do cd /var/openresty/nginx/logs#统计nginx日志,将结果重定向到文件blackip.txt中 cat access.log|gr ...
- [OC笔记]我的第一个OC程序
这是我第一个OC程序,详情都写在了注释里面,讲的是Student和Book这两个类的创建和使用.(是不是觉得student这个类在各种语言入门代码里经常出现呢?真实有爱) 看完这些,你会发现,咱们其实 ...
- C/C++入门基础----指针(1)
指针其实就是一个变量, 和其他类型的变量一样.在32位计算机上, 指针占用四字节的变量.指针与其他变量的不同就在于它的值是一个内存地址,指向内存的另外一个地方, 指针能够直接访问内存和操作底层的数据, ...
- Lucene教程(转)
Lucene教程 1 lucene简介1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿来 ...