KatInterview

868. Push Dominoes

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the final state of a line of dominoes after some are pushed left ('L') or right ('R'). An 'R' pushes its right neighbor, and an 'L' pushes its left neighbor. This propagation happens over time. A domino with forces from both sides remains standing. This can be modeled as two opposing forces propagating through the line of dominoes.

Solution

Push Dominoes in Python

class Solution:
    def pushDominoes(self, dominoes: str) -> str:
        n = len(dominoes)
        forces = [0] * n

        # Calculate forces from right-pushing dominoes ('R')
        # Iterate from left to right
        force = 0
        for i in range(n):
            if dominoes[i] == 'R':
                force = n  # A new push starts with max force
            elif dominoes[i] == 'L':
                force = 0  # Blocked by a left-pushing domino
            else:  # It's a '.'
                force = max(0, force - 1)  # Force decreases with distance
            forces[i] += force

        # Calculate forces from left-pushing dominoes ('L')
        # Iterate from right to left
        force = 0
        for i in range(n - 1, -1, -1):
            if dominoes[i] == 'L':
                force = n  # A new push starts with max force
            elif dominoes[i] == 'R':
                force = 0  # Blocked by a right-pushing domino
            else:  # It's a '.'
                force = max(0, force - 1)  # Force decreases with distance
            forces[i] -= force  # Subtract to get the net force

        result = []
        for f in forces:
            if f > 0:
                result.append('R')
            elif f < 0:
                result.append('L')
            else:
                result.append('.')
        
        return "".join(result)

Push Dominoes in Java

class Solution {
    public String pushDominoes(String dominoes) {
        int n = dominoes.length();
        int[] forces = new int[n];

        // Calculate forces from right-pushing dominoes ('R')
        // Iterate from left to right
        int force = 0;
        for (int i = 0; i < n; i++) {
            if (dominoes.charAt(i) == 'R') {
                force = n; // A new push starts with max force
            } else if (dominoes.charAt(i) == 'L') {
                force = 0; // Blocked by a left-pushing domino
            } else { // It's a '.'
                force = Math.max(0, force - 1); // Force decreases with distance
            }
            forces[i] += force;
        }

        // Calculate forces from left-pushing dominoes ('L')
        // Iterate from right to left
        force = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (dominoes.charAt(i) == 'L') {
                force = n; // A new push starts with max force
            } else if (dominoes.charAt(i) == 'R') {
                force = 0; // Blocked by a right-pushing domino
            } else { // It's a '.'
                force = Math.max(0, force - 1); // Force decreases with distance
            }
            forces[i] -= force; // Subtract to get the net force
        }

        StringBuilder result = new StringBuilder(n);
        for (int f : forces) {
            if (f > 0) {
                result.append('R');
            } else if (f < 0) {
                result.append('L');
            } else {
                result.append('.');
            }
        }
        return result.toString();
    }
}

Push Dominoes in C++

class Solution {
public:
    string pushDominoes(string dominoes) {
        int n = dominoes.length();
        vector<int> forces(n, 0);

        // Calculate forces from right-pushing dominoes ('R')
        // Iterate from left to right
        int force = 0;
        for (int i = 0; i < n; ++i) {
            if (dominoes[i] == 'R') {
                force = n; // A new push starts with max force
            } else if (dominoes[i] == 'L') {
                force = 0; // Blocked by a left-pushing domino
            } else { // It's a '.'
                force = max(0, force - 1); // Force decreases with distance
            }
            forces[i] += force;
        }

        // Calculate forces from left-pushing dominoes ('L')
        // Iterate from right to left
        force = 0;
        for (int i = n - 1; i >= 0; --i) {
            if (dominoes[i] == 'L') {
                force = n; // A new push starts with max force
            } else if (dominoes[i] == 'R') {
                force = 0; // Blocked by a right-pushing domino
            } else { // It's a '.'
                force = max(0, force - 1); // Force decreases with distance
            }
            forces[i] -= force; // Subtract to get the net force
        }

        string result = "";
        result.reserve(n);
        for (int f : forces) {
            if (f > 0) {
                result += 'R';
            } else if (f < 0) {
                result += 'L';
            } else {
                result += '.';
            }
        }
        return result;
    }
};

Push Dominoes in TypeScript

function pushDominoes(dominoes: string): string {
    const n = dominoes.length;
    const forces: number[] = new Array(n).fill(0);

    // Calculate forces from right-pushing dominoes ('R')
    // Iterate from left to right
    let force = 0;
    for (let i = 0; i < n; i++) {
        if (dominoes[i] === 'R') {
            force = n; // A new push starts with max force
        } else if (dominoes[i] === 'L') {
            force = 0; // Blocked by a left-pushing domino
        } else { // It's a '.'
            force = Math.max(0, force - 1); // Force decreases with distance
        }
        forces[i] += force;
    }

    // Calculate forces from left-pushing dominoes ('L')
    // Iterate from right to left
    force = 0;
    for (let i = n - 1; i >= 0; i--) {
        if (dominoes[i] === 'L') {
            force = n; // A new push starts with max force
        } else if (dominoes[i] === 'R') {
            force = 0; // Blocked by a right-pushing domino
        } else { // It's a '.'
            force = Math.max(0, force - 1); // Force decreases with distance
        }
        forces[i] -= force; // Subtract to get the net force
    }

    const result: string[] = [];
    for (const f of forces) {
        if (f > 0) {
            result.push('R');
        } else if (f < 0) {
            result.push('L');
        } else {
            result.push('.');
        }
    }

    return result.join('');
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.