diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/132_pattern.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/132_pattern.cpp')
-rw-r--r-- | other/132_pattern.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/other/132_pattern.cpp b/other/132_pattern.cpp new file mode 100644 index 0000000..a9f0c43 --- /dev/null +++ b/other/132_pattern.cpp @@ -0,0 +1,32 @@ +#include "leetcode.h" + +class Solution { +public: + bool find132pattern(vector<int> &nums) { + int n = nums.size(), minI[n]; + stack<int> s; + minI[0] = nums[0]; + for (int i = 1; i < nums.size(); i++) + minI[i] = min(minI[i - 1], nums[i]); + for (int i = nums.size() - 1; i >= 0; i--) { + while (!s.empty() && s.top() <= minI[i]) + s.pop(); + if (!s.empty() && s.top() < nums[i]) + return true; + s.push(nums[i]); + } + return false; + } +}; + +int main() { + vector<int> nums = {1, 3, 2, 4}; + Solution obj; + for (int i = 0; i < nums.size(); i++) + cout << nums[i] << ' '; + cout << endl << "searching for '132'...\n"; + if (obj.find132pattern(nums)) + cout << "found"; + else + cout << "not found"; +} |