Skip to content

Commit 0abf8aa

Browse files
committed
May 02
1 parent d29af1c commit 0abf8aa

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution:
2+
def pushDominoes(self, dominoes: str) -> str:
3+
n = len(dominoes)
4+
forces = [0] * n
5+
6+
# forward pass
7+
force = 0
8+
for i in range(n):
9+
if dominoes[i] == 'L':
10+
force = 0
11+
elif dominoes[i] == 'R':
12+
force = n
13+
else:
14+
force = max(0, force - 1)
15+
forces[i] += force
16+
17+
# backward pass
18+
force = 0
19+
for i in range(n - 1, -1, -1):
20+
if dominoes[i] == 'L':
21+
force = n
22+
elif dominoes[i] == 'R':
23+
force = 0
24+
else:
25+
force = max(0, force - 1)
26+
forces[i] -= force
27+
28+
# final state
29+
result = ''
30+
for force in forces:
31+
if force > 0:
32+
result += 'R'
33+
elif force < 0:
34+
result += 'L'
35+
else:
36+
result += '.'
37+
return result
38+
39+
40+
def main():
41+
dominoes = 'RR.L'
42+
assert Solution().pushDominoes(dominoes) == 'RR.L'
43+
44+
dominoes = '.L.R...LR..L..'
45+
assert Solution().pushDominoes(dominoes) == 'LL.RR.LLRRLL..'
46+
47+
48+
if __name__ == '__main__':
49+
main()

2025-05-May-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| Day | Problem | Level | Status |
66
| --- | --- | --- | --- |
77
| May 01 | [2071. Maximum Number of Tasks You Can Assign](https://leetcode.com/problems/maximum-number-of-tasks-you-can-assign/) | Hard | Solved |
8-
| May 02 | []() | | |
8+
| May 02 | [838. Push Dominoes](https://leetcode.com/problems/push-dominoes/) | Medium | Solved |
99
| May 03 | []() | | |
1010
| May 04 | []() | | |
1111
| May 05 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 0 | 0 | 0 |
44-
| Medium | 0 | 0 | 0 |
44+
| Medium | 1 | 1 | 0 |
4545
| Hard | 1 | 1 | 0 |

0 commit comments

Comments
 (0)