|
| 1 | +# Minimum Number of Arrows to Burst Balloons |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D array `points` where `points[i] = [xstart, xend]` denotes a horizontal diameter of the balloon at `xstart` and `xend`. You do not know the exact y-coordinates of the balloons. |
| 6 | + |
| 7 | +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 you can shoot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path. |
| 8 | + |
| 9 | +Given the array `points`, return the minimum number of arrows that must be shot to burst all balloons. |
| 10 | + |
| 11 | +## Examples |
| 12 | + |
| 13 | +**Example 1:** |
| 14 | +``` |
| 15 | +Input: points = [[10,16],[2,8],[1,6],[7,12]] |
| 16 | +Output: 2 |
| 17 | +Explanation: The balloons can be burst by 2 arrows: |
| 18 | +- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6]. |
| 19 | +- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12]. |
| 20 | +``` |
| 21 | + |
| 22 | +## Approach |
| 23 | + |
| 24 | +### Method 1: Greedy Algorithm (Recommended) |
| 25 | +1. Sort balloons by end position |
| 26 | +2. Use greedy approach to shoot arrows |
| 27 | +3. Shoot arrow at end of first balloon |
| 28 | +4. Most efficient approach |
| 29 | + |
| 30 | +**Time Complexity:** O(n log n) - Sorting |
| 31 | +**Space Complexity:** O(1) - In-place modification |
| 32 | + |
| 33 | +### Method 2: Sorting by Start Position |
| 34 | +1. Sort balloons by start position |
| 35 | +2. Use different greedy logic |
| 36 | +3. Less efficient than sorting by end position |
| 37 | + |
| 38 | +**Time Complexity:** O(n log n) - Sorting |
| 39 | +**Space Complexity:** O(1) - In-place modification |
| 40 | + |
| 41 | +## Algorithm |
| 42 | + |
| 43 | +``` |
| 44 | +1. Sort balloons by end position |
| 45 | +2. Initialize arrows = 1, end = points[0][1] |
| 46 | +3. For i from 1 to n-1: |
| 47 | + a. If points[i][0] > end: arrows++, end = points[i][1] |
| 48 | +4. Return arrows |
| 49 | +``` |
| 50 | + |
| 51 | +## Key Insights |
| 52 | + |
| 53 | +- **Greedy Choice**: Always shoot arrow at end of first balloon |
| 54 | +- **Local Optimum**: Maximum balloons burst per arrow |
| 55 | +- **Global Optimum**: Minimum number of arrows needed |
| 56 | +- **Space Optimization**: Use only necessary space |
| 57 | + |
| 58 | +## Alternative Approaches |
| 59 | + |
| 60 | +1. **Sorting by Start**: Sort by start position |
| 61 | +2. **Sweep Line**: Use sweep line algorithm |
| 62 | +3. **Brute Force**: Try all possible arrow positions |
| 63 | + |
| 64 | +## Edge Cases |
| 65 | + |
| 66 | +- Empty balloons: Return 0 |
| 67 | +- Single balloon: Return 1 |
| 68 | +- No overlaps: Return n |
| 69 | +- All overlaps: Return 1 |
| 70 | + |
| 71 | +## Applications |
| 72 | + |
| 73 | +- Interval algorithms |
| 74 | +- Scheduling problems |
| 75 | +- Algorithm design patterns |
| 76 | +- Interview preparation |
| 77 | +- System design |
| 78 | + |
| 79 | +## Optimization Opportunities |
| 80 | + |
| 81 | +- **Greedy Algorithm**: Most efficient approach |
| 82 | +- **Space Optimization**: O(1) space complexity |
| 83 | +- **Logarithmic Time**: O(n log n) time complexity |
| 84 | +- **No Extra Space**: Use only necessary space |
0 commit comments