Leetcode 2039: The Time When the Network Becomes Idle

grid47
grid47
Exploring patterns and algorithms
Apr 17, 2024 9 min read

You are managing a network of servers connected by communication channels. One server, labeled as 0, acts as the master server, while the others are data servers. Each data server sends a message to the master server at the start and waits for a reply. The reply travels back via the same route the message took. If a server does not receive a reply within a specific period (defined by its patience value), it resends the message. The goal is to determine when the network will become idle, meaning there are no active messages being transmitted or received.
Problem
Approach
Steps
Complexity
Input: The network is represented by edges and patience values for each server.
Example: edges = [[0,1],[1,2]], patience = [0,3,2]
Constraints:
• n == patience.length
• 2 <= n <= 10^5
• patience[0] == 0
• 1 <= patience[i] <= 10^5 for 1 <= i < n
• 1 <= edges.length <= min(10^5, n * (n - 1) / 2)
• edges[i].length == 2
• 0 <= ui, vi < n
• ui != vi
• No duplicate edges exist, and all servers are connected.
Output: The function returns the earliest time (in seconds) when the network becomes idle.
Example: Output: 7
Constraints:
• The returned time is a positive integer.
Goal: Calculate the time at which the last reply is received, accounting for resending rules based on patience values.
Steps:
• Construct a graph representation of the network using the edges.
• Perform a Breadth-First Search (BFS) to compute the shortest distance from the master server (0) to all other servers.
• For each server, calculate the round-trip time for its messages.
• Determine the last time a server resends its message before the network becomes idle.
• Find the maximum time across all servers and return it as the result.
Goal: The problem constraints define the size and properties of the input.
Steps:
• All servers are directly or indirectly connected.
• The patience value for the master server is always 0.
Assumptions:
• Messages travel optimally between servers using the shortest path.
• Replies are sent instantly upon reaching the master server.
• Servers only resend messages if they do not receive a reply within their patience interval.
Input: edges = [[0,1],[1,2]], patience = [0,3,2]
Explanation: Server 1 sends its message and receives a reply by second 2, so no resends occur. Server 2 resends its message once before receiving a reply by second 4. The network becomes idle at second 7.

Input: edges = [[0,1],[0,2],[1,2]], patience = [0,5,5]
Explanation: Both servers 1 and 2 receive replies by second 2. No resends occur, and the network becomes idle at second 3.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus