# Car Fleet Problem

## Description

LeetCode Problem 853.

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 i^th car and speed[i] is the speed of the i^th car (in miles per hour).

A car can never pass another car ahead of it, but it can catch up to itand 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.

Example 1:

``````1
2
3
4
5
6
7
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Explanation:
The cars starting at 10 and 8 become a fleet, meeting each other at 12.
The car starting at 0 doesn't catch up to any other car, so it is a fleet by itself.
The cars starting at 5 (speed 3) and 3 (speed 1) 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.
``````

Example 2:

``````1
2
3
Input: target = 10, position = , speed = 
Output: 1
Explanation: 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: 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 <= 10^5
• 0 < target <= 10^6
• 0 <= position[i] < target
• All the values of position are unique.
• 0 < speed[i] <= 10^6

## Sample C++ Code

``````1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
// We need to sort vehicles accoring to the position and also
// store their time required to reach the target.
// We use 'map' here, which will keep the map sorted according to
// the key value (position). Once we have this,
// we iterate over the map from the end to start,
// this is because, first we process the vehicle that is closest
// to the target than latter and so on.
// While iterating we keep track of the vehicle that has reached
// target slowest until now. If the current vehicle requires less or equal time,
// they both will go in same fleet. If the current vehicle requires more time,
// a new fleet will be created containing the current vehicle
// (as it requires more time and will reach later).
int carFleet(int target, vector<int>& position, vector<int>& speed) {
map<int, double> m;
for (int i = 0; i < position.size(); i++) {
double d = (double)(target - position[i]) / speed[i];
m[position[i]] = d;
}
int fleets = 0;
double slowest = 0;
for (auto it = m.rbegin(); it != m.rend(); it++) {
if (slowest < it->second) {
slowest = it->second;
fleets++;
}
}
return fleets;
}
};
``````