[ Leetcode ] 456. 132 Pattern


456132 Pattern

one -> three -> twp patten

- assume every number in the iteration is three.
- use three to find the max two
- if you find the two and three, and now there's a number small than two. then you got it.




class Solution(object):
    def find132pattern(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        st = []
        two = float('-inf')
        # assume every number in the iteration is three.
        for i in range(len(nums) - 1, -1, -1):
            
            # if you find the two and three, and now there's a number small than two. then you got it.
            if nums[i] < two:
                return True
            
            # use three to find the max two    
            while len(st) != 0 and nums[i] > st[-1]:
                two = st.pop()
                
            st.append(nums[i])
        return False



留言