Domino Chain Reaction

Domino Chain Reaction – Final State of Falling Dominoes

Problem Statement:

You're given a string representing a row of dominoes. Each domino can be:

  • 'L': Pushed to the left

  • 'R': Pushed to the right

  • '.': Upright and not pushed

Each second:

  • A domino falling to the left pushes the adjacent left domino

  • A domino falling to the right pushes the adjacent right domino

  • If a domino receives force from both sides simultaneously, it stays upright

Return the final state of the dominoes after all movement stops.

Example:

Input: "RR.L"
Output: "RR.L"
Explanation: The leftward falling domino does not affect the earlier rightward chain due to the time difference.

Best Data Structure for the Job:

Use an integer array to simulate forces across the dominoes. This approach captures direction and intensity efficiently.

Different Approaches

Approach 1: Brute Force Simulation

Simulate each second, updating the state of dominoes until there are no more change.

public class BruteForceDominoes { public String pushDominoes(String dominoes) { char[] arr = dominoes.toCharArray(); int n = arr.length; boolean changed; do { changed = false; char[] temp = arr.clone(); for (int i = 0; i < n; i++) { if (arr[i] == '.') { boolean left = i > 0 && arr[i - 1] == 'R'; boolean right = i < n - 1 && arr[i + 1] == 'L'; if (left && !right) { temp[i] = 'R'; changed = true; } else if (!left && right) { temp[i] = 'L'; changed = true; } } } arr = temp; } while (changed); return new String(arr); } }

Approach 2: Force-based Simulation (Optimal)

Instead of simulating second by second, compute the force exerted by 'R' and 'L' across the string and resolve based on net force.

public class OptimalDominoes { public String pushDominoes(String dominoes) { int n = dominoes.length(); int[] forces = new int[n]; int force = 0; for (int i = 0; i < n; i++) { if (dominoes.charAt(i) == 'R') { force = n; } else if (dominoes.charAt(i) == 'L') { force = 0; } else { force = Math.max(force - 1, 0); } forces[i] += force; } force = 0; for (int i = n - 1; i >= 0; i--) { if (dominoes.charAt(i) == 'L') { force = n; } else if (dominoes.charAt(i) == 'R') { force = 0; } else { force = Math.max(force - 1, 0); } forces[i] -= force; } StringBuilder result = new StringBuilder(); for (int f : forces) { if (f > 0) result.append('R'); else if (f < 0) result.append('L'); else result.append('.'); } return result.toString(); } }

⚠️Common Pitfalls:

  • Forgetting to reset force when encountering an opposing domino

  • Overwriting instead of adding/subtracting forces

  • Not accounting for time-based influence reduction

📊 Time and Space Complexity:

Approach        Time Complexity     Space Complexity
Brute Force          O(N²)       O(N)
Force-based Optimal          O(N)       O(N)

🌍Real-world Applications:

  • Chain reactions in physics simulations

  • Modeling crowd dynamics or cascading animations

  • Balancing forces in algorithmic game development


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries