Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sortedcontainers import SortedList
- class Solution:
- def find132pattern(self, nums: List[int]) -> bool:
- N = len(nums)
- lmin = nums[0]
- rmin_heap = [] # All the values greater than lmin in a min heap
- rmax_heap = [] # All the values less than or equal to lmin in a max heap
- for i in range(2, N):
- n = nums[i]
- if n > lmin:
- heappush(rmin_heap, (n, i))
- else:
- heappush(rmax_heap, (-n, i))
- for i in range(1, N - 1):
- while rmax_heap:
- if rmax_heap[0][1] <= i:
- heappop(rmax_heap)
- continue
- if -rmax_heap[0][0] > lmin:
- rn, ri = heappop(rmax_heap)
- heappush(rmin_heap, (-rn, ri))
- continue
- break
- while rmin_heap and rmin_heap[0][1] <= i:
- heappop(rmin_heap)
- n = nums[i]
- if rmin_heap:
- rmin = rmin_heap[0][0]
- if lmin < rmin < n:
- return True
- lmin = min(lmin, n)
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement