mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #203 from LiangDazhu/patch-11
添加 0452.用最少数量的箭引爆气球 python版本
This commit is contained in:
@ -70,7 +70,7 @@
|
||||
|
||||
其实都可以!只不过对应的遍历顺序不同,我就按照气球的起始位置排序了。
|
||||
|
||||
既然按照其实位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
|
||||
既然按照起始位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
|
||||
|
||||
从前向后遍历遇到重叠的气球了怎么办?
|
||||
|
||||
@ -167,7 +167,19 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def findMinArrowShots(self, points: List[List[int]]) -> int:
|
||||
if len(points) == 0: return 0
|
||||
points.sort(key=lambda x: x[0])
|
||||
result = 1
|
||||
for i in range(1, len(points)):
|
||||
if points[i][0] > points[i - 1][1]: # 气球i和气球i-1不挨着,注意这里不是>=
|
||||
result += 1
|
||||
else:
|
||||
points[i][1] = min(points[i - 1][1], points[i][1]) # 更新重叠气球最小右边界
|
||||
return result
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user