forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path132 Pattern.java
More file actions
23 lines (23 loc) · 747 Bytes
/
132 Pattern.java
File metadata and controls
23 lines (23 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public boolean find132pattern(int[] nums) {
int n = nums.length;
int[] uptoIdxMin = new int[n];
uptoIdxMin[0] = nums[0];
for (int i = 1; i < n; i++) {
uptoIdxMin[i] = Math.min(uptoIdxMin[i - 1], nums[i]);
}
Stack<Integer> stack = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
if (nums[i] > uptoIdxMin[i]) {
while (!stack.isEmpty() && stack.peek() <= uptoIdxMin[i]) {
stack.pop();
}
if (!stack.isEmpty() && stack.peek() < nums[i]) {
return true;
}
stack.push(nums[i]);
}
}
return false;
}
}