用最少数量的箭引爆气球

在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了。开始坐标总是小于结束坐标。平面内最多存在104个气球。

一支弓箭可以沿着x轴从不同点完全垂直地射出。在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足  xstart ≤ x ≤ xend,则该气球会被引爆可以射出的弓箭的数量没有限制。 弓箭一旦被射出之后,可以无限地前进。我们想找到使得所有气球全部被引爆,所需的弓箭的最小数量。

Example:

输入:

[[10,16], [2,8], [1,6], [7,12]]

输出:

2

解释:

对于该样例,我们可以在x = 6(射爆[2,8],[1,6]两个气球)和 x = 11(射爆另外两个气球)。

思路

 class Solution {
public int findMinArrowShots(int[][] points) {
if(points.length==0)
return 0;
Ballon[] ballons = new Ballon[points.length];
for(int i=0;i<points.length;i++)
ballons[i]=new Ballon(points[i][0],points[i][1]); Arrays.sort(ballons, new Comparator<Ballon>() {
@Override
public int compare(Ballon o1, Ballon o2) {
return o1.end-o2.end;
}
});
int ans = 1;
int right = ballons[0].end;
for(Ballon ballon:ballons)
{
if(ballon.start>right)
{
right=ballon.end;
ans++;
}
}
return ans; }
} class Ballon{
int start;
int end; public Ballon(int start, int end) {
this.start = start;
this.end = end;
}
}

Leetcode 452.用最少数量的箭引爆气球的更多相关文章

  1. Java实现 LeetCode 452 用最少数量的箭引爆气球

    452. 用最少数量的箭引爆气球 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够 ...

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

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

  3. 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球

    在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...

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

  5. LeetCode:用最少的箭引爆气球【452】

    LeetCode:用最少的箭引爆气球[452] 题目描述 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道 ...

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

  7. Leetcode 200.岛屿的数量 - DFS、BFS

    Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7M ...

  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, provided ...

  9. leetcode 452用少量的箭射爆气球

    类似于区间调度问题,使用贪心算法:首先对所有气球按照起始坐标大小排序,然后每次总是优先选择起始坐标小的气球中的右边坐标,然后再选择下一个: 排完序之后,下一个可能有如上图所示几种情况, 1)   当n ...

随机推荐

  1. Oracle创建用户、表(1)

    Oracle创建用户.表(1) 1. 连接 C:\Users\LEI>sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on ...

  2. elasticsearch 2.4 windows版jvm内存设置

    本文编写目的是因为网上有很多es修改内存配置的文章,方法也各有不同,但在我的情况下(es 2.4 windows版)发现很多方法都是无效的,有效只有以下方法 第一个是xms,第二个是xmx

  3. MFC CDialog/CDialogEx DoModal ALT

    Questions: I'm using MFC CDialog/CDialogEx to show a modal dialog with DoModal.usually it works with ...

  4. zfs和ufs文件系统

    系统环境:freebsd 在zfs文件系统上的文件拷贝到ufs文件系统之后,大小翻了一倍.初步断定是文件系统导致的.不知道以后还会不会有新发现.come on!

  5. 一键部署基于GitLab的自托管Git项目仓库

    https://market.azure.cn/Vhd/Show?vhdId=9851&version=11921 产品详情 产品介绍GitLab https://about.gitlab.c ...

  6. ansys-表格

    转自http://blog.sina.com.cn/s/blog_833dee820102xwb3.html ANSYS中表格数组的定义及使用举例 ANSYS中会有许多的参数数据,这些参数的形成后要放 ...

  7. Robot Framework(一)入门

    1.1简介 Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架,用于端到端验收测试和验收测试驱动开发(ATDD).它可用于测试分布式异构应用程序,其中验证需要涉 ...

  8. springboot autoconfig

    springboot自动配置的核心思想是:springboot通过spring.factories能把main方法所在类路径以外的bean自动加载 springboot starter验证 我在spr ...

  9. Java替换手机号掩码

    String tel = "18304072984"; // 括号表示组,被替换的部分$n表示第n组的内容 tel = tel.replaceAll("(\\d{3})\ ...

  10. Windows平台下MySQL常用操作与命令

    Windows平台下MySQL常用操作与命令 Windows平台下MySQL常用操作与命令,学习mysql的朋友可以参考下. 1.导出整个数据库 mysqldump -u 用户名 -p --defau ...