forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0838-push-dominoes.cpp
39 lines (33 loc) · 1.13 KB
/
0838-push-dominoes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Time: O(n)
Space: O(n)
*/
class Solution {
public:
string pushDominoes(string dominoes) {
string res = "";
char prev;
int n = dominoes.size(), count = 1;
vector<int> left(n, 0), right(n, 0);
for (int i = 0; i < n; i++) {
if (dominoes[i] == 'R') { count = 1; prev = 'R'; }
else if (dominoes[i] != '.') prev = dominoes[i];
if (prev == 'R' && dominoes[i] == '.') right[i] = count++;
}
prev = '.';
for (int i = n-1; i >= 0; i--) {
if (dominoes[i] == 'L') { count = 1; prev = 'L'; }
else if (dominoes[i] != '.') prev = dominoes[i];
if (prev == 'L' && dominoes[i] == '.') left[i] = count++;
}
for (int i = 0; i < n; i++) {
if (!left[i] && !right[i]) res += dominoes[i];
else if (!left[i]) res += 'R';
else if (!right[i]) res += 'L';
else if (left[i] == right[i]) res += '.';
else if (left[i] < right[i]) res += 'L';
else res += 'R';
}
return res;
}
};