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(还挺好)的更多相关文章

  1. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  2. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  3. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  4. [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 ...

  5. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  7. [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 ...

  8. [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 ...

  9. 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  10. 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

    [抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...

随机推荐

  1. [工作积累] error: bad class file magic (cafebabe) or version (0033.0000)

    Update Android SDK build tool to latest can solve my problem.

  2. PE文件结构深入详解

    一.PE结构基础 看了很多PE结构类的东东,要不上来就是整体结构,要不就是一大堆ASM代码,看的我等菜鸟有点难受!所以自己写个帖·学习PE我们先来弄懂几个问题! 1:几个地址的概念 VA:虚拟地址,也 ...

  3. 我收集到的最好的jQuery和CSS3导航菜单

    jQuery和CSS3导航菜单在网页设计和开发的重要组成部分之一.利用jQuery+CSS3实现可以做出拥有各种动画效果的漂亮菜单.在这里,我们收集了一些最好的jQuery+CSS3实现的导航菜单. ...

  4. OpenLayers3 online build

    openlayers3使用了一个比较复杂的build工具,从github上下载下来的代码中并没有build之后的版本,要配置build环境又比较繁琐,好在官方的example中提供了在线的版本,下面就 ...

  5. 金融证券协议FIX/FAST/STEP

    金融信息交换协议FIX是适用于实时证券.金融电子交易的数据通信标准.它是把各类证券金融业务需求流程格式化,使之成为一个可用计 算机语言描述的功能流程,并在每个业务功能接口上统一交换格式.STEP(Se ...

  6. 如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用?(3)

    [编者按]本篇文章作者是 Reinder de Vries,既是一名企业家,也是优秀的程序员,发表多篇应用程序的博客.本篇文章中,作者主要介绍了如何基于 Parse 特点,打造一款类似 Instagr ...

  7. light oj 1140 - How Many Zeroes? 数位DP

    思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...

  8. Tomcat6启用Gzip压缩功能

    配置Tomcat根目录下/conf/server.xml文件: <Connector port="8080" protocol="HTTP/1.1" co ...

  9. 华为OJ:字符串加解密

    题目描述 1.对输入的字符串进行加解密,并输出. 2加密方法为: 当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B:字母Z时则替换为a: 当内容是数字时则把该 ...

  10. VS 解决方案目录结构设置

    涉及到的配置: 项目编译时使用的中间目录: 项目链接的输出目录: 项目编译结束后,拷贝生成的 dll 和 lib 到指定目录: 项目编译结束后,拷贝需要的 dll 到输出目录: 设置项目的工作目录: ...