150NeetCode 150

26.Car Fleet

MediumStack · 6/7

Problem

There are n cars on a one-lane road heading to the same destination at mile target. Car i starts at position[i] and drives at constant speed[i] miles per hour.

A car can never pass the car in front of it. If it catches up, it slows down and the two drive together bumper-to-bumper as a car fleet (moving at the slower car's speed). A single car is also a fleet. A car that catches a fleet exactly at target still counts as part of that fleet.

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

Examples

Input:  target = 12, position = [10, 8, 0, 5, 3], speed = [2, 4, 1, 1, 3]
Output: 3
Explanation:
  Car at 10 arrives at t=1; car at 8 also reaches 12 at t=1 → one fleet.
  Car at 5 arrives at t=7; car at 3 would arrive at t=3, catches it → one fleet.
  Car at 0 arrives at t=12 and never catches anyone → its own fleet.
Input:  target = 10, position = [3], speed = [3]
Output: 1
Input:  target = 100, position = [0, 2, 4], speed = [4, 2, 1]
Output: 1
Explanation: The car at 4 needs 96 h; the cars behind would be faster, so all merge into one fleet.

Constraints

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

💡 Key Insight

Only arrival times matter: car i would arrive alone at (target - position[i]) / speed[i]. Process cars from the one closest to the target backwards. A car whose solo time is <= the time of the fleet in front of it catches that fleet (and is slowed to it); a car that would take longer can never catch up and starts a new fleet. So after sorting by position, count how many times the arrival time strictly increases.

Brute Force

TimeO(n²)SpaceO(n)

Intuition

A car leads its own fleet iff it is slower to arrive than every car ahead of it — otherwise it would catch the fleet directly ahead (whose arrival time is the maximum among the cars ahead). Check that condition for each car by scanning all cars ahead.

Algorithm

  1. Compute time[i] = (target - position[i]) / speed[i] as a double.
  2. For each car i, check every car j with position[j] > position[i].
  3. If time[i] > time[j] for all such j, car i is a fleet leader → fleets++.
  4. Return fleets.

Visualization

target = 12
car (pos, time): (10, 1) (8, 1) (0, 12) (5, 7) (3, 3)

pos 10: nobody ahead                   → leader
pos 8 : time 1 vs 1 (pos 10) not >     → joins
pos 5 : 7 > 1, 7 > 1                   → leader
pos 3 : 3 > 1, 3 > 1, but 3 < 7        → joins
pos 0 : 12 > all                       → leader      fleets = 3

Code

java
class Solution {
    public int carFleet(int target, int[] position, int[] speed) {
        int n = position.length;
        double[] time = new double[n];
        for (int i = 0; i < n; i++) {
            time[i] = (double) (target - position[i]) / speed[i];
        }
        int fleets = 0;
        for (int i = 0; i < n; i++) {
            boolean leader = true;
            for (int j = 0; j < n; j++) {
                // a car ahead that arrives no earlier would block car i
                if (position[j] > position[i] && time[j] >= time[i]) {
                    leader = false;
                    break;
                }
            }
            if (leader) {
                fleets++;
            }
        }
        return fleets;
    }
}

Complexity

  • Time: O(n²) — every car is compared with every other car.
  • Space: O(n) — the arrival times.

Complexity Comparison

Approach Time Space
Brute Force O(n²) O(n)
Sort + Stack O(n log n) O(n)
Sort + Fleet Time O(n log n) O(n)

Common Pitfalls

  • Computing times with integer division — (12 - 3) / 3 and (12 - 4) / 3 both give 3; cast to double.
  • Processing cars from the back of the road: a car's fate depends on the fleet ahead, so sort by position descending.
  • Using >= when deciding a new fleet: equal arrival times mean the cars meet exactly at the target, which counts as one fleet.
  • Sorting position alone and losing track of which speed belongs to which car — sort pairs or sort indices.