Home
DSA Patterns
Intervals
Min Arrows to Burst Balloons
Min Arrows to Burst Balloons (LeetCode 452)
Min Arrows to Burst Balloons (LeetCode 452): find the minimum number of arrows to burst overlapping balloons.
Problem Understanding
You are given an array of balloons where points[i] = [start, end] represents a balloon. An arrow can be shot up vertically at any x. A balloon is popped if start <= x <= end. An arrow can pop infinite overlapping balloons. Find the minimum number of arrows needed to pop all balloons.
Example:
- Input:
[[10,16],[2,8],[1,6],[7,12]] - Output:
2 - Explanation: Shoot one arrow at
x=6(bursts[2,8], [1,6]). Shoot another atx=11(bursts[10,16], [7,12]).
Algorithm Strategy
This is a Greedy problem similar to Non-overlapping Intervals. We want to shoot an arrow that pops as many balloons as possible. The best spot is the end of the earliest finishing balloon because that maximizes the chance to catch subsequent balloons.
- Sort: Sort balloons by their end coordinate.
- Iterate:
- Initialize
arrows = 1. SetendPosto the end of the first balloon. - For each subsequent balloon: If its
startis greater thanendPos, it means the current arrow missed it. We must shoot a new arrow. Incrementarrowsand updateendPosto this new balloon's end. - Else (overlap), the current arrow also pops this balloon. Do nothing.
- Initialize
Interactive Visualization
4 BALLOONS
INPUT
balloons · as given
[10,16]
[2,8]
[1,6]
[7,12]
1
16
Arrow at
:
Arrows
0
arrows (x)
None yet
4 balloons: the fewest vertical arrows that burst them all
Next
Sort by end: [[1,6], [2,8], [7,12], [10,16]]: the first balloon to end must be hit at or before its end
Balloons
Visualize how sorting by end position allows a greedy arrow shot.
Dry Run
Input: [[10,16], [2,8], [1,6], [7,12]]
| Step | Balloon | Current Arrow Pos | Action |
|---|---|---|---|
| Init | Sort | - | [[1,6], [2,8], [7,12], [10,16]] |
| 1 | [1,6] |
6 | First balloon. Arrow at 6. Count = 1. |
| 2 | [2,8] |
6 | 2 <= 6. Popped! |
| 3 | [7,12] |
6 | 7 > 6. Missed! New Arrow at 12. Count = 2. |
| 4 | [10,16] |
12 | 10 <= 12. Popped! |
| Final | - | - | Answer: 2 |
Edge Cases & Common Mistakes
- Touch:
[1,2], [2,3]. Arrow at 2 bursts both.start <= endcondition holds. - Single Balloon: 1 Arrow.
- Negative Coordinates: Logic remains same.
Solution
Greedy approach O(N log N).
Min Arrows to Burst Balloons Code
1var findMinArrowShots = function(points) {2 if (!points.length) return 0;3 points.sort((a, b) => a[1] - b[1]);45 let arrows = 1;6 let end = points[0][1];78 for (let i = 1; i < points.length; i++) {9 if (points[i][0] > end) {10 arrows++;11 end = points[i][1];12 }13 }14 return arrows;15};
Min Arrows to Burst Balloons in Python, Java, C++, TypeScript and C
Min Arrows to Burst Balloons in Python
def findMinArrowShots(points):
if not points: return 0
points.sort(key=lambda x: x[1])
arrows = 1
end = points[0][1]
for i in range(1, len(points)):
if points[i][0] > end:
arrows += 1
end = points[i][1]
return arrowsMin Arrows to Burst Balloons in Java
class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length == 0) return 0;
// Use Integer.compare to avoid overflow
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
int arrows = 1;
int end = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] > end) {
arrows++;
end = points[i][1];
}
}
return arrows;
}
}Min Arrows to Burst Balloons in C++
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if (points.empty()) return 0;
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
return a[1] < b[1];
});
int arrows = 1;
int end = points[0][1];
for (int i = 1; i < points.size(); i++) {
if (points[i][0] > end) {
arrows++;
end = points[i][1];
}
}
return arrows;
}
};Min Arrows to Burst Balloons in TypeScript
function findMinArrowShots(points: number[][]): number {
if (!points.length) return 0;
points.sort((a, b) => a[1] - b[1]);
let arrows = 1;
let end = points[0][1];
for (let i = 1; i < points.length; i++) {
if (points[i][0] > end) {
arrows++;
end = points[i][1];
}
}
return arrows;
};Min Arrows to Burst Balloons in C
int compare(const void* a, const void* b) {
int* p1 = *(int**)a;
int* p2 = *(int**)b;
if (p1[1] < p2[1]) return -1;
if (p1[1] > p2[1]) return 1;
return 0;
}
int findMinArrowShots(int** points, int pointsSize, int* pointsColSize) {
if (pointsSize == 0) return 0;
qsort(points, pointsSize, sizeof(int*), compare);
int arrows = 1;
int end = points[0][1];
for (int i = 1; i < pointsSize; i++) {
if (points[i][0] > end) {
arrows++;
end = points[i][1];
}
}
return arrows;
}More practice: DSA interview questions with answers · patterns cheat sheet
