Skip to content

Latest commit

 

History

History

838

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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 ith domino has been pushed to the left,
  • dominoes[i] = 'R', if the ith domino has been pushed to the right, and
  • dominoes[i] = '.', if the ith 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 <= 105
  • dominoes[i] is either 'L', 'R', or '.'.

Companies:
Bloomberg

Related Topics:
Two Pointers, String, Dynamic Programming

Solution 1.

// OJ: https://leetcode.com/problems/push-dominoes/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
    string pushDominoes(string s) {
        int N = s.size();
        vector<int> v(N);
        for (int i = 0; i < N; ++i) {
            if (s[i] == 'L') {
                for (int j = i - 1; j >= 0 && s[j] != 'L'; --j) {
                    if (s[j] == 'R' && v[j] <= i - j) {
                        if (v[j] == i - j) s[j] = '.';
                        break;
                    }
                    s[j] = 'L';
                    v[j] = i - j;
                }
            } else if (s[i] == 'R') {
                if (v[i] != 0) continue;
                for (int j = i + 1; j < N && s[j] == '.'; ++j) {
                    s[j] = 'R';
                    v[j] = j - i;
                }
            }
        }
        return s;
    }
};

Solution 2. Two Pointers

// OJ: https://leetcode.com/problems/push-dominoes/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    string pushDominoes(string s) {
        int N = s.size(), right = -1;
        for (int i = 0; i < N; ++i) {
            if (s[i] == 'L') {
                if (right == -1) {
                    for (int j = i - 1; j >= 0 && s[j] == '.'; --j) s[j] = 'L';
                } else {
                    for (int j = right + 1, k = i - 1; j < k; ++j, --k) s[j] = 'R', s[k] = 'L';
                    right = -1;
                }
            } else if (s[i] == 'R') {
                if (right != -1) {
                    for (int j = right + 1; j < i; ++j) s[j] = 'R';
                }
                right = i;
            }
        }
        if (right != -1) {
            for (int j = right + 1; j < N; ++j) s[j] = 'R';
        }
        return s;
    }
};

Or

// OJ: https://leetcode.com/problems/push-dominoes/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    string pushDominoes(string s) {
        int N = s.size();
        for (int i = 0; i < N; ++i) {
            if (s[i] == '.') continue;
            if (s[i] == 'L') {
                int j = i - 1;
                while (j >= 0 && s[j] == '.') s[j--] = 'L';
            } else {
                int j = i + 1;
                while (j < N && s[j] == '.') ++j;
                if (j == N || s[j] == 'R') {
                    ++i;
                    while (i < j) s[i++] = 'R';
                    --i;
                } else {
                    int p = i + 1, q = j - 1;
                    while (p < q) {
                        s[p++] = 'R';
                        s[q--] = 'L';
                    }
                    i = j;
                }
            }
        }
        return s;
    }
};