Problem
Balloons are closed intervals on a number line. An arrow shot at point x bursts every balloon whose interval contains x. Return the fewest arrows needed to burst every balloon.
Original: LeetCode 452: Minimum Number of Arrows to Burst Balloons.
Examples
Example 1
`points = [[10, 16], [2, 8], [1, 6], [7, 12]]``2`Example 2
`points = [[1, 2], [3, 4], [5, 6], [7, 8]]``4`Example 3
`points = [[1, 2], [2, 3], [3, 4], [4, 5]]``2`Constraints
1 <= points.length <= 10^5points[i].length == 2-2^31 <= xstart < xend <= 2^31 - 1
Hints
Hint 1
Touching endpoints count as a hit, because the intervals are closed. One arrow can burst many overlapping balloons.
Hint 2
This is the same greedy as Non-overlapping Intervals, counting clusters that share a common point.
Solution
Tried it yourself? The approach, complexity, and code stay hidden until you choose.
Approach
Brute force. Try every candidate shot, which is exponential or at least quadratic if you are not careful.
Optimal. Sort by end, shoot at the current end, and skip every balloon that starts at or before that shot.
Alternative. Sorting by start and tracking the tightest remaining end also works if you shrink the end as you go.
Complexity
- Time
- O(n log n)
O(n log n) time, O(1) extra space besides the sort. Compare ends with Integer.compare in Java so values near the 32-bit range do not overflow a subtracted comparator.
- Space
- O(1)
Code
def find_min_arrow_shots(points: list[list[int]]) -> int:
points = sorted(points, key=lambda p: p[1])
arrows, end = 1, points[0][1]
for start, finish in points[1:]:
if start > end:
arrows += 1
end = finish
return arrows
if __name__ == "__main__":
assert find_min_arrow_shots([[10, 16], [2, 8], [1, 6], [7, 12]]) == 2
assert find_min_arrow_shots([[1, 2], [3, 4], [5, 6], [7, 8]]) == 4
assert find_min_arrow_shots([[1, 2], [2, 3], [3, 4], [4, 5]]) == 2
print("ok")import java.util.Arrays;
public class MinimumNumberOfArrowsToBurstBalloons {
static int findMinArrowShots(int[][] points) {
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
int arrows = 1, end = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] > end) {
arrows++;
end = points[i][1];
}
}
return arrows;
}
public static void main(String[] args) {
if (findMinArrowShots(new int[][] {{10, 16}, {2, 8}, {1, 6}, {7, 12}}) != 2) throw new AssertionError();
if (findMinArrowShots(new int[][] {{1, 2}, {3, 4}, {5, 6}, {7, 8}}) != 4) throw new AssertionError();
if (findMinArrowShots(new int[][] {{1, 2}, {2, 3}, {3, 4}, {4, 5}}) != 2) throw new AssertionError();
System.out.println("ok");
}
}Edge cases
- Official samples: [[10,16],[2,8],[1,6],[7,12]] needs 2 arrows; [[1,2],[3,4],[5,6],[7,8]] needs 4; [[1,2],[2,3],[3,4],[4,5]] needs 2, because a shared endpoint is still a hit.
- The overlap test is start > end, not start >= end, because the intervals are closed.
Follow-ups
- Non-overlapping Intervals uses start >= end to decide that two intervals are already compatible, which is the other side of the same comparison.
Interview script
"I sort by end and shoot at the current end, because that arrow still hits the balloon I just considered and as many later balloons as possible. I only spend a new arrow when a balloon starts after that shot."
Source: LeetCode 452: Minimum Number of Arrows to Burst Balloons Source: Java SE 17 API: Integer.compare)
Sources (2)
- LeetCode 452: Minimum Number of Arrows to Burst Balloonsleetcode.com
- Java SE 17 API: Integer.comparedocs.oracle.com