#853
Car Fleet
medium· Stackruns: 0There are n cars driving to the same destination along a one-lane road. Given the integer target distance and arrays position and speed for each car, return the number of car fleets that will arrive at the destination. A car fleet is a group of one or more cars driving at the same speed; a faster car cannot pass a slower one but can catch up and join its fleet.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 367
class Solution:
def carFleet(
self, target: int, position: list[int], speed: list[int]
) -> int:
cars = sorted(zip(position, speed), reverse=True)
stack = []
for pos, spd in cars:
time = (target - pos) / spd
if not stack or time > stack[-1]:
stack.append(time)
return len(stack)
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.