File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } cars
3
+ * @return {number[] }
4
+ */
5
+ var getCollisionTimes = function ( cars ) {
6
+ const n = cars . length
7
+ const ans = Array ( n ) . fill ( 0 )
8
+ const stack = [ ]
9
+ for ( let i = n - 1 ; i >= 0 ; i -- ) {
10
+ while ( stack . length ) {
11
+ if ( cars [ stack [ stack . length - 1 ] ] [ 1 ] >= cars [ i ] [ 1 ] ) stack . pop ( )
12
+ else {
13
+ if ( ans [ stack [ stack . length - 1 ] ] < 0 ) break
14
+ const d = ans [ stack [ stack . length - 1 ] ] * ( cars [ i ] [ 1 ] - cars [ stack [ stack . length - 1 ] ] [ 1 ] )
15
+ if ( d > cars [ stack [ stack . length - 1 ] ] [ 0 ] - cars [ i ] [ 0 ] ) break
16
+ else stack . pop ( )
17
+ }
18
+ }
19
+ if ( stack . length === 0 ) ans [ i ] = - 1
20
+ else {
21
+ const t = ( cars [ stack [ stack . length - 1 ] ] [ 0 ] - cars [ i ] [ 0 ] ) / ( cars [ i ] [ 1 ] - cars [ stack [ stack . length - 1 ] ] [ 1 ] )
22
+ ans [ i ] = t
23
+ }
24
+ stack . push ( i )
25
+ }
26
+ return ans
27
+ } ;
28
+
29
+ // another
30
+
1
31
/**
2
32
* @param {number[][] } cars
3
33
* @return {number[] }
You can’t perform that action at this time.
0 commit comments