-
Notifications
You must be signed in to change notification settings - Fork 4
/
TheTimeWhenTheNetworkBecomesIdle.java
38 lines (35 loc) · 1.19 KB
/
TheTimeWhenTheNetworkBecomesIdle.java
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
package com.dbc;
import java.util.*;
public class TheTimeWhenTheNetworkBecomesIdle {
public int networkBecomesIdle(int[][] edges, int[] patience) {
List<Integer>[] graph = new List[patience.length];
for (int i = 0; i < patience.length; ++i) {
graph[i] = new ArrayList<Integer>();
}
for (int[] edge : edges) {
graph[edge[0]].add(edge[1]);
graph[edge[1]].add(edge[0]);
}
Queue<Integer> queue = new ArrayDeque<Integer>(){{
add(0);
}};
int res = 0, level = 1;
boolean[] visited = new boolean[patience.length];
Arrays.fill(visited, false);
visited[0] = true;
while (!queue.isEmpty()) {
int curLen = queue.size();
for (int i = 0; i < curLen; i++) {
int curr = queue.poll();
for (int v : graph[curr]) {
if (visited[v]) continue;
visited[v] = true;
queue.add(v);
res = Math.max(res, patience[v] * ((2 * level - 1) / patience[v]) + 2 * level + 1);
}
}
level++;
}
return res;
}
}