-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00853-car_fleet.py
43 lines (30 loc) · 938 Bytes
/
00853-car_fleet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 853: Car Fleet
# https://leetcode.com/problems/car-fleet/
import operator
class Solution:
class car:
def __init__(self, p: int, s: int):
self.p = p
self.s = s
# SOLUTION
def carFleet(self, target: int, position: list[int], speed: list[int]) -> int:
cars: list[car] = []
n = len(position)
for i in range(n):
cars.append(self.car(position[i], speed[i]))
cars.sort(key=operator.attrgetter('p'))
s: list[float] = []
for i in range(n):
time = (target - cars[i].p) / cars[i].s
while len(s)!=0 and time >= s[-1]: s.pop()
s.append(time)
return len(s)
if __name__ == "__main__":
o = Solution()
# INPUT
target: int = 12
position: list[int] = [10,8,0,5,3]
speed: list[int] = [2,4,1,1,3]
# OUTPUT
result = o.carFleet(target, position, speed)
print(result)