17
17
// Traverse neighbor nodes and only queue the neighbor if we have used all inbound edges.
18
18
// Record the minimum cost to reach each node.
19
19
20
- // Note: This solution is TLE, passes 626/636 test cases.
21
20
// n = number of nodes, m = max(edge cost), e = number of edges
22
- // Time Complexity: O((n + e) log(m))
23
- // Space Complexity: O(n + e)
21
+ // Time Complexity: O((n + e) log(m)) 872ms
22
+ // Space Complexity: O(n + e) 130MB
24
23
function findMaxPathScore ( edges , online , k ) {
25
24
const n = online . length , graph = Array ( n ) . fill ( 0 ) . map ( ( ) => [ ] ) ;
26
25
const initialIndegrees = Array ( n ) . fill ( 0 ) ;
@@ -32,43 +31,82 @@ function findMaxPathScore(edges, online, k) {
32
31
maxCost = Math . max ( maxCost , cost ) ;
33
32
}
34
33
}
35
- const initialQueue = [ ] ;
34
+ const initialQueue = new LinkedList ( ) ;
36
35
for ( let i = 0 ; i < n ; i ++ ) {
37
36
if ( initialIndegrees [ i ] === 0 ) {
38
- initialQueue . push ( i ) ;
37
+ initialQueue . add ( i ) ;
39
38
}
40
39
}
41
- let low = 0 , high = maxCost , res = - 1 ;
40
+ let low = 0 , high = maxCost ;
42
41
while ( low < high ) {
43
42
const mid = Math . ceil ( ( low + high ) / 2 ) ;
44
43
if ( canReachTarget ( mid ) ) {
45
44
low = mid ;
46
- res = mid ;
47
45
} else {
48
46
high = mid - 1 ;
49
47
}
50
48
}
51
- return res ;
49
+ return canReachTarget ( low ) ? low : - 1 ;
52
50
53
51
function canReachTarget ( x ) {
54
- const queue = [ ... initialQueue ] , indegrees = [ ...initialIndegrees ] ;
52
+ const queue = initialQueue . copy ( ) , indegrees = [ ...initialIndegrees ] ;
55
53
const minCost = Array ( n ) . fill ( Infinity ) ;
56
54
minCost [ 0 ] = 0 ;
57
- while ( queue . length ) {
58
- let node = queue . shift ( ) ;
55
+ while ( queue . size ) {
56
+ let node = queue . remove ( ) ;
59
57
for ( let [ nei , cost ] of graph [ node ] ) {
60
58
if ( cost >= x ) {
61
59
minCost [ nei ] = Math . min ( minCost [ nei ] , minCost [ node ] + cost ) ;
62
60
}
63
61
if ( -- indegrees [ nei ] === 0 ) {
64
- queue . push ( nei ) ;
62
+ queue . add ( nei ) ;
65
63
}
66
64
}
67
65
}
68
66
return minCost [ n - 1 ] <= k ;
69
67
}
70
68
} ;
71
69
70
+ class LinkedList {
71
+ constructor ( ) {
72
+ this . head = new Node ( null ) ;
73
+ this . tail = this . head ;
74
+ this . size = 0 ;
75
+ }
76
+ add ( val ) {
77
+ const newNode = new Node ( val ) ;
78
+ this . tail . next = newNode ;
79
+ this . tail = newNode ;
80
+ this . size ++ ;
81
+ }
82
+ remove ( ) {
83
+ if ( this . size === 0 ) return - 1 ;
84
+ const head = this . head . next ;
85
+ this . head . next = this . head . next . next ;
86
+ if ( head === this . tail ) {
87
+ this . tail = this . head ;
88
+ }
89
+ this . size -- ;
90
+ return head . val ;
91
+ }
92
+ copy ( ) {
93
+ const newQueue = new LinkedList ( ) ;
94
+ let node = this . head . next ;
95
+ while ( node ) {
96
+ newQueue . add ( node . val ) ;
97
+ node = node . next ;
98
+ }
99
+ return newQueue ;
100
+ }
101
+ }
102
+
103
+ class Node {
104
+ constructor ( val ) {
105
+ this . val = val ;
106
+ this . next = null ;
107
+ }
108
+ }
109
+
72
110
// Two test cases
73
111
console . log ( findMaxPathScore ( [ [ 0 , 1 , 5 ] , [ 1 , 3 , 10 ] , [ 0 , 2 , 3 ] , [ 2 , 3 , 4 ] ] , [ true , true , true , true ] , 10 ) ) // 3
74
112
console . log ( findMaxPathScore ( [ [ 0 , 1 , 7 ] , [ 1 , 4 , 5 ] , [ 0 , 2 , 6 ] , [ 2 , 3 , 6 ] , [ 3 , 4 , 2 ] , [ 2 , 4 , 6 ] ] , [ true , true , true , false , true ] , 12 ) ) // 6
0 commit comments