Skip to content

Commit ea4d1c1

Browse files
authored
Create 0838-push-dominoes.py
1 parent 91ddab2 commit ea4d1c1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

python/0838-push-dominoes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def pushDominoes(self, dominoes: str) -> str:
3+
dom = list(dominoes)
4+
q = collections.deque()
5+
for i, d in enumerate(dom):
6+
if d != '.':
7+
q.append((i, d))
8+
9+
while q:
10+
i, d = q.popleft()
11+
12+
if d == 'L' and i > 0 and dom[i - 1] == '.':
13+
q.append((i - 1, 'L'))
14+
dom[i - 1] = 'L'
15+
elif d == 'R':
16+
if i + 1 < len(dom) and dom[i + 1] == '.':
17+
if i + 2 < len(dom) and dom[i + 2] == 'L':
18+
q.popleft()
19+
else:
20+
q.append((i + 1, 'R'))
21+
dom[i + 1] = 'R'
22+
23+
return ''.join(dom)

0 commit comments

Comments
 (0)