Skip to content

Commit bfa2851

Browse files
authoredDec 29, 2022
Merge pull request #1606 from crazysamurai/LeetCode-456
Create: 456-132-Pattern.java
2 parents 4fee6df + 32c1628 commit bfa2851

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
 

Diff for: ‎java/0456-132-Pattern.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
/*
3+
* Mono Stack
4+
* TC: O(n)
5+
* SC: O(n)
6+
*/
7+
public boolean find132pattern(int[] nums) {
8+
int n = nums.length;
9+
Stack<Integer> st = new Stack<>();
10+
int secondMax = Integer.MIN_VALUE;
11+
12+
for (int x = n - 1; x >= 0; x--) {
13+
if (nums[x] < secondMax)
14+
return true;
15+
16+
while (!st.isEmpty() && st.peek() < nums[x])
17+
secondMax = Math.max(st.pop(), secondMax);
18+
19+
st.push(nums[x]);
20+
}
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.