Minimum Number of Arrows to Burst Balloons

Medium · Intervals · Array · Greedy · Sorting

Problem

Balloons are taped to a wall. Balloon i covers the horizontal range points[i] = [x_start, x_end]; their heights do not matter.

An arrow shot straight up from position x bursts every balloon with x_start <= x <= x_end and keeps flying. Return the fewest arrows that burst all the balloons.

Examples

Input: points = [[1,5],[3,7],[8,12],[10,11]]
Output: 2

An arrow at 4 bursts the first two balloons, and an arrow at 10 bursts the last two.

Input: points = [[1,2],[2,3],[3,4]]
Output: 2

An arrow at 2 bursts the first two (touching ends count); the third needs a second arrow at 3 or 4.

Input: points = [[-4,-1],[0,3]]
Output: 2

The two balloons share no position, so each needs its own arrow.

Constraints

Approaches

Follow-up

What if you also had to report where to shoot? The greedy already knows: every arrow it counts is fired at the end of the balloon that triggered it, so collecting those ends gives one optimal set of positions.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…