https://leetcode.com/problems/car-fleet/
How many car fleets will arrive at the destination?
Note:
https://leetcode.com/problems/car-fleet/discuss/139999/Easy-understanding-JAVA-TreeMap-Solution-with-explanation-and-comment
X. https://leetcode.com/problems/car-fleet/discuss/139850/C%2B%2BJavaPython-Straight-Forward
https://leetcode.com/articles/car-fleet/
https://leetcode.com/problems/car-fleet/discuss/180287/Java-Priority-Queue-Explained
N
cars are going to the same destination along a one lane road. The destination is target
miles away.
Each car
i
has a constant speed speed[i]
(in miles per hour), and initial position position[i]
miles towards the target along the road.
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 distance between these two cars is ignored - 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.
How many car fleets will arrive at the destination?
Example 1:
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 and 3 become a fleet, meeting each other at 6. Note that no other cars meet these fleets before the destination, so the answer is 3.
Note:
0 <= N <= 10 ^ 4
0 < target <= 10 ^ 6
0 < speed[i] <= 10 ^ 6
0 <= position[i] < target
- All initial positions are different.
If one car catch up the one before it, it means the time it takes to reach the target is shorter than the one in front(if no blocking).
For example:
car A at pos a with speed sa
car B at pos b with speed sb
(b < a < target)
Their distances to the target are (target-a) and (target-b).
If (target-a)/sa > (target-b)/sb it means car A take more time to reach target so car B will catch up car A and form a single group.
For example:
car A at pos a with speed sa
car B at pos b with speed sb
(b < a < target)
Their distances to the target are (target-a) and (target-b).
If (target-a)/sa > (target-b)/sb it means car A take more time to reach target so car B will catch up car A and form a single group.
So we use the distance to target as key and speed as value, iterate through all cars in order of their distances to the target.
keep track of currently slowest one(which might block the car behind), if a car can catch up current slowest one, it will not cause a new group.
Otherwise, we count a new group and update the info of slowest
keep track of currently slowest one(which might block the car behind), if a car can catch up current slowest one, it will not cause a new group.
Otherwise, we count a new group and update the info of slowest
public int carFleet(int target, int[] position, int[] speed) {
TreeMap<Integer, Integer> map = new TreeMap<>();
int n = position.length;
for(int i=0; i<n; ++i){
map.put(target - position[i], speed[i]);
}
int count = 0;
double r = -1.0;
/*for all car this value must > 0, so we can count for the car closeset to target*/
for(Map.Entry<Integer, Integer> entry: map.entrySet()){
int d = entry.getKey(); // distance
int s = entry.getValue(); // speed
double t = 1.0*d/s; // time to target
if(t>r){ // this car is unable to catch up previous one, form a new group and update the value
++count;
r = t;
}
}
return count;
}
X. https://leetcode.com/problems/car-fleet/discuss/139850/C%2B%2BJavaPython-Straight-Forward
Calculate time needed to arrive the target, sort by the start position.
Loop on each car from the end to the beginning.
If another car needs less or equal time than
Otherwise it will become the new slowest car, that is new lead of a car fleet.
Loop on each car from the end to the beginning.
cur
recorde the current biggest time (the slowest).If another car needs less or equal time than
cur
, it can catch up this car.Otherwise it will become the new slowest car, that is new lead of a car fleet.
public int carFleet(int target, int[] pos, int[] speed) {
TreeMap<Integer, Double> m = new TreeMap<>();
for (int i = 0; i < pos.length; ++i) m.put(-pos[i], (double)(target - pos[i]) / speed[i]);
int res = 0; double cur = 0;
for (double time : m.values()) {
if (time > cur) {
cur = time;
res++;
}
}
return res;
}
https://leetcode.com/articles/car-fleet/
Call the "lead fleet" the fleet furthest in position.
If the car
S
(Second) behind the lead car F
(First) would arrive earlier, then S
forms a fleet with the lead car F
. Otherwise, fleet F
is final as no car can catch up to it - cars behind S
would form fleets with S
, never F
.
Algorithm
A car is a
(position, speed)
which implies some arrival time (target - position) / speed
. Sort the cars by position.
Now apply the above reasoning - if the lead fleet drives away, then count it and continue. Otherwise, merge the fleets and continue.
public int carFleet(int target, int[] position, int[] speed) {
int N = position.length;
Car[] cars = new Car[N];
for (int i = 0; i < N; ++i)
cars[i] = new Car(position[i], (double) (target - position[i]) / speed[i]);
Arrays.sort(cars, (a, b) -> Integer.compare(a.position, b.position));
int ans = 0, t = N;
while (--t > 0) {
if (cars[t].time < cars[t - 1].time)
ans++; // if cars[t] arrives sooner, it can't be caught
else
cars[t - 1] = cars[t]; // else, cars[t-1] arrives at same time as cars[t]
}
return ans + (t == 0 ? 1 : 0); // lone car is fleet (if it exists)
}
class Car {
int position;
double time;
Car(int p, double t) {
position = p;
time = t;
}
}
https://leetcode.com/problems/car-fleet/discuss/180287/Java-Priority-Queue-Explained
The key point here is that, once a car catches another car, it can't pass it but drives in a fleet. That means the car that's in the front will decide what speed to drive and what time of arrival. Any car behind that can catch up to that car before it arrives and becomes fleet. So in the test example, we order the car by position:
Position | Speed | Arrival | Comment |
---|---|---|---|
10 | 2 | 1s | first car |
8 | 4 | 1s | can catch up before first car arrives |
5 | 1 | 7s | can't catch up with above car, forms own fleet |
3 | 3 | 3s/7s | catches up with above car, actual arrival should be 7s |
0 | 1 | 12s | can't catch up with above car, forms own fleet |
public int carFleet(int target, int[] position, int[] speed) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
return b[1] - a[1];
});
for (int i = 0; i < position.length; i++) {
pq.offer(new int[]{ i, position[i] });
}
double time = 0;
int count = 0;
while (!pq.isEmpty()) {
int[] next = pq.poll();
int index = next[0];
int pos = next[1];
int spd = speed[index];
double needTime = (double)(target - pos)/spd;
if (needTime > time) {
count++;
time = needTime;
}
}
return count;
}