SDE & MLE Prep

452. Minimum Number of Arrows to Burst Balloons

Problem

Balloons are horizontal ranges [xstart, xend]. An arrow shot straight up at x bursts every balloon with xstart ≤ x ≤ xend. Return the minimum number of arrows needed to burst them all.

Example: [[10,16],[2,8],[1,6],[7,12]] → 2 (shoot at x = 6 and x = 11)

Constraints: up to 10⁵ balloons; coordinates span the full 32-bit range.

Sort by end; shoot at the first end. Every balloon starting ≤ that point is burst; the next balloon starting after it needs a new arrow at its end.

Complexity: O(n log n).

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        arrows, pos = 0, float("-inf")
        for s, e in sorted(points, key=lambda p: p[1]):
            if s > pos:
                arrows += 1
                pos = e
        return arrows