Stacks and Queues Monotonic stack
222. 132 Pattern
You are given an array of integers nums. Return true if it contains a 132 pattern, and false
otherwise.
A 132 pattern is three positions i < j < k with nums[i] < nums[k] < nums[j]. The first value is the
smallest, the second is the largest, and the third falls strictly between them, the way the digits of 132 do.
The three positions do not have to be next to each other.
Example 1
Input: nums = [4, 1, 7, 2, 5]
trueExplanation: Take 1, 7 and 5, at indexes 1, 2 and 4. The 1 is the smallest, the 7 is the largest, and the 5 falls between them. Other choices work too, such as 4, 7, 5, but one is enough.
Example 2
Input: nums = [-5, -5, -2, -2, 0]
falseExplanation: The values never go down, so no value has a smaller one after it. In −5, −2, −2 the last value equals the middle one, and the pattern needs it to be strictly smaller.
Example 3
Input: nums = [3, 1, 2]
falseExplanation: The only choice is 3, 1, 2, in that order. The smallest value comes second, so the shape is 312, not 132.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 2 × 105-109 ≤ nums[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.