forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-dominoes.cpp
38 lines (35 loc) · 971 Bytes
/
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
// Time: O(n)
// Space: O(n)
class Solution {
public:
string pushDominoes(string dominoes) {
vector<int> force(dominoes.length());
int f = 0;
for (int i = 0; i < dominoes.length(); ++i) {
if (dominoes[i] == 'R') {
f = dominoes.length();
} else if (dominoes[i] == 'L') {
f = 0;
} else {
f = max(f - 1, 0);
}
force[i] += f;
}
f = 0;
for (int i = dominoes.length() - 1; i >= 0; --i) {
if (dominoes[i] == 'L') {
f = dominoes.length();
} else if (dominoes[i] == 'R') {
f = 0;
} else {
f = max(f - 1, 0);
}
force[i] -= f;
}
string result;
for (const auto& f : force) {
result.push_back((f == 0) ? '.' : ((f > 0) ? 'R' : 'L'));
}
return result;
}
};