Minimum Number of Arrows to Burst Balloons - Problem

Imagine you're defending a castle with a bow and arrow! ๐Ÿฐ There are spherical balloons floating at different heights along a wall, and you need to burst all of them using the minimum number of arrows.

Each balloon is represented by an interval [x_start, x_end] showing its horizontal span on the wall. When you shoot an arrow vertically at position x, it will burst all balloons whose intervals contain that point (where x_start โ‰ค x โ‰ค x_end).

Your mission: Find the minimum number of arrows needed to burst all balloons!

Example: If you have balloons at [[10,16], [2,8], [1,6], [7,12]], you can burst them all with just 2 arrows - one at position 6 (hits balloons [2,8] and [1,6]) and another at position 11 (hits balloons [10,16] and [7,12]).

Input & Output

example_1.py โ€” Basic Case
$ Input: points = [[10,16],[2,8],[1,6],[7,12]]
โ€บ Output: 2
๐Ÿ’ก Note: One way is to shoot arrows at x = 6 (bursting [2,8] and [1,6]) and x = 11 (bursting [10,16] and [7,12]). Two arrows are sufficient.
example_2.py โ€” Non-overlapping
$ Input: points = [[1,2],[3,4],[5,6],[7,8]]
โ€บ Output: 4
๐Ÿ’ก Note: Since none of the balloons overlap, we need one arrow for each balloon. Four arrows are required.
example_3.py โ€” All Overlapping
$ Input: points = [[1,2],[2,3],[3,4],[4,5]]
โ€บ Output: 2
๐Ÿ’ก Note: We can shoot at x = 2 (hits [1,2] and [2,3]) and x = 4 (hits [3,4] and [4,5]). Two arrows are needed.

Constraints

  • 1 โ‰ค points.length โ‰ค 105
  • points[i].length == 2
  • -231 โ‰ค xstart < xend โ‰ค 231 - 1
  • All balloon coordinates are valid integers

Visualization

Tap to expand
๐Ÿฐ Castle Defense StrategyBefore: Unsorted Balloons[10,16][2,8][1,6][7,12]After: Sorted by End Points[1,6][2,8][7,12][10,16]Arrow 1Arrow 2๐ŸŽฏ Greedy Strategy Explanation1. Sort by end points: [1,6], [2,8], [7,12], [10,16]2. First arrow at x=6: Hits [1,6] and [2,8] (both end โ‰ฅ 6)3. Check [7,12]: Starts at 7 > 6, so need new arrow4. Second arrow at x=12: Hits [7,12] and [10,16]Result: 2 arrows (optimal!)
Understanding the Visualization
1
Sort by End Points
Arrange balloons by when they end - this helps us find the optimal shooting positions
2
Shoot at First End
Place the first arrow at the end point of the first balloon - this maximizes coverage
3
Check Coverage
For each subsequent balloon, check if our current arrow can still reach it
4
New Arrow When Needed
Only when the current arrow can't reach a balloon do we need to shoot a new one
Key Takeaway
๐ŸŽฏ Key Insight: Always shoot at the earliest end point of overlapping balloons - this guarantees maximum coverage while using the minimum number of arrows!
Asked in
Google 42 Amazon 38 Microsoft 29 Meta 24
67.3K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen