-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2179-count-good-triplets-in-an-array.py
More file actions
54 lines (46 loc) · 1.36 KB
/
2179-count-good-triplets-in-an-array.py
File metadata and controls
54 lines (46 loc) · 1.36 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# time complexity: O(nlogn)
# space complexity: O(n)
from typing import List
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
'''
prefix = [2, 0, 0, 0]
nums = [2, 0, 1, 3]
suffix = [3, 3, 3, 3]
'''
nums1 = [2, 0, 1, 3]
nums2 = [0, 1, 2, 3]
print(Solution().goodTriplets(nums1, nums2))
nums1 = [4, 0, 1, 3, 2]
nums2 = [4, 1, 0, 2, 3]
print(Solution().goodTriplets(nums1, nums2))