Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fb018a3

Browse files
committed
Add solution #2039
1 parent 4bee36c commit fb018a3

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,701 LeetCode solutions in JavaScript
1+
# 1,702 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1563,6 +1563,7 @@
15631563
2035|[Partition Array Into Two Arrays to Minimize Sum Difference](./solutions/2035-partition-array-into-two-arrays-to-minimize-sum-difference.js)|Hard|
15641564
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
15651565
2038|[Remove Colored Pieces if Both Neighbors are the Same Color](./solutions/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.js)|Medium|
1566+
2039|[The Time When the Network Becomes Idle](./solutions/2039-the-time-when-the-network-becomes-idle.js)|Medium|
15661567
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
15671568
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
15681569
2071|[Maximum Number of Tasks You Can Assign](./solutions/2071-maximum-number-of-tasks-you-can-assign.js)|Hard|
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)