853. Car Fleet
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 14, 2024
Last updated : July 01, 2024
Related Topics : Array, Stack, Sorting, Monotonic Stack
Acceptance Rate : 52.49 %
class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
stk = sorted([(pos, spd) for pos, spd in zip(position, speed)], key=lambda x: abs(x[0] - target), reverse=True)
countFleets = 1
currPos, currSpd = stk.pop()
timeTaken = (target - currPos) / currSpd
longestTimeTaken = timeTaken
while stk :
currPos, currSpd = stk.pop()
timeTaken = (target - currPos) / currSpd
if (not timeTaken <= longestTimeTaken) :
countFleets += 1
longestTimeTaken = timeTaken
return countFleets