Description
There are n cars going to the same destination along a one-lane road. Return the number of car fleets that will arrive at the destination.
Examples
target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]3Cars form 3 fleets.
target = 20, position = [15, 12, 18, 6], speed = [1, 3, 2, 4]2Calculate time to reach target for each car: Car at position 15 with speed 1 takes (20-15)/1 = 5 time units. Car at position 12 with speed 3 takes (20-12)/3 = 2.67 time units. Car at position 18 with speed 2 takes (20-18)/2 = 1 time unit. Car at position 6 with speed 4 takes (20-6)/4 = 3.5 time units. Sorting by position from target: [18,15,12,6] with times [1,5,2.67,3.5]. The car at 18 arrives first in 1 unit. The car at 15 takes 5 units, so it forms its own fleet. The car at 12 would arrive in 2.67 units but gets blocked by the car at 15, joining its fleet. The car at 6 takes 3.5 units, also joining the fleet led by car at 15. This results in 2 fleets total.
target = 8, position = [1, 4, 7], speed = [2, 1, 1]3Calculate time to reach target: Car at position 1 with speed 2 takes (8-1)/2 = 3.5 time units. Car at position 4 with speed 1 takes (8-4)/1 = 4 time units. Car at position 7 with speed 1 takes (8-7)/1 = 1 time unit. Sorting by position from target: [7,4,1] with times [1,4,3.5]. Each car arrives at different times without any car catching up to block another, so all 3 cars form separate fleets. The car at position 7 is closest and fastest (1 time unit), the car at position 1 is farthest but fast enough (3.5 time units), and the car at position 4 is slowest (4 time units).
Constraints
- •
n == position.length == speed.length - •
1 ≤ n ≤ 10⁵