Minimum Arrows to Burst Balloons
Problem
Given a set of balloons (start, end), find number of arrows needed to burst. An arrow bursts all the balloons in its vertical path.Solution
- Greedy.
- Trick is to sort them by 'end'.
- Keep a current end that for the current row.
- Every time, a balloon starts after the current end Increase the arrow count.
# Greedy
def findMinArrowShots(self, points: List[List[int]]) -> int:
if not points:
return 0
points.sort(key = lambda x: x[1])
cur_end = points[0][1]
arrow = 1
for start, end in points:
if start > cur_end:
arrow += 1
cur_end = end
return arrow
Comments
Post a Comment