Push Dominoes Problem
Description
LeetCode Problem 838.
There are n dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.
After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
You are given a string dominoes representing the initial state where:
- dominoes[i] = 'L', if the i^th domino has been pushed to the left,
- dominoes[i] = 'R', if the i^th domino has been pushed to the right, and
- dominoes[i] = '.', if the i^th domino has not been pushed.
Return a string representing the final state.
Example 1:
Input: dominoes = "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
Example 2:
Input: dominoes = ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."
Constraints:
- n == dominoes.length
- 1 <= n <= 10^5
- dominoes[i] is either 'L', 'R', or '.'.
Solution
Approach: Segments Between Pushed Dominoes
Simulating second by second can take O(n2) time. But the final state of each run of standing dominoes . depends only on the nearest pushed domino on each side, so we can handle the string one segment at a time.
Look at every pair of consecutive pushed dominoes, at indices i < j, with only . between them:
R...RorL...L: everything in between falls the same way.R...L: the two sides fall toward each other. FillRfrom the left andLfrom the right with two pointers until they meet. If the segment has odd length, the middle domino gets hit from both sides at the same moment and stays up.L...R: the left one falls away to the left and the right one away to the right, so the dominoes in between stay up.
The dominoes before the first pushed one and after the last one need care: ...L all fall left, R... all fall right, but ...R and L... stay up. A clean way to get this for free is to pretend there is an L at index -1 and an R at index n. These imaginary dominoes push nothing inward, so the general rules above give the right result at both ends.
Example: in ".L.R...LR..L..", the imaginary L and the first L make index 0 fall left; L.R leaves index 2 up; R...L becomes RR.LL; R..L becomes RRLL; and the final .. lies between an L and the imaginary R, so it stays up. The result is "LL.RR.LLRRLL..".
Complexity: O(n) time, since each domino is filled at most once, and O(n) space for the result, where n is the length of dominoes.
class Solution:
def pushDominoes(self, dominoes: str) -> str:
n = len(dominoes)
result = list(dominoes)
# Pretend there is an 'L' before the first domino and an 'R' after the last one:
# they push nothing inward, so the dominoes at the ends are handled like any other segment.
prev_index, prev_push = -1, "L"
for i in range(n + 1):
push = dominoes[i] if i < n else "R"
if push == ".":
continue
if push == prev_push:
# "R...R" or "L...L": everything in between falls the same way.
for j in range(prev_index + 1, i):
result[j] = push
elif prev_push == "R" and push == "L":
# "R...L": the two sides fall toward each other; an odd middle domino stays up.
left, right = prev_index + 1, i - 1
while left < right:
result[left] = "R"
result[right] = "L"
left += 1
right -= 1
# "L...R": nothing pushes the dominoes in between, they stay up.
prev_index, prev_push = i, push
return "".join(result)