-
Notifications
You must be signed in to change notification settings - Fork 0
/
132Pattern6.cpp
47 lines (39 loc) · 894 Bytes
/
132Pattern6.cpp
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
46
47
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
class Solution {
public:
bool find132pattern(vector<int>& nums) {
stack<pair<int, int> > s;
for(int num : nums) {
if(s.empty() || num < s.top().first) s.push(make_pair(num, num));
else if(num > s.top().first){
auto last = s.top();
s.pop();
if(num < last.second) return true;
else {
last.second = num;
while(!s.empty() && num >= s.top().second) s.pop();
if(!s.empty() && s.top().first < num ) return true;
s.push(last);
}
}
}
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;
}