Skip to content

Commit 67032f8

Browse files
authored
feat: add solutions to lc problems: No.2038,2039 (#1763)
* No.2038.Remove Colored Pieces if Both Neighbors are the Same Color * No.2039.The Time When the Network Becomes Idle
1 parent 675481a commit 67032f8

File tree

10 files changed

+344
-183
lines changed

10 files changed

+344
-183
lines changed

solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ ABBBB<strong><em>B</em></strong>BBAA -&gt; ABBBBBBAA
7777

7878
**方法一:计数**
7979

80-
统计字符串 `colors` 中连续出现 $3$ 个 `'A'` 或 $3$ 个 `'B'` 的个数,分别记为 $a$ 和 $b$。
80+
我们统计字符串 `colors` 中连续出现 $3$ 个 `'A'` 或 $3$ 个 `'B'` 的个数,分别记为 $a$ 和 $b$。
8181

8282
最后判断 $a$ 是否大于 $b$,是则返回 `true`,否则返回 `false`
8383

84-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 `colors` 的长度。
84+
时间复杂度 $O(n)$,其中 $n$ 为字符串 `colors` 的长度。空间复杂度 $O(1)$
8585

8686
<!-- tabs:start -->
8787

@@ -178,6 +178,29 @@ func winnerOfGame(colors string) bool {
178178
}
179179
```
180180

181+
### **TypeScript**
182+
183+
```ts
184+
function winnerOfGame(colors: string): boolean {
185+
const n = colors.length;
186+
let [a, b] = [0, 0];
187+
for (let i = 0, j = 0; i < n; i = j) {
188+
while (j < n && colors[j] === colors[i]) {
189+
++j;
190+
}
191+
const m = j - i - 2;
192+
if (m > 0) {
193+
if (colors[i] === 'A') {
194+
a += m;
195+
} else {
196+
b += m;
197+
}
198+
}
199+
}
200+
return a > b;
201+
}
202+
```
203+
181204
### **...**
182205

183206
```

solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README_EN.md

+31
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ Thus, Bob wins, so return false.
7272

7373
## Solutions
7474

75+
**Solution 1: Counting**
76+
77+
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.
78+
79+
Finally, we check whether $a$ is greater than $b$. If it is, we return `true`. Otherwise, we return `false`.
80+
81+
The time complexity is $O(n)$, where $n$ is the length of the string `colors`. The space complexity is $O(1)$.
82+
7583
<!-- tabs:start -->
7684

7785
### **Python3**
@@ -163,6 +171,29 @@ func winnerOfGame(colors string) bool {
163171
}
164172
```
165173

174+
### **TypeScript**
175+
176+
```ts
177+
function winnerOfGame(colors: string): boolean {
178+
const n = colors.length;
179+
let [a, b] = [0, 0];
180+
for (let i = 0, j = 0; i < n; i = j) {
181+
while (j < n && colors[j] === colors[i]) {
182+
++j;
183+
}
184+
const m = j - i - 2;
185+
if (m > 0) {
186+
if (colors[i] === 'A') {
187+
a += m;
188+
} else {
189+
b += m;
190+
}
191+
}
192+
}
193+
return a > b;
194+
}
195+
```
196+
166197
### **...**
167198

168199
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function winnerOfGame(colors: string): boolean {
2+
const n = colors.length;
3+
let [a, b] = [0, 0];
4+
for (let i = 0, j = 0; i < n; i = j) {
5+
while (j < n && colors[j] === colors[i]) {
6+
++j;
7+
}
8+
const m = j - i - 2;
9+
if (m > 0) {
10+
if (colors[i] === 'A') {
11+
a += m;
12+
} else {
13+
b += m;
14+
}
15+
}
16+
}
17+
return a > b;
18+
}

solution/2000-2099/2039.The Time When the Network Becomes Idle/README.md

+76-42
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@
8686

8787
<!-- 这里可写通用的实现逻辑 -->
8888

89-
用 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`,找出所有空间时间的最大值即可。
89+
**方法一:BFS**
90+
91+
我们先根据二维数组 $edges$ 构建无向图 $g$,其中 $g[u]$ 表示节点 $u$ 的所有邻居节点。
92+
93+
然后,我们可以使用广度优先搜索的方式,找出每个节点 $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$ 秒的处理时间,即为该数据服务器变为空闲的最早时间。我们找出最晚的这个时间,即为计算机网络变为空闲的最早时间。
94+
95+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点数。
9096

9197
<!-- tabs:start -->
9298

@@ -103,18 +109,17 @@ class Solution:
103109
g[v].append(u)
104110
q = deque([0])
105111
vis = {0}
106-
ans = step = 0
112+
ans = d = 0
107113
while q:
108-
step += 1
114+
d += 1
115+
t = d * 2
109116
for _ in range(len(q)):
110117
u = q.popleft()
111118
for v in g[u]:
112-
if v in vis:
113-
continue
114-
vis.add(v)
115-
q.append(v)
116-
d, t = step * 2, patience[v]
117-
ans = max(ans, (d - 1) // t * t + d + 1)
119+
if v not in vis:
120+
vis.add(v)
121+
q.append(v)
122+
ans = max(ans, (t - 1) // patience[v] * patience[v] + t + 1)
118123
return ans
119124
```
120125

@@ -127,37 +132,35 @@ class Solution {
127132
public int networkBecomesIdle(int[][] edges, int[] patience) {
128133
int n = patience.length;
129134
List<Integer>[] g = new List[n];
130-
boolean[] vis = new boolean[n];
131135
Arrays.setAll(g, k -> new ArrayList<>());
132136
for (int[] e : edges) {
133137
int u = e[0], v = e[1];
134138
g[u].add(v);
135139
g[v].add(u);
136140
}
137-
int ans = 0;
138-
int step = 0;
139141
Deque<Integer> q = new ArrayDeque<>();
140142
q.offer(0);
143+
boolean[] vis = new boolean[n];
141144
vis[0] = true;
145+
int ans = 0, d = 0;
142146
while (!q.isEmpty()) {
143-
++step;
147+
++d;
148+
int t = d * 2;
144149
for (int i = q.size(); i > 0; --i) {
145150
int u = q.poll();
146151
for (int v : g[u]) {
147-
if (vis[v]) {
148-
continue;
152+
if (!vis[v]) {
153+
vis[v] = true;
154+
q.offer(v);
155+
ans = Math.max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
149156
}
150-
vis[v] = true;
151-
q.offer(v);
152-
int d = step * 2;
153-
int t = patience[v];
154-
ans = Math.max(ans, (d - 1) / t * t + d + 1);
155157
}
156158
}
157159
}
158160
return ans;
159161
}
160162
}
163+
161164
```
162165

163166
### **C++**
@@ -167,27 +170,29 @@ class Solution {
167170
public:
168171
int networkBecomesIdle(vector<vector<int>>& edges, vector<int>& patience) {
169172
int n = patience.size();
170-
vector<vector<int>> g(n);
171-
vector<bool> vis(n);
173+
vector<int> g[n];
172174
for (auto& e : edges) {
173175
int u = e[0], v = e[1];
174176
g[u].push_back(v);
175177
g[v].push_back(u);
176178
}
177179
queue<int> q{{0}};
180+
bool vis[n];
181+
memset(vis, false, sizeof(vis));
178182
vis[0] = true;
179-
int ans = 0, step = 0;
183+
int ans = 0, d = 0;
180184
while (!q.empty()) {
181-
++step;
182-
for (int i = q.size(); i > 0; --i) {
185+
++d;
186+
int t = d * 2;
187+
for (int i = q.size(); i; --i) {
183188
int u = q.front();
184189
q.pop();
185190
for (int v : g[u]) {
186-
if (vis[v]) continue;
187-
vis[v] = true;
188-
q.push(v);
189-
int d = step * 2, t = patience[v];
190-
ans = max(ans, (d - 1) / t * t + d + 1);
191+
if (!vis[v]) {
192+
vis[v] = true;
193+
q.push(v);
194+
ans = max(ans, (t - 1) / patience[v] * patience[v] + t + 1);
195+
}
191196
}
192197
}
193198
}
@@ -199,35 +204,32 @@ public:
199204
### **Go**
200205
201206
```go
202-
func networkBecomesIdle(edges [][]int, patience []int) int {
207+
func networkBecomesIdle(edges [][]int, patience []int) (ans int) {
203208
n := len(patience)
204209
g := make([][]int, n)
205-
vis := make([]bool, n)
206210
for _, e := range edges {
207211
u, v := e[0], e[1]
208212
g[u] = append(g[u], v)
209213
g[v] = append(g[v], u)
210214
}
211215
q := []int{0}
216+
vis := make([]bool, n)
212217
vis[0] = true
213-
ans, step := 0, 0
214-
for len(q) > 0 {
215-
step++
218+
for d := 1; len(q) > 0; d++ {
219+
t := d * 2
216220
for i := len(q); i > 0; i-- {
217221
u := q[0]
218222
q = q[1:]
219223
for _, v := range g[u] {
220-
if vis[v] {
221-
continue
224+
if !vis[v] {
225+
vis[v] = true
226+
q = append(q, v)
227+
ans = max(ans, (t-1)/patience[v]*patience[v]+t+1)
222228
}
223-
vis[v] = true
224-
q = append(q, v)
225-
d, t := step*2, patience[v]
226-
ans = max(ans, (d-1)/t*t+d+1)
227229
}
228230
}
229231
}
230-
return ans
232+
return
231233
}
232234
233235
func max(a, b int) int {
@@ -238,6 +240,38 @@ func max(a, b int) int {
238240
}
239241
```
240242

243+
### **TypeScript**
244+
245+
```ts
246+
function networkBecomesIdle(edges: number[][], patience: number[]): number {
247+
const n = patience.length;
248+
const g: number[][] = Array.from({ length: n }, () => []);
249+
for (const [u, v] of edges) {
250+
g[u].push(v);
251+
g[v].push(u);
252+
}
253+
const vis: boolean[] = Array.from({ length: n }, () => false);
254+
vis[0] = true;
255+
let q: number[] = [0];
256+
let ans = 0;
257+
for (let d = 1; q.length > 0; ++d) {
258+
const t = d * 2;
259+
const nq: number[] = [];
260+
for (const u of q) {
261+
for (const v of g[u]) {
262+
if (!vis[v]) {
263+
vis[v] = true;
264+
nq.push(v);
265+
ans = Math.max(ans, (((t - 1) / patience[v]) | 0) * patience[v] + t + 1);
266+
}
267+
}
268+
}
269+
q = nq;
270+
}
271+
return ans;
272+
}
273+
```
274+
241275
### **...**
242276

243277
```

0 commit comments

Comments
 (0)