Car fleets
Mediumstacksortingmonotonic stack
Cars head to the same target on a one-lane road. Car i starts at position[i] with speed speed[i]. No passing: a car that catches the one ahead slows to its speed and they form one fleet. Return how many fleets arrive.
Example 1
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] Output: 3
Example 2
Input: target = 10, position = [3], speed = [3] Output: 1
Constraints
- 1 ≤ n ≤ 10⁵
- 0 < target ≤ 10⁶
- 0 ≤ position[i] < target, all distinct
- 1 ≤ speed[i] ≤ 10⁶
Submit checks your solution against 7 tests, including hidden ones.
/**
* @param {int} target
* @param {int[]} position
* @param {int[]} speed
* @return {int}
*/
function carFleet(target, position, speed) {
}