Skip to main content

838 - Push Dominoes

Details

KeyValue
Linkhttps://leetcode.com/problems/push-dominoes/
LanguagePython 3
Runtime462 ms, faster than 65.19% of Python3 online submissions for Push Dominoes
Memory Usage15.5 MB, less than 95.79% of Python3 online submissions for Push Dominoes
Datastructuresstr, int
AlgorithmsTwo-Pointers
ComplexityTime: O(N) Memory: O(N)

Procedure

  1. ...

Code

class Solution:
def pushDominoes(self, dominoes: str) -> str:
dominoes = 'L' + dominoes + 'R'
result, left = "", 0

for right in range(1, len(dominoes)):
if dominoes[right] == '.': continue

if left: result += dominoes[left]

middle = right - left - 1
if dominoes[left] == dominoes[right]:
result += dominoes[left] * middle
elif dominoes[left] == 'L' and dominoes[right] == 'R':
result += '.' * middle
else:
result += 'R' * (middle // 2) + '.' * (middle % 2) + 'L' * (middle // 2)

left = right

return result