-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path132 Pattern.cpp
41 lines (28 loc) · 983 Bytes
/
132 Pattern.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
/*
Solution by Rahul Surana
***********************************************************
Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k]
such that i < j < k and nums[i] < nums[k] < nums[j].
Return true if there is a 132 pattern in nums, otherwise, return false.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
bool find132pattern(vector<int>& nums) {
int mi = INT_MIN;
stack<int> s;
for(int i = nums.size()-1; i >= 0; i--){
if(nums[i] < mi) return true;
else{
while(!s.empty() && s.top() < nums[i]){
mi = s.top();
s.pop();
}
}
// if(s.empty() || s.top() > nums[i])
s.push(nums[i]);
}
return false;
}
};