Last active
May 23, 2018 11:09
-
-
Save fpdjsns/eeb1e3297d283c6999e7fa486124649a to your computer and use it in GitHub Desktop.
[leetcode] 838. Push Dominoes : https://leetcode.com/problems/push-dominoes/description/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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