132 Pattern LeetCode solution in Java

1
class Solution {
2
public boolean find132pattern(int[] nums) {
3
int min = Integer.MIN_VALUE;
4
int peak = nums.length;
5
for (int i = nums.length - 1; i >= 0; i--) {
6
// We find a "132" pattern if nums[i] < min, so return true...
7
if (nums[i] < min) return true;
8
// If peak < nums.length & nums[i] is greater than the peak element...
9
while (peak < nums.length && nums[i] > nums[peak]) min = nums[peak++];
10
// Now we have nums[i] <= nums[peak]
11
// We push nums[i] to the "stack"
12
peak--;
13
nums[peak] = nums[i];
14
}
15
return false;
16
}
17
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0