forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-dominoes.py
35 lines (29 loc) · 831 Bytes
/
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
# Time: O(n)
# Space: O(n)
class Solution(object):
def pushDominoes(self, dominoes):
"""
:type dominoes: str
:rtype: str
"""
force = [0]*len(dominoes)
f = 0
for i in xrange(len(dominoes)):
if dominoes[i] == 'R':
f = len(dominoes)
elif dominoes[i] == 'L':
f = 0
else:
f = max(f-1, 0)
force[i] += f
f = 0
for i in reversed(xrange(len(dominoes))):
if dominoes[i] == 'L':
f = len(dominoes)
elif dominoes[i] == 'R':
f = 0
else:
f = max(f-1, 0)
force[i] -= f
return "".join('.' if f == 0 else 'R' if f > 0 else 'L'
for f in force)