-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-good-triplets-in-an-array.py
More file actions
36 lines (32 loc) · 1.03 KB
/
count-good-triplets-in-an-array.py
File metadata and controls
36 lines (32 loc) · 1.03 KB
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
class FenwickTree:
def __init__(self, size):
self.tree = [0] * (size + 1)
def update(self, index, delta):
index += 1
while index <= len(self.tree) - 1:
self.tree[index] += delta
index += index & -index
def query(self, index):
index += 1
res = 0
while index > 0:
res += self.tree[index]
index -= index & -index
return res
class Solution:
def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
pos2, reversedIndexMapping = [0] * n, [0] * n
for i, num2 in enumerate(nums2):
pos2[num2] = i
for i, num1 in enumerate(nums1):
reversedIndexMapping[pos2[num1]] = i
tree = FenwickTree(n)
res = 0
for value in range(n):
pos = reversedIndexMapping[value]
left = tree.query(pos)
tree.update(pos, 1)
right = (n - 1 - pos) - (value - left)
res += left * right
return res