diff --git a/cpp/2205/220507.cpp b/cpp/2205/220507.cpp new file mode 100644 index 0000000..5a24a21 --- /dev/null +++ b/cpp/2205/220507.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +/** + * 456. 132 Pattern + * Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j]. + * Return true if there is a 132 pattern in nums, otherwise, return false. + */ + +class Solution { +public: + static bool find132pattern(const std::vector& nums) { + const int n = nums.size(); + int ptr = 0; + int* s = new int[n]; + int e3 = -0x7FFFFFFF - 1, t, st; + // for (auto it = nums.crbegin(); it != nums.crend(); ++it) { + for (int i = n - 1; i >= 0; --i) { + t = nums[i]; + if (t < e3) + return true; + while (ptr && (st = s[ptr - 1]) < t) { + e3 = st; + --ptr; + } + s[ptr++] = t; + } + return false; + } +}; + +class SolutionOld { +public: + static bool find132pattern(const std::vector& nums) { + const int n = nums.size(); + if (n < 3) + return false; + std::map m; + for (int i = 1; i < n; ++i) + ++m[nums[i]]; + int min = nums[0], pivot = 1; + while (pivot < n - 1) { + auto it = std::lower_bound(m.cbegin(), m.cend(), std::pair(min, 0x7FFFFFFF)); + if (it != m.end() && it->first < nums[pivot]) + return true; + + // Advance pivot + int prevPivot = pivot, prevMin = min; + while (pivot < n - 1 && ((nums[pivot] <= nums[prevPivot] && prevMin == min) || nums[pivot] <= nums[pivot + 1])) { + if (--m[nums[pivot]] <= 0) { + m.erase(nums[pivot]); + } + min = std::min(min, nums[pivot]); + ++pivot; + } + if (pivot == prevPivot) { + min = std::min(min, nums[pivot]); + ++pivot; + } + } + return false; + } +}; + +class Stopwatch { +private: + std::chrono::high_resolution_clock::time_point start; +public: + Stopwatch() { + this->start = std::chrono::high_resolution_clock::now(); + } + + ~Stopwatch() { + std::printf("Time elapsed: %.6lf ms\n", std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - start).count() / 1000000.0); + } +}; + +int main() { + const int N = 200000; + std::vector args(N); + for (int i = 0; i < N; ++i) + args[i] = (i & 1) ? -i : i; + + bool ans; + { + Stopwatch s; + ans = Solution::find132pattern(args); + } + + std::printf(ans ? "true" : "false"); + return 0; +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 9e0ca75..5ad543e 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 220507-CN.cpp) +ADD_EXECUTABLE(2205 220507.cpp)