-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path132Pattern5.cpp
More file actions
45 lines (35 loc) · 799 Bytes
/
132Pattern5.cpp
File metadata and controls
45 lines (35 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
class Solution {
public:
bool find132pattern(vector<int>& nums) {
int n = nums.size();
vector<int> mn(nums.begin(), nums.end());
for(int i = 1; i < n; i++) {
mn[i] = min(mn[i], mn[i - 1]);
}
for(int j = n - 1, top = n; j >= 0; j--) {
if(nums[j] <= mn[j]) continue;
while(top < n && mn[top] <= mn[j]) top++;
if(top < n && nums[j] > mn[top]) return true;
mn[--top] = nums[j];
}
return false;
}
};
int main() {
int n;
cin>>n;
vector<int> nums;
for(int i = 0; i < n; i++) {
int num;
cin>>num;
nums.push_back(num);
}
Solution *solution = new Solution();
solution->find132pattern(nums) ? cout<<"true" : cout<<"false";
return 0;
}