diff --git a/cpp/2205/220509-CN.cpp b/cpp/2205/220509-CN.cpp new file mode 100644 index 0000000..12746b3 --- /dev/null +++ b/cpp/2205/220509-CN.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +/** + * 942. DI String MatchA permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where: + * + * s[i] == 'I' if perm[i] < perm[i + 1], and + * s[i] == 'D' if perm[i] > perm[i + 1]. + * Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them. + * + */ + +class Solution { +public: + static std::vector diStringMatch(const std::string& s) { + int p = 1, n = -1; + std::vector ret; + ret.reserve(s.length() + 1); + ret.push_back(0); + for (char c : s) + ret.push_back(c == 'I' ? p++ : n--); + n = (-n) - 1; + for (int& i : ret) + i += n; + return ret; + } +}; + +int main() { + auto r = Solution::diStringMatch("DDI"); + for (const auto& i : r) + std::cout << i << ' '; + return 0; +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 6be1653..293aad9 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220508-CN.cpp) +ADD_EXECUTABLE(2205 220509-CN.cpp)