452. 用最少数量的箭引爆气球

https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/

解法一:贪心

先将各气球以终点排序,然后从第0个气球开始,设end为其终点,初始箭数为1,即该气球肯定要用一支箭,然后遍历后面的气球i,若i的起点大于end,则需要增加箭数,同时end更新为气球i的终点。

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        if len(points) == 0:
            return 0
        points.sort(key=lambda x: x[1])
        end = points[0][1]
        res = 1
        for p in points[1:]:
            if p[0] > end:
                res += 1
                end = p[1]
        return res

最后更新于