There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xendbursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.

Example:

Input:
[[10,16], [2,8], [1,6], [7,12]] Output:
2 Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).

给一堆气球,用区间[start,end]来表示气球大小,会有重叠区间。箭从某一个位置发射,只要区间包含这个点的就可以被射中。求用最少的箭数将所有的气球打爆。

解法:贪婪算法Greedy,先给区间排序,遍历区间,第一个区间时,先加1箭,记录区间的end,然后比较后面的区间的start,如果start <= end,说明两个区间有重合,箭从重合区间发射就可以同时打爆这两个气球。重新记录这两个区间里end小的值,在和后面的气球比较。如果start > end,说明没有重合区间,得在发射1箭,箭数加1。遍历结束就能得到所需的最少箭数。

Java:

public class Solution {
public int findMinArrowShots(int[][] points) {
if(points==null || points.length==0) return 0; Arrays.sort(points, ( x , y) -> x[0] == y[0] ? x[1] - y[1] : x[0] - y[0]);
int count = 1;
int arrowLimit = points[0][1];
//贪心法,基于上一个箭,记录当前能够射穿的所有
for(int i = 1;i < points.length;i++) {
if(points[i][0] <= arrowLimit) {
arrowLimit = Math.min(arrowLimit, points[i][1]);
} else {
count++;
arrowLimit = points[i][1];
}
}
return count;
}
}

Python:

class Solution(object):
def findMinArrowShots(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
if not points:
return 0 points.sort() result = 0
i = 0
while i < len(points):
j = i + 1
right_bound = points[i][1]
while j < len(points) and points[j][0] <= right_bound:
right_bound = min(right_bound, points[j][1])
j += 1
result += 1
i = j
return result

C++:

class Solution {
public:
int findMinArrowShots(vector<pair<int, int>>& points) {
if (points.empty()) {
return 0;
} sort(points.begin(), points.end()); int result = 0;
for (int i = 0; i < points.size(); ++i) {
int j = i + 1;
int right_bound = points[i].second;
while (j < points.size() && points[j].first <= right_bound) {
right_bound = min(right_bound, points[j].second);
++j;
}
++result;
i = j - 1;
}
return result;
}
};

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 452. Minimum Number of Arrows to Burst Balloons 最少箭数爆气球的更多相关文章

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

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

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

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

  5. 452. 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. 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

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

  8. [LC] 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. Minimum Number of Arrows to Burst Balloons

    题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...

随机推荐

  1. JVM垃圾回收算法分析与演示【纯理论】

    继续接着上一次[https://www.cnblogs.com/webor2006/p/10729649.html]的来学习,上次在结尾处提到了JVM常见的GC算法,如下: 接下来则逐一的对其进行学习 ...

  2. 微信小程序之执行环境

    明白了小程序中的 JavaScript 同浏览器以及NodeJS有所不同后,开发者还需要注意到另外一个问题,不同的平台的小程序的脚本执行环境也是有所区别的. 小程序目前可以运行在三大平台: iOS平台 ...

  3. LGOJP3959 宝藏

    题目链接 题目链接 题解 一开始想了一个错误的状压dp,水了40分. 这里先记录一下错误的做法: 错解: 设\(g[i,j,S]\)从\(i\)到\(j\),只经过集合\(S\)中的点的最短路,这个可 ...

  4. postgres高可用学习篇二:通过pgbouncer连接池工具来管理postgres连接

    安装pgbouncer yum install libevent -y yum install libevent-devel -y wget http://www.pgbouncer.org/down ...

  5. webpack的plugin原理

    plugin是webpack生态的重要组成,它为用户提供了一种可以直接访问到webpack编译过程的方式.它可以访问到编译过程触发的所有关键事件. 1. 基本概念 1. 如何实现一个插件 1. plu ...

  6. Kali Linux 2019.4中文乱码解决

    1.先换源deb http://mirrors.aliyun.com/kali kali-rolling main non-free contribdeb-src http://mirrors.ali ...

  7. LOJ P10018 数的划分 题解

    每日一题 day52 打卡 Analysis 这道题直接搜索会TLE到**,但我们发现有很多没有用的状态可以删去,比如 1,1,5; 1,5,1; 5,1,1; 所以很容易想到一个优化:按不下降的顺序 ...

  8. HBase 基本入门篇

    无论是 NoSQL,还是大数据领域,HBase 都是非常”炙热”的一门数据库.本文将对 HBase 做一些基础性的介绍,旨在入门. 一.简介 HBase 是一个开源的.面向列的非关系型分布式数据库,目 ...

  9. S1_搭建分布式OpenStack集群_09 cinder 控制节点配置

    一.创建数据库创建数据库以及用户:# mysql -uroot -p12345678MariaDB [(none)]> CREATE DATABASE cinder;MariaDB [(none ...

  10. 了解Python-白 驹 过 隙 , 忽 然 而 已

    白 驹 过 隙 , 忽 然 而 已 人 生 苦 短,我 用 Python -- Life is short , you need Python 代码量少,同一样问题 ,用不同的语言解决时,一般情况下P ...