Skip to content

Commit b3ed3bb

Browse files
committed
Sync LeetCode submission Runtime - 275 ms (23.36%), Memory - 22.6 MB (57.57%)
1 parent 1d20a2c commit b3ed3bb

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

0868-push-dominoes/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>There are <code>n</code> dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.</p>
2+
3+
<p>After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.</p>
4+
5+
<p>When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.</p>
6+
7+
<p>For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.</p>
8+
9+
<p>You are given a string <code>dominoes</code> representing the initial state where:</p>
10+
11+
<ul>
12+
<li><code>dominoes[i] = &#39;L&#39;</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the left,</li>
13+
<li><code>dominoes[i] = &#39;R&#39;</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the right, and</li>
14+
<li><code>dominoes[i] = &#39;.&#39;</code>, if the <code>i<sup>th</sup></code> domino has not been pushed.</li>
15+
</ul>
16+
17+
<p>Return <em>a string representing the final state</em>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> dominoes = &quot;RR.L&quot;
24+
<strong>Output:</strong> &quot;RR.L&quot;
25+
<strong>Explanation:</strong> The first domino expends no additional force on the second domino.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/05/18/domino.png" style="height: 196px; width: 512px;" />
30+
<pre>
31+
<strong>Input:</strong> dominoes = &quot;.L.R...LR..L..&quot;
32+
<strong>Output:</strong> &quot;LL.RR.LLRRLL..&quot;
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>n == dominoes.length</code></li>
40+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
41+
<li><code>dominoes[i]</code> is either <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, or <code>&#39;.&#39;</code>.</li>
42+
</ul>

0868-push-dominoes/solution.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach 2: Calculate Force
2+
3+
# Time: O(N)
4+
5+
class Solution:
6+
def pushDominoes(self, dominoes: str) -> str:
7+
N = len(dominoes)
8+
force = [0] * N
9+
10+
# Left to Right
11+
f = 0
12+
for i in range(N):
13+
if dominoes[i] == 'R':
14+
f = N
15+
elif dominoes[i] == 'L':
16+
f = 0
17+
else:
18+
f = max(f - 1, 0)
19+
force[i] += f
20+
21+
# Right to Left
22+
f = 0
23+
for i in range(N - 1, -1, -1):
24+
if dominoes[i] == 'L':
25+
f = N
26+
elif dominoes[i] == 'R':
27+
f = 0
28+
else:
29+
f = max(f - 1, 0)
30+
force[i] -= f
31+
32+
return ''.join('.' if f == 0
33+
else 'R' if f > 0
34+
else 'L'
35+
for f in force)

0 commit comments

Comments
 (0)