Search lands in PR-5.1 (Pagefind).

How-to Intermediate

Chapter 2 Updated

Minimum Arrows to Burst Balloons

Sort by end; each group of overlapping intervals needs one arrow.

  • Full 5m
  • Revision
  • Flow

Problem Statement

Balloons are represented as horizontal intervals [x_start, x_end]. A vertical arrow shot at position x bursts every balloon whose range contains x. Return the minimum number of arrows needed to burst every balloon.

Input

An array points where each entry is a balloon’s horizontal span.

Example input
[[10,16],[2,8],[1,6],[7,12]]

Output

A single integer — the minimum number of arrows.

Expected output
2

Constraints

  • 1 ≤ points.length ≤ 10⁵
  • points[i].length == 2
  • -2³¹ ≤ xstartᵢ ≤ xendᵢ ≤ 2³¹ − 1

Intuition

Sort by end coordinate. Shoot the first arrow at the first balloon’s right edge — it automatically pops every balloon that starts at or before that edge. The next balloon whose start exceeds the arrow position begins a new group → another arrow. This is exactly the Activity Selection problem, counting groups instead of kept intervals.

Why sort by END
Sort by end:  [1,6]  [7,12]  [10,16]  [2,8]
                ↑      ↑        ↑       ↑
Arrow at 6:   BURST  (start 7 > 6, new group)
Arrow at 12:         BURST   BURST    BURST (start 2 ≤ 12)
 
Total arrows: 2

Solution

min_arrows.py
def findMinArrowShots(points: list[list[int]]) -> int:
    if not points:
        return 0
 
    points.sort(key=lambda x: x[1])
    arrows, pos = 1, points[0][1]
 
    for start, end in points[1:]:
        if start > pos:
            arrows += 1
            pos = end
 
    return arrows

Complexity

MetricValueReason
TimeO(n log n)Sorting dominates; the scan is O(n)
SpaceO(1)Only two scalar variables tracked

Edge Case Thinking

ScenarioHandling
Empty pointsReturn 0.
All balloons overlap on a common pointOnly 1 arrow needed — the first arrow bursts all.
No two balloons overlapOne arrow per balloon → arrows = n.
Touching edges [1,2] & [2,3]start > pos is 2 > 2 = false → same arrow; the > (not ) matters.
Sort-by-start variantPicking the wrong sort order fails — a [1,100] greedy would wrongly claim one arrow for the universe.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.