Advertisement
kosievdmerwe

456

May 6th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. from sortedcontainers import SortedList
  2.  
  3. class Solution:
  4.     def find132pattern(self, nums: List[int]) -> bool:
  5.         N = len(nums)
  6.         lmin = nums[0]
  7.        
  8.         rmin_heap = []  # All the values greater than lmin in a min heap
  9.         rmax_heap = []  # All the values less than or equal to lmin in a max heap
  10.        
  11.         for i in range(2, N):
  12.             n = nums[i]
  13.             if n > lmin:
  14.                 heappush(rmin_heap, (n, i))
  15.             else:
  16.                 heappush(rmax_heap, (-n, i))
  17.        
  18.         for i in range(1, N - 1):
  19.             while rmax_heap:
  20.                 if rmax_heap[0][1] <= i:
  21.                     heappop(rmax_heap)
  22.                     continue
  23.                
  24.                 if -rmax_heap[0][0] > lmin:
  25.                     rn, ri = heappop(rmax_heap)
  26.                     heappush(rmin_heap, (-rn, ri))
  27.                     continue
  28.                
  29.                 break
  30.                
  31.             while rmin_heap and rmin_heap[0][1] <= i:
  32.                 heappop(rmin_heap)
  33.            
  34.             n = nums[i]
  35.             if rmin_heap:
  36.                 rmin = rmin_heap[0][0]
  37.                 if lmin < rmin < n:
  38.                     return True
  39.            
  40.             lmin = min(lmin, n)
  41.  
  42.         return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement