forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPush Dominoes.py
43 lines (43 loc) · 1.26 KB
/
Push Dominoes.py
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
32
33
34
35
36
37
38
39
40
41
42
43
class Solution:
def pushDominoes(self, dom: str) -> str:
from collections import deque
n = len(dom)
d = set()
q = deque()
arr = [0 for i in range(n)]
for i in range(n):
if dom[i] == "L":
arr[i] = -1
d.add(i)
q.append((i,"L"))
if dom[i] == "R":
arr[i] = 1
d.add(i)
q.append((i,"R"))
while q:
t1 = set()
for _ in range(len(q)):
t = q.popleft()
if t[1] == "L":
if t[0]-1 >= 0 and t[0]-1 not in d:
t1.add(t[0]-1)
arr[t[0]-1] -= 1
else:
if t[0]+1 < n and t[0]+1 not in d:
t1.add(t[0]+1)
arr[t[0]+1] += 1
for val in t1:
d.add(val)
if arr[val] > 0:
q.append((val,"R"))
elif arr[val]<0:
q.append((val,"L"))
ans = ""
for val in arr:
if val<0:
ans += "L"
elif val>0:
ans += "R"
else:
ans += "."
return ans