#452

Minimum Number of Arrows to Burst Balloons

Medium Sort by end

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Balloons span intervals on the x-axis. A vertical arrow at x bursts every balloon whose interval contains x (ends included). Return the fewest arrows needed.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: fewest arrows to pop all balloons.

Why this works: shoot at the end of the earliest-ending balloon; it pops every balloon that has already started.

Example: [[10,16],[2,8],[1,6],[7,12]] sorted by end

arrow at 6 pops [1,6], [2,8]arrow at 12 pops [7,12], [10,16] → 2 arrows

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Comparator a[1] − b[1] overflows with values near ±2^31. Use Integer.compare.

Solve on LeetCode ↗
Complexity
  • Time: O(n log n).
  • Space: O(log n).
Topic: Greedy
🎬 Episodes 69–73: video coming soon