Car Fleet - Problem

There are n cars at given miles away from the starting mile 0, traveling to reach the mile target. You are given two integer arrays position and speed, both of length n, where position[i] is the starting mile of the ith car and speed[i] is the speed of the ith car in miles per hour.

A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car. A car fleet is a single car or a group of cars driving next to each other. The speed of the car fleet is the minimum speed of any car in the fleet.

If a car catches up to a car fleet at the mile target, it will still be considered as part of the car fleet. Return the number of car fleets that will arrive at the destination.

Input & Output

Example 1 — Multiple Fleets
$ Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
💡 Note: Car at position 10 (speed 2) takes 1.0 time. Car at position 8 (speed 4) takes 1.0 time, joining the first fleet. Car at position 5 (speed 1) takes 7.0 time, forming a new fleet. Cars at positions 3 and 0 join existing fleets based on their arrival times.
Example 2 — Single Fleet
$ Input: target = 10, position = [3], speed = [3]
Output: 1
💡 Note: Only one car, so it forms one fleet by itself.
Example 3 — No Catching Up
$ Input: target = 100, position = [0,2,4], speed = [4,2,1]
Output: 1
💡 Note: All cars take 25.0, 49.0, and 96.0 time respectively. The fastest car catches up to others, forming one large fleet.

Constraints

  • n == position.length == speed.length
  • 1 ≤ n ≤ 105
  • 0 < target ≤ 106
  • 0 ≤ position[i] < target
  • All the values of position are unique
  • 0 < speed[i] ≤ 106

Visualization

Tap to expand
Car Fleet Problem INPUT Target: 12 pos:0 pos:3 pos:5 pos:8 pos:10 Position Array: 10 8 0 5 3 Speed Array: 2 4 1 1 3 Time to Target: time = (target-pos)/speed 1.0 1.0 12.0 7.0 3.0 target = 12 ALGORITHM STEPS 1 Sort by Position Descending (closest to target first) p:10 t:1 p:8 t:1 p:5 t:7 p:3 t:3 p:0 t:12 2 Calculate Arrival Time time = (target - pos) / speed 3 Track Max Time If time > maxTime: new fleet 4 Count Fleets Increment count for each new fleet Car(10): t=1.0 > 0 [OK] fleet=1 Car(8): t=1.0 = 1 merge Car(5): t=7.0 > 1 [OK] fleet=2 Car(3): t=3.0 < 7 merge Car(0): t=12 > 7 [OK] fleet=3 FINAL RESULT 3 Car Fleets Formed Fleet 1 (arrives at t=1.0) 10 8 Fleet 2 (arrives at t=7.0) 5 3 Fleet 3 (arrives at t=12.0) 0 Output: 3 car fleets Key Insight: Sort cars by position (descending). A car behind cannot pass one ahead, so if it arrives sooner, it merges into the fleet ahead. Count new fleets when arrival time exceeds the current maximum. Time Complexity: O(n log n) | Space Complexity: O(n) TutorialsPoint - Car Fleet | Optimal Solution (Stack-based Approach)
Asked in
Amazon 45 Google 38 Microsoft 32 Facebook 28
168.4K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen