Участник:Gadaevtamaz/Push Dominoes

Материал из DISCOPAL
Перейти к: навигация, поиск
class Solution(object):
    def pushDominoes(self, dominoes):
        falling = [(-1, 'L')] + [(i, d) for i, d in enumerate(dominoes) if d != '.'] + [(len(dominoes), 'R')]
        final_state = list(dominoes)
        for (i, a), (j, b) in zip(falling, falling[1:]):
            if (a == 'R') and (b == 'L'):
                for l in range(i + 1, j):
                    simultanious = (l - i) == (j - l)
                    left = (l - i) > (j - l)
                    right = (l - i) < (j - l)
                    final_state[l] = simultanious * '.' + left * 'L' + right * 'R'
            elif a == b:
                for l in range(i + 1, j):
                    final_state[l] = a
 
        return ''.join(final_state)