 All Problems
Car Fleet
medium
stack
sorting
monotonic stack
google
amazon

There are n cars going to the same destination along a one-lane road. The destination is target miles away.

You are given two arrays position and speed, both of length n, where position[i] is the position of the i-th car and speed[i] is its speed in miles per hour.

A car can never pass another car; if it catches up, they form a fleet and travel at the slower car's speed. A fleet is also a single car by itself.

Return the number of fleets that will arrive at the destination.

Example 1:

Input:
5
10 8 0 5 3
2 4 1 1 3
12
Output: 3

Example 2:

Input:
1
0
1
10
Output: 1

Constraints:

  • 1 ≤ n ≤ 10⁵
  • 0 ≤ position[i] < target
  • 0 < speed[i] ≤ 10⁶
  • All position values are unique.

Input format: First line: n. Second line: space-separated positions. Third line: space-separated speeds. Fourth line: target.

Output format: A single integer (number of fleets).

Run to check your code against the sample cases, or submit to run every case