oneby7

Greedy The best choice now

505. Minimum Number of Arrows to Burst Balloons

Medium

Some balloons hang above a straight line, the x-axis. You are given an array points, where points[i] = [xstart, xend] means that balloon i stretches over every position from xstart to xend.

An arrow shot straight up from position x bursts every balloon with xstart ≤ x ≤ xend. The arrow keeps flying after it bursts a balloon, so one arrow can burst many. Return the fewest arrows you need to burst every balloon.

An arrow that only touches the edge of a balloon still bursts it. So an arrow at 5 bursts both [2,5] and [5,9]. A balloon can also be a single point, with xstart = xend.

Example 1

Input: points = [[3, 8], [1, 4], [10, 14], [7, 11], [9, 12]]

Output: 2

Explanation: An arrow at 4 bursts [1,4] and [3,8], and an arrow at 11 bursts [7,11], [9,12] and [10,14]. No position lies inside all five balloons, so one arrow is not enough.

Example 2

Input: points = [[2, 5], [5, 9], [9, 12], [12, 15]]

Output: 2

Explanation: An arrow at 5 bursts the first two balloons, which touch there. An arrow at 12 bursts the other two.

Example 3

Input: points = [[2147483647, 2147483647], [-2147483648, -2147483648]]

Output: 2

Explanation: Each balloon is a single point, one at each end of the range, so each one needs its own arrow.

Check your understanding

Work out the answer for this input before you write any code.

points = [[1, 10], [2, 3], [5, 6], [8, 9]]

Constraints

  • 1 ≤ points.length ≤ 105
  • points[i].length = 2
  • -231 ≤ xstart ≤ xend ≤ 231 − 1

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.