Minimum Number of Arrows to Burst Balloons - LeetCode Daily Challenge
Problem Statement
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] = [xstart, xend]
denotes a balloon whose horizontal diameter stretches between xstart
and xend
. You do not know the exact y-coordinates of the balloons.
Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart
and xend
is burst by an arrow shot at x
if xstart <= x <= xend
. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
Given the array points
, return the minimum number of arrows that must be shot to burst all balloons.
Example 1
Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
Example 2
Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4
Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
Try here before watching the video.
Video Explanation
Java Code
class Solution {
public 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(end >= points[i][0])
continue;
arrows += 1;
end = points[i][1];
}
return arrows;
}
}
C++ Code
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
return a[1] < b[1];
});
int arrows = 1, end = points[0][1];
for (int i = 1; i < points.size(); i++) {
if (end >= points[i][0])
continue;
arrows += 1;
end = points[i][1];
}
return arrows;
}
};
Python Code
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
points.sort(key=lambda x: x[1])
arrows = 1
end = points[0][1]
for i in range(1, len(points)):
if end >= points[i][0]:
continue
arrows += 1
end = points[i][1]
return arrows
Javascript Code
var findMinArrowShots = function(points) {
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 (end >= points[i][0])
continue;
arrows += 1;
end = points[i][1];
}
return arrows;
};
Go Code
func findMinArrowShots(points [][]int) int {
sort.Slice(points, func(i, j int) bool {
return points[i][1] < points[j][1]
})
arrows := 1
end := points[0][1]
for i := 1; i < len(points); i++ {
if end >= points[i][0] {
continue
}
arrows++
end = points[i][1]
}
return arrows
}
Complexity Analysis
Time Complexity: O(N*logN), this is because we're sorting the array.
Space Complexity: O(1), we're not taking any extra space, but to be specific, based on the sorting algorithms it might take some extra space based on language.