Ecommet

Minimum Number of Arrows to Burst Balloons

Medium Problem 144 of 151 Java and Python

Problem

Balloons are taped to a wall; balloon i spans the horizontal range points[i] = [xstart, xend]. An arrow shot straight up at position x bursts every balloon with xstart ≤ x ≤ xend.

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

Example 1

Input
points = [[10, 16], [2, 8], [1, 6], [7, 12]]
Output
2
Why
Shoot at x = 6 (bursts [2, 8] and [1, 6]) and x = 11 (bursts [10, 16] and [7, 12]).

Example 2

Input
points = [[1, 2], [3, 4], [5, 6], [7, 8]]
Output
4

Example 3

Input
points = [[1, 2], [2, 3], [3, 4], [4, 5]]
Output
2
Why
Touching ends share an arrow.

Constraints

  • 1 ≤ points.length ≤ 10⁵
  • -2³¹ ≤ xstart < xend ≤ 2³¹ − 1

Explanation

The balloon that ends first must be hit by some arrow at or before its end. Shooting exactly at its end hits it and as many later balloons as possible.

Sort by end; shoot at the first end; every balloon starting at or before that point is burst. The next balloon starting after it needs a new arrow.

Approach 1 · Better

Sort by start, shrink the overlap

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

Sort by start. Keep the overlap of the current group of balloons; when the next balloon starts after the overlap ends, a new arrow is needed.

  1. Sort by start; arrows = 1, end = first end.
  2. For each balloon: if it starts after end, add an arrow and set end to its end; otherwise end = min(end, its end).
Java
import java.util.*;

class Solution {
    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
        int arrows = 1;
        int end = points[0][1];
        for (int[] p : points) {
            if (p[0] > end) {
                arrows++;
                end = p[1];
            } else {
                end = Math.min(end, p[1]);
            }
        }
        return arrows;
    }
}
Python
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        points = sorted(points)
        arrows, end = 1, points[0][1]
        for start, finish in points:
            if start > end:
                arrows += 1
                end = finish
            else:
                end = min(end, finish)
        return arrows

Approach 2 · Optimal

Sort by end, shoot at each end

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

Sort by end. Shoot at the first end; skip every balloon that starts at or before it; the next one starts a new arrow.

  1. Sort by end; arrows = 1, shot = first end.
  2. For each balloon: if start > shot, add an arrow at its end.
Java
import java.util.*;

class Solution {
    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
        int arrows = 1;
        int shot = points[0][1];
        for (int[] p : points) {
            if (p[0] > shot) {
                arrows++;
                shot = p[1];
            }
        }
        return arrows;
    }
}
Python
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        points = sorted(points, key=lambda p: p[1])
        arrows, shot = 1, points[0][1]
        for start, end in points:
            if start > shot:
                arrows += 1
                shot = end
        return arrows

Complexity compared

ApproachTimeSpace
Better Sort by start, shrink the overlapO(n log n)O(1) extra
Optimal Sort by end, shoot at each endO(n log n)O(1) extra

Key takeaways

  • Use Integer.compare in Java comparators, not a[1] - b[1]: the values reach ±2³¹ and subtraction overflows.

Your notes

Saved in this browser as you type.

Solved it on your own?

Mark it done to track your progress, or star it to come back before an interview.

Next problem