Minimum Number of Arrows to Burst Balloons
Burst every balloon with the fewest arrows by always shooting at the end of the balloon that ends first.
The problem
Balloons are stuck to a wall, and each one covers a horizontal range points[i] = [xstart, xend]. An arrow shot straight up at position x bursts every balloon with xstart <= x <= xend and keeps flying. Return the minimum number of arrows needed to burst all the balloons.
Input points = [[1, 4], [3, 7], [6, 9], [8, 12]]
Output 2
An arrow at x = 4 bursts the first two balloons, and one at x = 9 bursts the last two.
Input points = [[0, 9], [1, 2], [7, 8]]
Output 2
Arrows at x = 2 and x = 8 are needed for the small balloons, and either one also bursts the wide balloon [0, 9].
Key constraint: Coordinates cover the whole 32-bit range, from -2^31 to 2^31 - 1, so a comparator that subtracts two values can overflow.
How to spot the pattern
- You need the fewest points that touch every interval.
- Overlapping intervals can share one point, so it is a greedy choice over intervals sorted by end.
The idea
Look at the balloon that ends first. Some arrow has to hit it, and shooting at its right end is the best place: it still bursts that balloon, and it also hits every other balloon that has already started by then. So sort by end, shoot at the first end, skip every balloon that starts at or before that position, and shoot the next arrow at the end of the first balloon that was missed.
Brute force: Try sets of arrow positions taken from the balloon ends, smallest sets first, until one bursts every balloon, which takes exponential time.
Step by step
- 1Sort the balloons by their end coordinate.
- 2Shoot the first arrow at the end of the first balloon:
arrows = 1andarrowX = points[0][1]. - 3For each next balloon, if its start is greater than
arrowX, the arrow misses it, so add an arrow and setarrowXto its end. - 4Otherwise the current arrow already bursts it, so move on.
- 5Return
arrows.
See the pattern move
The intervals pattern on a small example. Step through it, or press play.
Code
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
points.sort(key=lambda p: p[1]) # earliest end first
arrows = 1
arrow_x = points[0][1] # shoot at the end of the balloon that ends first
for start, end in points[1:]:
if start > arrow_x: # this balloon is missed: shoot a new arrow
arrows += 1
arrow_x = end
return arrowsComplexity
Time
O(n log n)
Space
O(1)
Sorting dominates the time; apart from the memory your language's sort uses, the sweep keeps one position and a counter.
Watch out for
- Sorting with a comparator like
a[1] - b[1]in Java or C++, which overflows for values near -2^31 and 2^31 - 1 and sorts wrongly; compare the values directly. - Using
>=instead of>for a miss: a balloon that starts exactly atarrowXis still burst. - Starting from a sentinel like
Integer.MIN_VALUEinstead of the first balloon: a balloon that starts at -2^31 then looks as if it was already burst.
Follow-up questions
How is this different from Non-overlapping Intervals?
It is the same greedy on intervals sorted by end, and the number of arrows equals the number of intervals that greedy keeps. The only change is that touching balloons can share an arrow, so a new arrow is needed only when start > arrowX.
What if you also have to report where to shoot?
Record arrowX every time you add an arrow; those balloon ends are valid positions.
In Hinglish
Jo balloon sabse pehle khatam hota hai, uske end par teer chalao; woh saath mein aur balloon bhi phod dega. Jo chhoot jaaye, agla teer uske end par.