Skip to content

Commit 0d3100b

Browse files
authored
Create push-dominoes.cpp
1 parent 26a7e08 commit 0d3100b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

C++/push-dominoes.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
string pushDominoes(string dominoes) {
7+
vector<int> force(dominoes.length());
8+
9+
int f = 0;
10+
for (int i = 0; i < dominoes.length(); ++i) {
11+
if (dominoes[i] == 'R') {
12+
f = dominoes.length();
13+
} else if (dominoes[i] == 'L') {
14+
f = 0;
15+
} else {
16+
f = max(f - 1, 0);
17+
}
18+
force[i] += f;
19+
}
20+
21+
f = 0;
22+
for (int i = dominoes.length() - 1; i >= 0; --i) {
23+
if (dominoes[i] == 'L') {
24+
f = dominoes.length();
25+
} else if (dominoes[i] == 'R') {
26+
f = 0;
27+
} else {
28+
f = max(f - 1, 0);
29+
}
30+
force[i] -= f;
31+
}
32+
string result;
33+
for (const auto& f : force) {
34+
result.push_back((f == 0) ? '.' : ((f > 0) ? 'R' : 'L'));
35+
}
36+
return result;
37+
}
38+
};

0 commit comments

Comments
 (0)