Skip to content

Commit 1b206a7

Browse files
Merge pull request youngyangyang04#1444 from silaslll/patch-2
Update 0452.用最少数量的箭引爆气球.md
2 parents 02d318d + 9f38af3 commit 1b206a7

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

problems/0452.用最少数量的箭引爆气球.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,28 @@ public:
136136

137137
### Java
138138
```java
139+
/**
140+
时间复杂度 : O(NlogN) 排序需要 O(NlogN) 的复杂度
141+
142+
空间复杂度 : O(logN) java所使用的内置函数用的是快速排序需要 logN 的空间
143+
*/
139144
class Solution {
140145
public int findMinArrowShots(int[][] points) {
141146
if (points.length == 0) return 0;
142-
Arrays.sort(points, (o1, o2) -> Integer.compare(o1[0], o2[0]));
143-
147+
//用x[0] - y[0] 会大于2147483647 造成整型溢出
148+
Arrays.sort(points, (x, y) -> Integer.compare(x[0], y[0]));
149+
//count = 1 因为最少需要一个箭来射击第一个气球
144150
int count = 1;
145-
for (int i = 1; i < points.length; i++) {
146-
if (points[i][0] > points[i - 1][1]) {
151+
//重叠气球的最小右边界
152+
int leftmostRightBound = points[0][1];
153+
//如果下一个气球的左边界大于最小右边界
154+
if (points[i][0] > leftmostRightBound ) {
155+
//增加一次射击
147156
count++;
157+
leftmostRightBound = points[i][1];
158+
//不然就更新最小右边界
148159
} else {
149-
points[i][1] = Math.min(points[i][1],points[i - 1][1]);
160+
leftmostRightBound = Math.min(leftmostRightBound , points[i][1]);
150161
}
151162
}
152163
return count;

0 commit comments

Comments
 (0)