oneby7

Stacks and Queues Monotonic stack

222. 132 Pattern

Medium

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]

Output: true

Explanation: 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]

Output: false

Explanation: 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]

Output: false

Explanation: 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.

nums = [3, 5, 2, 1]

Constraints

  • 1 ≤ nums.length ≤ 2 × 105
  • -109 ≤ nums[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.