[Swift]LeetCode838. 推多米诺 | 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.

Given a string "S" representing the initial state. S[i] = ‘L‘, if the i-th domino has been pushed to the left; S[i] = ‘R‘, if the i-th domino has been pushed to the right; S[i] = ‘.‘, if the i-th domino has not been pushed.

Return a string representing the final state.

Example 1:

Input: ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."

Example 2:

Input: "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.

Note:

  1. 0 <= N <= 10^5
  2. String dominoes contains only ‘L‘, ‘R‘ and ‘.‘


一行中有 N 张多米诺骨牌,我们将每张多米诺骨牌垂直竖立。

在开始时,我们同时把一些多米诺骨牌向左或向右推。

每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌。

同样地,倒向右边的多米诺骨牌也会推动竖立在其右侧的相邻多米诺骨牌。

如果同时有多米诺骨牌落在一张垂直竖立的多米诺骨牌的两边,由于受力平衡, 该骨牌仍然保持不变。

就这个问题而言,我们会认为正在下降的多米诺骨牌不会对其它正在下降或已经下降的多米诺骨牌施加额外的力。

给定表示初始状态的字符串 "S" 。如果第 i 张多米诺骨牌被推向左边,则 S[i] = ‘L‘;如果第 i 张多米诺骨牌被推向右边,则 S[i] = ‘R‘;如果第 i 张多米诺骨牌没有被推动,则 S[i] = ‘.‘

返回表示最终状态的字符串。

示例 1:

输入:".L.R...LR..L.."
输出:"LL.RR.LLRRLL.."

示例 2:

输入:"RR.L"
输出:"RR.L"
说明:第一张多米诺骨牌没有给第二张施加额外的力。

提示:

  1. 0 <= N <= 10^5
  2. 表示多米诺骨牌状态的字符串只含有 ‘L‘‘R‘; 以及 ‘.‘;


Runtime: 284 ms

Memory Usage: 20.8 MB

 1 class Solution {
 2     func pushDominoes(_ dominoes: String) -> String {
 3         var n:Int = dominoes.count
 4         var arrChar = Array(dominoes)
 5         var cnt:[Int] = [Int](repeating:0,count:n)
 6         for i in 1..<n
 7         {
 8             if arrChar[i - 1] == "R" && arrChar[i] == "."
 9             {
10                 arrChar[i] = "R"
11                 cnt[i] = cnt[i - 1] + 1
12             }
13         }
14         var cur:Int = 0
15         for i in stride(from:n - 2,through:0,by:-1)
16         {
17             if arrChar[i + 1] != "L" {continue}
18             cur = cnt[i + 1] + 1
19             if arrChar[i] == "." || cnt[i] > cur
20             {
21                 arrChar[i] = "L"
22                 cnt[i] = cur
23             }
24             else if arrChar[i] == "R" && cnt[i] == cur
25             {
26                  arrChar[i] = "."
27             }
28         }
29         return String(arrChar)
30     }
31 }


