There are
You are given two integer array
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.
Output: 3
Explanation:
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 is 3.
Output: 1
Explanation: There is only one car, hence there is only one fleet.
Output: 1
Explanation:
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.
Constraints:
n == position.length == speed.length 1 <= n <= 105 0 < target <= 106 0 <= position[i] < target - All the values of
position are unique. 0 < speed[i] <= 106
Contents
Solution 1 - Using Sorting and Stack Solution 2 - Pre compute the time it takes to reach destination for all positions
In this problem statement, it is mentioned that the cars are going in a single lane road, this means that if there is a slower ahead of a faster car, then the faster car will join fleet along with the slower car.
So, in this approach, since we dont know which car the is closest one to the destination, whether it is a faster car or slower car, previous position car
can only either join fleet along with the closest car to destination or it will form a new fleet. Inorder to achieve this, we are going to sort the
Let's look at an example,
-
After sorting the
position andspeed arrays as a pair of numbers, it looks like this[ (10, 2) , (8, 4), (5, 1), (3, 3), (0, 1) ] -
Car at position
10 will reach destination12 intime = (target - position) / speed =12 - 10 / 2 = 1. So, we will add this to the stack, so stack =[1] . -
Car at position
8 will reach destination12 in time12 - 8 / 4 = 1. Now, we will check the the top element in stack, it is1 , same as current, so this car will join the fleet, so we will skip this position. -
Car at position
5 will reach destination12 in time12 - 5 / 1 = 7. Now, we will check the the top element in stack, it is1 , it is less than current, so we will add this to the stack, since it wont be able to catch the fleet infront of it. so stack =[1, 7] . -
Car at position
3 will reach destination12 in time12 - 3 / 3 = 4. Now, we will check the the top element in stack, it is7 , it is greater than current, so we will skip this position, since it will join the fleet infront of it. -
Car at position
0 will reach destination12 in time12 - 0 / 1 = 12. Now, we will check the the top element in stack, it is7 , it is less than current, so we will add this to the stack, since it wont be able to catch the fleet infront of it. so stack =[1, 7, 12] . - So, as you can see, there are total 3 fleets in total.
Complexity Analysis:
Time complexity: All the above operations runs in O(n log n) time since we are sorting the
Space complexity: O(n) for storing stack elements into stack.
In this approach, we are going to pre-compute time takes for each position to reach destination and store it in a temporary array
Let's look at implementation steps with an example,
-
First, declare a temporary array with size as
destination , since we are going to pre-compute time it takes to reach destination for every position inposition array. -
Compute
time[i] = distace[i] / speed[i] , so thetime array will be[12.0, 0.0, 0.0, 3.0, 0.0, 7.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0] -
Car at position
10 will reach destination12 intime = (target - position) / speed =12 - 10 / 2 = 1. So, we will add this to the stack, so stack =[1] . -
Now, declare two variables
previous to store time value, andresult to store total number of fleets that it will form. -
Then, loop through
time array from right to left and check if the current element is less than or equal toprevious , this means that the car on the back is going at fast speed that current one, but the way is a single line road, so this is going to the join the fleet, so we will ignore that element. otherwise, update theprevious with current value and also incrementresult since it will form a new fleet. For example in above array car at position10 and the car at position8 are going to form a fleet, and the car at position5 is going to another fleet since it is greater than all of its right side elements.
Complexity Analysis:
Time complexity: All the above operations runs in O(n) time, where
Space complexity: O(n) for the temporary
Above implementations source code can be found at