305. Car Fleet

MediumStack

There are n cars at given positions on a single-lane road heading to the same destination target miles away. You are given arrays position and speed, where position[i] and speed[i] are the position and speed of the i-th car. A car can never pass another car ahead of it, but it can catch up and then drive bumper to bumper at the same speed. The faster car slows down to match the car ahead, and the pair forms a single car fleet. A single car is also a fleet. Return the number of car fleets that will arrive at the destination.

Examples

Input: 12 [10,8,0,5,3] [2,4,1,1,3]

Output: 3

Explanation: Cars at 10&8 merge (both arrive at t=1), cars at 5&3 merge, car at 0 is alone: 3 fleets.

Constraints

  • n == position.length == speed.length
  • 1 <= n <= 10^5
  • 0 < target <= 10^6
  • 0 <= position[i] < target
  • All the values of position are unique.
  • 0 < speed[i] <= 10^6
Loading...

Run checks all cases above. Submit evaluates all test cases.