-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0838.cpp
More file actions
43 lines (35 loc) · 851 Bytes
/
0838.cpp
File metadata and controls
43 lines (35 loc) · 851 Bytes
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
40
41
42
43
class Solution {
public:
string pushDominoes(string dominoes) {
int n = dominoes.size();
vector<int> idxs(n + 2, 0);
vector<char> syms(n + 2, '.');
int l = 1;
idxs[0] = -1;
syms[0] = 'L';
for (int i = 0; i < n; i++) {
if (dominoes[i] != '.') {
idxs[l] = i;
syms[l++] = dominoes[i];
}
}
idxs[l] = n;
syms[l++] = 'R';
string res = dominoes;
for (int idx = 0; idx < l - 1; idx++) {
int i = idxs[idx];
int j = idxs[idx + 1];
char x = syms[idx];
char y = syms[idx + 1];
char write;
if (x == y) {
for (int k = i + 1; k < j; k++)
res[k] = x;
} else if (x > y) {
for (int k = i + 1; k < j; k++)
res[k] = (k - i == j - k) ? '.' : (k - i < j - k) ? 'R' : 'L';
}
}
return res;
}
};