-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution2.py
33 lines (29 loc) · 883 Bytes
/
Solution2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
def update(self, x, delta):
while x <= self.n:
self.c[x] += delta
x += x & -x
def query(self, x):
s = 0
while x:
s += self.c[x]
x -= x & -x
return s
class Solution:
def find132pattern(self, nums: List[int]) -> bool:
s = sorted(set(nums))
n = len(nums)
left = [inf] * (n + 1)
for i, x in enumerate(nums):
left[i + 1] = min(left[i], x)
tree = BinaryIndexedTree(len(s))
for i in range(n - 1, -1, -1):
x = bisect_left(s, nums[i]) + 1
y = bisect_left(s, left[i]) + 1
if x > y and tree.query(x - 1) > tree.query(y):
return True
tree.update(x, 1)
return False