There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array
points
wherepoints[i] = [xstart, xend]
denotes a balloon whose horizontal diameter stretches betweenxstart
andxend
. You do not know the exact y-coordinates of the balloons.Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with
xstart
andxend
is burst by an arrow shot atx
ifxstart <= x <= xend
. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.Given the array
points
, return the minimum number of arrows that must be shot to burst all balloons.
class Solution:
def findMinArrowShots(self, points):
# 按每个气球的 end 排序
p = sorted(points, key=lambda x: x[1])
res = 0
arrow = p[0][0] - 1
# 比较每个气球的 start 和 arrow
for each in p:
# 需要多一支箭,有效范围到 end
if each[0] > arrow:
res += 1
arrow = each[1]
return res