There are n
cars at given miles away from the starting mile 0, traveling to reach the mile target
.
You are given two integer array position
and speed
, both of length n
, where position[i]
is the starting mile of the i<sup>th</sup>
car and speed[i]
is the speed of the i<sup>th</sup>
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 car or 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.
Example 1:
target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Example 2:
target = 10, position = [3], speed = [3]
Output: 1
Example 3:
target = 100, position = [0,2,4], speed = [4,2,1]
Output: 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to the car fleet problem is like watching each car individually and tracking how it moves. We simulate the entire journey for every car to see when and if they form a fleet. This involves calculating each car's arrival time at the destination based on its initial position and speed.
Here's how the algorithm would work step-by-step:
def car_fleet_brute_force(target, position, speed):
car_count = len(position)
arrival_times = []
for i in range(car_count):
arrival_time = (target - position[i]) / speed[i]
arrival_times.append(arrival_time)
# Sort cars based on their position
car_positions_and_times = sorted(zip(position, arrival_times))
fleets = 0
# If there are no cars, there are no fleets
if not car_positions_and_times:
return 0
fleets = 1
leader_position, leader_arrival_time = car_positions_and_times[-1]
# Iterate from the back to the front
for i in range(car_count - 2, -1, -1):
current_position, current_arrival_time = car_positions_and_times[i]
# Check if the current car catches up to the leader car
if current_arrival_time > leader_arrival_time:
# If it does not catch up, it forms a new fleet
fleets += 1
leader_position, leader_arrival_time = current_position, current_arrival_time
return fleets
The key to solving the car fleet problem efficiently is to figure out which cars will form a fleet and when they will arrive. The cars that arrive at the destination together are the ones that form a fleet. We can determine fleet formation by calculating arrival times.
Here's how the algorithm would work step-by-step:
def carFleet(target, position, speed):
cars = sorted(zip(position, speed))
arrival_times = [(target - position) / speed for position, speed in cars]
number_of_fleets = 0
max_arrival_time = 0.0
for arrival_time in reversed(arrival_times):
# If current car arrives later, it forms a new fleet.
if arrival_time > max_arrival_time:
number_of_fleets += 1
max_arrival_time = arrival_time
return number_of_fleets
Case | How to Handle |
---|---|
Empty positions or speeds array | Return 0 immediately as no cars exist, thus no car fleets. |
Positions and speeds arrays have different lengths | Throw an IllegalArgumentException to signal invalid input. |
Single car; positions array of size 1 | Return 1, as a single car trivially forms a fleet. |
Cars with the same position but different speeds | The faster car will eventually overtake the slower car and merge into the fleet, which needs accurate calculation of arrival times. |
Cars with very large positions and speeds (close to Integer.MAX_VALUE) causing overflow during time calculation | Use long to store intermediate time calculations to prevent integer overflow. |
Cars with speed of 0.0 (zero speed) | Consider these cars as never reaching the target, and exclude them from fleet formation calculation or throw an IllegalArgumentException if zero speed is invalid. |
Cars already at the target position | Treat these cars as arriving immediately (time = 0) and factor them into the fleet count. |
Target position is smaller than some car positions (negative relative distance) | Exclude these cars as they are moving away from the target, or throw an IllegalArgumentException if invalid. |