Skip to content

Commit da6d039

Browse files
committed
Sync LeetCode submission Runtime - 561 ms (83.02%), Memory - 37.8 MB (91.51%)
1 parent 038e074 commit da6d039

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>You are given two <strong>0-indexed</strong> arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>, both of which are <strong>permutations</strong> of <code>[0, 1, ..., n - 1]</code>.</p>
2+
3+
<p>A <strong>good triplet</strong> is a set of <code>3</code> <strong>distinct</strong> values which are present in <strong>increasing order</strong> by position both in <code>nums1</code> and <code>nums2</code>. In other words, if we consider <code>pos1<sub>v</sub></code> as the index of the value <code>v</code> in <code>nums1</code> and <code>pos2<sub>v</sub></code> as the index of the value <code>v</code> in <code>nums2</code>, then a good triplet will be a set <code>(x, y, z)</code> where <code>0 &lt;= x, y, z &lt;= n - 1</code>, such that <code>pos1<sub>x</sub> &lt; pos1<sub>y</sub> &lt; pos1<sub>z</sub></code> and <code>pos2<sub>x</sub> &lt; pos2<sub>y</sub> &lt; pos2<sub>z</sub></code>.</p>
4+
5+
<p>Return <em>the <strong>total number</strong> of good triplets</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums1 = [2,0,1,3], nums2 = [0,1,2,3]
12+
<strong>Output:</strong> 1
13+
<strong>Explanation:</strong>
14+
There are 4 triplets (x,y,z) such that pos1<sub>x</sub> &lt; pos1<sub>y</sub> &lt; pos1<sub>z</sub>. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3).
15+
Out of those triplets, only the triplet (0,1,3) satisfies pos2<sub>x</sub> &lt; pos2<sub>y</sub> &lt; pos2<sub>z</sub>. Hence, there is only 1 good triplet.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
22+
<strong>Output:</strong> 4
23+
<strong>Explanation:</strong> The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>n == nums1.length == nums2.length</code></li>
31+
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
32+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= n - 1</code></li>
33+
<li><code>nums1</code> and <code>nums2</code> are permutations of <code>[0, 1, ..., n - 1]</code>.</li>
34+
</ul>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Approach 1: Binary Indexed Tree
2+
3+
# Time: O(n * log n)
4+
# Space: O(n)
5+
6+
class FenwickTree:
7+
def __init__(self, size):
8+
self.tree = [0] * (size + 1)
9+
10+
def update(self, index, delta):
11+
index += 1
12+
while index <= len(self.tree) - 1:
13+
self.tree[index] += delta
14+
index += index & -index
15+
16+
def query(self, index):
17+
index += 1
18+
res = 0
19+
while index > 0:
20+
res += self.tree[index]
21+
index -= index & -index
22+
return res
23+
24+
class Solution:
25+
def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int:
26+
n = len(nums1)
27+
pos2, reversed_index_mapping = [0] * n, [0] * n
28+
29+
for i, num2 in enumerate(nums2):
30+
pos2[num2] = i
31+
32+
for i, num1 in enumerate(nums1):
33+
reversed_index_mapping[pos2[num1]] = i
34+
35+
tree = FenwickTree(n)
36+
res = 0
37+
38+
for value in range(n):
39+
pos = reversed_index_mapping[value]
40+
left = tree.query(pos)
41+
tree.update(pos, 1)
42+
right = (n - 1 - pos) - (value - left)
43+
res += left * right
44+
45+
return res
46+
47+

0 commit comments

Comments
 (0)