Car Fleet
Several cars drive in the same direction along a one-lane road towards a destination target miles from the start. Car i begins at position[i] (a distance from the start, always less than target) and drives at a constant speed of speed[i] miles per hour.
Cars cannot pass one another. If a faster car catches up to a slower car ahead of it, it has to slow down and follow at the same speed, and from then on they travel together. A group of cars driving together is a fleet; a single car by itself is also a fleet. A car that catches another exactly at the destination counts as part of its fleet.
Return the number of fleets that arrive at target. All starting positions are different.
Constraints
0 <= position.length === speed.length <= 10^50 < target <= 10^60 <= position[i] < target, all positions distinct1 <= speed[i] <= 10^6
Examples
Example 1
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] Output: 3
The cars at 10 and 8 each need 1 time unit, so they arrive together. The car at 5 needs 7, and the car at 3 would need only 3 but is stuck behind the car at 5, so those two form one fleet. The car at 0 needs 12 and never catches anyone. Three fleets.
Example 2
Input: target = 10, position = [3], speed = [3] Output: 1
A single car is a single fleet.
Example 3
Input: target = 100, position = [0,2,4], speed = [4,2,1] Output: 1
The car behind is faster at each step, but cars cannot pass, so all of them reach the target together as one fleet.
JavaScript you'll use: Stacks, queues and deques Sorting and comparators
/**
* @param {number} target
* @param {number[]} position
* @param {number[]} speed
* @return {number}
*/
function carFleet(target, position, speed) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.