Minimum Number of Arrows to Burst Balloons (LeetCode 452): Solution and Explanation
Each balloon covers a horizontal interval, and a vertical arrow shot at position x bursts every balloon whose interval contains x, including at the endpoints. Find the minimum number of arrows needed to burst all of them.
Watch it run step by step
Try your own input and write the code →Intuition
Restated, you want the smallest set of points such that every interval contains at least one point. Trying every subset of candidate positions is exponential, so you look for a greedy rule that is provably safe.
Consider the balloon that ends first. Some arrow must hit it, so some arrow has to be at or before its end. Shooting exactly at its end is the best possible choice for that arrow: any other position that hits this balloon is further left, and moving the arrow right up to that end can only hit more of the remaining balloons, never fewer. So the first arrow goes at the smallest end.
After that, every balloon that starts at or before that position also contains it, because its end is at least as large (the list is sorted by end). Those are all burst. The first balloon that starts strictly after the arrow is the next one needing a new arrow, and by the same argument its end is the best place to shoot. Sorting by end and sweeping once gives the answer. This is the same greedy idea as interval scheduling, where choosing the interval that finishes earliest leaves the most room for the rest.
It is worth seeing why sorting by start fails if you are careless. With balloons [1,10] and [2,3], the first sorted by start is [1,10], and shooting at its end, 10, misses [2,3] entirely. Sorting by end puts [2,3] first, the arrow lands at 3, and that single arrow also bursts [1,10]. Choosing the earliest end is what makes each arrow as useful as possible.
Approach: Greedy sort by end point
- Return 0 if there are no balloons.
- Sort the balloons by their end coordinate.
- Shoot the first arrow at the end of the first balloon:
arrows = 1andend = points[0][1]. - For each remaining balloon, if its start is greater than
end, the current arrow misses it. - In that case, add an arrow and move
endto this balloon's end. - Otherwise the balloon is already burst; after the loop, return
arrows.
Step-by-step example
points = [[10,16],[2,8],[1,6],[7,12]]
- Sorting by end gives [1,6], [2,8], [7,12], [10,16].
- The first arrow goes at x = 6, bursting [1,6].
- [2,8] starts at 2, which is not greater than 6, so the arrow at 6 already bursts it.
- [7,12] starts at 7, which is greater than 6, so a second arrow is shot at 12.
- [10,16] starts at 10, which is not greater than 12, so it is burst too. The answer is 2.
Code: Python, Java, C++
1class Solution:
2 def findMinArrowShots(self, points: List[List[int]]) -> int:
3 if not points:
4 return 0
5
6 points.sort(key=lambda x: x[1])
7 arrows = 1
8 end = points[0][1]
9
10 for i in range(1, len(points)):
11 if points[i][0] > end:
12 arrows += 1
13 end = points[i][1]
14
15 return arrows1class Solution {
2 public int findMinArrowShots(int[][] points) {
3 if (points.length == 0) return 0;
4
5 Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
6 int arrows = 1;
7 int end = points[0][1];
8
9 for (int i = 1; i < points.length; i++) {
10 if (points[i][0] > end) {
11 arrows++;
12 end = points[i][1];
13 }
14 }
15
16 return arrows;
17 }
18}1class Solution {
2public:
3 int findMinArrowShots(vector<vector<int>>& points) {
4 if (points.empty()) return 0;
5
6 sort(points.begin(), points.end(), [](auto& a, auto& b) {
7 return a[1] < b[1];
8 });
9 int arrows = 1;
10 int end = points[0][1];
11
12 for (int i = 1; i < points.size(); i++) {
13 if (points[i][0] > end) {
14 arrows++;
15 end = points[i][1];
16 }
17 }
18
19 return arrows;
20 }
21};Time and space complexity
O(n log n)
Sorting the balloons dominates, and the sweep afterward is linear.
O(1)
Only a counter and the current arrow position are stored beyond the in-place sort (which itself may use O(n) auxiliary space in Python).
Other approaches
Sort by start and shrink the overlap (time O(n log n), space O(1))
Sort by start, keep the current group's smallest end, and start a new arrow when a balloon starts after that end; otherwise shrink the end to the minimum. Equivalent result with slightly more bookkeeping.
Count overlaps via non-overlapping intervals (time O(n log n), space O(1))
The answer equals the size of the largest set of pairwise disjoint balloons, which you can compute with the same end-sorted sweep. It is a useful way to connect this problem to interval scheduling.
Common mistakes
- Using
>=instead of>when comparing the start to the arrow position; touching endpoints count as burst, so a balloon starting exactly atendneeds no new arrow. - Sorting by start and keeping the first balloon's end without shrinking it, which can place an arrow that misses a shorter nested balloon.
- Sorting with a comparator that subtracts values, like
a[1] - b[1]in Java, overflows for coordinates near the 32-bit limits. - Forgetting the empty input case, where
points[0]would fail.
Frequently asked questions
Why sort by end point instead of start point?
The balloon that ends first forces an arrow at or before its end, and shooting exactly at that end hits as many other balloons as possible. Sorting by end makes this choice fall out of a single sweep.
Do balloons that touch at an endpoint share an arrow?
Yes. An arrow at x bursts a balloon whose start or end equals x, which is why the comparison is a strict greater-than.
How is this related to non-overlapping intervals?
Both use the same greedy of sorting by end. Here the number of arrows equals the maximum number of balloons that are pairwise disjoint.
What is the time complexity?
O(n log n) for sorting, plus O(n) for the sweep.