diff options
author | Jack Sangdahl <[email protected]> | 2025-05-01 20:21:54 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-01 20:21:54 -0600 |
commit | e820af62f134a5cfd0a02b583d24207af3379b08 (patch) | |
tree | 6ec0540753349608cbdbf21240f86b76a5099391 /push_dominoes.py | |
parent | 395ece1fa6936500eceb9e2ce825a653cafcba51 (diff) | |
download | leetcode-e820af62f134a5cfd0a02b583d24207af3379b08.tar.gz leetcode-e820af62f134a5cfd0a02b583d24207af3379b08.zip |
838. push dominoes
Diffstat (limited to 'push_dominoes.py')
-rw-r--r-- | push_dominoes.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/push_dominoes.py b/push_dominoes.py new file mode 100644 index 0000000..a33dd56 --- /dev/null +++ b/push_dominoes.py @@ -0,0 +1,32 @@ +# 838. 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 purpose of this question, we will +consider that a falling domino expends no additional force to a falling or +already fallen domino. +""" + + +class Solution(object): + def pushDominoes(self, dominoes): + """ + :type dominoes: str + :rtype: str + """ + while "R." in dominoes or ".L" in dominoes: + dominoes = ( + dominoes.replace("R.L", "xxx").replace(".L", "LL").replace("R.", "RR") + ) + return dominoes.replace("xxx", "R.L") + + +if __name__ == "__main__": + obj = Solution() + print(obj.pushDominoes(dominoes="RR.L")) + print(obj.pushDominoes(dominoes=".L.R...LR..L..")) |