Start learning free

LESSON 556 OF 673 · INTERVALS

Minimum Arrows to Burst Balloons in Python: O(n log n) Greedy by End

Practice O(n log n) Read the full explanation

You get a list points. Each pair [start, end] in it is one balloon. The balloon covers every point on a line from its start to its end.

An arrow shot at point x bursts every balloon with start <= x <= end. Return the fewest arrows that burst all the balloons.

Balloons can overlap. One arrow can burst several of them.

 

Example 1:

Input: points = [[-4,2],[0,6],[5,9],[1,3],[8,8]]
Output: 2
Explanation: An arrow at x = 2 bursts [-4,2], [0,6] and [1,3].
An arrow at x = 8 bursts [5,9] and [8,8].
[-4,2] and [8,8] share no point.
So one arrow is not enough.

Example 2:

Input: points = [[0,20],[3,4],[6,8],[15,30]]
Output: 3
Explanation: [3,4], [6,8] and [15,30] share no point.
So each one needs its own arrow.
[0,20] contains [3,4], so the same arrow bursts it.

Example 3:

Input: points = [[1,3],[3,6]]
Output: 1
Explanation: Both balloons cover the point 3.
One arrow at x = 3 bursts both.

 

Constraints:

  • 1 to 100,000 balloons.
  • Each start and end is a whole number from -231 to 231 - 1.
  • No start is greater than its end.
  • The balloons come in any order.

Note:

Your solution should run in O(n log n) time.

STEP 1 OF 8
    Ctrl + Enter to check

    PYTHON · INTERVALS

    Minimum Number Of Arrows To Burst Balloons in Python, explained

    Problem overview

    The method findMinArrowShots receives a list points. Each pair [start, end] is one balloon that covers every point on a line from its start to its end. An arrow shot at the point x bursts every balloon with start <= x <= end. The method returns the fewest arrows that burst all the balloons. Balloons may overlap, and one arrow can burst several of them.

    For [[-4, 2], [0, 6], [5, 9], [1, 3], [8, 8]] the answer is 2. An arrow at x = 2 bursts [-4, 2], [0, 6] and [1, 3], and an arrow at x = 8 bursts [5, 9] and [8, 8]. For [[0, 20], [3, 4], [6, 8], [15, 30]] the answer is 3: the balloons [3, 4], [6, 8] and [15, 30] share no point, so each needs its own arrow, while [0, 20] is burst by one of those arrows. For [[1, 3], [3, 6]] the answer is 1, since both balloons cover the point 3.

    The lesson asks for O(n log n) time. There are up to 100,000 balloons, in any order, with ends anywhere from about minus 2 billion to 2 billion.

    Approach: sort by end and shoot at the first end

    Looking for the best arrow positions on a line of billions of points would be hopeless, but only a few positions matter. Take the balloon that ends first. Any arrow that bursts it flies at its end or earlier. Moving that arrow right, to the balloon's end, loses nothing: every other balloon that the earlier position would burst starts at or before that position, and ends at or after the first end, because no balloon ends sooner. So it contains the end point as well.

    So shoot the first arrow at the smallest end. It bursts every balloon that starts at or before that point. For the balloons left, repeat: take the one that ends first, and shoot there.

    Sorting by end makes this one pass. After the sort, a balloon that starts at or before the last arrow's position arrowX also ends at or after it, because the ends are sorted, so the last arrow already bursts it. A balloon that starts after arrowX is out of reach of every arrow so far, and it needs a new one, shot at its own end.

    Step by step

    1. Sort points by end: points.sort(key=lambda p: p[1]). The key function receives a pair p and returns what to sort by, here its second element.
    2. Create arrows = 1. There is at least one balloon, so at least one arrow is needed.
    3. Create arrowX = points[0][1], the end of the first balloon after the sort.
    4. Loop with balloon over points. The first balloon is visited too; it starts at or before arrowX, so nothing changes.
    5. If balloon[0] > arrowX, the last arrow misses it. Use >, not >=: a balloon that starts exactly at arrowX is burst, as in [[1, 3], [3, 6]].
    6. Add 1 to arrows. All earlier arrows flew at arrowX or before.
    7. Set arrowX = balloon[1]. Of the balloons still standing, this one ends first, so its end is the best spot.
    8. Return arrows.

    On the first example, sorting by end gives this order:

    [[-4, 2], [1, 3], [0, 6], [8, 8], [5, 9]]

    The first arrow goes to x = 2. The balloons [1, 3] and [0, 6] start at 1 and 0, both at most 2, so they are already burst. The balloon [8, 8] starts at 8, which is greater than 2, so a second arrow goes to x = 8. The last balloon, [5, 9], starts at 5, at most 8, so it is burst. The answer is 2.

    The solution in Python

    class Solution:
        def findMinArrowShots(self, points: List[List[int]]) -> int:
            points.sort(key=lambda p: p[1])
            arrows = 1
            arrowX = points[0][1]
            for balloon in points:
                if balloon[0] > arrowX:
                    arrows += 1
                    arrowX = balloon[1]
            return arrows
    

    Python details:

    • list.sort() sorts in place and returns None. That is why the code calls it on its own line and then keeps using points. Writing points = points.sort(...) would make points equal to None. The in-place sort also reorders the caller's list. For the first example, the input list ends up in the order shown above.
    • The sort is stable and compares only the ends, so balloons with the same end keep their relative order. The order among them does not change the count.
    • Python integers have no fixed size, so ends near plus or minus 2 billion need no special care.

    Complexity

    Time: O(n log n). The sort takes O(n log n), and the loop adds one O(n) pass.

    Space: O(1) extra, apart from what the sort uses internally, since the code only keeps arrows and arrowX.

    Edge cases and common mistakes

    • A single balloon. [[-2147483648, 2147483647]] returns 1.
    • Touching balloons. [[1, 3], [3, 6]] returns 1 because of the strict >. With >= the answer would be 2.
    • Balloons that don't overlap at all. [[1, 2], [3, 4], [5, 6]] returns 3: each one needs its own arrow.
    • A balloon inside another. [[1, 5], [2, 3], [4, 9]] returns 2: the arrow at 3 bursts [2, 3] and [1, 5], and [4, 9] needs the second.
    • Sorting by start instead of end. Then the first balloon may stretch far to the right, and shooting at its end could miss balloons that a shot further left would have burst. The end is the right key.

    Testing

    Results from running the method:

    pointsResult
    [[-4, 2], [0, 6], [5, 9], [1, 3], [8, 8]]2
    [[0, 20], [3, 4], [6, 8], [15, 30]]3
    [[1, 3], [3, 6]]1
    [[1, 2], [3, 4], [5, 6]]3
    [[-2147483648, 2147483647]]1
    [[1, 5], [2, 3], [4, 9]]2
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free