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 ...
随机推荐
- 将apk安装包安装在Android真机或者模拟器
例子如下: 一.准备 打开MAC PC上的Android模拟器方法:打开eclipse-—>window->Android Virtual Device Manager 如果是安装在真机上 ...
- 《JavaScript DOM编程艺术》
第2章JS语法关联数组在为新元素给出下标时,不必局限于整数数字.数组下标可以是字符串逻辑与&&只有两个操作数都是true时结果才为true逻辑或||只有两个操作数都是false时结果才 ...
- 百度之星A
Scenic Popularity Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Gitlab仓库规范实践建议
记录一下Gitlab仓库实践信息: 仓库是指一个可以git clone的地址,用于存储某个服务,模块,子系统或某类辅助代码的地方 仓库的visibility level一般设置为Private(访问需 ...
- JavaScript 中的 this
JavaScript 语言中的 this 由于其运行期绑定的特性,JavaScript 中的 this 含义要丰富得多,它可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式.JavaSc ...
- 100个经典的C算法
1.题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔 子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数 为多少? #include<stdio.h&g ...
- Sublime Text 编辑器
1.从http://www.sublimetext.com/2 下载Sublime Text 2编辑器. 2.安装Package Control 管理器,用于管理和安装插件包. 下载地址:https: ...
- main函数和启动例程
为什么汇编程序的入口是_start,而C程序的入口是main函数呢?本节就来解释这个问题.在讲例 18.1 “最简单的汇编程序”时,我们的汇编和链接步骤是: $ as hello.s -o hello ...
- POJ 3185
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4088 Accepted: 1609 D ...
- NDK 编译可执行程序
以Hello Android工程为例. 建立好工程hello-a,在jni目录下创建文件hello-a.c,文件内容如下.(注意是jni目录,使用src目录编译会出错) #include <st ...