在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以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(射爆另外两个气球)。
详见:https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/

C++:

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

参考:https://www.cnblogs.com/grandyang/p/6050562.html

452 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] 452 Minimum Number of Arrows to Burst Balloons

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

  4. 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. 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, pr ...

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

  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. QtQuick桌面应用开发指导 1)关于教程 2)原型和设计 3)实现UI和功能_A

    Release1.0 http://qt-project.org/wiki/developer-guides Qt Quick Application Developer Guide for Desk ...

  2. C++对象模型——解构语意学(第五章)

    5.4    对象的效率 (Object Efficiency) 在下面的效率測试中,对象构造和拷贝所须要的成本是以Point3d class声明为基准,从简单形式逐渐到复杂形式,包含Plain Ol ...

  3. HDU 1824 Let&#39;s go home (2-SAT判定)

    Let's go home Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. 亲測Mysql表结构为InnoDB类型从ibd文件恢复数据

    客户的机器系统异常关机,重新启动后mysql数据库不能正常启动,重装系统后发现数据库文件损坏,悲催的是客户数据库没有进行及时备份,仅仅能想办法从数据库文件其中恢复,查找资料,试验各种方法,确认以下步骤 ...

  5. <input type=XXXXX>

    选框,提交/重置按钮等,下面一一介绍. 1,type=text  输入类型是text,这是我们见的最多也是使用最多的,比如登陆输入用户名,注册输入电话号码,电子邮件,家庭住址等等.当然这也是Input ...

  6. order by 特殊排序技巧

    if object_id('tempdb..#temp') is not null drop table #temp ), col2 )) insert into #temp ' ' ' ' go - ...

  7. 搭建基于Maven的SSM框架

    先展示文件结构图对工程结构有大致了解: 主要为  ssm-parent (用来管理jar包版本)是每个工程的父工程,ssm-common(用来处理底层数据),ssm-manager(对数据库信息进行操 ...

  8. IntelliJ Idea 工具

     IntelliJ Idea 优化: 1.如何取消文件自动保存 File->Settings -> Appearance&Behavior -> System Setting ...

  9. bzoj3612: [Heoi2014]平衡

    首先不可重的整数规划是fi,j=fi-1,j-i+fi,j-i的 然后现在加了一个限制,分成的数不能超过n,那么对于拼大于n的数的时候多减一个fi-1,j-n-1 接下来是优化代码暴露我自带巨大常数的 ...

  10. SecureCRT连接(内网和外网)虚拟机中的Linux系统(Ubuntu)

    最近在学习Linux,看了网上很多SecureCRT连接本地虚拟机当中的Linux系统,很多都是需要设置Linux的配置文件,有点繁琐,所以自己就摸索了一下,把相关操作贴出来分享一下. SecureC ...