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 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:
0 <= N <= 10^5
String dominoes contains only 'L', 'R' and '.'

My Solution(100ms 28.8MB)

public class Solution {
    public string PushDominoes (string dominoes) {
        var sb = new StringBuilder (dominoes);
        for (int i = 0; i < sb.Length; i++) {
            if (sb[i] == '.') {
                continue;
            } else if (sb[i] == 'L') {
                for (int j = i - 1; j > -1; j--) {
                    if (sb[j] == '.') {
                        sb[j] = 'L';
                    }
                    else {
                        break;
                    }
                }
            } else {
                int j = i;
                while (j < sb.Length && dominoes[j] != 'L') {
                    if (sb[j] == 'R') {
                        for (int k = i; k < j; k++)
                        {
                            sb[k] = 'R';
                        }
                        i = j;
                    }
                    j++;
                }
                //No L til the end
                if (j == sb.Length && dominoes[j - 1] != 'L') {
                    for (int k = i; k < sb.Length; k++) {
                        sb[k] = 'R';
                    }
                    break;
                } else {
                    int remainder = (i + j) % 2;
                    int step = 0;
                    if (remainder == 1) {
                        //All change
                        step = (j - i) / 2;
                    } else {
                        step = (j - i) / 2 - 1;
                    }

                    for (int k = i + 1; k <= i + step; k++) {
                        sb[k] = 'R';
                    }
                    for (int l = j - 1; l >= j - step; l--) {
                        sb[l] = 'L';
                    }

                    //Set cursor to current position;
                    i = j;
                }
            }
        }
        return sb.ToString ();
    }
}

No.1 Solution(92ms)

public class Solution {
    public string PushDominoes(string dominoes) 
    {
        char[] res = dominoes.ToCharArray();
        
        int lastSeenL = -1, lastSeenR = -1;
        
        for (int i = 0; i <= dominoes.Length; i++)
        {
            if (i == res.Length || res[i] == 'R') 
            {
                if (lastSeenR > lastSeenL)
                {
                    while (lastSeenR < i)
                    {
                        res[lastSeenR++] = 'R';
                    }
                }
                lastSeenR = i;
            } 
            else if (res[i] == 'L')
            {
                if (lastSeenL > lastSeenR || (lastSeenR == -1 && lastSeenL == -1))
                {
                    while (++lastSeenL < i)
                    {
                        res[lastSeenL] = 'L';
                    }
                    lastSeenL = i;
                }
                else 
                {
                    lastSeenL = i;
                    int low = lastSeenR + 1;
                    int high = lastSeenL - 1;
                    while (low < high) 
                    { 
                        res[low++] = 'R';
                        res[high--] = 'L';
                    }
                }
            }
        }
        
        return new String(res);
    }
}

Things to improve

Compare to the fastest solution, my code has too many loops. It’s not efficient enough. However, the fastest code used more memory.

Leave a Reply

Your email address will not be published. Required fields are marked *