[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).
箭射气球,气球是一个区间,区间重叠的气球可以一起被射下来。问最少需要多少支箭。
【思路】
1、重载sort
Arrays.sort(points, (a, b) -> a[1] - b[1])等价于:(更快)
Arrays.sort(points, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
2、贪心
设[1,4] [2,5] [6,9] [7,11] [12,19]
当区间重叠时,可以用同一箭射下。
因此仅在 p[0] > tmp[1],即第i个点的left>边界tmp的right时,cnt++。
【代码】
class Solution {
public int findMinArrowShots(int[][] points) {
if(points.length==0)
return 0;
int cnt=1;
Arrays.sort(points, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
});
int pos = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] > pos) {
pos = points[i][1];
cnt++;
}
}
return cnt;
}
更容易理解:
class Solution {
public int findMinArrowShots(int[][] points) {
if(points.length==0)
return 0;
int cnt=1;
Arrays.sort(points, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
});
int pos = points[0][1];
for (int[] p : points) {
if (p[0] > pos) {
pos = p[1];
cnt++;
}
}
return cnt;
}
}
[Leetcode 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 ...
- [LeetCode] 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 ...
- [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 ...
- 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球
在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...
- [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
题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...
随机推荐
- 每天一套题打卡|河南省第十一届ACM/ICPC
A 计划日 题意:已知李明在YYYY年MM月DD日星期W订了学习计划,现在想看看李明N天后的完成情况和个人总结,你能告诉我那天的日期和星期几吗? 模拟日期计算: 计算星期可以用基姆拉尔森公式 //中国 ...
- 极其简单的VSCode C++环境配置
下载我打包的文件VSCode-cpp.7z.001 和 VSCode-cpp.7z.002,解压. 在系统环境变量中添加:你解压的路径\Project\.vscode\MinGW\bin. 打开你解压 ...
- SQL Server同一表不同列数据同步
直接上脚本 update table set a=b where a=xxx table==表名称 a==需要同步的列 b==数据源列,就是a列要的数据是b列的数据 where 条件.不加where则 ...
- 记一次VM虚拟机Ubuntu无法联网问题
突然ubuntu获取不到ipv4地址,手动设置静态ip也ping不通本机, 在网上试了一堆的方法也不行,就怀疑是vm设置问题了.因为 作业环境我的VM需要经常性的改变桥接的网卡,所以检查了一 下这里, ...
- 8_管理及IO重定向
五大类:运算器.控制器:CPU存储器:RAM输入设备/输出设备 程序:是由指令和数据组成的 控制器:读取指令运算器:存储器: 地址总线:内存寻址数据总线:传输数据控制总线:控制指令 寄存器:CPU暂时 ...
- java扫描文件。
前言:一步一步来实现迷你ioc框架,前面的容器工厂也是一个铺垫,这次的扫描文件也是一个铺垫…… 需求:扫描当前项目下所有文件.包括文件夹下文件夹里面的文件.利用递归进行扫描 ScanFileUtil类 ...
- Linux系统安装笔记
1.下载CentOS系统镜像: 很多资料都是CentOS6的,7的比较少,所以我决定还是用CentOS6来学习. 地址:http://vault.centos.org/6.8/isos/x86_64/ ...
- node使用ftp模块获取文件夹信息,中文乱码问题
c.on('ready', function () { c.list(function (err, list) { if (err) throw err list.map(item => { i ...
- time to set up Goals
Function Goal Training for new guys, meanings who takes part of QA jobs and make other QA members g ...
- Oracle 并发创建索引
建索引时,我们为了建索引快,会加上并行,加上并行之后,此列索引就会是并行了.访问有并行度的索引时,CBO可能可能会考虑并行执行,这可能会引发一些问题,如在服务器资源紧张的时候用并行会引起更加严重的争用 ...