-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-PushDominoes.cpp
More file actions
98 lines (77 loc) · 2.59 KB
/
Copy pathLeetCode-PushDominoes.cpp
File metadata and controls
98 lines (77 loc) · 2.59 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class Solution {
public:
string pushDominoes(string dominoes) {
int n = dominoes.size();
vector<int> pos;
for (int i = 0; i < n; ++i) {
if (dominoes[i] == 'L' || dominoes[i] == 'R') {
pos.push_back(i);
}
}
string finalState;
char lastDominoSeen = 'L';
int j = 0;
for (int i = 0; i < n;) {
if (dominoes[i] == 'L' || dominoes[i] == 'R') {
lastDominoSeen = dominoes[i];
finalState += lastDominoSeen;
++j;
++i;
continue;
}
if (lastDominoSeen == 'R') {
if (j >= pos.size()) {
while (i < n) {
finalState += 'R';
++i;
}
break;
}
char nextDomino = dominoes[pos[j]];
if (nextDomino == 'R') {
while (i < pos[j]) {
finalState += 'R';
++i;
}
} else {
int len = pos[j] - i;
int k = 0;
while (k < (len / 2)) {
finalState += 'R';
++k;
++i;
}
if (len & 1) {
finalState += '.';
++i;
}
while (i < pos[j]) {
finalState += 'L';
++i;
}
}
} else {
if (j >= pos.size()) {
while (i < n) {
finalState += '.';
++i;
}
break;
}
char nextDomino = dominoes[pos[j]];
if (nextDomino == 'L') {
while (i < pos[j]) {
finalState += 'L';
++i;
}
} else {
while (i < pos[j]) {
finalState += '.';
++i;
}
}
}
}
return finalState;
}
};