forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0743-network-delay-time.js
123 lines (95 loc) · 2.81 KB
/
0743-network-delay-time.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Graph - BFS
* Queue - Space (WIDTH)
* Array - Greedy
* https://leetcode.com/problems/network-delay-time/
* @param {number[][]} times
* @param {number} n
* @param {number} k
* @return {number}
*/
var networkDelayTime = (times, n, k) => {
const { graph, maxTime, queue } = buildGraph(times, n, k);
bfs(queue, graph, maxTime, k);
return checkAns(maxTime);
};
var initGraph = (n, k) => ({
graph: Array.from({ length: n + 1}).fill().map(() => []),
maxTime: Array.from({ length: n + 1}).fill(Infinity),
queue: new Queue([[ k, 0 ]])
})
var buildGraph = (times, n, k) => {
const { graph, maxTime, queue } = initGraph(n, k);
for (const [ src, dst, weight ] of times ) {
graph[src].push([ dst, weight ]);
};
maxTime[0] = 0;
return { graph, maxTime, queue };
}
var bfs = (queue, graph, maxTime) => {
while (!queue.isEmpty()) {
for (let level = (queue.size() -1); (0 <= level); level-- ) {
checkNeighbors(queue, graph, maxTime);
}
}
}
var checkNeighbors = (queue, graph, maxTime) => {
const [ node, time ] = queue.dequeue();
const canUpdate = (time < maxTime[node]);
if (!canUpdate) return;
maxTime[node] = time;
for (const [ dst, weight ] of graph[node]) {
queue.enqueue([ dst, (weight + time) ]);
}
}
var checkAns = (maxTime) => {
const max = Math.max(...maxTime);
return (max < Infinity)
? max
: (-1);
}
/**
* https://leetcode.com/problems/network-delay-time/
* @param {number[][]} times
* @param {number} n
* @param {number} k
* @return {number}
*/
var networkDelayTime = (times, n, k) => {
const { graph, seen, minHeap } = buildGraph(times, n, k)
const maxTime = getTime(graph, seen, minHeap);
return (seen.size === n)
? maxTime
: -1;
};
var initGraph = (n, k) => ({
graph: Array.from({ length: n + 1}).fill().map(() => []),
seen: new Set(),
minHeap: new MinPriorityQueue()
})
var buildGraph = (times, n, k) => {
const { graph, seen, minHeap } = initGraph(n, k);
for (const [ src, dst, weight ] of times ) {
graph[src].push([ dst, weight ]);
};
minHeap.enqueue([k, 0], 0);
return { graph, seen, minHeap };
}
const getTime = (graph, seen, minHeap, maxTime = 0) => {
while (!minHeap.isEmpty()) {
const [ node, cost ] = minHeap.dequeue().element;
if (seen.has(node)) continue;
seen.add(node);
maxTime = Math.max(maxTime, cost);
checkNeighbors(graph, node, cost, seen, minHeap);
}
return maxTime;
}
var checkNeighbors = (graph, src, srcCost, seen, minHeap) => {
for (const [ dst, dstCost ] of graph[src]) {
if (seen.has(dst)) continue;
const cost = (dstCost + srcCost)
const node = [ dst, cost];
minHeap.enqueue(node, cost);
}
}