-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0456-132-pattern.cpp
More file actions
26 lines (26 loc) · 821 Bytes
/
0456-132-pattern.cpp
File metadata and controls
26 lines (26 loc) · 821 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
class Solution {
public:
bool find132pattern(vector<int>& nums) {
//fix index j
//we now want to find the SMALLEST from left
//and the LARGEST from right (that im still larger than him)
int n = (int) nums.size();
multiset<int> suffix;
for (int i = 1; i < n; i++) {
suffix.insert(nums[i]);
}
int minPrefix = nums[0];
for (int j = 1; j < n; j++) {
suffix.erase(suffix.find(nums[j]));
auto it = suffix.lower_bound(nums[j]);
if (it != suffix.begin()) {
int cur = *prev(it);
if (cur < nums[j] && cur > minPrefix) {
return true;
}
}
minPrefix = min(minPrefix, nums[j]);
}
return false;
}
};