Skip to content

Commit 00f3fed

Browse files
committed
feat: solve new question
1 parent 1c64a41 commit 00f3fed

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

car-fleet/main.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} target
3+
* @param {number[]} position
4+
* @param {number[]} speed
5+
* @return {number}
6+
*/
7+
var carFleet = function(target, position, speed) {
8+
const cars = position.map((pos, idx) => [pos, speed[idx]]);
9+
cars.sort((a, b) => b[0] - a[0]);
10+
const stack = [];
11+
12+
for (const [pos, speed] of cars) {
13+
let time = (target - pos) / speed;
14+
15+
if (stack.length == 0 || time > stack[stack.length - 1]) {
16+
stack.push(time);
17+
}
18+
}
19+
20+
return stack.length;
21+
};

0 commit comments

Comments
 (0)