philosyang.com

853. Car Fleet

 1class Solution:
 2    def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
 3        # ideology:
 4        # if a car takes less time to reach the target
 5        # compared to the car in front
 6        # that means they will be in a fleet midway
 7
 8        pos_time = [(p, (target - p) / s) for p, s in zip(position, speed)]
 9        pos_time.sort(reverse=True)
10
11        fleet_count = 0
12        last_duration = 0.0
13
14        for pos, time in pos_time:
15            if time > last_duration:
16                fleet_count += 1
17                last_duration = time
18
19        return fleet_count

#Neetcode150 #Array #Sorting #Python