#452

Minimum Number of Arrows to Burst Balloons

Medium
ArrayGreedySortingGreedySortingInterval Scheduling
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n log n)
Space
O(1)
O(1)
💡

Intuition

Time O(n log n)Space O(1)

The optimal approach uses a greedy algorithm. By sorting the balloons by their end positions, we can ensure that we always shoot the arrow at the end of the current balloon that overlaps with others, minimizing the number of arrows needed.

⚙️

Algorithm

3 steps
  1. 1Step 1: Sort the balloons by their end positions.
  2. 2Step 2: Initialize arrows to 0 and set the current_end to the end of the first balloon.
  3. 3Step 3: Iterate through the sorted balloons, and if the start of the current balloon is greater than current_end, increment arrows and update current_end.
solution.py11 lines
1def findMinArrowShots(points):
2    if not points:
3        return 0
4    points.sort(key=lambda x: x[1])
5    arrows = 1
6    current_end = points[0][1]
7    for start, end in points[1:]:
8        if start > current_end:
9            arrows += 1
10            current_end = end
11    return arrows

Complexity note: The complexity is dominated by the sorting step, which is O(n log n). The subsequent iteration through the points is O(n).

  • 1Shooting an arrow at the end of a balloon can potentially burst multiple overlapping balloons.
  • 2Sorting the balloons by their end positions allows us to minimize the number of arrows needed.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.