Minimum Number of Arrows to Burst Balloons
Problem
Each balloon spans a horizontal range [xstart, xend]. An arrow shot straight up at position x bursts every balloon whose range includes x, endpoints included. Return the minimum number of arrows needed to burst all balloons.
Examples
Constraints
- • 1 <= points.length <= 10^5
- • -2^31 <= xstart < xend <= 2^31 - 1
Hints & approach
Hint 1
Shooting at the right edge of some balloon is never worse than shooting further left.
Hint 2
Sort by end and shoot at the end of the first balloon not yet burst.
Approachtry the hints first
Sort balloons by their end. Fire the first arrow at the end of the first balloon. For each subsequent balloon, if its start is at or before the last arrow position it is already burst; otherwise fire a new arrow at its end. Placing each arrow as far right as possible covers the most later balloons. Compare values rather than subtracting to avoid overflow in languages with fixed-width integers.
Time O(n log n) · Space O(1)