|
| 1 | +# Minimum Number of Arrows to Burst Baloons |
| 2 | + |
| 3 | +## Problem Description |
| 4 | + |
| 5 | +There are some spherical balloons taped onto a flat wall that represents the XY-plane. |
| 6 | +The balloons are represented as a 2D integer array points where |
| 7 | +`points[i] = [xstart, xend]` denotes a balloon whose horizontal diameter |
| 8 | +stretches between `xstart` and `xend`. You do not know the exact y-coordinates |
| 9 | +of the balloons. |
| 10 | + |
| 11 | +Arrows can be shot up directly vertically (in the positive y-direction) |
| 12 | +from different points along the x-axis. A balloon with `xstart` and `xend` |
| 13 | +is burst by an arrow shot at x if `xstart <= x <= xend`. There is no limit |
| 14 | +to the number of arrows that can be shot. A shot arrow keeps traveling up |
| 15 | +infinitely, bursting any balloons in its path. |
| 16 | + |
| 17 | +Given the array points, return the minimum number of arrows that must be shot |
| 18 | +to burst all balloons. |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +Input: `points = [[10,16],[2,8],[1,6],[7,12]]` |
| 23 | +Output: `2` |
| 24 | +Explanation: The balloons can be burst by 2 arrows: |
| 25 | +- Shoot an arrow at `x = 6`, bursting the balloons `[2,8]` and `[1,6]`. |
| 26 | +- Shoot an arrow at `x = 11`, bursting the balloons `[10,16]` and `[7,12]`. |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +Input: `points = [[1,2],[3,4],[5,6],[7,8]]` |
| 31 | +Output: `4` |
| 32 | +Explanation: One arrow needs to be shot for each balloon for a total of `4` arrows. |
| 33 | + |
| 34 | +**Example 3:** |
| 35 | + |
| 36 | +Input: `points = [[1,2],[2,3],[3,4],[4,5]]` |
| 37 | +Output: `2` |
| 38 | +Explanation: The balloons can be burst by 2 arrows: |
| 39 | +- Shoot an arrow at `x = 2`, bursting the balloons `[1,2]` and `[2,3]`. |
| 40 | +- Shoot an arrow at `x = 4`, bursting the balloons `[3,4]` and `[4,5]`. |
| 41 | + |
| 42 | +**Constraints:** |
| 43 | + |
| 44 | +* `1 <= points.length <= 105` |
| 45 | +* `points[i].length == 2` |
| 46 | +* `-231 <= xstart < xend <= 231 - 1` |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +## Solution |
| 52 | + |
| 53 | +```python |
| 54 | +def find_min_arrow_shots(points: list[list[int]]) -> int: |
| 55 | + """ |
| 56 | + Calculate the minimum number of arrows needed to burst all balloons. |
| 57 | +
|
| 58 | + The balloons are represented as intervals on a 2D plane (start and end points). |
| 59 | + An arrow shot at x-coordinate x bursts all balloons whose interval includes x. |
| 60 | + The function uses a greedy algorithm to find the optimal shooting points. |
| 61 | +
|
| 62 | + :param points: A list of balloon intervals, where each interval is represented as [start, end] |
| 63 | + :return: The minimum number of arrows required to burst all balloons |
| 64 | + """ |
| 65 | + points.sort(key=lambda x: x[1]) |
| 66 | + |
| 67 | + shots = 1 |
| 68 | + current_point = points[0] |
| 69 | + |
| 70 | + for point in points: |
| 71 | + if point[0] > current_point[1]: |
| 72 | + shots += 1 |
| 73 | + current_point = point |
| 74 | + return shots |
| 75 | +``` |
| 76 | + |
| 77 | +* **Time Complexity:** $O(n\cdot log(n)$ |
| 78 | +* **Space Complexity:** $O(n)$ |
| 79 | + |
| 80 | +## Explanation of the Solution |
| 81 | + |
| 82 | +1. Sort Balloons by End Points |
| 83 | + * First, we sort all balloons based on their end coordinates in ascending order. |
| 84 | + * Why? This allows us to process balloons in an order where we can greedily select the earliest possible "burst point." |
| 85 | +2. Initialize Variables |
| 86 | + * `shots = 1` (we need at least one arrow). |
| 87 | + * `current_end = points[0][1]` (the end of the first balloon). |
| 88 | + |
| 89 | +3. Greedy Arrow Placement |
| 90 | + * Iterate through each balloon in the sorted list: |
| 91 | + * If the start of the current balloon > current end: |
| 92 | + * This means the balloon cannot be burst by the previous arrow. |
| 93 | + * We increment shots (a new arrow is needed). |
| 94 | + * Update current_end to the end of this new balloon. |
| 95 | + * Else: |
| 96 | + * The current balloon can be burst by the same arrow (since intervals overlap). |
| 97 | + |
| 98 | +4. Return the Result |
| 99 | + * The variable shots now holds the minimum number of arrows needed. |
| 100 | + |
0 commit comments