minimum-number-of-arrows-to-burst-balloons(还挺好)
https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
与会议室排期问题,很相似。
package com.company; import java.util.*; class Balloon {
int[] points;
boolean check;
Balloon(int s, int e) {
points = new int[2];
points[0] = s;
points[1] = e;
check = false;
}
} class MyComparator implements Comparator<Balloon> { int index;
MyComparator(int i) {
index = i;
} @Override
public int compare(Balloon o1, Balloon o2) {
return o1.points[index] - o2.points[index];
}
} class Solution {
public int findMinArrowShots(int[][] points) {
List<Balloon> endList = new ArrayList<>();
List<Balloon> startList = new ArrayList<>(); for (int i=0; i<points.length; i++) {
Balloon balloon = new Balloon(points[i][0], points[i][1]);
endList.add(balloon);
startList.add(balloon);
}
Collections.sort(endList, new MyComparator(1));
Collections.sort(startList, new MyComparator(0)); int index = 0;
int ret = 0;
Iterator<Balloon> iter = endList.iterator();
while (iter.hasNext()) {
Balloon tmp = iter.next();
if (tmp.check) {
continue;
}
ret++;
while (index < points.length && startList.get(index).points[0] <= tmp.points[1]) {
startList.get(index).check = true;
index++;
}
}
return ret;
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
int[][] points = {{10,16}, {2,8}, {1,6}, {7,12}};
int ret = solution.findMinArrowShots(points);
System.out.printf("ret:%d\n", ret); System.out.println(); } }
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] 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 ...
- 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, provided ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- 全面认识JVM技术
本文向大家描述一下JVM的概念,JVM(Java虚拟机)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该系统上运行. J ...
- geotools解析SLD中的elsefilter为什么里面的filter无效
原因是在org.geotools.renderer.lite.StreamingRenderer中的process函数: /** * @param rf * @param feature * @par ...
- display:inline-block 在IE6中实现{转}
IE6/IE7下对display:inline-block的支持性不好. 1.inline元素的display属性设置为inline-block时,所有的浏览器都支持: 2.block元素的displ ...
- Win7无法使用VPN的原因与解决方法(一)
如果Windows 7不是通过正常安装途径的话,像Ghost错误.系统环境改变等,都有可能导致无法使用VPN,而且由于原因不同,给出的提示也不尽相同.实际上,万变不离其宗, VPN是要依靠Window ...
- 大并发连接的oracle在Linux下内存不足的问题的分析
大并发连接的oracle在Linux下内存不足的问题的分析 2010-01-28 20:06:21 分类: Oracle 最近一台装有Rhel5.3的40G内存的机器上有一个oracle数据库,数据库 ...
- 用VBS将PPT转为图片
'使用方法:把ppt文件拖放到该文件上. '机器上要安装Powerpoint程序 On Error Resume Next Set ArgObj = WScript.Arguments pptfile ...
- 导出含有图片的项目成jar文件后运行,图片不显示
在编写完Java程序后,打包成Jar时发布,会发现找不到Jar文件中的图片和文本文件,其原因是程序中载入图片或文本文件时,使用了以当前工作路径为基准的方式来指定文件和路径.这与用户运行Jar包时的当前 ...
- HDU2295 Radar (DLX)
下面的代码99%参考了这个网站http://www.cnblogs.com/183zyz/archive/2011/08/07/2130193.html 人生的第一道DLX肯定是需要作一些参考的啦. ...
- 10个免费开源的JS音乐播放器插件
点这里 音乐播放器在网页设计中有时候会用到,比如一些时尚类.音乐或影视类等项目,但这些 网页播放器 插件比较少见,所以这里为大家整理一个集合,也许会有用到的时候. 下面整理的播放器有些是支持自适应的, ...
- 二叉查找树的查找、插入和删除 - Java实现
http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html 作者: yangecnu(yangecnu's Blog on ...