308ms

  1 class Solution {
  2     func pushDominoes(_ dominoes: String) -> String {
  3     guard dominoes.count > 1 else{
  4         return dominoes
  5     }
  6     var dominoes = Array(dominoes)
  7     var startIndex = 0
  8     var movingIndex = 0
  9
 10     while movingIndex < dominoes.count{
 11         if dominoes[startIndex] == "."{
 12             if dominoes[movingIndex] == "."{
 13                 movingIndex += 1
 14             }else if dominoes[movingIndex] == "L"{
 15                 pushLeft(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 16                 movingIndex += 1
 17                 startIndex = movingIndex
 18             }else{
 19                 // if dominoes[movingIndex] = right
 20                 startIndex = movingIndex
 21             }
 22         }else if dominoes[startIndex] == "L"{
 23             if dominoes[movingIndex] == "L"{
 24                 if startIndex != movingIndex{
 25                     pushLeft(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 26                     startIndex = movingIndex
 27                 }
 28                 movingIndex += 1
 29             }else if dominoes[movingIndex] == "."{
 30                 movingIndex += 1
 31             }else{
 32                 // now we R
 33                 startIndex = movingIndex
 34             }
 35         }else{
 36             // if dominoes[startIndex] = R
 37             // then what do we do
 38             if dominoes[movingIndex] == "R"{
 39                 if startIndex != movingIndex{
 40                     pushRight(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 41                     startIndex = movingIndex
 42                 }
 43                 movingIndex += 1
 44             }else if dominoes[movingIndex] == "."{
 45                 movingIndex += 1
 46             }else{
 47                 // we meet L
 48                 pushRightLeft(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 49                 movingIndex += 1
 50                 startIndex = movingIndex
 51             }
 52         }
 53     }
 54
 55     if startIndex < movingIndex && dominoes[startIndex] == "R"{
 56         while startIndex < movingIndex{
 57             dominoes[startIndex] = "R"
 58             startIndex += 1
 59         }
 60     }
 61
 62
 63     return String(dominoes)
 64 }
 65 func pushLeft(dominoes : inout [Character], startIndex : Int, endIndex : Int){
 66     guard startIndex >= 0 && endIndex < dominoes.count && startIndex < endIndex else{
 67         fatalError("Index Not Correct")
 68     }
 69     var startIndex = startIndex
 70     while startIndex < endIndex{
 71         dominoes[startIndex] = "L"
 72         startIndex += 1
 73     }
 74 }
 75
 76 func pushRight(dominoes : inout [Character], startIndex : Int, endIndex : Int){
 77     guard startIndex >= 0 && endIndex < dominoes.count && startIndex < endIndex else{
 78         fatalError("Index Not Correct")
 79     }
 80     var startIndex = startIndex
 81     while startIndex < endIndex{
 82         dominoes[startIndex] = "R"
 83         startIndex += 1
 84     }
 85 }
 86
 87 func pushRightLeft(dominoes : inout [Character], startIndex : Int, endIndex : Int){
 88     guard startIndex >= 0 && endIndex < dominoes.count && startIndex < endIndex else{
 89         fatalError("Index Not Correct")
 90     }
 91
 92     var startIndex = startIndex
 93     var endIndex = endIndex
 94     while startIndex < endIndex{
 95         dominoes[startIndex] = "R"
 96         dominoes[endIndex] = "L"
 97         startIndex += 1
 98         endIndex -= 1
 99     }
100   }
101 }

原文地址:https://www.cnblogs.com/strengthen/p/10577534.html

时间: 2024-07-31 05:35:11

[Swift]LeetCode838. 推多米诺 | Push Dominoes的相关文章

[LeetCode] 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 adjac

[Swift]LeetCode790. 多米诺和托米诺平铺 | Domino and Tromino Tiling

We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may be rotated. XX <- domino XX <- "L" tromino X Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7. (

POJ 1135 Domino Effect(最短路 多米诺骨牌)

题意 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两张骨牌之间只有很短的空隙.如果排列得很好,当你推 倒第 1张骨牌,会使其他骨牌连续地倒下(这就是短语"多米诺效应"的由来). 然而当骨牌数量很少时,这种玩法就没多大意思了,所以一些人在 80 年代早期开创了另一个 极端的多米诺骨牌游戏:用上百万张不同颜色.不同材料的骨牌拼成一幅复杂的图案.他们开创 了一种流行的艺术.在这种骨牌游戏中,通常有多行骨牌同时

poj 1717==洛谷P1282 多米诺骨牌

Dominoes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6571   Accepted: 2178 Description A domino is a flat, thumbsized tile, the face of which is divided into two squares, each left blank or bearing from one to six dots. There is a ro

FZU 2163 多米诺骨牌

Problem Description Vasya很喜欢排多米诺骨牌.他已经厌倦了普通的多米诺骨牌,所以他用不同高度的多米诺骨牌.他从左边到右边,把n个多米诺骨牌沿一个轴放在桌子上.每一个多米诺骨牌垂直于该轴,使该轴穿过其底部的中心.第i个多米诺骨牌具有坐标xi与高度hi.现在Vasya想要知道,对于每一个多米诺骨牌如果他推倒的话,右侧会有多少个多米诺骨牌也会倒下. 想想看,一个多米诺倒下,如果它严格的触动右侧的多米诺骨牌,被触碰的也会倒下.换句话说,如果多米诺骨牌(初始坐标x和高度h)倒下,会

ZOJ 3650(多米诺骨牌 dp + 线段树优化)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3650 题意: 给你n个骨牌,每个骨牌有一个在x轴上的位置和高度,每个骨牌可以想左推也可以向右推,问最少多少步可以把骨牌全部推倒. 思路: 之前Goodbye 2014 上的E题就是一道多米诺骨牌的题目,虽然跟这道题目不太一样但是还是提供了一下思路, 附题解的链接: http://blog.csdn.net/u013649253/article/details/4

多米诺牌集

多米诺gu牌覆盖问题也是经典的题目了,主要是由1*k的牌覆盖m*n的矩阵之类的.有递推求解的,也有的如下: 如POJ 2411,由1*2的小矩形去覆盖m*n的矩形,问有多少种方案. 这道题其实只要计算出上下两行之前的可能存在的状态转换,这样就很容易求解了.如下: void dfs(int pre,int now,int l){//上一行的状态,当前行的状态,当前列的位置 if(l>w) return; if(l==w){ trans[nt][0]=pre,trans[nt++][1]=now;

棋盘的多米诺覆盖:Dimer Lattice Model,Pfaff 多项式,Kasteleyn 定理

这次来介绍计数组合学里面一个经典的问题:Dimer Lattice Model.问题是这样的:一个有 64 个方格的国际象棋棋盘,有多少种不同的多米诺骨牌覆盖?这里的覆盖是指不重复不遗漏地盖住整个棋盘. 下图是一种可能的覆盖方式(图片来自 Wiki 百科): 这个问题的答案是 12988816,非常大的一个数字,绝对不是一个一个数出来的.1961 年德国物理学家 Kasteleyn 借助于线性代数中的一个结论首先解决了这个问题,我们接下来就介绍他的方法. ~~~~~~~~~~~~~~~~~~~~

洛谷 P1282 多米诺骨牌

题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9,S2=1+5+3+2=11,|S1-S2|=2.每个多米诺骨牌可以旋转180°,使得上下两个方块互换位置. 编程用最少的旋转次数使多米诺骨牌上下2行点数之差达到最小. 对于图中的例子,只要将最后一个多米诺骨牌旋转180°,可使上下2行点数之差为0. 输入输出格式 输入格式: 输入文件的第一行是一个正