AlgoMaster Logo

132 Pattern

nums=[3, 1, 4, 2]
1public boolean find132pattern(int[] nums) {
2    if (nums.length < 3) return false;
3
4    int third = Integer.MIN_VALUE;
5    Stack<Integer> stack = new Stack<>();
6
7    for (int i = nums.length - 1; i >= 0; i--) {
8        if (nums[i] < third) {
9            return true;
10        }
11
12        while (!stack.isEmpty() && nums[i] > stack.peek()) {
13            third = stack.pop();
14        }
15
16        stack.push(nums[i]);
17    }
18
19    return false;
20}
0 / 12
nums3142stackthird: -∞result: false