Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加0452.用最少数量的箭引爆气球,python,基于右侧坐标排序的算法,节约表达式中的判断步骤 #2890

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions problems/0452.用最少数量的箭引爆气球.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ class Solution: # 不改变原数组
curr_min_right = min(curr_min_right, i[1])
return count
```
```python
class Solution: # 不改变原数组,基于右侧坐标排序
def findMinArrowShots(self, points: List[List[int]]) -> int:
# 题目说明points列表长度至少为1
if len(points) == 1:
return 1

# 根据右侧坐标排序
points.sort(key=lambda x: x[1])

# 初始化
arrow = 0
curr_min_right = float('-inf')

# 根据右侧坐标排序后,更新curr_min_right时的point[1]自动保证为剩余气球中最小的
# 右侧坐标值,节约else表达式中的判断步骤
for point in points:
if point[0] > curr_min_right:
arrow += 1
curr_min_right = point[1]

return arrow
```
### Go
```go
func findMinArrowShots(points [][]int) int {
Expand Down