Approach
Breadth-first search
For The Time When the Network Becomes Idle, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 39 lines of Java from the credited upstream file 2039.java.
- The implementation visibly relies on sequence storage, work queue.
- 5 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int networkBecomesIdle(int[][] edges, int[] patience) {3 final int n = patience.length;4 int ans = 0;5 List<Integer>[] graph = new List[n];6 Queue<Integer> q = new ArrayDeque<>(List.of(0));7 int[] dist = new int[n]; 8 Arrays.fill(dist, Integer.MAX_VALUE);9 dist[0] = 0;10 Arrays.setAll(graph, i -> new ArrayList<>());11 12 for (int[] edge : edges) {13 final int u = edge[0];14 final int v = edge[1];15 graph[u].add(v);16 graph[v].add(u);17 }18 19 while (!q.isEmpty())20 for (int sz = q.size(); sz > 0; --sz) {21 final int u = q.poll();22 for (final int v : graph[u])23 if (dist[v] == Integer.MAX_VALUE) {24 dist[v] = dist[u] + 1;25 q.offer(v);26 }27 }28 29 for (int i = 1; i < n; ++i) {30 final int numResending = (dist[i] * 2 - 1) / patience[i];31 final int lastResendingTime = patience[i] * numResending;32 final int lastArrivingTime = lastResendingTime + dist[i] * 2;33 ans = Math.max(ans, lastArrivingTime);34 }35 36 return ans + 1;37 }38}39