leetcode_452. Minimum Number of Arrows to Burst Balloons 用最小的箭击破气球,python列表按元素排序

题目:

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

Subscribe to see which companies asked this question


题意:

气球放在水平轴上,给出气球在水平轴的坐标(xstart , xend),表示气球的起始位置和终止位置。先用箭来击破气球,当箭的位置x落在xstart ≤ x ≤ xend 的时候,就能击破气球。  先给出一堆气球的坐标,问至少需要多少箭能将所有的气球击破。


代码:

class Solution(object):
    def findMinArrowShots(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        
        n = len(points)         #统计气球个数
        
        if n < 1 :
            return 0
        else :
            points = sorted(points, key=lambda x:(x[0],x[1]))            #对气球坐标进行排序,先按气球起始位置从小到大排序,如果起始位置相同,则按终止位置从小到大排序
            
            strat = points[0][0]                #记录当前并集的起始位置
            end = points[0][1]               #记录当前并集的终止位置
            
            count = 1                         #记录并集个数
            
            for i in range(1,n) :              #从第二个气球开始遍历
                if points[i][0] <= end :             #因为points中每个气球的起始位置从小到大排列,故当前气球i与当前并集,继续求并集的时候,就比较简单,因为当前并集的起始位置肯定<=气球i的起始位置,于是只用比较气球i的起始位置与当前并集的终止位置end进行比较,如果气球i的起始位置<=当前并集的终止位置end,则两个集合有交集,于是更新当前的并集,起始位置为气球i的起始位置,终止位置为 并集的终止位置与气球i的终止位置 的较小者。
                    strat = points[i][0]
                    end = min(end,points[i][1])
                else :                       #否则,当前气球i与当前并集没有交集,需要产生一个新的并集,于是箭的数量就增加1。更新并集为气球i的坐标,继续循环。
                    count += 1
                    strat = points[i][0]
                    end = points[i][1]
            
            return count
                        
        
笔记:

其实这道题实际是求所有坐标区间中,一共有多少个并集。有多少个并集,就需要多少支箭。

对points进行排序是个关键,不然,直接遍历的话,判断并集的条件就很复杂,而且需要记录所有并集,下一次进来一个坐标,要跟所有的并集再求并集,就很复杂。但是,先对points排序后再遍历,就很简单了。


学习了python对list的内容进行排序的方法:

使用sorted函数进行排序,是新建立了一个list。

points = sorted(points, key=lambda x:(x[0],x[1]))   ,按照points中元素的第一个关键字排序,然后再按照第二个关键字排序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值