Skip to content

Commit 9a882f5

Browse files
authored
Create 1776-car-fleet-ii.js
1 parent ce78e98 commit 9a882f5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

1776-car-fleet-ii.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[][]} cars
3+
* @return {number[]}
4+
*/
5+
var getCollisionTimes = function (cars) {
6+
let n = cars.length,
7+
t = 0,
8+
i
9+
const ans = Array(n)
10+
for (let i = 0; i < n; i++) ans[i] = -1
11+
const s = []
12+
s[++t] = n - 1
13+
for (let i = n - 2; ~i; i--) {
14+
while (t && cars[s[t]][1] >= cars[i][1]) t--
15+
while (
16+
t > 1 &&
17+
(cars[s[t]][0] - cars[i][0]) * (cars[i][1] - cars[s[t - 1]][1]) >
18+
(cars[s[t - 1]][0] - cars[i][0]) * (cars[i][1] - cars[s[t]][1])
19+
)
20+
t--
21+
if (t) ans[i] = (cars[s[t]][0] - cars[i][0]) / (cars[i][1] - cars[s[t]][1])
22+
s[++t] = i
23+
}
24+
return ans
25+
}

0 commit comments

Comments
 (0)