diff --git a/cpp/2202/220221-CN.cpp b/cpp/2202/220221-CN.cpp new file mode 100644 index 0000000..7849ffe --- /dev/null +++ b/cpp/2202/220221-CN.cpp @@ -0,0 +1,49 @@ +#include +#include + +class Solution { +public: + std::string pushDominoes(const std::string& dominoes) { + std::queue 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; + } +}; \ No newline at end of file diff --git a/cpp/2202/CMakeLists.txt b/cpp/2202/CMakeLists.txt index 5ac029e..3decd39 100644 --- a/cpp/2202/CMakeLists.txt +++ b/cpp/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220220.cpp) +ADD_EXECUTABLE(2202 220221-CN.cpp)