<jupyter_start><jupyter_text>单调栈问题<jupyter_code>说明：
    给定n个整数nums的数组，则 132个模式是三个整数nums [i]，nums [j]和nums [k]的子序列，
    使得i <j <k 和 nums [i] <nums [k] <nums [j]。
    如果以数字为单位的132个模式，则返回true，否则返回false。

#　中间的值最大，左边的值最小，右边的值次之
    
Example 1:
    Input: nums = [1,2,3,4]
    Output: false
    Explanation: There is no 132 pattern in the sequence.

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

Example 3:
    Input: nums = [-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].

Constraints:
    1、n == nums.length
    2、1 <= n <= 3 * 104
    3、-109 <= nums[i] <= 109<jupyter_output><empty_output><jupyter_text><jupyter_code>class Solution:
    def find132pattern(self, nums):
        stacks = []
        mid = float('-inf')
        for i in range(len(nums)-1, -1, -1):
            val = nums[i]
            if val < mid:
                return True
            else:
                while stacks and stacks[-1] < val:
                    mid = stacks.pop()
            stacks.append(val)
        return False
nums_ = [1,4,0,-1,-2,-3,-1,-2]
solution = Solution()
solution.find132pattern(nums_)
class Solution:
    def find132pattern(self, nums):
        res = []
        for num in nums:
            if len(res) == 3:
                return True
            elif len(res) == 1 and res[0] >= num:
                res.pop()
            elif len(res) == 2:
                if res[0] >= num:
                    continue
                elif res[-1] <= num:
                    res.pop()
            res.append(num)
        print(res)
        return len(res) == 3
nums_ = [1,4,0,-1,-2,-3,-1,-2]
solution = Solution()
solution.find132pattern(nums_)
class Solution:
    def find132pattern(self, nums):
        self.res = []
        for num in nums:
            self.helper(num)
            if len(self.res) == 3:
                return True
        return len(self.res) == 3
        
    def helper(self, num):
        if len(self.res) == 1 and self.res[0] >= num:
            self.res.pop()
            self.res.append(num)
        elif len(self.res) == 2:
            if self.res[0] >= num:
                return
            if self.res[0] >= num:
                self.res.pop(0)
                if self.res[-1] <= num:
                    self.res.pop()
                self.res.append(num)
            if self.res and self.res[-1] <= num:
                self.res.pop()
                self.res.append(num)
            else:
                self.res.append(num)
        else:
            self.res.append(num)
nums_ = [3,5,0,3,4]
solution = Solution()
solution.find132pattern(nums_)
class Solution:
    def find132pattern(self, nums):
        res = []
        for num in nums:
            if len(res) == 3:
                return True
            
            if len(res) == 1 and res[-1] > num:
                res.pop()
            elif len(res) == 2:
                if res[0] > num:
                    res.pop(0)
                    if res[-1] < num:
                        res.pop()
                if res and res[-1] < num:
                    res.pop()
        res.append(num)
        return len(res) == 3        
nums_ = [3,1,4,2]
solution = Solution()
solution.find132pattern(nums_)
a= 3
a == 3
a = [1, 2, 3]
a.clear()
a<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13636363636363635, 'fraction_numerical': 0.04105571847507331, 'mean_word_length': 1.4807272727272727, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '378019', 'license': 'Apache-2.0', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1196, 'n_words': 288, 'path': 'Stack/0915/456. 132 Pattern.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '3410'}