Leetcode 每日一题
题目链接: 452. 用最少数量的箭引爆气球
难度: 中等
解题思路: 先按照区间的左端点为第一关键字,右端点为第二关键字升序排序。从左到右若某个点的左端点大于前一个的右端点则说明要使用一只箭,同时太的更新前一个的右端点为当前的最小右端点!
题解:
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
if points == []:
return 0
# 现根据左端点排序,再根据右端点排序
points.sort(key = lambda x : (x[0], x[1]))
print(points)
cnt = 1
for i in range(1, len(points)):
if points[i][0] > points[i - 1][1]:
cnt += 1
else:
points[i][1] = min(points[i][1], points[i - 1][1])
return cnt