Skip to content

Commit 710ed36

Browse files
committed
chore: update
1 parent 9e45ed2 commit 710ed36

File tree

2 files changed

+73
-91
lines changed

2 files changed

+73
-91
lines changed

solution/0800-0899/0838.Push Dominoes/README.md

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ tags:
2929
<p>给你一个字符串 <code>dominoes</code> 表示这一行多米诺骨牌的初始状态,其中:</p>
3030

3131
<ul>
32-
<li><code>dominoes[i] = 'L'</code>,表示第 <code>i</code> 张多米诺骨牌被推向左侧,</li>
33-
<li><code>dominoes[i] = 'R'</code>,表示第 <code>i</code> 张多米诺骨牌被推向右侧,</li>
34-
<li><code>dominoes[i] = '.'</code>,表示没有推动第 <code>i</code> 张多米诺骨牌。</li>
32+
<li><code>dominoes[i] = 'L'</code>,表示第 <code>i</code> 张多米诺骨牌被推向左侧,</li>
33+
<li><code>dominoes[i] = 'R'</code>,表示第 <code>i</code> 张多米诺骨牌被推向右侧,</li>
34+
<li><code>dominoes[i] = '.'</code>,表示没有推动第 <code>i</code> 张多米诺骨牌。</li>
3535
</ul>
3636

3737
<p>返回表示最终状态的字符串。</p>
@@ -57,9 +57,9 @@ tags:
5757
<p><strong>提示:</strong></p>
5858

5959
<ul>
60-
<li><code>n == dominoes.length</code></li>
61-
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
62-
<li><code>dominoes[i]</code> 为 <code>'L'</code>、<code>'R'</code> 或 <code>'.'</code></li>
60+
<li><code>n == dominoes.length</code></li>
61+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
62+
<li><code>dominoes[i]</code> 为 <code>'L'</code>、<code>'R'</code> 或 <code>'.'</code></li>
6363
</ul>
6464

6565
<!-- description:end -->
@@ -72,34 +72,24 @@ tags:
7272

7373
把所有初始受到推力的骨牌(`L` 或 `R`)视作 **源点**,它们会同时向外扩散各自的力。用队列按时间层级(0, 1, 2 …)进行 BFS:
7474

75-
1. **建模**
75+
我们定义 $\text{time[i]}$ 记录第 *i* 张骨牌第一次受力的时刻,`-1` 表示尚未受力,定义 $\text{force[i]}$ 是一个长度可变的列表,存放该骨牌在同一时刻收到的方向(`'L'``'R'`)。初始时把所有 `L/R` 的下标压入队列,并将它们的时间置 0。
7676

77-
- `time[i]` 记录第 *i* 张骨牌第一次受力的时刻,`-1` 表示尚未受力。
78-
- `force[i]` 是一个长度可变的列表,存放该骨牌在同一时刻收到的方向(`'L'``'R'`)。
79-
- 初始时把所有 `L/R` 的下标压入队列,并将它们的时间置 0。
77+
当弹出下标 *i* 时,若 $\text{force[i]}$ 只有一个方向,骨牌就会倒向该方向 $f$。设下一张骨牌下标为
8078

81-
2. **扩散规则**
79+
$$
80+
j =
81+
\begin{cases}
82+
i - 1, & f = L,\\
83+
i + 1, & f = R.
84+
\end{cases}
85+
$$
8286

83-
- 当弹出下标 *i* 时,若 `force[i]` 只有一个方向,骨牌就会倒向该方向 `f`
84-
- 设下一张骨牌下标为
87+
若 $0 \leq j < n$:
8588

86-
$$
87-
j=\begin{cases}
88-
i-1,& f=L\\[2pt]
89-
i+1,& f=R
90-
\end{cases}
91-
$$
89+
- 若 $\text{time[j]}=-1$,说明 *j* 从未受力,记录 $\text{time[j]}=\text{time[i]}+1$ 并入队,同时把 $f$ 写入 $\text{force[j]}$。
90+
- 若 $\text{time[j]}=\text{time[i]}+1$,说明它在同一“下一刻”已受过另一股力,此时只把 $f$ 追加到 $\text{force[j]}$,形成对冲;后续因 `len(force[j])==2`,它将保持竖直。
9291

93-
若 `0 ≤ j < n`
94-
95-
- 若 `time[j]==-1`,说明 *j* 从未受力,记录 `time[j]=time[i]+1` 并入队,同时把 `f` 写入 `force[j]`
96-
- 若 `time[j]==time[i]+1`,说明它在同一“下一刻”已受过另一股力,此时只把 `f` 追加到 `force[j]`,形成对冲;后续因 `len(force[j])==2`,它将保持竖直。
97-
98-
3. **终态判定**
99-
100-
- 队列清空后,所有 `force[i]` 长度为 1 的位置倒向对应方向;长度为 2 的位置保持 `.`。最终将字符数组拼接为答案。
101-
102-
最终相似字符串组的数量就是并查集中连通分量的数量。
92+
队列清空后,所有 $\text{force[i]}$ 长度为 1 的位置倒向对应方向;长度为 2 的位置保持 `.`。最终将字符数组拼接为答案。
10393

10494
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是骨牌的数量。
10595

solution/0800-0899/0838.Push Dominoes/README_EN.md

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,26 @@ tags:
6969

7070
### Solution 1: Multi-Source BFS
7171

72-
Treat all dominoes initially pushed (`L` or `R`) as **sources**, which simultaneously propagate their forces outward. Use a queue to perform BFS layer by layer (0, 1, 2, ...):
72+
Treat all initially pushed dominoes (`L` or `R`) as **sources**, which simultaneously propagate their forces outward. Use a queue to perform BFS layer by layer (0, 1, 2, ...):
7373

