Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active May 23, 2018 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/eeb1e3297d283c6999e7fa486124649a to your computer and use it in GitHub Desktop.
Save fpdjsns/eeb1e3297d283c6999e7fa486124649a to your computer and use it in GitHub Desktop.
class Solution {
public:
string pushDominoes(string dominoes) {
int len = dominoes.size();
vector<int> cnt(len, 0);
for (int i = 0; i < len; i++) {
char now = dominoes[i];
if (now == '.') continue;
if (now == 'L') {//-1
cnt[i] = -1;
for (int j = 1; i-j >= 0; j++) {
if (dominoes[i - j] != '.') break;
if (cnt[i - j] == j) { //1.
cnt[i - j] = 0;
break;
}
else if (cnt[i-j] !=0 && cnt[i - j] < j) { //2.
break;
}
cnt[i - j] = -j;
}
}
else { //'R' +1
cnt[i] = 1;
for (int j = 1; i + j < len; j++) {
if (dominoes[i + j] != '.' ) break;
if (cnt[i + j] == -j) { //1.
cnt[i - j] = 0;
break;
}
else if (cnt[i + j] != 0 && -cnt[i + j] < j) { //2.
break;
}
cnt[i + j] = j;
}
}
}
string ans = "";
for (int i = 0; i < len; i++) {
if (cnt[i] > 0) ans.push_back('R');
else if (cnt[i] < 0) ans.push_back('L');
else ans.push_back('.');
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment