Minimum Number of Arrows to Burst Balloons
Master this topic with zero to advance depth.
Minimum Number of Arrows to Burst Balloons
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 where points[i] = [x_start, x_end] denotes a balloon whose horizontal diameter stretches between x_start and x_end.
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with x_start and x_end is burst by an arrow shot at x if x_start <= x <= x_end. There is no limit to the number of arrows that can be shot.
Return the minimum number of arrows that must be shot to burst all balloons.
Visual Representation
Points: [[10,16], [2,8], [1,6], [7,12]]
Sort by end points:
[1, 6], [2, 8], [7, 12], [10, 16]
Arrow 1: Shoot at x=6. Bursts [1,6] and [2,8].
Arrow 2: Shoot at x=12. Bursts [7,12] and [10,16].
Total: 2 arrows.Examples
One arrow at 6 bursts [1,6] and [2,8]. Another arrow at 12 bursts [7,12] and [10,16].
Level I: Greedy Simulation (Brute Force)
Intuition
If we don't know the optimal sorting trick, a natural brute-force thought is: 'Let's find the coordinate that bursts the maximum number of balloons right now, shoot an arrow there, remove the burst balloons, and repeat until no balloons are left.' The candidate coordinates to shoot at are simply the start and end points of the remaining balloons.
Thought Process
- Maintain a list of remaining balloons.
- While the list is not empty, iterate through all start and end points of the remaining balloons.
- For each point, count how many balloons it intersects. Find the point that yields the maximum count.
- Increment
arrows, filter out the burst balloons, and repeat.
Detailed Dry Run
points = [[1,6],[2,8],[7,12]]
- Points: 1,6,2,8,7,12. Count 6: bursts [1,6],[2,8] (2).
- Max burst is 2 at x=6. arrows=1. Remaining: [[7,12]]
- Repeat for [[7,12]]: best point 12. arrows=2. Empty.
Level II: Greedy (Sort by Start & Find Overlap)
Intuition
If we sort by start point, we can maintain a 'current intersection' range. Every new balloon must overlap with this intersection. If it doesn't, we shoot an arrow and start a new intersection range.
Thought Process
- Sort by
start point. - Pick the first balloon as the initial intersection
[currentStart, currentEnd]. - For each subsequent balloon
[bStart, bEnd]:- If
bStart <= currentEnd(they overlap):- Narrow the intersection:
currentEnd = min(currentEnd, bEnd).
- Narrow the intersection:
- Else (no overlap with current intersection):
- Shoot an arrow!
- Update
currentEnd = bEndand incrementarrows.
- If
Pattern: Intersection Tracking
Detailed Dry Run
pointsSorted = [[1,6],[2,8],[7,12]]
- p[0]=[1,6]. currEnd=6, arrows=1.
- p[1]=[2,8]. 2 <= 6 (overlap). currEnd = min(6, 8) = 6.
- p[2]=[7,12]. 7 > 6 (no overlap). arrows=2, currEnd=12.
Level III: Optimal Greedy (Sort by End)
Intuition
To minimize arrows, we want each arrow to burst as many balloons as possible. By sorting by the end point, we fix the rightmost boundary for an arrow. If a balloon's start point is within the current arrow's range (i.e., start <= current_arrow_pos), it is burst. Otherwise, we need a new arrow at the new balloon's end point.
Why Sort by End?
Sorting by the end point ensures that the current balloon we are considering is the one that 'expires' soonest. Putting an arrow at its end point is the best greedy choice because it covers this balloon AND has the maximum chance of covering future balloons that start before this end point.
Pattern: Interval Overlap
Detailed Dry Run
points = [[10,16], [2,8], [1,6], [7,12]] After Sort: [[1,6], [2,8], [7,12], [10,16]]
| Step | Balloon | Range | lastArrowAt | arrows | Action |
|---|---|---|---|---|---|
| 1 | [1, 6] | [1, 6] | 6 | 1 | Shoot at 6 (end of first) |
| 2 | [2, 8] | [2, 8] | 6 | 1 | 2 <= 6? YES (Burst!) |
| 3 | [7, 12] | [7, 12] | 12 | 2 | 7 > 6? YES. Shoot at 12 (end) |
| 4 | [10, 16] | [10, 16] | 12 | 2 | 10 <= 12? YES (Burst!) |
Visualization:
(1)-------(6) <- Arrow 1 at 6
(2)-----------(8)
(7)-------(12) <- Arrow 2 at 12
(10)--------(16)Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.