Leetcode 452. Minimum Number of Arrows to Burst Balloons
You are given a set of n
balloons, each represented as an interval [start, end]
, where start
is the start position and end
is the end position of the balloon. The balloons can be burst by arrows. An arrow can be shot from any point along the coordinate axis, and it bursts all balloons that it touches. You need to find the minimum number of arrows required to burst all the balloons.
Input: [[10,16], [2,8], [1,6], [7,12]]
Output: 2
Explanation:
One way is to shoot one arrow for example at x = 6 (bursting balloons [2,8], [1,6]) and another arrow at x = 11 (bursting [10,16], [7,12]).
The key idea is to use a greedy algorithm to determine the minimum number of arrows required. Here’s the step-by-step strategy:
By following this approach, we minimize the need for arrows by always greedily shooting an arrow at the end of the balloon that finishes the earliest and covering as many subsequent overlapping balloons as possible.
Here is the C++ code implementing the above strategy:
#include <vector>
#include <algorithm>
int findMinArrowShots(std::vector<std::vector<int>>& points) {
if (points.empty()) return 0;
// Sort the intervals by their end points
std::sort(points.begin(), points.end(), [](const std::vector<int>& a, const std::vector<int>& b) {
return a[1] < b[1];
});
int arrows = 1; // Start with one arrow
int end = points[0][1]; // Shoot the first arrow at the end of the first balloon
for(const auto& balloon : points) {
// If the current balloon starts after the position of the last arrow
if (balloon[0] > end) {
arrows++; // Need another arrow
end = balloon[1]; // Update the end to the current balloon's end
}
}
return arrows;
}
Hence, the overall time complexity is (O(n \log n)), dominated by the sorting step. This approach ensures that we achieve the desired efficiency.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?