Minimum Number of Arrows to Burst Balloons
Minimum Number of Arrows to Burst Balloons is LeetCode 452 (Medium). Each balloon covers a horizontal range [xstart, xend] on a wall. An arrow shot straight up at position x bursts every balloon with xstart <= x <= xend, ends included, and keeps flying.
Return the fewest arrows needed to burst every balloon.
There can be 100,000 balloons, and the coordinates use the whole 32-bit integer range.
- 1 <= points.length <= 10⁵
- points[i].length == 2
- -2³¹ <= xstart < xend <= 2³¹ - 1
Intuition
Look at the balloon that ends first. Some arrow has to hit it, somewhere at or before its end. Sliding that arrow right to exactly its end loses nothing: every other balloon ends at the same point or later, so any balloon the arrow touched before still contains the new position, and the arrow may now reach more.
So sort by end, shoot at the first end, and pass over every balloon that starts at or before that arrow. The next balloon that starts after the arrow becomes the new balloon that ends first, and the same argument repeats. Every arrow is forced, so the count cannot be lowered.
Points or arrows that each "cover" every interval containing them, and a request for the fewest of them: that is interval stabbing, solved greedily by sorting on the right end. Non-overlapping Intervals (LeetCode 435) and Meeting Rooms use the same end-sorted scan.
Approach
Before reading on, take the balloon that ends first and decide where its arrow should go so that it bursts as many other balloons as possible. Then decide which balloon the next arrow is for. Aim for O(n log n).
Sort the balloons by their right end
After sorting by xend, the next balloon that is still whole is always the one whose last chance to be hit comes soonest, which is the balloon the greedy argument is about.
Begin with no arrow
Set the arrow position to minus infinity and the count to 0. The first balloon starts after minus infinity, so it always gets the first arrow.
Pass over balloons the arrow already bursts
If a balloon has xstart <= arrow, it also ends at or after the arrow, because it comes later in end order. The arrow is inside its range, so the balloon is already burst and nothing changes.
Shoot a new arrow when a balloon starts past it
If xstart > arrow, no arrow shot so far reaches this balloon. Add 1 to the count and move the arrow to this balloon's xend. After the last balloon, return the count.
Code in Python, C++ and Java
Common pitfalls
Shooting again when balloons only touch
if start >= arrow_x:
if start > arrow_x:
Balloons that share only an endpoint can still be burst together, because the ends are included. [[1,2],[2,3],[3,4],[4,5]] needs 2 arrows, at 2 and 4, but the >= test fires at every balloon and returns 4.
Sorting by the left end without shrinking the arrow
points.sort() # by xstart # same loop: arrow_x = end of the first balloon in a group
points.sort(key=lambda p: p[1])
In start order, a wide balloon can come first and push the arrow far to the right. On [[1,10],[2,3],[4,5]] the arrow lands at 10, both later balloons start before 10 and are counted as burst, and the result is 1. The arrow at 10 misses both of them; the answer is 2.
Comparing by subtraction in Java
Arrays.sort(points, (a, b) -> a[1] - b[1]);
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
Ends can be as far apart as -2³¹ and 2³¹ - 1, so a[1] - b[1] overflows and flips its sign. On [[-2147483646,-2147483645],[2147483646,2147483647]] the order comes out reversed, the far-left balloon looks already burst, and the count is 1 instead of 2.
Starting the arrow at 0
arrow_x = 0
arrow_x = float("-inf")Coordinates can be negative. On [[-5,-3]] the balloon does not start after 0, so no arrow is ever shot and the result is 0 instead of 1.
Complexity
Minimum Number of Arrows to Burst Balloons FAQ
Can I sort by the start instead?
Yes, if the arrow position shrinks: while balloons overlap the current group, set the arrow to min(arrow, xend), and start a new arrow only when a balloon starts after it. Sorting by the end makes that min unnecessary.
How is this related to Non-overlapping Intervals?
LeetCode 435 asks how many intervals to remove so that none overlap, and it uses the same end-sorted scan. There, intervals that only touch do not overlap, so the test is start >= end, and the answer is n minus the number of intervals kept.