forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path132 Pattern.cpp
24 lines (24 loc) · 855 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
class Solution {
public:
bool find132pattern(vector<int>& nums) {
// Initialise a empty stack "s"...
stack<int> s;
// To keep track of minimum element...
int min = INT_MIN;
// Run a Loop from last to first index...
for (int i = nums.size() - 1; i >= 0; i--) {
// If min is greater than nums[i], return true...
if (nums[i] < min)
return true;
// If stack is not empty & nums[i] is greater than the top element of stack, then pop the element...
while (!s.empty() && nums[i] > s.top()) {
min = s.top();
s.pop();
}
// Otherwise, push nums[i] into stack...
s.push(nums[i]);
}
// If the condition is not satisfied, return false.
return false;
}
};