1
var networkBecomesIdle = function (edges, patience) {
2
let graph = {};
3
for (let [edge, neighbor] of edges) {
4
if (!(edge in graph)) graph[edge] = [];
5
graph[edge].push(neighbor);
6
if (!(neighbor in graph)) graph[neighbor] = [];
7
graph[neighbor].push(edge);
8
}
9

10
// Initialize queue with server 0
11
let q = [0];
12
let seconds = 0,
13
complete = {};
14
while (q.length) {
15
let newQ = [];
16
while (q.length) {
17
let current = q.pop();
18
if (current in complete) continue;
19
complete[current] = seconds * 2;
20
for (let edge of graph[current]) {
21
if (complete[edge]) continue;
22
newQ.push(edge);
23
}
24
}
25
q = newQ;
26
seconds++;
27
}
28

29
// Total servers
30
let servers = Object.keys(graph).length;
31

32
let maxSeconds = 0;
33
for (let i = 1; i < servers; i++) {
34
let timeToComplete = complete[i];
35
let resendInterval = patience[i];
36

37
// If timeToComplete == 4 and resendInterval == 2, the msg will be resent 1 time at second 2 and not second 4
38
// If timeToComplete == 5 and resendInterval == 2, the msg will be resent 2 times at second 2 and second 4
39
// Hence we use Math.ceil(...) - 1
40
let resendCount = Math.ceil(timeToComplete / resendInterval) - 1;
41

42
let seconds = timeToComplete + resendInterval * resendCount;
43
maxSeconds = Math.max(maxSeconds, seconds);
44
}
45
return maxSeconds + 1;
46
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0