LeetCode #452 Medium

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.

Constraints
  • 1 <= points.length <= 10⁵
  • points[i].length == 2
  • -2³¹ <= xstart < xend <= 2³¹ - 1
intervalsgreedysorting
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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).

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findMinArrowShots(self, points):
▶3 points.sort(key=lambda p: p[1])
▶4 arrows = 0
▶5 arrow_x = float("-inf")
▶6 for start, end in points:
▶7 if start > arrow_x:
▶8 arrows += 1
▶9 arrow_x = end
▶10 return arrows
balloons as given10–162–81–67–12101628167124 balloons, no arrows yetarrows0
points[[10,16], [2,8], [1,6], [7,12]]
Each bar is one balloon's range. An arrow at position x bursts every bar that covers x, ends included.
sorted by right end1–62–87–1210–1616287121016sorted: earliest end on toparrows0
points[[1,6], [2,8], [7,12], [10,16]]by right end
Sorting by the right end puts on top the balloon whose last chance to be hit comes first. Some arrow must hit it, and its right end is the best place for that arrow.
sorted by right end1–62–87–1210–16arrow 116287121016first balloon → shoot at 6arrows1
arrows1
arrow_x6
No arrow exists yet, so this balloon needs one. Shooting at its right end, 6, bursts it and reaches as far right as possible, because every balloon below it ends at 6 or later.
sorted by right end1–62–87–1210–16arrow 116287121016starts at 2, arrow at 6 is insidearrows1
arrows1
arrow_x62 ≤ 6
This balloon starts at 2, not after the arrow at 6, and it ends at 8, not before it, since it comes later in end order. The arrow already passes through it, so no new arrow is needed.
sorted by right end1–62–87–1210–16arrow 1arrow 216287121016starts at 7, past 6 → shoot at 12arrows2
arrows2
arrow_x12
This balloon starts at 7, to the right of the last arrow at 6, so no arrow so far touches it. Shooting at its right end, 12, bursts it and reaches as far right as possible, because every balloon below it ends at 12 or later.
sorted by right end1–62–87–1210–16arrow 1arrow 216287121016starts at 10, arrow at 12 is insidearrows2
arrows2
arrow_x1210 ≤ 12
This balloon starts at 10, not after the arrow at 12, and it ends at 16, not before it, since it comes later in end order. The arrow already passes through it, so no new arrow is needed.
sorted by right end1–62–87–1210–16arrow 1arrow 216287121016every balloon burst → answer 2arrows2
result2
All balloons are burst with 2 arrows. Each arrow was fired only for a balloon that every earlier arrow had missed, so no smaller count can work.
05

Common pitfalls

Shooting again when balloons only touch

✗ Wrong
if start >= arrow_x:
✓ Right
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

✗ Wrong
points.sort()  # by xstart
# same loop: arrow_x = end of the first balloon in a group
✓ Right
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

✗ Wrong
Arrays.sort(points, (a, b) -> a[1] - b[1]);
✓ Right
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

✗ Wrong
arrow_x = 0
✓ Right
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.

06

Complexity

Time
O(n log n)
Space
O(log n)
Sorting dominates; the scan after it is a single pass. The only extra memory is what the sort itself uses.
07

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.