forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-good-triplets-in-an-array.py
41 lines (36 loc) · 1.07 KB
/
count-good-triplets-in-an-array.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
34
35
36
37
38
39
40
41
# Time: O(nlogn)
# Space: O(n)
class BIT(object): # 0-indexed.
def __init__(self, n):
self.__bit = [0]*(n+1) # Extra one for dummy node.
def add(self, i, val):
i += 1 # Extra one for dummy node.
while i < len(self.__bit):
self.__bit[i] += val
i += (i & -i)
def query(self, i):
i += 1 # Extra one for dummy node.
ret = 0
while i > 0:
ret += self.__bit[i]
i -= (i & -i)
return ret
# bit, fenwick tree, combinatorics
class Solution(object):
def goodTriplets(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: int
"""
lookup = [0]*len(nums1)
for i, x in enumerate(nums1):
lookup[x] = i
result = 0
bit = BIT(len(nums1))
for i, x in enumerate(nums2):
smaller = bit.query(lookup[x]-1)
larger = (len(nums1)-(lookup[x]+1))-(i-smaller)
result += smaller*larger
bit.add(lookup[x], 1)
return result