74-
1. **Modeling**
74+
We define $\text{time[i]}$ to record the first moment when the _i_-th domino is affected by a force, with `-1` indicating it has not been affected yet. We also define $\text{force[i]}$ as a variable-length list that stores the directions (`'L'`, `'R'`) of forces acting on the domino at the same moment. Initially, push all indices of `L/R` dominoes into the queue and set their `time` to 0.
7575

76-
- `time[i]` records the first moment when the _i_-th domino is affected by a force, with `-1` indicating it has not been affected yet.
77-
- `force[i]` is a variable-length list that stores the directions (`'L'`, `'R'`) of forces acting on the domino at the same moment.
78-
- Initially, push all indices of `L/R` dominoes into the queue and set their `time` to 0.
76+
When dequeuing index _i_, if $\text{force[i]}$ contains only one direction, the domino will fall in that direction $f$. Let the index of the next domino be:
7977

80-
2. **Propagation Rules**
78+
$$
79+
j =
80+
\begin{cases}
81+
i - 1, & f = L,\\
82+
i + 1, & f = R.
83+
\end{cases}
84+
$$
8185

82-
- When dequeuing index _i_, if `force[i]` contains only one direction, the domino will fall in that direction `f`.
83-
- Let the index of the next domino be:
86+
If $0 \leq j < n$:
8487

85-
$$
86-
j=\begin{cases}
87-
i-1,& f=L\\[2pt]
88-
i+1,& f=R
89-
\end{cases}
90-
$$
88+
- If $\text{time[j]} = -1$, it means _j_ has not been affected yet. Record $\text{time[j]} = \text{time[i]} + 1$, enqueue it, and append $f$ to $\text{force[j]}$.
89+
- If $\text{time[j]} = \text{time[i]} + 1$, it means _j_ has already been affected by another force at the same "next moment." In this case, append $f$ to $\text{force[j]}$, causing a standoff. Subsequently, since $\text{len(force[j])} = 2$, it will remain upright.
9190

92-
If $0 \leq j < n$:
93-
94-
- If `time[j] == -1`, it means _j_ has not been affected yet. Record `time[j] = time[i] + 1`, enqueue it, and append `f` to `force[j]`.
95-
- If `time[j] == time[i] + 1`, it means _j_ has already been affected by another force at the same "next moment." In this case, append `f` to `force[j]`, causing a standoff. Subsequently, since `len(force[j]) == 2`, it will remain upright.
96-
97-
3. **Final State Determination**
98-
99-
- After the queue is emptied, all positions where `force[i]` has a length of 1 will fall in the corresponding direction, while positions with a length of 2 will remain as `.`. Finally, concatenate the character array to form the answer.
91+
After the queue is emptied, all positions where $\text{force[i]}$ has a length of 1 will fall in the corresponding direction, while positions with a length of 2 will remain as `.`. Finally, concatenate the character array to form the answer.
10092

10193
The complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of dominoes.
10294

@@ -224,46 +216,46 @@ public:
224216

225217
```go
226218
func pushDominoes(dominoes string) string {
227-
n := len(dominoes)
228-
q := []int{}
229-
time := make([]int, n)
230-
for i := range time {
231-
time[i] = -1
232-
}
233-
force := make([][]byte, n)
234-
for i, c := range dominoes {
235-
if c != '.' {
236-
q = append(q, i)
237-
time[i] = 0
238-
force[i] = append(force[i], byte(c))
239-
}
240-
}
241-
242-
ans := bytes.Repeat([]byte{'.'}, n)
243-
for len(q) > 0 {
244-
i := q[0]
245-
q = q[1:]
246-
if len(force[i]) > 1 {
247-
continue
248-
}
249-
f := force[i][0]
250-
ans[i] = f
251-
j := i - 1
252-
if f == 'R' {
253-
j = i + 1
254-
}
255-
if 0 <= j && j < n {
256-
t := time[i]
257-
if time[j] == -1 {
258-
q = append(q, j)
259-
time[j] = t + 1
260-
force[j] = append(force[j], f)
261-
} else if time[j] == t+1 {
262-
force[j] = append(force[j], f)
263-
}
264-
}
265-
}
266-
return string(ans)
219+
n := len(dominoes)
220+
q := []int{}
221+
time := make([]int, n)
222+
for i := range time {
223+
time[i] = -1
224+
}
225+
force := make([][]byte, n)
226+
for i, c := range dominoes {
227+
if c != '.' {
228+
q = append(q, i)
229+
time[i] = 0
230+
force[i] = append(force[i], byte(c))
231+
}
232+
}
233+
234+
ans := bytes.Repeat([]byte{'.'}, n)
235+
for len(q) > 0 {
236+
i := q[0]
237+
q = q[1:]
238+
if len(force[i]) > 1 {
239+
continue
240+
}
241+
f := force[i][0]
242+
ans[i] = f
243+
j := i - 1
244+
if f == 'R' {
245+
j = i + 1
246+
}
247+
if 0 <= j && j < n {
248+
t := time[i]
249+
if time[j] == -1 {
250+
q = append(q, j)
251+
time[j] = t + 1
252+
force[j] = append(force[j], f)
253+
} else if time[j] == t+1 {
254+
force[j] = append(force[j], f)
255+
}
256+
}
257+
}
258+
return string(ans)
267259
}
268260
```
269261

0 commit comments

Comments
 (0)