Merge pull request #1444 from silaslll/patch-2

Update 0452.用最少数量的箭引爆气球.md
This commit is contained in:
程序员Carl 2022-07-12 08:26:53 +08:00 committed by GitHub
commit 1b206a742f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 5 deletions

View File

@ -136,17 +136,28 @@ public:
### Java
```java
/**
时间复杂度 : O(NlogN) 排序需要 O(NlogN) 的复杂度
空间复杂度 : O(logN) java所使用的内置函数用的是快速排序需要 logN 的空间
*/
class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length == 0) return 0;
Arrays.sort(points, (o1, o2) -> Integer.compare(o1[0], o2[0]));
//用x[0] - y[0] 会大于2147483647 造成整型溢出
Arrays.sort(points, (x, y) -> Integer.compare(x[0], y[0]));
//count = 1 因为最少需要一个箭来射击第一个气球
int count = 1;
for (int i = 1; i < points.length; i++) {
if (points[i][0] > points[i - 1][1]) {
//重叠气球的最小右边界
int leftmostRightBound = points[0][1];
//如果下一个气球的左边界大于最小右边界
if (points[i][0] > leftmostRightBound ) {
//增加一次射击
count++;
leftmostRightBound = points[i][1];
//不然就更新最小右边界
} else {
points[i][1] = Math.min(points[i][1],points[i - 1][1]);
leftmostRightBound = Math.min(leftmostRightBound , points[i][1]);
}
}
return count;