-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0838_Push_Dominoes.py
More file actions
31 lines (28 loc) · 996 Bytes
/
0838_Push_Dominoes.py
File metadata and controls
31 lines (28 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
def pushDominoes(self, dominoes: str) -> str:
dominoes = list(dominoes)
n = len(dominoes)
last_right = -1
last_left = 0
for i, d in enumerate(dominoes):
if d == 'R':
if last_right != -1:
for j in range(last_right + 1, i):
dominoes[j] = 'R'
last_right = i
elif d == 'L':
if last_right != -1:
l, r = last_right + 1, i - 1
while l < r:
dominoes[l], dominoes[r] = 'R', 'L'
l += 1
r -= 1
last_right = -1
else:
for j in range(last_left, i):
dominoes[j] = 'L'
last_left = i
if last_right != -1:
for i in range(last_right + 1, n):
dominoes[i] = 'R'
return ''.join(dominoes)