cr: https://leetcode-cn.com/problems/push-dominoes/

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:

  1. dominoes[i] = ‘L’, if the ith domino has been pushed to the left,
  2. dominoes[i] = ‘R’, if the ith domino has been pushed to the right, and
  3. dominoes[i] = ‘.’, if the ith domino has not been pushed.
  4. Return a string representing the final state.

  • Example1:

    Input: dominoes = “RR.L”
    Output: “RR.L”
    Explanation: The first domino expends no additional force on the second domino.

  • Example2:

    Input: dominoes = “.L.R…LR..L..”
    Output: “LL.RR.LLRRLL..”

Analyse

This solution is made by Fuxuemingzhu(2022), and here is my understanding about his/her solution.

According to “we will consider that a falling domino expends no additional force to a falling or already fallen domino”, we could know that only dominoes which are not been pushed in the initial statement could be pushed.

Here is a trick! Very Important! If there is a list of “.” and the dominoes at right and left could affect this list of “.”. Here are four circumstance :

  • When here is L …….. L or R ……. R → Then all “.” will convert to equal to the left and right dominoes.
  • When here is L …… R → all “.” have no change.
  • When here is R……L → half of the dominoes are R and another half of the dominoes are L. If the length of the list of “.” is not even, then the middle domino of the list should have no change.

Another circumstance(which is so specific) we can make it general, which is input like “……L……..R……”, We can add “L” and “R” to every input’s two sides. For example, “……L……..R……” should convert to “L……L……..R……R”. And remember not to add those two into the output.

In summary, this question could be solved by the following process:

  1. Add “L” to the start of the input and add “R” to the end of the input.
  2. Initialize the left pointer with value 0. Create the result list.
  3. Traverse the whole input string and find the list of “.” with the right pointer:
    1. if the current character is equal to “.” the right pointer move, otherwise the right pointer stop
    2. According to the aforementioned, change the list of “.” and add it to the res.
    3. update the left pointer to equal to the right one.
  4. Return the output string combined with the result list.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution(object):
def pushDominoes(self, dominoes):
dominoes = "L" + dominoes + "R"
l = 0
res = []
for r in range(1, len(dominoes)):
# 当r遇到的是“.”时直接跳过就好 右端指针右移
# if the current character is equal to “.” the right pointer move, otherwise the right pointer stop
if dominoes[r] == ".":
continue
mid = r - l - 1
if l:
res.append(dominoes[l])
# 判断左右指针情况 然后进行不同的输入
# Define left and right pointers circumstances and add different results to the result list
if dominoes[l] == dominoes[r]:
res.append(dominoes[l] * mid)
elif dominoes[l] == "L" and dominoes[r] == "R":
res.append("."* mid)
else:
res.append("R" * (mid // 2) + "." * (mid % 2) + "L" * (mid // 2))
# 更新左指针与右指针位置一致
# update the left pointer to equal to the right one.
l = r
return "".join(res)

Reference

  1. Fumingxuezhu. (2022). Understand in seconds series: detailed ideas, clear diagrams. Retrieved from https://leetcode-cn.com/problems/push-dominoes/solution/fu-xue-ming-zhu-miao-dong-xi-lie-xiang-x-xkts/.