Skip to content
BytePatterns

Fewest Shots to Burst Every Balloon

MediumIntervals#intervals#greedy#sort-by-end~25m

Problem

Balloons are taped to a wall, and each one spans the horizontal range [start, end]. A vertical shot fired at position x bursts every balloon with start less than or equal to x and x less than or equal to end. Return the fewest shots that burst every balloon.

Examples

Input:  balloons = [[10, 16], [2, 8], [1, 6], [7, 12]]
Output: 2
Why:    a shot at 6 bursts [2, 8] and [1, 6], and a shot at 12 bursts [7, 12] and [10, 16]
Input:  balloons = [[1, 2], [3, 4], [5, 6], [7, 8]]
Output: 4
Why:    no two balloons overlap
Input:  balloons = [[1, 2], [2, 3], [3, 4], [4, 5]]
Output: 2
Why:    edge case, balloons that only touch at an end can share a shot, at 2 and at 4

Hints

0 / 3

Stuck on the idea rather than the code? Interval Scheduling covers it.