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 ...
随机推荐
- 监听HTML input输入框值的即时变化onpropertychange、oninput兼容IE,Chrome,FF,Opera等
转自:http://blog.csdn.net/itchiang/article/details/7769337 要达到的效果 很多情况下我们都会即时监听输入框值的变化,以便作出即时动作去引导浏览者增 ...
- CSS兼容问题大全
1.chorme 最小字体的兼容性. 问题描述:ff和IE最小字体可设置为1px,可是chorme中文版最小字体是12px,小于12px的字体全部显示为12px.解决方案:chorme支持CSS3的, ...
- android 关于Location of the Android SDK has not been setup in the preferences的解决方法
今天在部署android开发环境的时候,每次打开eclipse的时候点击AVD Manager的按钮就会弹出Location of the Android SDK has not been setup ...
- ErrorCode枚举类型返回错误码信息测试,手动抛出异常信息,在事务中根据错误码来回滚事务的思路。
ErrorCode.java 简单测试代码,具体应用思路:手动抛出异常信息,在事务中根据错误码来回滚事务的思路. public enum ErrorCode { //系统级 SUCCESS(" ...
- *args和**kw魔法参数
学Python挺久了,现在才搞懂这个还是有点惭愧 *args:传入元组,无关键字 **kw:传入字典,有关键字 示例: *args **kw 一起使用时args的参数需在前:
- Pytho中两种方式导入模块的差别
1.使用import module,只是把模块导入,访问模块中的函数名或者是属性是必须使用点运算符(.)来访问,否则直接访问会提示找不到这些函数或者属性. 2.使用from numpy import ...
- ASP.NET Excel 导入 Oracle 方法2
先谈思路:前半部分和之前那篇日志的内容是一样的,把Excel数据导入到DataSet中,不同之处在于数据插入的方式: 本方法是拼接 INSERT INTO TABLE VALUES() 字符串,对,就 ...
- uva 10817
Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spring Field School is cons ...
- POJ 2186
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22189 Accepted: 9076 Des ...
- E文阅读
Lesson 9 A cold welcome 冷遇 What does 'a cold welcome' refer to?On Wednesday evening, we went to the ...