Car Fleet - Problem

Imagine cars racing on a highway toward a finish line! ๐Ÿ

There are n cars positioned at different miles from the start (mile 0), all heading toward a target destination. Each car has its own starting position[i] and travels at a constant speed[i] miles per hour.

Here's the twist: Cars cannot overtake each other! If a faster car catches up to a slower one, it must slow down and form a car fleet - traveling together at the speed of the slowest car in the group.

Your mission: Determine how many separate car fleets will arrive at the destination. A fleet can be a single car or multiple cars traveling together.

Key rules:

  • Cars maintain their lanes (no passing)
  • When cars meet, they form a fleet at the slower speed
  • Even if cars meet exactly at the target, they're considered one fleet

Input & Output

example_1.py โ€” Basic Fleet Formation
$ Input: target = 12 position = [10,8,0,5,3] speed = [2,4,1,1,3]
โ€บ Output: 3
๐Ÿ’ก Note: Car at position 10 (speed 2): time = (12-10)/2 = 1. Car at position 8 (speed 4): time = (12-8)/4 = 1. These form one fleet. Car at position 5 (speed 1): time = (12-5)/1 = 7. Car at position 3 (speed 3): time = (12-3)/3 = 3. Car at position 0 (speed 1): time = (12-0)/1 = 12. Cars at positions 5,3,0 each form separate fleets since faster cars behind cannot catch up.
example_2.py โ€” Single Fleet
$ Input: target = 10 position = [3] speed = [3]
โ€บ Output: 1
๐Ÿ’ก Note: Only one car exists, so it forms exactly one fleet by itself.
example_3.py โ€” No Fleet Formation
$ Input: target = 100 position = [0,2,4] speed = [4,2,1]
โ€บ Output: 1
๐Ÿ’ก Note: Car at 0 (speed 4): time = 100/4 = 25. Car at 2 (speed 2): time = 98/2 = 49. Car at 4 (speed 1): time = 96/1 = 96. The fastest car catches up to all others, forming one large fleet.

Constraints

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

Visualization

Tap to expand
Car Fleet Formation VisualizationTargetStartApos=10, spd=2time=1.0Bpos=8, spd=4time=1.0Cpos=5, spd=1time=7.0Dpos=3, spd=3time=3.0Epos=0, spd=1time=12.0Fleet Formation AnalysisFleet 1:Cars A & B (both arrive at time 1.0)Fleet 2:Car D (arrives at time 3.0, can't catch Fleet 1)Fleet 3:Car C (arrives at time 7.0, can't catch Fleet 2)Fleet 4:Car E (arrives at time 12.0, can't catch Fleet 3)Answer: 4 fleets will arrive at the destination
Understanding the Visualization
1
Initial Setup
Cars start at different positions with different speeds, all heading to the same target
2
Calculate Arrival Times
Each car has a predicted arrival time based on distance and speed
3
Process from Front
Starting from cars closest to target, check if cars behind can catch up
4
Fleet Formation
Cars that arrive at same time or later form fleets with slower cars ahead
Key Takeaway
๐ŸŽฏ Key Insight: Sort cars by position and process from closest to target. Cars that take longer to arrive than previous fleets cannot be caught up to, forming new fleets!
Asked in
Amazon 45 Google 38 Microsoft 28 Meta 22
42.0K Views
High Frequency
~25 min Avg. Time
1.9K 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