InterviewPrepKit

Home / Coding / Greedy

Minimum Number of Arrows to Burst Balloons

medium Original ↗
Solving tips
  • Reframe as 'fewest points that stab all intervals' — the classic interval point-cover / activity-selection greedy.
  • Sort by right endpoint, place an arrow at the earliest end, and only shoot a new arrow when a balloon's start exceeds the current arrow position.
  • Use strict > for the new-arrow test: endpoints are inclusive, so start == arrow_x still bursts.
  • Target O(n log n) time (sort-dominated) and O(1) extra space; sorting by start instead of end is the usual mistake.

Problem

Each balloon is described by a horizontal interval points[i] = [start, end], meaning it spans the x-range start to end (inclusive). Arrows are shot straight up from an x-coordinate; an arrow shot at x bursts every balloon whose interval contains x, i.e. every balloon with start <= x <= end. An arrow keeps travelling upward and can burst any number of overlapping balloons.

Return the minimum number of arrows needed to burst all balloons.

Examples

  • points = [[10,16],[2,8],[1,6],[7,12]]2 — one arrow at x = 6 bursts [2,8] and [1,6]; one arrow at x = 12 bursts [10,16] and [7,12].
  • points = [[1,2],[3,4],[5,6],[7,8]]4 — no two intervals overlap, so each needs its own arrow.
  • points = [[1,2],[2,3],[3,4],[4,5]]2 — an arrow at x = 2 bursts [1,2],[2,3]; an arrow at x = 4 bursts [3,4],[4,5].

Constraints

  • 1 <= len(points) <= 10^5
  • points[i] = [start, end] with start <= end, values fitting in 32-bit signed range.
  • The 10^5 bound (and huge coordinate range) means an O(n log n) sort-then-scan is the target; enumerating arrow positions is out.

Think about it first

Hint 1 This is really "find the minimum number of points that stab all intervals." An arrow position is a point; a balloon is stabbed if the point lies inside its interval.
Hint 2 Sort the balloons by their right endpoint. Think about the balloon that ends first — where should you place an arrow so it bursts, while also catching as many others as possible?
Hint 3 Placing the arrow exactly at that smallest right endpoint is optimal: it still bursts that balloon and reaches as far right as any valid position could. Burst everything that arrow covers, then repeat with the next still-alive balloon.
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.