leetcode-452. 用最少数量的箭引爆气球

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public int findMinArrowShots(int[][] points) {
PriorityQueue<int[]> queue = new PriorityQueue<>(((o1, o2) -> {
return Integer.compare(o1[0], o2[0]);
}));

for(int[] point : points){
queue.offer(point);
}

int[] arr = null;
int count = 0;
while (!queue.isEmpty()){
if (arr == null){
count ++;
arr = queue.poll();
}else {
// 进行区间缩小
int[] point = queue.poll();
if (arr[1] >= point[0]){
// 有重合区间,此时缩短区间
arr[0] = point[0];
arr[1] = Math.min(arr[1], point[1]);
}else {
count ++;
arr = point;
}
}
}
return count;
}
}