Skip to content

Commit

Permalink
Create 0838-push-dominoes.py
Browse files Browse the repository at this point in the history
  • Loading branch information
GraydenH authored Mar 8, 2023
1 parent 91ddab2 commit ea4d1c1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions python/0838-push-dominoes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def pushDominoes(self, dominoes: str) -> str:
dom = list(dominoes)
q = collections.deque()
for i, d in enumerate(dom):
if d != '.':
q.append((i, d))

while q:
i, d = q.popleft()

if d == 'L' and i > 0 and dom[i - 1] == '.':
q.append((i - 1, 'L'))
dom[i - 1] = 'L'
elif d == 'R':
if i + 1 < len(dom) and dom[i + 1] == '.':
if i + 2 < len(dom) and dom[i + 2] == 'L':
q.popleft()
else:
q.append((i + 1, 'R'))
dom[i + 1] = 'R'

return ''.join(dom)

0 comments on commit ea4d1c1

Please sign in to comment.