#452

Minimum Number of Arrows to Burst Balloons

specialist · 690 · lc medium +30 · verified · 61.2% accepted · 8,062 likes · top 61%

play →

Description

Balloons span horizontal intervals on a wall: balloon i occupies [points[i][0], points[i][1]]. Shooting an arrow vertically at position x pops every balloon whose interval contains x (inclusive on both ends).

Given points, return the minimum number of arrows needed to pop all balloons.

Example 1:

Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].

Example 2:

Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4
Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.

Example 3:

Input: points = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].

Code

1
2
3