Skip to content

Commit 6cf2dd6

Browse files
committed
Add solution
1 parent 200b9e4 commit 6cf2dd6

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

Topological Sort/3620.network-recovery-pathways.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
// Traverse neighbor nodes and only queue the neighbor if we have used all inbound edges.
1818
// Record the minimum cost to reach each node.
1919

20-
// Note: This solution is TLE, passes 626/636 test cases.
2120
// 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
2423
function findMaxPathScore(edges, online, k) {
2524
const n = online.length, graph = Array(n).fill(0).map(() => []);
2625
const initialIndegrees = Array(n).fill(0);
@@ -32,43 +31,82 @@ function findMaxPathScore(edges, online, k) {
3231
maxCost = Math.max(maxCost, cost);
3332
}
3433
}
35-
const initialQueue = [];
34+
const initialQueue = new LinkedList();
3635
for (let i = 0; i < n; i++) {
3736
if (initialIndegrees[i] === 0) {
38-
initialQueue.push(i);
37+
initialQueue.add(i);
3938
}
4039
}
41-
let low = 0, high = maxCost, res = -1;
40+
let low = 0, high = maxCost;
4241
while (low < high) {
4342
const mid = Math.ceil((low + high) / 2);
4443
if (canReachTarget(mid)) {
4544
low = mid;
46-
res = mid;
4745
} else {
4846
high = mid - 1;
4947
}
5048
}
51-
return res;
49+
return canReachTarget(low) ? low : -1;
5250

5351
function canReachTarget(x) {
54-
const queue = [...initialQueue], indegrees = [...initialIndegrees];
52+
const queue = initialQueue.copy(), indegrees = [...initialIndegrees];
5553
const minCost = Array(n).fill(Infinity);
5654
minCost[0] = 0;
57-
while (queue.length) {
58-
let node = queue.shift();
55+
while (queue.size) {
56+
let node = queue.remove();
5957
for (let [nei, cost] of graph[node]) {
6058
if (cost >= x) {
6159
minCost[nei] = Math.min(minCost[nei], minCost[node] + cost);
6260
}
6361
if (--indegrees[nei] === 0) {
64-
queue.push(nei);
62+
queue.add(nei);
6563
}
6664
}
6765
}
6866
return minCost[n - 1] <= k;
6967
}
7068
};
7169

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+
72110
// Two test cases
73111
console.log(findMaxPathScore([[0,1,5],[1,3,10],[0,2,3],[2,3,4]], [true,true,true,true], 10)) // 3
74112
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

Comments
 (0)