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 xendbursts 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).
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
二维数组就写个points.len = 0 就行了,没必要写points[0].len = 0
[思维问题]:
知道是扫描线,忘了怎么写了:更新结尾。必要时+count
[英文数据结构或算法,为什么不用别的数据结构或算法]:
扫描线要先对取件进行排序。
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
为了一把箭能涉及到全部,end选取的是min,需要因地制宜

[一刷]:
- 循环过程中要依据end来进行更新
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
以后强制性写test case了:
/*
Input:
[[10,16], [2,8], [1,6], [7,12]]
sort:
Input:
[[1,6], [2,8], [7,12], [10,16]]
end 6 6 12 12
count 1 1 +1=2 2
*/
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int findMinArrowShots(int[][] points) {
//corner cases
if (points == null || points.length == 0) return 0;
//initialization: sort
int count = 1;
Arrays.sort(points, (a, b) -> (a[0] - b[0]));
//for loop and get count
int end = points[0][1];
for (int i = 1; i < points.length; i++) {
if (end < points[i][0]) {
count++;
end = points[i][1];
}
else end = Math.min(end, points[i][1]);
}
/*
Input:
[[10,16], [2,8], [1,6], [7,12]]
sort:
Input:
[[1,6], [2,8], [7,12], [10,16]]
end 6 6 12 12
count 1 1 +1=2 2
*/
//return
return count;
}
}
452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少的更多相关文章
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- [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 ...
- 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 ...
- [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 ...
- [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坐标就足够了.开始坐标总是小于结束坐标.平面 ...
随机推荐
- Java BigInterger类
BigInteger概述 可以让超过Integer范围内的数据进行运算 构造方法 public BigInteger(String val) 成员方法 public BigInteger add(Bi ...
- Itellij Idea全局搜索
Ctrl+N按名字搜索类 1 相当于eclipse的ctrl+shift+R,输入类名可以定位到这个类文件 2 就像idea在其它的搜索部分的表现一样,搜索类名也能对你所要搜索的内容多个部分进行匹 ...
- Linux之cd、pwd、mkdir、rmdir
cd.pwd.mkdir.rmdir 命令功能: 切换到指定的目录,可用绝对路径和相对路径 命令格式: cd directory 命令参数: 无 命令实例: 1.切换到/bin目录 vbird@Ubu ...
- 查看mysql的版本号
查看mysql的版本号 1.1 在命令行登录mysql,即可看到mysql的版本号 [root@heyong ~]# mysql -uroot -p Enter password: Welcome t ...
- 【C++】atof()
转自:https://blog.csdn.net/zhaoyl03/article/details/8176387 atof 是ascII to float的缩写,它将ascII字符串转换为相应的单精 ...
- jsp中forward与redirect
一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: request.getRequestDispatcher("new.jsp").forward(reques ...
- Windows不要使用记事本编辑文本文件
摘自:廖雪峰 千万不要使用Windows自带的记事本编辑任何文本文件.原因是Microsoft开发记事本的团队使用了一个非常弱智的行为来保存UTF-8编码的文件,他们自作聪明地在每个文件开头添加了0x ...
- 【剑指offer】求树中满足和为给定数字的路径
题目: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大的 ...
- 关于spring boot在IDE工具中可以启动成功,但是打成jar包以及运行jar包失败的问题
1. 运行jar包报错,如下图: 2. 首先,找到pom.xml,把下面的build块中的内容改成如下所示: 3. 然后,请千万不要用Intellij idea来打包项目为Jar,你应该来到项目的根目 ...
- ssh免密钥之上厕所
ssh服务简单介绍 SSH协议框架中最主要的部分是三个协议: *传输层协议(The Transport Layer Protocol)提供服务器认证,数据机密性,信息完整性等的支持; *用户认证协议( ...