132 Pattern LeetCode solution in C++

1
class Solution {
2
public:
3
bool find132pattern(vector<int> &nums) {
4
// Initialise a empty stack "s"...
5
stack<int> s;
6
// To keep track of minimum element...
7
int min = INT_MIN;
8
// Run a Loop from last to first index...
9
for (int i = nums.size() - 1; i >= 0; i--) {
10
// If min is greater than nums[i], return true...
11
if (nums[i] < min) return true;
12
// If stack is not empty & nums[i] is greater than the top element of
13
// stack, then pop the element...
14
while (!s.empty() && nums[i] > s.top()) {
15
min = s.top();
16
s.pop();
17
}
18
// Otherwise, push nums[i] into stack...
19
s.push(nums[i]);
20
}
21
// If the condition is not satisfied, return false.
22
return false;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0