LeetCode 452: Minimum Number of Arrows to Burst Balloons — Python Solution

LeetCode 452: Minimum Number of Arrows to Burst Balloons is a Medium intervals problem. This Python walkthrough develops a greedy earliest end solution, ties every code decision to a concrete invariant, and includes the regression check used before publication.

This independent guide paraphrases the task rather than reproducing LeetCode’s prompt. Use the official page for the exact statement, examples, constraints, and submission runner.

DifficultyMedium
TopicIntervals
Reusable patterngreedy earliest end
ComplexityO(n log n) time and O(1) auxiliary space aside from sorting

Recognizing the pattern

Sorting interval boundaries exposes a monotonic frontier: once an interval begins beyond it, earlier intervals cannot overlap again.

For this problem specifically, sort balloons by ending coordinate and shoot at the earliest possible end; add an arrow only when the next interval starts after that point. The invariant worth writing beside the code is: The current arrow bursts every interval processed since the last arrow and leaves maximum room for future overlaps.

Step-by-step algorithm

  1. Identify the input state consumed by findMinArrowShots(points) and initialize the data required by the greedy earliest end pattern.
  2. Sort balloons by ending coordinate and shoot at the earliest possible end; add an arrow only when the next interval starts after that point.
  3. After each update, verify the page’s central invariant: The current arrow bursts every interval processed since the last arrow and leaves maximum room for future overlaps.
  4. Finish only after the boundary behavior is covered: Intervals touching the arrow coordinate are burst by the same arrow.

Python solution

class Solution:
    def findMinArrowShots(self, points):
        if not points:
            return 0
        points.sort(key=lambda interval: interval[1])
        arrows, position = 1, points[0][1]
        for start, end in points[1:]:
            if start > position:
                arrows += 1
                position = end
        return arrows

Reading the implementation

The main entry point is findMinArrowShots(points). The named working state includes arrows, position; those variables make the greedy earliest end state visible instead of hiding it in incidental control flow.

A single main loop advances the algorithm, which is the key reason the traversal does not revisit already resolved input unnecessarily. Early returns stop as soon as the answer is forced, avoiding work that cannot change the result. In concrete terms, sort balloons by ending coordinate and shoot at the earliest possible end; add an arrow only when the next interval starts after that point.

Correctness argument

Initialization. Before the scan begins, the unresolved range contains every candidate and the processed range is empty, so no answer has been lost.

Preservation. Sort balloons by ending coordinate and shoot at the earliest possible end; add an arrow only when the next interval starts after that point. The chosen movement discards only candidates that cannot improve the answer; afterward, the current arrow bursts every interval processed since the last arrow and leaves maximum room for future overlaps.

Termination. A boundary advances on every iteration. Once the active range is exhausted, every viable candidate was either measured or safely eliminated, so the stored result is optimal.

Complexity and trade-offs

O(n log n) time and O(1) auxiliary space aside from sorting. The auxiliary-space figure excludes the returned output unless the output itself is the structure being built.

Checking every pair avoids sorting but does quadratic work and complicates transitive overlaps. That comparison is useful in an interview because it explains why the final implementation is preferable, not merely that it passes.

Regression check

The published implementation belongs to a 100-problem suite that is compiled and exercised behaviorally before deployment.

One reference assertion from that suite is shown below. It targets the normal path while the edge conditions in the next section cover the failure-prone boundaries.

assert Solution().findMinArrowShots([[10,16],[2,8],[1,6],[7,12]]) == 2

Common mistakes and edge cases

  • Problem-specific boundary: Intervals touching the arrow coordinate are burst by the same arrow.
  • Pattern-level pitfall: State whether touching endpoints count as overlap and preserve the appropriate endpoint when merging.
  • Invariant check: after every update, confirm that the current arrow bursts every interval processed since the last arrow and leaves maximum room for future overlaps.

Interview review checklist

  • Explain why greedy earliest end matches the structure of this input.
  • State the invariant in one sentence before tracing code: The current arrow bursts every interval processed since the last arrow and leaves maximum room for future overlaps.
  • Derive O(n log n) time and O(1) auxiliary space aside from sorting from how many times each element or state is visited.
  • Test the boundary explicitly: Intervals touching the arrow coordinate are burst by the same arrow.

Browse the searchable 100 LeetCode Python Solutions hub. Previous: 57. Insert Interval · Next: 20. Valid Parentheses