838. Push Dominoes —— weekly contest 85

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 ‘.‘
1 class Solution {
2 public:
3     string pushDominoes(string dominoes) {
4         string res;
5         res = dominoes;
6         int n = dominoes.size();
7         for(int i = 0; i < n; i++){
8             if(dominoes[i] == .){
9                 char left = .;
10                 char right = .;
11                 int j,k;
12                 for(j = i - 1; j >= 0;j--){
13                     if(dominoes[j]!=.){
14                         left = dominoes[j];
15                         break;
16                     }
17                 }
18                 for(k = i + 1; k < n; k++){
19                     if(dominoes[k]!=.){
20                         right = dominoes[k];
21                         break;
22                     }
23                 }
24                 if(left==R&&right==L){
25                     if(i-j!=k-i){
26                         res[i] = i-j < k-i?left:right;
27                     }
28                 }else if(left ==  R){
29                     res[i] = left;
30                 }else if(right == L){
31                     res[i] = right;
32                 }
33
34             }
35         }
36         return res;
37     }
38 };

Another easier solution:

1     string pushDominoes(string d) {
2         d = L + d + R;
3         string res = "";
4         for (int i = 0, j = 1; j < d.length(); ++j) {
5             if (d[j] == .) continue;
6             int middle = j - i - 1;
7             if (i > 0) res += d[i];
8             if (d[i] == d[j]) res += string(middle, d[i]);
9             else if (d[i] == L && d[j] == R) res += string(middle, .);
10             else res += string(middle / 2, R) + string(middle % 2,.) + string(middle / 2, L);
11             i = j;
12         }
13         return res;
14     }

838. Push Dominoes —— weekly contest 85

(0)
(0) © 2014 mamicode.com 版权所有 京ICP备13008772号-2