452. Minimum Number of Arrows to Burst Balloons
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of arrows to burst all balloons. Each balloon is represented by a horizontal interval [xstart, xend]. An arrow shot at position x bursts any balloon whose interval contains x. This is a classic interval scheduling problem. The goal is to find the minimum number of points that can "hit" or "cover" all given intervals.
Solution
Minimum Number of Arrows to Burst Balloons in Python
from typing import List
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
# If the list of points is empty, no arrows are needed.
if not points:
return 0
# Sort the points based on their end coordinates (p[1]).
# This greedy approach focuses on the balloon that finishes earliest.
points.sort(key=lambda p: p[1])
# We need at least one arrow for the first balloon.
arrows = 1
# The position of the first arrow is at the end of the first balloon.
# This greedy choice maximizes the chance of bursting subsequent balloons.
arrow_pos = points[0][1]
# Iterate through the sorted points starting from the second balloon.
for i in range(1, len(points)):
# If the start of the current balloon is after the arrow's position,
# a new arrow is required.
if points[i][0] > arrow_pos:
# Increment arrow count.
arrows += 1
# Update the arrow position to the end of the current balloon.
arrow_pos = points[i][1]
# Otherwise, the existing arrow can burst the current balloon.
return arrows
Minimum Number of Arrows to Burst Balloons in Java
import java.util.Arrays;
import java.util.Comparator;
class Solution {
public int findMinArrowShots(int[][] points) {
// If there are no balloons, no arrows are needed.
if (points.length == 0) {
return 0;
}
// Sort the points based on their end coordinates.
// Using Integer.compare to prevent potential integer overflow from `a[1] - b[1]`.
// This is the core of the greedy strategy.
Arrays.sort(points, Comparator.comparingInt(a -> a[1]));
// We need at least one arrow for the first balloon (the one that ends earliest).
int arrows = 1;
// Position the first arrow at the end of the first balloon.
// This position is the greedy choice to maximize overlap with future balloons.
int arrowPos = points[0][1];
// Iterate through the rest of the balloons.
for (int i = 1; i < points.length; i++) {
// If the start of the current balloon is to the right of the last arrow's position,
// it cannot be burst by the previous arrow.
if (points[i][0] > arrowPos) {
// We need a new arrow.
arrows++;
// Update the arrow's position to the end of the current balloon.
arrowPos = points[i][1];
}
// If the balloon starts at or before arrowPos, the existing arrow can burst it.
}
return arrows;
}
}
Minimum Number of Arrows to Burst Balloons in C++
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
// If there are no balloons, no arrows are needed.
if (points.empty()) {
return 0;
}
// Sort the points based on their end coordinates in ascending order.
// This greedy approach helps us handle balloons that finish earlier first.
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
return a[1] < b[1];
});
// We need at least one arrow for the first balloon.
int arrows = 1;
// The first arrow is shot at the end of the first balloon's interval.
// This maximizes its chance to burst subsequent balloons.
int arrowPos = points[0][1];
// Iterate through the rest of the balloons.
for (size_t i = 1; i < points.size(); ++i) {
// If the start of the current balloon is after the position of the last arrow,
// it means the last arrow cannot burst the current balloon.
if (points[i][0] > arrowPos) {
// So, we need a new arrow.
arrows++;
// And we place this new arrow at the end of the current balloon.
arrowPos = points[i][1];
}
// Otherwise, the current balloon is burst by the existing arrow, so we do nothing.
}
return arrows;
}
};
Minimum Number of Arrows to Burst Balloons in TypeScript
function findMinArrowShots(points: number[][]): number {
// If there are no balloons, no arrows are needed.
if (points.length === 0) {
return 0;
}
// Sort the points array based on the end coordinate of each balloon.
// The subtraction `a[1] - b[1]` works for sorting numbers in ascending order.
points.sort((a, b) => a[1] - b[1]);
// Initialize with one arrow for the first balloon.
let arrows = 1;
// The first arrow is positioned at the end of the first balloon (after sorting).
// This is a greedy choice to maximize coverage for subsequent balloons.
let arrowPos = points[0][1];
// Iterate through the rest of the balloons.
for (let i = 1; i < points.length; i++) {
// If the start of the current balloon is past the position of the last arrow,
// it means the last arrow cannot burst this balloon.
if (points[i][0] > arrowPos) {
// Therefore, a new arrow is needed.
arrows++;
// Update the arrow position to the end of the current balloon.
arrowPos = points[i][1];
}
// If the balloon starts at or before arrowPos, the current arrow can burst it.
}
return arrows;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.