Skip to content

2039. The Time When the Network Becomes Idle 👍

  • Time: $O(|V| + |E|)$, where $|V| = n$ and $|E| = |\texttt{edges}|$
  • Space: $O(|V| + |E|)$, where $|V| = n$ and $|E| = |\texttt{edges}|$
 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
class Solution {
 public:
  int networkBecomesIdle(vector<vector<int>>& edges, vector<int>& patience) {
    const int n = patience.size();
    int ans = 0;
    vector<vector<int>> graph(n);
    queue<int> q{{0}};
    vector<int> dist(n, INT_MAX);  // dist[i] := the distance between i and 0
    dist[0] = 0;

    for (const vector<int>& edge : edges) {
      const int u = edge[0];
      const int v = edge[1];
      graph[u].push_back(v);
      graph[v].push_back(u);
    }

    while (!q.empty())
      for (int sz = q.size(); sz > 0; --sz) {
        const int u = q.front();
        q.pop();
        for (const int v : graph[u])
          if (dist[v] == INT_MAX) {
            dist[v] = dist[u] + 1;
            q.push(v);
          }
      }

    for (int i = 1; i < n; ++i) {
      const int numResending = (dist[i] * 2 - 1) / patience[i];
      const int lastResendingTime = patience[i] * numResending;
      const int lastArrivingTime = lastResendingTime + dist[i] * 2;
      ans = max(ans, lastArrivingTime);
    }

    return ans + 1;
  }
};
 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
class Solution {
  public int networkBecomesIdle(int[][] edges, int[] patience) {
    final int n = patience.length;
    int ans = 0;
    List<Integer>[] graph = new List[n];
    Queue<Integer> q = new ArrayDeque<>(List.of(0));
    int[] dist = new int[n]; // dist[i] := the distance between i and 0
    Arrays.fill(dist, Integer.MAX_VALUE);
    dist[0] = 0;

    for (int i = 0; i < n; ++i)
      graph[i] = new ArrayList<>();

    for (int[] edge : edges) {
      final int u = edge[0];
      final int v = edge[1];
      graph[u].add(v);
      graph[v].add(u);
    }

    while (!q.isEmpty())
      for (int sz = q.size(); sz > 0; --sz) {
        final int u = q.poll();
        for (final int v : graph[u])
          if (dist[v] == Integer.MAX_VALUE) {
            dist[v] = dist[u] + 1;
            q.offer(v);
          }
      }

    for (int i = 1; i < n; ++i) {
      final int numResending = (dist[i] * 2 - 1) / patience[i];
      final int lastResendingTime = patience[i] * numResending;
      final int lastArrivingTime = lastResendingTime + dist[i] * 2;
      ans = Math.max(ans, lastArrivingTime);
    }

    return ans + 1;
  }
}
 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
class Solution:
  def networkBecomesIdle(
      self,
      edges: list[list[int]],
      patience: list[int],
  ) -> int:
    n = len(patience)
    ans = 0
    graph = [[] for _ in range(n)]
    q = collections.deque([0])
    dist = [math.inf] * n  # dist[i] := the distance between i and 0
    dist[0] = 0

    for u, v in edges:
      graph[u].append(v)
      graph[v].append(u)

    while q:
      for _ in range(len(q)):
        u = q.popleft()
        for v in graph[u]:
          if dist[v] == math.inf:
            dist[v] = dist[u] + 1
            q.append(v)

    for i in range(1, n):
      numResending = (dist[i] * 2 - 1) // patience[i]
      lastResendingTime = patience[i] * numResending
      lastArrivingTime = lastResendingTime + dist[i] * 2
      ans = max(ans, lastArrivingTime)

    return ans + 1