diff --git a/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README.md b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README.md
index e613394cdadaf..812d234dc4ea5 100644
--- a/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README.md
+++ b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README.md
@@ -77,11 +77,11 @@ ABBBBBBBAA -> ABBBBBBAA
**方法一:计数**
-统计字符串 `colors` 中连续出现 $3$ 个 `'A'` 或 $3$ 个 `'B'` 的个数,分别记为 $a$ 和 $b$。
+我们统计字符串 `colors` 中连续出现 $3$ 个 `'A'` 或 $3$ 个 `'B'` 的个数,分别记为 $a$ 和 $b$。
最后判断 $a$ 是否大于 $b$,是则返回 `true`,否则返回 `false`。
-时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 `colors` 的长度。
+时间复杂度 $O(n)$,其中 $n$ 为字符串 `colors` 的长度。空间复杂度 $O(1)$。
@@ -178,6 +178,29 @@ func winnerOfGame(colors string) bool {
}
```
+### **TypeScript**
+
+```ts
+function winnerOfGame(colors: string): boolean {
+ const n = colors.length;
+ let [a, b] = [0, 0];
+ for (let i = 0, j = 0; i < n; i = j) {
+ while (j < n && colors[j] === colors[i]) {
+ ++j;
+ }
+ const m = j - i - 2;
+ if (m > 0) {
+ if (colors[i] === 'A') {
+ a += m;
+ } else {
+ b += m;
+ }
+ }
+ }
+ return a > b;
+}
+```
+
### **...**
```
diff --git a/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README_EN.md b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README_EN.md
index 076641bb6ceb4..57e0a28703afd 100644
--- a/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README_EN.md
+++ b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README_EN.md
@@ -72,6 +72,14 @@ Thus, Bob wins, so return false.
## Solutions
+**Solution 1: Counting**
+
+We count the number of times that the string `colors` contains three consecutive `'A'`s or three consecutive `'B'`s, denoted as $a$ and $b$, respectively.
+
+Finally, we check whether $a$ is greater than $b$. If it is, we return `true`. Otherwise, we return `false`.
+
+The time complexity is $O(n)$, where $n$ is the length of the string `colors`. The space complexity is $O(1)$.
+
### **Python3**
@@ -163,6 +171,29 @@ func winnerOfGame(colors string) bool {
}
```
+### **TypeScript**
+
+```ts
+function winnerOfGame(colors: string): boolean {
+ const n = colors.length;
+ let [a, b] = [0, 0];
+ for (let i = 0, j = 0; i < n; i = j) {
+ while (j < n && colors[j] === colors[i]) {
+ ++j;
+ }
+ const m = j - i - 2;
+ if (m > 0) {
+ if (colors[i] === 'A') {
+ a += m;
+ } else {
+ b += m;
+ }
+ }
+ }
+ return a > b;
+}
+```
+
### **...**
```
diff --git a/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/Solution.ts b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/Solution.ts
new file mode 100644
index 0000000000000..2a3ba641c75c6
--- /dev/null
+++ b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/Solution.ts
@@ -0,0 +1,18 @@
+function winnerOfGame(colors: string): boolean {
+ const n = colors.length;
+ let [a, b] = [0, 0];
+ for (let i = 0, j = 0; i < n; i = j) {
+ while (j < n && colors[j] === colors[i]) {
+ ++j;
+ }
+ const m = j - i - 2;
+ if (m > 0) {
+ if (colors[i] === 'A') {
+ a += m;
+ } else {
+ b += m;
+ }
+ }
+ }
+ return a > b;
+}
diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/README.md b/solution/2000-2099/2039.The Time When the Network Becomes Idle/README.md
index 3a430998cbdf8..8adeff0303410 100644
--- a/solution/2000-2099/2039.The Time When the Network Becomes Idle/README.md
+++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/README.md
@@ -86,7 +86,13 @@
-用 BFS 获取主服务器 0 到每个数据服务器的最短距离 step。每个数据服务器 v 从发出信息到收到主服务器的响应信息,所经过的距离(或者时间) `d = step * 2`,由于数据服务器 v 可能每隔 `t = patience[v]` 就会重复发送一次消息,可以推算出每个数据服务器 v 最后一次发送消息的时间是 `⌊(d - 1) / t⌋ * t`,所以它最后一次收到主服务器的响应信息时间是 `⌊(d - 1) / t⌋ * t + d`,空闲时间是 `⌊(d - 1) / t⌋ * t + d + 1`,找出所有空间时间的最大值即可。
+**方法一:BFS**
+
+我们先根据二维数组 $edges$ 构建无向图 $g$,其中 $g[u]$ 表示节点 $u$ 的所有邻居节点。
+
+然后,我们可以使用广度优先搜索的方式,找出每个节点 $i$ 距离主服务器的最短距离 $d_i$,那么节点 $i$ 发出信息后,最早能收到回复的时间为 $2 \times d_i$。由于每个数据服务器 $i$ 每隔 $patience[i]$ 会重发一条信息,因此,每个数据服务器最后一次发出信息的时间为 $(2 \times d_i - 1) / patience[i] \times patience[i]$,那么最后收到回复的时间为 $(2 \times d_i - 1) / patience[i] \times patience[i] + 2 \times d_i$,再加上 $1$ 秒的处理时间,即为该数据服务器变为空闲的最早时间。我们找出最晚的这个时间,即为计算机网络变为空闲的最早时间。
+
+时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点数。
@@ -103,18 +109,17 @@ class Solution:
g[v].append(u)
q = deque([0])
vis = {0}
- ans = step = 0
+ ans = d = 0
while q:
- step += 1
+ d += 1
+ t = d * 2
for _ in range(len(q)):
u = q.popleft()
for v in g[u]:
- if v in vis:
- continue
- vis.add(v)
- q.append(v)
- d, t = step * 2, patience[v]
- ans = max(ans, (d - 1) // t * t + d + 1)
+ if v not in vis:
+ vis.add(v)
+ q.append(v)
+ ans = max(ans, (t - 1) // patience[v] * patience[v] + t + 1)
return ans
```
@@ -127,37 +132,35 @@ class Solution {
public int networkBecomesIdle(int[][] edges, int[] patience) {
int n = patience.length;
List[] g = new List[n];
- boolean[] vis = new boolean[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int[] e : edges) {
int u = e[0], v = e[1];
g[u].add(v);
g[v].add(u);
}
- int ans = 0;
- int step = 0;
Deque q = new ArrayDeque<>();
q.offer(0);
+ boolean[] vis = new boolean[n];
vis[0] = true;
+ int ans = 0, d = 0;
while (!q.isEmpty()) {
- ++step;
+ ++d;
+ int t = d * 2;
for (int i = q.size(); i > 0; --i) {
int u = q.poll();
for (int v : g[u]) {
- if (vis[v]) {
- continue;
+ if (!vis[v]) {
+ vis[v] = true;
+ q.offer(v);
+ ans = Math.max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
}
- vis[v] = true;
- q.offer(v);
- int d = step * 2;
- int t = patience[v];
- ans = Math.max(ans, (d - 1) / t * t + d + 1);
}
}
}
return ans;
}
}
+
```
### **C++**
@@ -167,27 +170,29 @@ class Solution {
public:
int networkBecomesIdle(vector>& edges, vector& patience) {
int n = patience.size();
- vector> g(n);
- vector vis(n);
+ vector g[n];
for (auto& e : edges) {
int u = e[0], v = e[1];
g[u].push_back(v);
g[v].push_back(u);
}
queue q{{0}};
+ bool vis[n];
+ memset(vis, false, sizeof(vis));
vis[0] = true;
- int ans = 0, step = 0;
+ int ans = 0, d = 0;
while (!q.empty()) {
- ++step;
- for (int i = q.size(); i > 0; --i) {
+ ++d;
+ int t = d * 2;
+ for (int i = q.size(); i; --i) {
int u = q.front();
q.pop();
for (int v : g[u]) {
- if (vis[v]) continue;
- vis[v] = true;
- q.push(v);
- int d = step * 2, t = patience[v];
- ans = max(ans, (d - 1) / t * t + d + 1);
+ if (!vis[v]) {
+ vis[v] = true;
+ q.push(v);
+ ans = max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
+ }
}
}
}
@@ -199,35 +204,32 @@ public:
### **Go**
```go
-func networkBecomesIdle(edges [][]int, patience []int) int {
+func networkBecomesIdle(edges [][]int, patience []int) (ans int) {
n := len(patience)
g := make([][]int, n)
- vis := make([]bool, n)
for _, e := range edges {
u, v := e[0], e[1]
g[u] = append(g[u], v)
g[v] = append(g[v], u)
}
q := []int{0}
+ vis := make([]bool, n)
vis[0] = true
- ans, step := 0, 0
- for len(q) > 0 {
- step++
+ for d := 1; len(q) > 0; d++ {
+ t := d * 2
for i := len(q); i > 0; i-- {
u := q[0]
q = q[1:]
for _, v := range g[u] {
- if vis[v] {
- continue
+ if !vis[v] {
+ vis[v] = true
+ q = append(q, v)
+ ans = max(ans, (t-1)/patience[v]*patience[v]+t+1)
}
- vis[v] = true
- q = append(q, v)
- d, t := step*2, patience[v]
- ans = max(ans, (d-1)/t*t+d+1)
}
}
}
- return ans
+ return
}
func max(a, b int) int {
@@ -238,6 +240,38 @@ func max(a, b int) int {
}
```
+### **TypeScript**
+
+```ts
+function networkBecomesIdle(edges: number[][], patience: number[]): number {
+ const n = patience.length;
+ const g: number[][] = Array.from({ length: n }, () => []);
+ for (const [u, v] of edges) {
+ g[u].push(v);
+ g[v].push(u);
+ }
+ const vis: boolean[] = Array.from({ length: n }, () => false);
+ vis[0] = true;
+ let q: number[] = [0];
+ let ans = 0;
+ for (let d = 1; q.length > 0; ++d) {
+ const t = d * 2;
+ const nq: number[] = [];
+ for (const u of q) {
+ for (const v of g[u]) {
+ if (!vis[v]) {
+ vis[v] = true;
+ nq.push(v);
+ ans = Math.max(ans, (((t - 1) / patience[v]) | 0) * patience[v] + t + 1);
+ }
+ }
+ }
+ q = nq;
+ }
+ return ans;
+}
+```
+
### **...**
```
diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/README_EN.md b/solution/2000-2099/2039.The Time When the Network Becomes Idle/README_EN.md
index f7851babbdc03..1fd23020e868f 100644
--- a/solution/2000-2099/2039.The Time When the Network Becomes Idle/README_EN.md
+++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/README_EN.md
@@ -81,7 +81,13 @@ From the beginning of the second 3, the network becomes idle.
## Solutions
-BFS.
+**Solution 1: BFS**
+
+First, we construct an undirected graph $g$ based on the 2D array $edges$, where $g[u]$ represents all neighboring nodes of node $u$.
+
+Then, we can use breadth-first search (BFS) to find the shortest distance $d_i$ from each node $i$ to the main server. The earliest time that node $i$ can receive a reply after sending a message is $2 \times d_i$. Since each data server $i$ resends a message every $patience[i]$ seconds, the last time that each data server sends a message is $(2 \times d_i - 1) / patience[i] \times patience[i]$. Therefore, the latest time that the network becomes idle is $(2 \times d_i - 1) / patience[i] \times patience[i] + 2 \times d_i$, plus 1 second for processing time. We find the latest of these times, which is the earliest time that the computer network becomes idle.
+
+The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes.
@@ -96,18 +102,17 @@ class Solution:
g[v].append(u)
q = deque([0])
vis = {0}
- ans = step = 0
+ ans = d = 0
while q:
- step += 1
+ d += 1
+ t = d * 2
for _ in range(len(q)):
u = q.popleft()
for v in g[u]:
- if v in vis:
- continue
- vis.add(v)
- q.append(v)
- d, t = step * 2, patience[v]
- ans = max(ans, (d - 1) // t * t + d + 1)
+ if v not in vis:
+ vis.add(v)
+ q.append(v)
+ ans = max(ans, (t - 1) // patience[v] * patience[v] + t + 1)
return ans
```
@@ -118,31 +123,28 @@ class Solution {
public int networkBecomesIdle(int[][] edges, int[] patience) {
int n = patience.length;
List[] g = new List[n];
- boolean[] vis = new boolean[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int[] e : edges) {
int u = e[0], v = e[1];
g[u].add(v);
g[v].add(u);
}
- int ans = 0;
- int step = 0;
Deque q = new ArrayDeque<>();
q.offer(0);
+ boolean[] vis = new boolean[n];
vis[0] = true;
+ int ans = 0, d = 0;
while (!q.isEmpty()) {
- ++step;
+ ++d;
+ int t = d * 2;
for (int i = q.size(); i > 0; --i) {
int u = q.poll();
for (int v : g[u]) {
- if (vis[v]) {
- continue;
+ if (!vis[v]) {
+ vis[v] = true;
+ q.offer(v);
+ ans = Math.max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
}
- vis[v] = true;
- q.offer(v);
- int d = step * 2;
- int t = patience[v];
- ans = Math.max(ans, (d - 1) / t * t + d + 1);
}
}
}
@@ -158,27 +160,29 @@ class Solution {
public:
int networkBecomesIdle(vector>& edges, vector& patience) {
int n = patience.size();
- vector> g(n);
- vector vis(n);
+ vector g[n];
for (auto& e : edges) {
int u = e[0], v = e[1];
g[u].push_back(v);
g[v].push_back(u);
}
queue q{{0}};
+ bool vis[n];
+ memset(vis, false, sizeof(vis));
vis[0] = true;
- int ans = 0, step = 0;
+ int ans = 0, d = 0;
while (!q.empty()) {
- ++step;
- for (int i = q.size(); i > 0; --i) {
+ ++d;
+ int t = d * 2;
+ for (int i = q.size(); i; --i) {
int u = q.front();
q.pop();
for (int v : g[u]) {
- if (vis[v]) continue;
- vis[v] = true;
- q.push(v);
- int d = step * 2, t = patience[v];
- ans = max(ans, (d - 1) / t * t + d + 1);
+ if (!vis[v]) {
+ vis[v] = true;
+ q.push(v);
+ ans = max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
+ }
}
}
}
@@ -190,35 +194,32 @@ public:
### **Go**
```go
-func networkBecomesIdle(edges [][]int, patience []int) int {
+func networkBecomesIdle(edges [][]int, patience []int) (ans int) {
n := len(patience)
g := make([][]int, n)
- vis := make([]bool, n)
for _, e := range edges {
u, v := e[0], e[1]
g[u] = append(g[u], v)
g[v] = append(g[v], u)
}
q := []int{0}
+ vis := make([]bool, n)
vis[0] = true
- ans, step := 0, 0
- for len(q) > 0 {
- step++
+ for d := 1; len(q) > 0; d++ {
+ t := d * 2
for i := len(q); i > 0; i-- {
u := q[0]
q = q[1:]
for _, v := range g[u] {
- if vis[v] {
- continue
+ if !vis[v] {
+ vis[v] = true
+ q = append(q, v)
+ ans = max(ans, (t-1)/patience[v]*patience[v]+t+1)
}
- vis[v] = true
- q = append(q, v)
- d, t := step*2, patience[v]
- ans = max(ans, (d-1)/t*t+d+1)
}
}
}
- return ans
+ return
}
func max(a, b int) int {
@@ -229,6 +230,38 @@ func max(a, b int) int {
}
```
+### **TypeScript**
+
+```ts
+function networkBecomesIdle(edges: number[][], patience: number[]): number {
+ const n = patience.length;
+ const g: number[][] = Array.from({ length: n }, () => []);
+ for (const [u, v] of edges) {
+ g[u].push(v);
+ g[v].push(u);
+ }
+ const vis: boolean[] = Array.from({ length: n }, () => false);
+ vis[0] = true;
+ let q: number[] = [0];
+ let ans = 0;
+ for (let d = 1; q.length > 0; ++d) {
+ const t = d * 2;
+ const nq: number[] = [];
+ for (const u of q) {
+ for (const v of g[u]) {
+ if (!vis[v]) {
+ vis[v] = true;
+ nq.push(v);
+ ans = Math.max(ans, (((t - 1) / patience[v]) | 0) * patience[v] + t + 1);
+ }
+ }
+ }
+ q = nq;
+ }
+ return ans;
+}
+```
+
### **...**
```
diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp
index 981c4671b0aab..8e0f2a2407d1f 100644
--- a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp
+++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp
@@ -1,31 +1,33 @@
-class Solution {
-public:
- int networkBecomesIdle(vector>& edges, vector& patience) {
- int n = patience.size();
- vector> g(n);
- vector vis(n);
- for (auto& e : edges) {
- int u = e[0], v = e[1];
- g[u].push_back(v);
- g[v].push_back(u);
- }
- queue q{{0}};
- vis[0] = true;
- int ans = 0, step = 0;
- while (!q.empty()) {
- ++step;
- for (int i = q.size(); i > 0; --i) {
- int u = q.front();
- q.pop();
- for (int v : g[u]) {
- if (vis[v]) continue;
- vis[v] = true;
- q.push(v);
- int d = step * 2, t = patience[v];
- ans = max(ans, (d - 1) / t * t + d + 1);
- }
- }
- }
- return ans;
- }
+class Solution {
+public:
+ int networkBecomesIdle(vector>& edges, vector& patience) {
+ int n = patience.size();
+ vector g[n];
+ for (auto& e : edges) {
+ int u = e[0], v = e[1];
+ g[u].push_back(v);
+ g[v].push_back(u);
+ }
+ queue q{{0}};
+ bool vis[n];
+ memset(vis, false, sizeof(vis));
+ vis[0] = true;
+ int ans = 0, d = 0;
+ while (!q.empty()) {
+ ++d;
+ int t = d * 2;
+ for (int i = q.size(); i; --i) {
+ int u = q.front();
+ q.pop();
+ for (int v : g[u]) {
+ if (!vis[v]) {
+ vis[v] = true;
+ q.push(v);
+ ans = max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
+ }
+ }
+ }
+ }
+ return ans;
+ }
};
\ No newline at end of file
diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.go b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.go
index 792f63d927611..0a6462f45e487 100644
--- a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.go
+++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.go
@@ -1,32 +1,29 @@
-func networkBecomesIdle(edges [][]int, patience []int) int {
+func networkBecomesIdle(edges [][]int, patience []int) (ans int) {
n := len(patience)
g := make([][]int, n)
- vis := make([]bool, n)
for _, e := range edges {
u, v := e[0], e[1]
g[u] = append(g[u], v)
g[v] = append(g[v], u)
}
q := []int{0}
+ vis := make([]bool, n)
vis[0] = true
- ans, step := 0, 0
- for len(q) > 0 {
- step++
+ for d := 1; len(q) > 0; d++ {
+ t := d * 2
for i := len(q); i > 0; i-- {
u := q[0]
q = q[1:]
for _, v := range g[u] {
- if vis[v] {
- continue
+ if !vis[v] {
+ vis[v] = true
+ q = append(q, v)
+ ans = max(ans, (t-1)/patience[v]*patience[v]+t+1)
}
- vis[v] = true
- q = append(q, v)
- d, t := step*2, patience[v]
- ans = max(ans, (d-1)/t*t+d+1)
}
}
}
- return ans
+ return
}
func max(a, b int) int {
diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.java b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.java
index bda6c1eecfd9e..b259a633ab166 100644
--- a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.java
+++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.java
@@ -1,35 +1,32 @@
-class Solution {
- public int networkBecomesIdle(int[][] edges, int[] patience) {
- int n = patience.length;
- List[] g = new List[n];
- boolean[] vis = new boolean[n];
- Arrays.setAll(g, k -> new ArrayList<>());
- for (int[] e : edges) {
- int u = e[0], v = e[1];
- g[u].add(v);
- g[v].add(u);
- }
- int ans = 0;
- int step = 0;
- Deque q = new ArrayDeque<>();
- q.offer(0);
- vis[0] = true;
- while (!q.isEmpty()) {
- ++step;
- for (int i = q.size(); i > 0; --i) {
- int u = q.poll();
- for (int v : g[u]) {
- if (vis[v]) {
- continue;
- }
- vis[v] = true;
- q.offer(v);
- int d = step * 2;
- int t = patience[v];
- ans = Math.max(ans, (d - 1) / t * t + d + 1);
- }
- }
- }
- return ans;
- }
+class Solution {
+ public int networkBecomesIdle(int[][] edges, int[] patience) {
+ int n = patience.length;
+ List[] g = new List[n];
+ Arrays.setAll(g, k -> new ArrayList<>());
+ for (int[] e : edges) {
+ int u = e[0], v = e[1];
+ g[u].add(v);
+ g[v].add(u);
+ }
+ Deque q = new ArrayDeque<>();
+ q.offer(0);
+ boolean[] vis = new boolean[n];
+ vis[0] = true;
+ int ans = 0, d = 0;
+ while (!q.isEmpty()) {
+ ++d;
+ int t = d * 2;
+ for (int i = q.size(); i > 0; --i) {
+ int u = q.poll();
+ for (int v : g[u]) {
+ if (!vis[v]) {
+ vis[v] = true;
+ q.offer(v);
+ ans = Math.max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
+ }
+ }
+ }
+ }
+ return ans;
+ }
}
\ No newline at end of file
diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.py b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.py
index dfe03fb7d318b..51a365cb69498 100644
--- a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.py
+++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.py
@@ -1,21 +1,20 @@
-class Solution:
- def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
- g = defaultdict(list)
- for u, v in edges:
- g[u].append(v)
- g[v].append(u)
- q = deque([0])
- vis = {0}
- ans = step = 0
- while q:
- step += 1
- for _ in range(len(q)):
- u = q.popleft()
- for v in g[u]:
- if v in vis:
- continue
- vis.add(v)
- q.append(v)
- d, t = step * 2, patience[v]
- ans = max(ans, (d - 1) // t * t + d + 1)
- return ans
+class Solution:
+ def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
+ g = defaultdict(list)
+ for u, v in edges:
+ g[u].append(v)
+ g[v].append(u)
+ q = deque([0])
+ vis = {0}
+ ans = d = 0
+ while q:
+ d += 1
+ t = d * 2
+ for _ in range(len(q)):
+ u = q.popleft()
+ for v in g[u]:
+ if v not in vis:
+ vis.add(v)
+ q.append(v)
+ ans = max(ans, (t - 1) // patience[v] * patience[v] + t + 1)
+ return ans
diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.ts b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.ts
new file mode 100644
index 0000000000000..47e031cafcc26
--- /dev/null
+++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.ts
@@ -0,0 +1,27 @@
+function networkBecomesIdle(edges: number[][], patience: number[]): number {
+ const n = patience.length;
+ const g: number[][] = Array.from({ length: n }, () => []);
+ for (const [u, v] of edges) {
+ g[u].push(v);
+ g[v].push(u);
+ }
+ const vis: boolean[] = Array.from({ length: n }, () => false);
+ vis[0] = true;
+ let q: number[] = [0];
+ let ans = 0;
+ for (let d = 1; q.length > 0; ++d) {
+ const t = d * 2;
+ const nq: number[] = [];
+ for (const u of q) {
+ for (const v of g[u]) {
+ if (!vis[v]) {
+ vis[v] = true;
+ nq.push(v);
+ ans = Math.max(ans, (((t - 1) / patience[v]) | 0) * patience[v] + t + 1);
+ }
+ }
+ }
+ q = nq;
+ }
+ return ans;
+}