LeetCode 题解工作台

132 模式

给你一个整数数组 nums ,数组中共有 n 个整数。 132 模式的子序列 由三个整数 nums[i] 、 nums[j] 和 nums[k] 组成,并同时满足: i 和 nums[i] 。 如果 nums 中存在 132 模式的子序列 ,返回 true ;否则,返回 false 。 示例 1: …

category

5

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·搜索·答案·空间

bolt

答案摘要

我们可以枚举从右往左枚举整数 ,并维护一个单调栈,栈中的元素从栈底到栈顶单调递减。维护一个变量 ,表示 右侧且小于 的最大值。初始时, 的值为 。 我们从右往左遍历数组,对于遍历到的每个元素 ,如果 小于 ,则说明我们找到了一个满足 $nums[i] \lt nums[k] \lt nums[j]$ 的三元组,返回 `true`。否则,如果栈顶元素小于 ,则我们循环将栈顶元素出栈,并且更新 …

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 nums ,数组中共有 n 个整数。132 模式的子序列 由三个整数 nums[i]nums[j]nums[k] 组成,并同时满足:i < j < knums[i] < nums[k] < nums[j]

如果 nums 中存在 132 模式的子序列 ,返回 true ;否则,返回 false

 

示例 1:

输入:nums = [1,2,3,4]
输出:false
解释:序列中不存在 132 模式的子序列。

示例 2:

输入:nums = [3,1,4,2]
输出:true
解释:序列中有 1 个 132 模式的子序列: [1, 4, 2] 。

示例 3:

输入:nums = [-1,3,2,0]
输出:true
解释:序列中有 3 个 132 模式的的子序列:[-1, 3, 2]、[-1, 3, 0] 和 [-1, 2, 0] 。

 

提示:

  • n == nums.length
  • 1 <= n <= 2 * 105
  • -109 <= nums[i] <= 109
lightbulb

解题思路

方法一:单调栈

我们可以枚举从右往左枚举整数 nums[i]nums[i],并维护一个单调栈,栈中的元素从栈底到栈顶单调递减。维护一个变量 vkvk,表示 nums[i]nums[i] 右侧且小于 nums[i]nums[i] 的最大值。初始时,vkvk 的值为 -\infty

我们从右往左遍历数组,对于遍历到的每个元素 nums[i]nums[i],如果 nums[i]nums[i] 小于 vkvk,则说明我们找到了一个满足 nums[i]<nums[k]<nums[j]nums[i] \lt nums[k] \lt nums[j] 的三元组,返回 true。否则,如果栈顶元素小于 nums[i]nums[i],则我们循环将栈顶元素出栈,并且更新 vkvk 的值为出栈元素,直到栈为空或者栈顶元素大于等于 nums[i]nums[i]。最后,我们将 nums[i]nums[i] 入栈。

如果遍历结束后仍未找到满足条件的三元组,说明不存在这样的三元组,返回 false

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 为数组的长度。

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def find132pattern(self, nums: List[int]) -> bool:
        vk = -inf
        stk = []
        for x in nums[::-1]:
            if x < vk:
                return True
            while stk and stk[-1] < x:
                vk = stk.pop()
            stk.append(x)
        return False
speed

复杂度分析

指标
时间complexity is O(n) using a single pass with a monotonic stack or O(n log n) when binary searching over a sorted set of '2' candidates. Space complexity is O(n) for storing the stack or ordered set for candidate tracking.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate considers tracking min values for '1' positions and stack management for '3'.

  • question_mark

    Candidate attempts O(n^2) brute force and may need guidance toward monotonic stack or binary search over valid answers.

  • question_mark

    Candidate mentions handling negative numbers and duplicate elements carefully to avoid false negatives.

warning

常见陷阱

外企场景
  • error

    Assuming the 132 pattern elements must be consecutive rather than a subsequence.

  • error

    Failing to update or correctly maintain the stack or min value, causing missed patterns.

  • error

    Overcomplicating with unnecessary nested loops instead of using a single-pass stack approach.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Detect 321 or 213 patterns instead, adjusting stack logic accordingly.

  • arrow_right_alt

    Return all indices of valid 132 patterns instead of just true/false.

  • arrow_right_alt

    Handle streaming input where the array is too large to store entirely in memory.

help

常见问题

外企场景

132 模式题解:二分·搜索·答案·空间 | LeetCode #456 中等