題目會給定一個輸入陣列points,陣列元素都是一組pair,
points[i] = [starti, endi],分別代表每顆氣球的左邊界,和右邊界。
假設弓箭射出去後,動能不會減弱,沿路上的氣球都會被射穿。
請問最少需要幾隻弓箭,才可以射穿所有氣球?
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].
Constraints:
1 <= points.length <= 10^5
輸入陣列長度介於1~十萬之間。
points[i].length == 2
每個陣列元素都是一組Pair
-2^31 <= x
start
< x
end
<= 2^31 - 1
每顆氣球的左邊界、右邊界介於 32bit整數範圍之內。
和前一題 排序應用題: 不重複的區間 有異曲同工之妙。
想要讓弓箭的使用數量最精簡,就是用同一支箭去射穿區間有重複的氣球。
可以想到先讓所有區間都依終點的大小作排序。