Given an array of n integers `nums`, a 132 pattern is a subsequence `nums[i]`, `nums[j]`, `nums[k]` with `i < j < k` and `nums[i] < nums[k] < nums[j]`. Return `true` if there is a 132 pattern in `nums`, otherwise return `false`.
Given an array of n integers nums, a 132 pattern is a subsequence nums[i], nums[j], nums[k] with i < j < k and nums[i] < nums[k] < nums[j]. Return true if there is a 132 pattern in nums, otherwise return false.
Example 1:
Input: {"nums":[1,2,3,4]}
Output: false
Constraints:
n==nums.length
1≤n≤2×105
−109≤nums[i]≤109
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[1,2,3,4]}
Scan from the right. The stack holds candidates for the 3, and third holds the best 2 found so far (none yet, so -inf).
nums, scanned right to left
1
2
3
4
0
1
2
3
current i
on stack
3 (popper)
2 (third)
1 (below third)
stack (bottom to top, decreasing)
empty
Every pop means a bigger value sits to its left, so the popped value is a valid 2. Keeping the largest such 2 gives the 1 the most room.