You are given n
cars at different positions heading towards a common target. Each car has a position and a speed. A car fleet is a group of cars where no car can overtake another, but they can catch up and travel together at the speed of the slowest car in the fleet.
Given:
target
: The target mile marker.position
: An array of starting positions for each car.speed
: An array of speeds for each car.Your task is to return the number of car fleets that will reach the target. For example:
Example 1:
target = 12
, position = [10, 8, 0, 5, 3]
, speed = [2, 4, 1, 1, 3]
Output: 3
Explanation:
Example 2:
target = 10
, position = [3]
, speed = [3]
Output: 1
Constraints:
1 <= n <= 10^5
0 < target <= 10^6
0 <= position[i] < target
0 < speed[i] <= 10^6
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. |