add: 220221-CN [cpp]
This commit is contained in:
parent
23639b3e1d
commit
36142c641d
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include <string>
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
std::string pushDominoes(const std::string& dominoes) {
|
||||||
|
std::queue<int> q;
|
||||||
|
int n = dominoes.length();
|
||||||
|
|
||||||
|
int result[100005]{}, lastModified[100005]{};
|
||||||
|
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if ('.' != dominoes[i]) {
|
||||||
|
q.push(i);
|
||||||
|
q.push(1);
|
||||||
|
lastModified[i] = 1;
|
||||||
|
result[i] = ((dominoes[i] == 'L') ? -1 : 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!q.empty()) {
|
||||||
|
int pos = q.front();
|
||||||
|
q.pop();
|
||||||
|
int tP = q.front();
|
||||||
|
q.pop();
|
||||||
|
|
||||||
|
if (pos && (lastModified[pos - 1] == tP + 1 || !lastModified[pos - 1]) && result[pos] < 0) {
|
||||||
|
if (!lastModified[pos - 1]) {
|
||||||
|
q.push(pos - 1);
|
||||||
|
q.push(lastModified[pos - 1] = tP + 1);
|
||||||
|
}
|
||||||
|
--result[pos - 1];
|
||||||
|
} else if (pos < n - 1 && (lastModified[pos + 1] == tP + 1 || !lastModified[pos + 1]) && result[pos] > 0) {
|
||||||
|
if (!lastModified[pos + 1]) {
|
||||||
|
q.push(pos + 1);
|
||||||
|
q.push(lastModified[pos + 1] = tP + 1);
|
||||||
|
}
|
||||||
|
++result[pos + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ret;
|
||||||
|
ret.reserve(n);
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
ret.push_back(result[i] ? (result[i] > 0 ? 'R' : 'L') : '.');
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2202)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2202 220220.cpp)
|
ADD_EXECUTABLE(2202 220221-CN.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue