There are n cars going to the same destination along a one-lane road. The destination is target miles away.
You are given two integer array position and speed, both of length n, where position[i] is the position of the ith car and speed[i] is the speed of the ith car (in miles per hour).
A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper at the same speed. The faster car will slow down to match the slower car’s speed. The distance between these two cars is ignored (i.e., they are assumed to have the same position).
A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.
If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.
Return the number of car fleets that will arrive at the destination.
Input: target =12, position =[10,8,0,5,3], speed =[2,4,1,1,3]Output: 3Explanation:
The cars starting at 10(speed 2) and 8(speed 4) become a fleet, meeting each other at 12.The car starting at 0 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.Note that no other cars meet these fleets before the destination, so the answer is3.
Example 2:
1
2
3
Input: target =10, position =[3], speed =[3]Output: 1Explanation: There is only one car, hence there is only one fleet.
Example 3:
1
2
3
4
5
Input: target =100, position =[0,2,4], speed =[4,2,1]Output: 1Explanation:
The cars starting at 0(speed 4) and 2(speed 2) become a fleet, meeting each other at 4. The fleet moves at speed 2.Then, the fleet(speed 2) and the car starting at 4(speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
As illustrated in the example, Vehicle 1 is closest to the target and will lead a fleet since no vehicle behind it can overtake it. Vehicle 2, starting farther away but moving faster, would ideally reach the target first if Vehicle 1 wasn’t ahead. However, Vehicle 2 will join the fleet led by Vehicle 1 upon catching up. Vehicle 3 arrives later (at t2 time) than the fleet in front and thus forms its own fleet.
We can take following steps:
Sort the vehicles by their (position, speed) pairs.
The first vehicle will form its own fleet. From the second vehicle onward, compare each vehicle’s ideal arrival time with the top of the stack, representing the latest arrival time of the current fleet.
If a vehicle arrives earlier, it joins the existing fleet.
Otherwise, it starts a new fleet, and its arrival time is added to the stack.
The stack then contains the distinct arrival times, representing the number of fleets.
classSolution {
publicintcarFleet(int target, int[] pos, int[] speed) {
int n = pos.length;
double[][] arrivalTimes =newdouble[n][2];
// Calculate arrival timesfor (int i = 0; i < n; i++) {
arrivalTimes[i]=newdouble[] {pos[i], (double)(target - pos[i]) / speed[i]};
}
// Sort the cars by their positions Arrays.sort(arrivalTimes, (a, b) -> Double.compare(a[0], b[0]));
double cur = 0;
int ans = 0;
// Traverse the sorted array from the farthest carfor (int i = n - 1; i >= 0; i--) {
if (arrivalTimes[i][1]> cur) {
cur = arrivalTimes[i][1];
ans++;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defcarFleet(self, target: int, position: List[int], speed: List[int]) -> int:
n = len(position)
arrival_times = [(position[i], (target - position[i]) / speed[i]) for i in range(n)]
# Sort the array by positions arrival_times.sort(key=lambda x: x[0])
cur =0.0 ans =0# Traverse the sorted list from the farthest carfor pos, time in reversed(arrival_times):
if time > cur:
cur = time
ans +=1return ans
classSolution {
publicintcarFleet(int target, int[] pos, int[] speed) {
Map<Integer, Double> map =new TreeMap<>(Collections.reverseOrder());
// Calculate arrival times and store in TreeMapfor (int i = 0; i < pos.length; i++) {
map.put(pos[i], (double)(target - pos[i]) / speed[i]);
}
int res = 0;
double cur = 0;
// Iterate over the values in the TreeMapfor (double time : map.values()) {
if (time > cur) {
cur = time;
res++;
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defcarFleet(self, target: int, position: List[int], speed: List[int]) -> int:
# Calculate arrival times and store in a sorted dictionary arrival_times = {pos: (target - pos) / speed for pos, speed in zip(position, speed)}
sorted_arrival_times = collections.OrderedDict(sorted(arrival_times.items(), key=lambda item: item[0], reverse=True))
res =0 cur =0.0# Iterate over the values in the sorted dictionaryfor time in sorted_arrival_times.values():
if time > cur:
cur = time
res +=1return res