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 xend bursts 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).
class Solution(object):
def findMinArrowShots(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
points.sort(self.my_cmp)
i = 0
length = len(points)
ans = 0
while i<length:
end = points[i][1]
ans += 1
while i+1<length and points[i+1][0]<=end:
end = min(end, points[i+1][1])
i += 1
i += 1
return ans def my_cmp(self, x1, x2):
if x1[0] == x2[0]:
return cmp(x1[1], x2[1])
else:
return cmp(x1[0], x2[0])

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

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

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

  8. 【leetcode】452. Minimum Number of Arrows to Burst Balloons

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

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

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

随机推荐

  1. CUBRID学习笔记 3 net连接数据库并使用cubrid教程示例

    接上文 数据库安装好后,也可以测试语句了. 下面我们用c#写一个控制台程序,连接数据库,并读取数据. 一 下载驱动  net版的下 CUBRID ADO.NET Data Provider 9.3.0 ...

  2. hnoi 2016 省选总结

    感觉省选好难的说...反正我数据结构太垃圾正解想到了也打不出来打一打暴力就滚粗了! DAY1 0+20+30 DAY2 60-20+0+60 最后170-20分,暴力分还是没有拿全! 然而这次是给了5 ...

  3. 关于【bootstrap modal 模态框弹出瞬间消失的问题】

    前提是你没有重复引入bootstrap.js\bootstrap.min.js和modal.js.一下提供一个小例子. <button class="btnbtn-primary bt ...

  4. Http协议简单学习笔记

    HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程. 在HTTP1.0 ...

  5. 初识Oracle

    登录时无法登陆:   无监听程序 ———>启动服务OracleXETNListener   监听程序当前无法识别连接描述中的请求服务————>重启服务       OracleServic ...

  6. SAP接口编程 之 JCo3.0系列(03) : Table参数

    Table参数作为export parameter BAPI_COMPANYCODE_GETDETAIL是一个适合演示的函数,没有import paramter参数,调用后COMPANYCODE_GE ...

  7. boost线程的问题:

    可以看看这里:http://blog.csdn.net/misskissc/article/details/9859753 我的总结: 1,用thread类来创建一个线程,它的构结函数 : (1)th ...

  8. C++中的虚函数与纯虚函数

    这个吧,我也不怎么知道,所以,大家来看这两篇文章哦: http://blog.csdn.net/hackbuteer1/article/details/7558868 http://blog.csdn ...

  9. Traffic Sign Recognition with Multi-Scale Convolutional Networks

    总结一下文中几点值得学习的地方: 1,卷积神经网络的结构图:Multi-Scale Features.   因为它提取的特征的分层的,对吧,虽然最后 一层可以提供全局信息,但是呢,前面的几层可以提供更 ...

  10. JavaMail 发送邮件案例

    #----------------这两个是构建session必须的字段---------- #smtp服务器 mail.smtp.host=smtp.exmail.qq.com #身份验证 mail. ...