LC853 - 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 array 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 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

Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]

Output: 3

Explanation:

  • The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target.

  • The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.

  • The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.

Solution

We want to iterate starting from the max position, so first combine position and speed arrays into an array of tuples and sort. Iterating in reverse from the max position, calculate the time to arrival. If eta is less than earlier departed fleets, merge that car into the fleet with the longer eta. Hence, we only add to fleets if the eta duration is longer than the fleet maximum.

Time complexity is O(nlogā”n)O(n \log n) due to the sort. Space complexity is O(n)O(n).

def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
	posSpd = []
	fleets = 0
	fleetMax = 0

	for i in range(len(position)):
		pos, spd = position[i], speed[i]
		posSpd.append((pos, spd))		
	posSpd.sort()

	for i in range(len(posSpd)-1, -1, -1):
		pos, spd = posSpd[i][0], posSpd[i][1]
		eta = (target - pos)/spd

		if eta > fleetMax:
			fleets += 1
			fleetMax = eta
	
	return fleets

Last updated