Car Fleet LeetCode solution in JavaScript

1
var carFleet = function (target, position, speed) {
2
for (let i = 0; i < position.length; i++) {
3
position[i] = [target - position[i], speed[i]];
4
}
5
position.sort((a, b) => {
6
return a[0] - b[0];
7
});
8
let count = 1,
9
prev = position[0][0] / position[0][1];
10
for (let i = 1; i < position.length; i++) {
11
// if the time taken is longer then it will cause another fleet
12
if (position[i][0] / position[i][1] > prev) {
13
count++;
14
prev = position[i][0] / position[i][1];
15
}
16
}
17

18
return count;
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0