5888. When the network is idle

Keywords: Java leetcode

5888. When the network is idle

Give you one with n   Computer network of servers with server number   0   reach   n - 1  . And give you a two-dimensional array of integers   edges  , among   edges[i] = [ui, vi]   Represents the server   UI and   vi   There is an information line between   One second   They can be transmitted between them   arbitrarily   Number of information. I'll give you another one with a length of n   And subscript from   0   Starting integer array   patience  .

Ensure that all servers are connected   That is to say, an information from any server can reach any other server directly or indirectly through these information lines.

The number is 0   Your server is the master   Server, other servers are data   The server. Each data server sends a message to the master server and waits for a reply. Information is optimally distributed between servers   Line transmission, that is, each message will be transmitted in the least time   To the primary server. The master server handles all   Newly arrived information and immediately   Send the reply message in the opposite direction according to the route when each message comes.

At 0   At the beginning of seconds, all data servers will send the information they need to process. From 1st   Start every second   The beginning of a second   Each data server will check whether it has received the reply information from the master server (including the reply information of the newly sent message):

  • If no reply has been received, the server will periodically   retransmission   Information. Data server   i   each   patience[i]   Every second, a message will be retransmitted, that is, the data server   i   Patience [i] after last sending information to the master server   Seconds later   Will resend a message to the primary server.
  • Otherwise, the data server   No retransmission   Information.
    When no information is transmitted on the line or reaches a server, the computer network becomes idle   Status.

Please return to the computer network to become idle   Stateful   Earliest seconds  .

Example 1:

Input: edges = [[0,1],[1,2]], patience = [0,2,1]
Output: 8
0 At the beginning of the second,
- Data server 1 sends a message to the master server (use 1) A Indicates).
- The data server 2 sends a message to the master server (2 A Indicates).

1 Seconds,
- Message 1 A Upon reaching the master server, the master server immediately processes information 1 A And send 1 A Reply message.
- Data server 1 has not received any reply. One second (1) has passed since the last message was sent < patience[1] = 2),So it won't resend the message.
- Data server 2 has not received any reply. 1 second (1 second) has passed since the last message was sent == patience[2] = 1),So it retransmits a message (with 2 B Indicates).

2 Seconds,
- Reply message 1 A When it reaches server 1, server 1 will not resend the message.
- Information 2 A Upon reaching the master server, the master server immediately processes information 2 A And send 2 A Reply message.
- Server 2 retransmits a message (using 2 C Indicates).
4 Seconds,
- Reply message 2 A When it reaches server 2, server 2 will not resend the message.
7 Second, reply to message 2 D Reached server 2.

From the 8th second, no more information is transmitted between servers and no more information arrives at the server.
So the 8th second is the earliest time when the network becomes idle.
Example 2:

Input: edges = [[0,1],[0,2],[1,2]], patience = [0,10,10]
Output: 3
 Explanation: data servers 1 and 2 receive a reply at the beginning of the second second second.
From the third second, the network becomes idle.

Problem solving ideas

  1. First, use breadth first search to calculate the shortest path before each node and node 0

  2. Because the data server I's patience[i] after last sending information to the master server Seconds later, a message will be retransmitted to the master server. Therefore, it can be inferred that when the first message sent receives a reply, it will not continue to send messages. Therefore, we only need to calculate how many retransmitted messages we sent to node 0 before the first reply message arrives, and calculate the time when the latest retransmitted message receives a reply, that is, the node becomes empty Idle time, calculate the time of all nodes, and compare the largest time, which is the earliest idle time


class Solution {
    public int networkBecomesIdle(int[][] edges, int[] patience) {

        int n=patience.length;
        Map<Integer,List<Integer>>map=new HashMap<>();
        for (int[] edge : edges) {
            if (!map.containsKey(edge[0]))
                map.put(edge[0],new ArrayList<>());

            if (!map.containsKey(edge[1]))
                map.put(edge[1],new ArrayList<>());

        Map<Integer,Integer> dist=new HashMap<>();

        Queue<Integer> queue=new LinkedList<>();

        int d=1;
        while (!queue.isEmpty()){
            int size=queue.size();
            for (int i = 0; i < size; i++) {
                int cur=queue.poll();
                for (Integer next : map.get(cur)) {
                    if (dist.containsKey(next))

        int max=0;
        for (int i = 1; i < patience.length; i++) {
            int cost=2*dist.get(i);
            int late=(cost-1)/patience[i];
        return max+1;


Posted by justinwhite93 on Sun, 17 Oct 2021 18:12:37 -0700