You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>You are given a string <code>dominoes</code> representing the initial state where:</p>
30
30
31
31
<ul>
32
-
<li><code>dominoes[i] = 'L'</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the left,</li>
33
-
<li><code>dominoes[i] = 'R'</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the right, and</li>
34
-
<li><code>dominoes[i] = '.'</code>, if the <code>i<sup>th</sup></code> domino has not been pushed.</li>
32
+
<li><code>dominoes[i] = 'L'</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the left,</li>
33
+
<li><code>dominoes[i] = 'R'</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the right, and</li>
34
+
<li><code>dominoes[i] = '.'</code>, if the <code>i<sup>th</sup></code> domino has not been pushed.</li>
35
35
</ul>
36
36
37
37
<p>Return <em>a string representing the final state</em>.</p>
@@ -56,9 +56,9 @@ tags:
56
56
<p><strong>Constraints:</strong></p>
57
57
58
58
<ul>
59
-
<li><code>n == dominoes.length</code></li>
60
-
<li><code>1 <= n <= 10<sup>5</sup></code></li>
61
-
<li><code>dominoes[i]</code> is either <code>'L'</code>, <code>'R'</code>, or <code>'.'</code>.</li>
59
+
<li><code>n == dominoes.length</code></li>
60
+
<li><code>1 <= n <= 10<sup>5</sup></code></li>
61
+
<li><code>dominoes[i]</code> is either <code>'L'</code>, <code>'R'</code>, or <code>'.'</code>.</li>
62
62
</ul>
63
63
64
64
<!-- description:end -->
@@ -67,7 +67,38 @@ tags:
67
67
68
68
<!-- solution:start -->
69
69
70
-
### Solution 1
70
+
### Solution 1: Multi-Source BFS
71
+
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, ...):
73
+
74
+
1.**Modeling**
75
+
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.
79
+
80
+
2.**Propagation Rules**
81
+
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:
84
+
85
+
$$
86
+
j=\begin{cases}
87
+
i-1,& f=L\\[2pt]
88
+
i+1,& f=R
89
+
\end{cases}
90
+
$$
91
+
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.
100
+
101
+
The complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of dominoes.
0 commit comments