File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n
3
+ * @param {number[][] } relations
4
+ * @param {number[] } time
5
+ * @return {number }
6
+ */
7
+ const minimumTime = function ( n , relations , time ) {
8
+ const graph = { } , dist = Array ( n ) . fill ( 0 ) , inDegree = Array ( n ) . fill ( 0 )
9
+ for ( let [ from , to ] of relations ) {
10
+ from -- , to --
11
+ if ( graph [ from ] == null ) graph [ from ] = [ ]
12
+ graph [ from ] . push ( to )
13
+ inDegree [ to ] ++
14
+ }
15
+ const q = [ ]
16
+ for ( let i = 0 ; i < n ; i ++ ) {
17
+ if ( inDegree [ i ] === 0 ) {
18
+ q . push ( i )
19
+ dist [ i ] = time [ i ]
20
+ }
21
+ }
22
+
23
+ while ( q . length ) {
24
+ const u = q . shift ( )
25
+ for ( const v of ( graph [ u ] || [ ] ) ) {
26
+ dist [ v ] = Math . max ( dist [ v ] , dist [ u ] + time [ v ] )
27
+ if ( -- inDegree [ v ] === 0 ) q . push ( v )
28
+ }
29
+ }
30
+
31
+ let res = 0
32
+ for ( let e of dist ) {
33
+ if ( e > res ) res = e
34
+ }
35
+ return res
36
+ } ;
You can’t perform that action at this time.
0 commit comments