diff --git a/cpp/2204/220418-CN.cpp b/cpp/2204/220418-CN.cpp new file mode 100644 index 0000000..60541a8 --- /dev/null +++ b/cpp/2204/220418-CN.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + +/** + * 386. Lexicographical Numbers + * Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order. + * You must write an algorithm that runs in O(n) time and uses O(1) extra space. + */ + +class Solution { +public: + static std::vector lexicalOrder(int n) { + auto next = [n](int x) { + if (10 * x <= n) + return 10 * x; + int nx = 1 + x; + while (!(nx % 10)) + nx /= 10; + while (nx > n) + nx = nx / 10 + 1; + while (!(nx % 10)) + nx /= 10; + return nx; + }; + + std::vector ret; + ret.reserve(n); + + for (int i = 0, j = 1; i < n; ++i, j = next(j)) + ret.push_back(j); + + return ret; + } +}; + +int main() { + const int T = 14959; + auto r = Solution::lexicalOrder(T); + auto n = r.size(); + std::unordered_set s; + s.insert(1); + for (int i = 1; i < n; ++i) { + if (std::to_string(r[i - 1]) >= std::to_string(r[i]) || r[i] > T || s.contains(r[i])) { + std::cout << std::format("Conflict: {} and {} at position {}\n", r[i - 1], r[i], i); + } + s.insert(r[i]); + } + for (int i = 1; i <= n; ++i) + if (!s.contains(i)) + std::cout << std::format("Missing: {}\n", i); + return 0; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 889d024..3e60554 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220417-CN.cpp) +ADD_EXECUTABLE(2204 220418-CN.cpp)