mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
删除冗余代码,增加详细注释
This commit is contained in:
@ -180,19 +180,25 @@ class Solution:
|
|||||||
```python
|
```python
|
||||||
class Solution: # 不改变原数组
|
class Solution: # 不改变原数组
|
||||||
def findMinArrowShots(self, points: List[List[int]]) -> int:
|
def findMinArrowShots(self, points: List[List[int]]) -> int:
|
||||||
|
if len(points) == 0:
|
||||||
|
return 0
|
||||||
|
|
||||||
points.sort(key = lambda x: x[0])
|
points.sort(key = lambda x: x[0])
|
||||||
sl,sr = points[0][0],points[0][1]
|
|
||||||
|
# points已经按照第一个坐标正序排列,因此只需要设置一个变量,记录右侧坐标(阈值)
|
||||||
|
# 考虑一个气球范围包含两个不相交气球的情况:气球1: [1, 10], 气球2: [2, 5], 气球3: [6, 10]
|
||||||
|
curr_min_right = points[0][1]
|
||||||
count = 1
|
count = 1
|
||||||
|
|
||||||
for i in points:
|
for i in points:
|
||||||
if i[0]>sr:
|
if i[0] > curr_min_right:
|
||||||
count+=1
|
# 当气球左侧大于这个阈值,那么一定就需要在发射一只箭,并且将阈值更新为当前气球的右侧
|
||||||
sl,sr = i[0],i[1]
|
count += 1
|
||||||
|
curr_min_right = i[1]
|
||||||
else:
|
else:
|
||||||
sl = max(sl,i[0])
|
# 否则的话,我们只需要求阈值和当前气球的右侧的较小值来更新阈值
|
||||||
sr = min(sr,i[1])
|
curr_min_right = min(curr_min_right, i[1])
|
||||||
return count
|
return count
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
### Go
|
### Go
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user