|
| 1 | +/** |
| 2 | + * 2039. The Time When the Network Becomes Idle |
| 3 | + * https://leetcode.com/problems/the-time-when-the-network-becomes-idle/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * There is a network of n servers, labeled from 0 to n - 1. You are given a 2D integer array |
| 7 | + * edges, where edges[i] = [ui, vi] indicates there is a message channel between servers ui |
| 8 | + * and vi, and they can pass any number of messages to each other directly in one second. |
| 9 | + * You are also given a 0-indexed integer array patience of length n. |
| 10 | + * |
| 11 | + * All servers are connected, i.e., a message can be passed from one server to any other |
| 12 | + * server(s) directly or indirectly through the message channels. |
| 13 | + * |
| 14 | + * The server labeled 0 is the master server. The rest are data servers. Each data server |
| 15 | + * needs to send its message to the master server for processing and wait for a reply. |
| 16 | + * Messages move between servers optimally, so every message takes the least amount of |
| 17 | + * time to arrive at the master server. The master server will process all newly arrived |
| 18 | + * messages instantly and send a reply to the originating server via the reversed path |
| 19 | + * the message had gone through. |
| 20 | + * |
| 21 | + * At the beginning of second 0, each data server sends its message to be processed. Starting |
| 22 | + * from second 1, at the beginning of every second, each data server will check if it has |
| 23 | + * received a reply to the message it sent (including any newly arrived replies) from the |
| 24 | + * master server: |
| 25 | + * - If it has not, it will resend the message periodically. The data server i will resend |
| 26 | + * the message every patience[i] second(s), i.e., the data server i will resend the message |
| 27 | + * if patience[i] second(s) have elapsed since the last time the message was sent from this |
| 28 | + * server. |
| 29 | + * - Otherwise, no more resending will occur from this server. |
| 30 | + * |
| 31 | + * The network becomes idle when there are no messages passing between servers or arriving |
| 32 | + * at servers. |
| 33 | + * |
| 34 | + * Return the earliest second starting from which the network becomes idle. |
| 35 | + */ |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {number[][]} edges |
| 39 | + * @param {number[]} patience |
| 40 | + * @return {number} |
| 41 | + */ |
| 42 | +var networkBecomesIdle = function(edges, patience) { |
| 43 | + const n = patience.length; |
| 44 | + const adjList = Array.from({ length: n }, () => []); |
| 45 | + for (const [u, v] of edges) { |
| 46 | + adjList[u].push(v); |
| 47 | + adjList[v].push(u); |
| 48 | + } |
| 49 | + |
| 50 | + const distances = new Array(n).fill(Infinity); |
| 51 | + distances[0] = 0; |
| 52 | + const queue = [0]; |
| 53 | + let maxTime = 0; |
| 54 | + |
| 55 | + while (queue.length) { |
| 56 | + const curr = queue.shift(); |
| 57 | + for (const next of adjList[curr]) { |
| 58 | + if (distances[next] === Infinity) { |
| 59 | + distances[next] = distances[curr] + 1; |
| 60 | + queue.push(next); |
| 61 | + if (next !== 0) { |
| 62 | + const roundTrip = 2 * distances[next]; |
| 63 | + const lastSent = Math.floor((roundTrip - 1) / patience[next]) * patience[next]; |
| 64 | + maxTime = Math.max(maxTime, lastSent + roundTrip); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return maxTime + 1; |
| 71 | +}; |
0 commit comments