We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Push Dominoes

Number: 868

Difficulty: Medium

Paid? No

Companies: Google


Problem Description

Given a string representing dominoes where each character is either 'L' (pushed left), 'R' (pushed right), or '.' (upright), determine the final state after all dominoes have fallen. Each second, a domino falling to the left pushes the adjacent left domino, and a domino falling to the right pushes the adjacent right domino. When a domino receives forces from both sides, it remains upright.


Key Insights

  • The forces from dominoes falling right propagate to the right, while forces from dominoes falling left propagate to the left.
  • By simulating these forces in two passes (left-to-right and right-to-left) and then comparing the net force at each domino, we determine its final direction.
  • Use an array (or list) to store the cumulative force from both directions.
  • Edge cases occur when forces cancel out, resulting in a domino staying upright.

Space and Time Complexity

Time Complexity: O(n) because each pass over the string takes linear time.
Space Complexity: O(n) for storing the forces for each domino.


Solution

The solution uses two passes:

  1. In the left-to-right pass, simulate the force from dominoes falling right. When a domino 'R' is encountered, assign maximum force (using n as a high constant), and decrement the force by 1 for each subsequent domino until a 'L' is found (which resets the force).
  2. In the right-to-left pass, simulate force from dominoes falling left similarly.
  3. For each domino, the net force is the difference between the rightward and leftward forces. If positive, the domino falls right ('R'); if negative, it falls left ('L'); if zero, it remains upright ('.').

Data structures:

  • An array (or list) to store net forces.
  • Simple iteration with constant time operations per domino.

Code Solutions

# Python solution for the "Push Dominoes" problem

class Solution:
    def pushDominoes(self, dominoes: str) -> str:
        n = len(dominoes)
        forces = [0] * n
        
        force = 0
        # Left-to-right pass: simulate rightward forces
        for i in range(n):
            if dominoes[i] == 'R':
                force = n  # maximum force for 'R'
            elif dominoes[i] == 'L':
                force = 0
            else:  # domino is upright '.'
                force = max(force - 1, 0)
            forces[i] += force
        
        force = 0
        # Right-to-left pass: simulate leftward forces
        for i in range(n - 1, -1, -1):
            if dominoes[i] == 'L':
                force = n  # maximum force for 'L'
            elif dominoes[i] == 'R':
                force = 0
            else:
                force = max(force - 1, 0)
            forces[i] -= force
        
        # Build the final state based on net force at each position
        result = []
        for f in forces:
            if f > 0:
                result.append('R')
            elif f < 0:
                result.append('L')
            else:
                result.append('.')
        return "".join(result)

# Example usage:
# solution = Solution()
# print(solution.pushDominoes(".L.R...LR..L.."))
← Back to All Questions