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.
Example 3:
Input: points = [[1,2],[2,3],[3,4],[4,5]] Output: 2 Explanation: The balloons can be burst by 2 arrows: - Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3]. - Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
Constraints:
1 <= points.length <= 105points[i].length == 2-231 <= xstart < xend <= 231 - 1Problem Overview: You are given a list of balloons where each balloon is represented as an interval [start, end] on the x-axis. One arrow shot at position x bursts every balloon whose interval contains x. The task is to compute the minimum number of arrows required to burst all balloons.
Approach 1: Greedy Approach Using Sorting by End Points (O(n log n) time, O(1) space)
This problem behaves like an interval covering problem. Sort the balloons by their end coordinate. Shoot the first arrow at the end of the first interval, then iterate through the sorted intervals. If the next balloon starts after the current arrow position, you need a new arrow and update the arrow position to the current balloon's end. Otherwise, the current arrow already bursts that balloon. The key insight: placing the arrow at the earliest possible end maximizes overlap with future balloons. This greedy strategy works because choosing the smallest end preserves the most room for upcoming intervals.
Sorting dominates the runtime at O(n log n), while the traversal is linear. The algorithm uses constant extra memory O(1) if sorting is done in-place. This approach directly leverages ideas from greedy algorithms, sorting, and interval processing on an array.
Approach 2: Interval Scheduling Maximization (O(n log n) time, O(1) space)
This perspective treats the problem as the classic interval scheduling variant. Instead of selecting the maximum number of non-overlapping intervals, you group overlapping intervals that can be covered by a single arrow. After sorting intervals by end coordinate, iterate through them and maintain the end of the current overlapping group. When a new interval starts beyond the current group end, the previous group requires one arrow and a new group begins. Conceptually, each arrow corresponds to one maximal set of overlapping intervals.
The mechanics are nearly identical to the greedy solution but framed through scheduling theory. Sorting costs O(n log n), and the single pass scan is O(n) with constant auxiliary memory O(1). This interpretation helps when connecting the problem to classic interval scheduling and greedy proofs.
Recommended for interviews: The greedy strategy that sorts intervals by end points is the expected solution. Interviewers look for the insight that placing the arrow at the earliest finishing balloon maximizes coverage of future balloons. Explaining the interval scheduling connection strengthens the reasoning. A brute-force overlap simulation shows understanding, but the O(n log n) greedy solution demonstrates strong algorithmic thinking.
This approach involves sorting the balloons by their end points. Once sorted, we shoot an arrow at the end point of the first balloon, and then continue moving through the list, checking if subsequent balloons overlap with the burst of the arrow. If a balloon does not overlap, we need an additional arrow for it.
This C solution uses standard libraries to sort a list of balloon intervals by their end points, ensuring we minimize the number of arrows used. Sorting ensures that at every step, we maximize the number of balloons burst by a single arrow.
The function compare is used with qsort to sort the intervals by their end points. Then, we maintain an `end` variable to store the position of the last arrow shot. We iterate through the intervals, and for each interval that does not overlap with the current `end`, we shoot a new arrow and update `end`.
Time Complexity: O(n log n) due to the sorting step, where n is the number of balloons.
Space Complexity: O(1) if we ignore the space used for sorting.
This approach is adapted from the interval scheduling maximization pattern, where we attempt to find the maximum number of non-overlapping intervals. By sorting the intervals, we can focus on selecting the maximum number of compatible balloons.
The C implementation sorts balloons by their starting x coordinate to maximize the intervals which can overlap and thus require fewer arrows.
Overlapping intervals are managed by checking the end position of current overlaps. If overlaps occur, the continuation of overlapping checks determines the next 'strongly ending' balloon that indicates a need for a new arrow, thereby ensuring no excess arrows.
Time Complexity: O(n log n) resulting from sorting operations.
Space Complexity: O(1) when sorting space considerations are minimized.
| Approach | Complexity |
|---|---|
| Greedy Approach Using Sorting by End Points | Time Complexity: O(n log n) due to the sorting step, where n is the number of balloons. |
| Interval Scheduling Maximization | Time Complexity: O(n log n) resulting from sorting operations. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Sorting by End Points | O(n log n) | O(1) | General case. Standard optimal solution used in interviews. |
| Interval Scheduling Maximization | O(n log n) | O(1) | When reasoning using classic interval scheduling theory. |
Minimum Number of Arrows to Burst Balloons - Leetcode 452 - Python • NeetCodeIO • 30,552 views views
Watch 9 more video solutions →Practice Minimum Number of Arrows to Burst Balloons with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor