forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar Fleet II.js
42 lines (37 loc) · 1.78 KB
/
Car Fleet II.js
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
32
33
34
35
36
37
38
39
40
41
42
var getCollisionTimes = function(cars) {
// create a stack to hold all the indicies of the cars that can be hit
const possibleCarsToHit = [];
const result = new Array(cars.length).fill(-1);
for (let i = cars.length - 1; i >= 0; i--) {
// if there are cars to hit, check if the current car will hit the car before or after hitting
// the car in front of them
// you can also think of this as if the current car will hit the car before or after being absorbed by
// the slower car in front.
while (possibleCarsToHit.length && result[possibleCarsToHit[possibleCarsToHit.length - 1]] >= 0) {
const nextCarIndex = possibleCarsToHit[possibleCarsToHit.length - 1];
const timeToCatchNextCar =
getTimeToCatchNextCar(cars[i], cars[nextCarIndex]);
const timeToCatchNextNextCar = result[nextCarIndex];
if (timeToCatchNextCar > 0 &&
timeToCatchNextCar <= timeToCatchNextNextCar) {
break;
}
// pop off the stack because the car was absorbed by the next car
// before getting hit by the current car
possibleCarsToHit.pop();
}
if (possibleCarsToHit.length) {
const nextCarIndex = possibleCarsToHit[possibleCarsToHit.length - 1];
result[i] = getTimeToCatchNextCar(cars[i], cars[nextCarIndex]);
}
possibleCarsToHit.push(i);
}
return result;
};
function getTimeToCatchNextCar(leftCar, rightCar) {
const [leftCarPosition, leftCarSpeed] = leftCar
const [rightCarPosition, rightCarSpeed] = rightCar;
const distanceToCatchCar = rightCarPosition - leftCarPosition;
const differenceInSpeed = leftCarSpeed - rightCarSpeed;
return differenceInSpeed > 0 ? distanceToCatchCar / differenceInSpeed : -1;
}