Skip to content

Commit bf0ddd9

Browse files
committed
452. 用最少数量的箭引爆气球
1 parent 10ef206 commit bf0ddd9

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.gatsby;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName: _452MinimumNumberOfArrowsToBurstBalloons
7+
* @Description: 452. 用最少数量的箭引爆气球
8+
* @author: Gatsby
9+
* @date: 2022/7/25 14:10
10+
*/
11+
12+
public class _452MinimumNumberOfArrowsToBurstBalloons {
13+
public int findMinArrowShots(int[][] points) {
14+
Arrays.sort(points, (int[] point1, int[] point2) -> {
15+
// case [[-2147483646,-2147483645],[2147483646,2147483647]]
16+
// 如果使用a-b的话会导致溢出
17+
return Integer.compare(point1[1], point2[1]);
18+
});
19+
int res = 1; // 至少要有一只箭
20+
int maxCover = points[0][1]; // 以最右端为箭的最远端,判断下一个气球的最左边能不能被cover
21+
22+
for (int[] point : points) {
23+
if (point[0] <= maxCover) {
24+
continue;
25+
} else {
26+
res++;
27+
maxCover = point[1];
28+
}
29+
}
30+
return res;
31+
}
32+
}
33+
34+

0 commit comments

Comments
 (0)