-
Notifications
You must be signed in to change notification settings - Fork 481
/
1395.py
32 lines (27 loc) · 941 Bytes
/
1395.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
class Solution:
def numTeams(self, rating: List[int]) -> int:
if len(rating) < 3: return 0
N = 100000
def update(tr, x, v):
i = x
while i <= N:
tr[i] += v
i += i & -i
def getPrefixSum(tr, x):
res, i = 0, x
while i:
res += tr[i]
i -= i & -i
return res
def getSuffixSum(tr, i):
return getPrefixSum(tr, N) - getPrefixSum(tr, i - 1)
leftTree, rightTree = [0] * (N + 1), [0] * (N + 1)
for r in rating:
update(rightTree, r, 1)
res = 0
for r in rating:
update(rightTree, r, -1)
res += getPrefixSum(leftTree, r - 1) * getSuffixSum(rightTree, r + 1) + \
getSuffixSum(leftTree, r + 1) * getPrefixSum(rightTree, r - 1)
update(leftTree, r, 1)
return res