-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind132pattern.cpp
55 lines (43 loc) · 1.18 KB
/
find132pattern.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
48
49
50
51
52
53
54
55
/*
O(n^2) Time and O(1) Space
*/
class Solution {
public:
bool find132pattern(vector<int>& nums) {
int n = nums.size();
if(n <= 2) return false;
int leftMin = INT_MAX;
for(int i = 0; i < n - 1; ++i) {
for(int j = i + 1; j < n; ++j) {
if(nums[j] > leftMin && nums[i] > nums[j])
return true;
}
leftMin = min(leftMin, nums[i]);
}
return false;
}
};
/*
O(N) Time and O(N) Space
*/
class Solution {
public:
bool find132pattern(vector<int>& nums) {
int n = nums.size();
int mins[n];
mins[0] = nums[0];
for(int i = 1; i < n; ++i)
mins[i] = min(mins[i - 1], nums[i]);
stack<int> s;
for(int j = n - 1; j >= 0; --j) {
if(nums[j] > mins[j]) {
while(!s.empty() && s.top() <= mins[j])
s.pop();
if(!s.empty() && s.top() < nums[j])
return true;
s.push(nums[j]);
}
}
return false;
}
};