leetle

Daily challenge for 2025-11-25

329. Car Fleet

There are n cars going to the same destination along a one-lane road. The destination is target miles away.

You are given two integer arrays position and speed, both of length n, where position[i] is the position of the ith car and speed[i] is its speed (in miles per hour).

A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper at the same speed. The faster car will slow down to match the slower car's speed. The distance between these two cars is ignored (they are assumed to be at the same position).

A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.

If a car catches up to a car fleet at the destination point, it will still be considered as one car fleet.

Return the number of car fleets that will arrive at the destination.

Example:
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3

Explanation: The cars starting at 10 (time=1) and 8 (time=1) form a fleet. The car starting at 0 (time=12) is a fleet. The cars starting at 5 (time=7) and 3 (time=3) form a fleet.

Run this challenge interactively in Python or JavaScript when the app loads.