> For the complete documentation index, see [llms.txt](https://wentao-shao.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wentao-shao.gitbook.io/leetcode/stack/456.132-pattern.md).

# 456.132-Pattern

## 456. 132 Pattern

## 题目地址

<https://leetcode.com/problems/132-pattern/>

## 题目描述

```
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.

Note: n will be less than 15,000.

Example 1:
Input: [1, 2, 3, 4]
Output: False
Explanation: There is no 132 pattern in the sequence.

Example 2:
Input: [3, 1, 4, 2]
Output: True
Explanation: There is a 132 pattern in the sequence: [1, 4, 2].

Example 3:
Input: [-1, 3, 2, 0]
Output: True
Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
```

## 代码

### Approach 1: Brute Force

The simplest solution is to consider every triplet (i, j, k) and check if the corresponding numbers satisfy the 132 criteria. If any such triplet is found, we can return a True value. If no such triplet is found, we need to return a False value.

**Complexity Analysis**

* Time complexity : O(n^3). Three loops are used to consider every possible triplet. Here, n*n* refers to the size of nums array.
* Space complexity : O(1). Constant extra space is used.

```java
class Solution {
    public boolean find132pattern(int[] nums) {
    for (int i = 0; i < nums.length - 2; i++) {
      for (int j = i + 1; j < nums.length - 1; j++) {
        for (int k = j + 1; k < nums.length; k++) {
          if (nums[k] > nums[i] && nums[j] > nums[k]) {
            return true;
          }
        }
      }
    }

    return false;
  }
}
```

### Approach #2 Better Brute Force

**Complexity Analysis**

* Time complexity : O(n^2). Two loops are used to find the nums\[j],nums\[k] pairs. Here, n refers to the size of nums array.
* Space complexity : O(1). Constant extra space is used.

```java
class Solution {
  public boolean find132pattern(int[] nums) {
    int min_i = Integer.MAX_VALUE;
    for (int j = 0; j < nums.length - 1; j++) {
      min_i = Math.min(min_i, nums[j]);
      for (int k = j + 1; k < nums.length; k++) {
        if (nums[k] < nums[j] && min_i < nums[k]) {
          return true;
        }
      }
    }

    return false;
  }
}
```

### Approach #3 Stack

### 单调递减栈

从左到右，生成min\[i]左边最小的数组

从右到左，找出 num\[j] > min\[j] && stack.peek() < num\[j] && stack.peek() > min\[j]

```java
class Solution {
  public boolean find132pattern(int[] nums) {
    if (nums.length < 3) return false;
    Stack < Integer > stack = new Stack < > ();
    int[] min = new int[nums.length];
    min[0] = nums[0];
    for (int i = 1; i < nums.length; i++)
        min[i] = Math.min(min[i - 1], nums[i]);
    for (int j = nums.length - 1; j >= 0; j--) {
        if (nums[j] > min[j]) {
            while (!stack.isEmpty() && stack.peek() <= min[j]) {
                stack.pop();
            }

            if (!stack.isEmpty() && stack.peek() < nums[j]) {
                return true;
            }

            stack.push(nums[j]);
        }
    }
    return false;
  }
}
```

### Approach 4: Using Array as a Stack

```java
public class Solution {
    public boolean find132pattern(int[] nums) {
        if (nums.length < 3)
            return false;
        int[] min = new int[nums.length];
        min[0] = nums[0];
        for (int i = 1; i < nums.length; i++)
            min[i] = Math.min(min[i - 1], nums[i]);
        for (int j = nums.length - 1, k = nums.length; j >= 0; j--) {
            if (nums[j] > min[j]) {
                while (k < nums.length && nums[k] <= min[j])
                    k++;
                if (k < nums.length && nums[k] < nums[j])
                    return true;
                nums[--k] = nums[j];
            }
        }
        return false;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/stack/456.132-pattern.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
