LeetCode 853. [Python] Car Fleet
原题链接
中等
作者:
徐辰潇
,
2021-04-14 23:11:36
,
所有人可见
,
阅读 372
class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
#TC: O(n log n)
#SC: O(n)
#n =len(position)
#Just consider the time to target of each car
if len(position) == 0:
return 0
car = []
for i in range(len(position)):
car.append((position[i], (target - position[i]) / speed[i]))
car.sort(reverse = True)
res = 1
pivot = car[0][1]
for i in range(1, len(position)):
if car[i][1] <= pivot:
continue
else:
pivot = car[i][1]
res += 1
return res