-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy path123Pattern.java
More file actions
34 lines (26 loc) · 815 Bytes
/
Copy path123Pattern.java
File metadata and controls
34 lines (26 loc) · 815 Bytes
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
//Leetcode solution
import java.util.*;
class Solution {
public boolean find132pattern(int[] nums) {
if (nums.length < 3)
return false;
int[] minI = new int[nums.length];
minI[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
minI[i] = Math.min(minI[i - 1], nums[i]);
}
Stack<Integer> stack = new Stack<>();
for (int j = nums.length - 1; j >= 0; j--) {
if (nums[j] > minI[j]) {
while (!stack.isEmpty() && stack.peek() <= minI[j]) {
stack.pop();
}
if (!stack.isEmpty() && stack.peek() < nums[j]) {
return true;
}
stack.push(nums[j]);
}
}
return false;
}
}