Push Dominoes
Work out how a row of dominoes settles after some of them are pushed left or right.
A row of dominoes is given as a string dominoes'L''R''.'
Example 1
Output: "RR.L"
Explanation: the third domino is hit from both sides in the same second, so it stays upright.
Example 2
Output: "LL.RR.LLRRLL.."
The Idea
You have probably set up a row of dominoes and tipped one over. Here several are tipped at once, some to the left and some to the right, and the question is what the row looks like when everything has stopped.
Take Example 2, ".L.R...LR..L.."LRL
Notice that an upright domino can only be reached by the nearest pushed domino on each side. Anything further away has to travel through that pushed domino, and a falling domino does not push one that is already falling. So the pushed dominoes cut the row into gaps, and each run of upright dominoes is decided by its two ends alone. Look at the picture below.
The top row is Example 2, and the bottom row is the result. Under each gap, the bracket shows the letters at its two ends. There are only four kinds of gap:
- L, then L: the right end falls left and knocks over every domino in the gap, so they all fall left.
- R, then R: the same, the other way round. They all fall right.
- L, then R: both ends fall away from the gap, so the dominoes in it stay upright.
- R, then L: both ends fall into the gap, and the two falls meet in the middle. The left half falls right and the right half falls left.
Look at the highlighted gap, R...LRR.LLR..LRRLLLR
What about the upright dominoes, the dots, before the first pushed domino and after the last one? They have a pushed domino on one side only. The dashed boxes in the picture solve that: an imaginary LR
Solution Approach
Turning that idea into code means settling three things: how to find the gaps, how to fill each kind of gap, and what to do at the two ends of the row.
Finding the gaps
How do we find each gap and its two ends in one walk along the row? Keep the position of the last pushed domino seen, prev'L''R'prevprevprev
The simple way, replaying the row one second at a time, may need as many seconds as there are dominoes. With n dominoes and a single 'R'
Filling each kind of gap
What does each kind of gap write? When both ends have the same letter, every position in the gap gets that letter. When the left end is 'R''L'lo'R'hi'L'
They stop as soon as lohi'.''L''R'
The ends of the row
How do the imaginary dominoes get into the row? Build a new string, "L" + dominoes + "R"
Java Solution
The method pushDominoes
- First, add the imaginary ends:
iss . Copy it into a"L" + dominoes + "R" array,char , which the gaps are written into.result - Set
to 0, the imaginaryprev . Then a loop moves'L' alongi from position 1 and skips everys .'.' - At a pushed domino, read the two ends of the gap:
is the letter atleft , andprev is the letter atright .i - If
andleft are the same, a short loop withright writes that letter into every position between them.k - If
isleft and'R' isright , a loop with two markers,'L' andlo , writeshi from the left and'R' from the right while'L' . Forlo < hi then'L' , nothing is written.'R' - Move
toprev , and carry on with the next character.i - When the loop is done, return the characters of
from position 1,result of them, which leaves out the two imaginary ends.dominoes.length()
The mainRL
public class PushDominoes {
public static String pushDominoes(String dominoes) {
String s = "L" + dominoes + "R"; // imaginary ends that push nothing inward
char[] result = s.toCharArray();
int prev = 0; // position of the last pushed domino seen
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == '.') {
continue;
}
char left = s.charAt(prev);
char right = s.charAt(i);
if (left == right) { // L...L or R...R: all fall the same way
for (int k = prev + 1; k < i; k++) {
result[k] = left;
}
} else if (left == 'R') { // R...L: each half falls toward the middle
for (int lo = prev + 1, hi = i - 1; lo < hi; lo++, hi--) {
result[lo] = 'R';
result[hi] = 'L';
}
} // L...R: the dominoes between stay upright
prev = i;
}
return new String(result, 1, dominoes.length());
}
public static void main(String[] args) {
String[] tests = {"RR.L", ".L.R...LR..L..", "R...L", "R..L", "..R..", "..L..", "..."};
for (String d : tests) {
System.out.println("\"" + d + "\" -> \"" + pushDominoes(d) + "\"");
}
}
}
Running it prints:
"RR.L" -> "RR.L"
".L.R...LR..L.." -> "LL.RR.LLRRLL.."
"R...L" -> "RR.LL"
"R..L" -> "RRLL"
"..R.." -> "..RRR"
"..L.." -> "LLL.."
"..." -> "..."
Look at "R...L" -> "RR.LL""R..L" -> "RRLL"lohi'.'lo <= hi'R''L'"R...L""RRLLL""RRLL""RR.L"
In "..R.." -> "..RRR"RLRR"..L.." -> "LLL..""..."LR
The original implementation,
Complexity
The work is one walk along the row, plus filling each gap once.
- Time: O(n), where n is the number of dominoes. The main loop visits each of the n + 2 characters once. The gaps do not overlap, so all the filling loops together write at most n positions.
- Space: O(n). The string
and the arrays each hold n + 2 characters, and the returned string holds n.result
Another way gives each domino a force: a push to the right from an 'R''L'