Car Fleet
MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.Cars drive along a one-lane road toward a destination at mile target. Car i starts at mile position[i] and drives at speed[i] miles per hour. A car can never pass the car in front of it: if it catches up, it slows down and the two travel together at the slower speed from then on.
Cars travelling together form a fleet. A single car on its own is also a fleet. If a car catches a fleet exactly at the destination, it still counts as part of that fleet. Return how many fleets arrive at the destination.
target = 12, position = [10, 8, 0, 5, 3], speed = [2, 4, 1, 1, 3] → 3
target = 10, position = [3], speed = [3] → 1
target = 100, position = [0, 2, 4], speed = [4, 2, 1] → 1In the first example the cars at 10 and 8 meet at mile 12, the car at 0 never catches anyone, and the cars at 5 and 3 meet at mile 6.
Constraints
n == position.length == speed.length1 <= n <= 10^50 < target <= 10^60 <= position[i] < target, and all positions are unique.0 < speed[i] <= 10^6