Skip to content

Commit 7253bdf

Browse files
authored
Added task 2039.
1 parent a4dc1f1 commit 7253bdf

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g2001_2100.s2039_the_time_when_the_network_becomes_idle;
2+
3+
// #Medium #Array #Breadth_First_Search #Graph
4+
// #2022_05_29_Time_134_ms_(72.29%)_Space_145.3_MB_(56.63%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.PriorityQueue;
9+
10+
public class Solution {
11+
public int networkBecomesIdle(int[][] edges, int[] pat) {
12+
int n = pat.length;
13+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
14+
for (int i = 0; i < n; i++) {
15+
adj.add(new ArrayList<>());
16+
}
17+
for (int[] arr : edges) {
18+
adj.get(arr[0]).add(arr[1]);
19+
adj.get(arr[1]).add(arr[0]);
20+
}
21+
int[] distance = new int[n];
22+
Arrays.fill(distance, 99999);
23+
distance[0] = 0;
24+
PriorityQueue<int[]> pq = new PriorityQueue<>((a1, a2) -> Integer.compare(a1[1], a2[1]));
25+
pq.add(new int[] {0, 0});
26+
while (!pq.isEmpty()) {
27+
int[] a = pq.poll();
28+
int node = a[0];
29+
for (Integer nn : adj.get(node)) {
30+
if (distance[node] + 1 < distance[nn]) {
31+
distance[nn] = 1 + distance[node];
32+
pq.add(new int[] {nn, distance[nn]});
33+
}
34+
}
35+
}
36+
int max = 0;
37+
for (int i = 1; i < n; i++) {
38+
int num1 = 2 * distance[i];
39+
int num2 = num1 / pat[i];
40+
if (num1 % pat[i] != 0) {
41+
num2++;
42+
}
43+
num2--;
44+
num2 *= pat[i];
45+
max = Math.max(max, num2 + num1);
46+
}
47+
return max + 1;
48+
}
49+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2039\. The Time When the Network Becomes Idle
2+
3+
Medium
4+
5+
There is a network of `n` servers, labeled from `0` to `n - 1`. You are given a 2D integer array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates there is a message channel between servers <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, and they can pass **any** number of messages to **each other** directly in **one** second. You are also given a **0-indexed** integer array `patience` of length `n`.
6+
7+
All servers are **connected**, i.e., a message can be passed from one server to any other server(s) directly or indirectly through the message channels.
8+
9+
The server labeled `0` is the **master** server. The rest are **data** servers. Each data server needs to send its message to the master server for processing and wait for a reply. Messages move between servers **optimally**, so every message takes the **least amount of time** to arrive at the master server. The master server will process all newly arrived messages **instantly** and send a reply to the originating server via the **reversed path** the message had gone through.
10+
11+
At the beginning of second `0`, each data server sends its message to be processed. Starting from second `1`, at the **beginning** of **every** second, each data server will check if it has received a reply to the message it sent (including any newly arrived replies) from the master server:
12+
13+
* If it has not, it will **resend** the message periodically. The data server `i` will resend the message every `patience[i]` second(s), i.e., the data server `i` will resend the message if `patience[i]` second(s) have **elapsed** since the **last** time the message was sent from this server.
14+
* Otherwise, **no more resending** will occur from this server.
15+
16+
The network becomes **idle** when there are **no** messages passing between servers or arriving at servers.
17+
18+
Return _the **earliest second** starting from which the network becomes **idle**_.
19+
20+
**Example 1:**
21+
22+
![example 1](https://assets.leetcode.com/uploads/2021/09/22/quiet-place-example1.png)
23+
24+
**Input:** edges = [[0,1],[1,2]], patience = [0,2,1]
25+
26+
**Output:** 8
27+
28+
**Explanation:**
29+
30+
At (the beginning of) second 0,
31+
32+
- Data server 1 sends its message (denoted 1A) to the master server.
33+
34+
- Data server 2 sends its message (denoted 2A) to the master server.
35+
36+
37+
At second 1,
38+
39+
- Message 1A arrives at the master server. Master server processes message 1A instantly and sends a reply 1A back.
40+
41+
- Server 1 has not received any reply. 1 second (1 < patience[1] = 2) elapsed since this server has sent the message, therefore it does not resend the message.
42+
43+
- Server 2 has not received any reply. 1 second (1 == patience[2] = 1) elapsed since this server has sent the message, therefore it resends the message (denoted 2B).
44+
45+
46+
At second 2,
47+
48+
- The reply 1A arrives at server 1. No more resending will occur from server 1.
49+
50+
- Message 2A arrives at the master server. Master server processes message 2A instantly and sends a reply 2A back.
51+
52+
- Server 2 resends the message (denoted 2C).
53+
54+
...
55+
56+
At second 4,
57+
58+
- The reply 2A arrives at server 2. No more resending will occur from server 2.
59+
60+
...
61+
62+
At second 7, reply 2D arrives at server 2.
63+
64+
65+
Starting from the beginning of the second 8, there are no messages passing between servers or arriving at servers.
66+
67+
This is the time when the network becomes idle.
68+
69+
**Example 2:**
70+
71+
![example 2](https://assets.leetcode.com/uploads/2021/09/04/network_a_quiet_place_2.png)
72+
73+
**Input:** edges = [[0,1],[0,2],[1,2]], patience = [0,10,10]
74+
75+
**Output:** 3
76+
77+
**Explanation:** Data servers 1 and 2 receive a reply back at the beginning of second 2.
78+
79+
From the beginning of the second 3, the network becomes idle.
80+
81+
**Constraints:**
82+
83+
* `n == patience.length`
84+
* <code>2 <= n <= 10<sup>5</sup></code>
85+
* `patience[0] == 0`
86+
* <code>1 <= patience[i] <= 10<sup>5</sup></code> for `1 <= i < n`
87+
* <code>1 <= edges.length <= min(10<sup>5</sup>, n * (n - 1) / 2)</code>
88+
* `edges[i].length == 2`
89+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
90+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
91+
* There are no duplicate edges.
92+
* Each server can directly or indirectly reach another server.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2001_2100.s2039_the_time_when_the_network_becomes_idle;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void networkBecomesIdle() {
11+
assertThat(
12+
new Solution()
13+
.networkBecomesIdle(new int[][] {{0, 1}, {1, 2}}, new int[] {0, 2, 1}),
14+
equalTo(8));
15+
}
16+
17+
@Test
18+
void networkBecomesIdle2() {
19+
assertThat(
20+
new Solution()
21+
.networkBecomesIdle(
22+
new int[][] {{0, 1}, {0, 2}, {1, 2}}, new int[] {0, 10, 10}),
23+
equalTo(3));
24+
}
25+
}

0 commit comments

Comments
 (0)