-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushDominoes.cpp
More file actions
31 lines (29 loc) · 918 Bytes
/
pushDominoes.cpp
File metadata and controls
31 lines (29 loc) · 918 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
// Source: https://leetcode.com/problems/push-dominoes/
// Author: Miao Zhang
// Date: 2021-03-16
class Solution {
public:
string pushDominoes(string dominoes) {
int n = dominoes.length();
vector<int> l(n, INT_MAX);
vector<int> r(n, INT_MAX);
for (int i = 0; i < n; i++) {
if (dominoes[i] == 'L') {
l[i] = 0;
for (int j = i - 1; j >= 0 && dominoes[j] == '.'; j--) {
l[j] = l[j + 1] + 1;
}
} else if (dominoes[i] == 'R') {
r[i] = 0;
for (int j = i + 1; j < n && dominoes[j] == '.'; j++) {
r[j] = r[j - 1] + 1;
}
}
}
for (int i = 0; i < n; i++) {
if (l[i] < r[i]) dominoes[i] = 'L';
else if (l[i] > r[i]) dominoes[i] = 'R';
}
return dominoes;
}
};