You are given two 0-indexed arrays nums1
and nums2
of length n
, both of which are permutations of [0, 1, ..., n - 1]
.
A good triplet is a set of 3
distinct values which are present in increasing order by position both in nums1
and nums2
. In other words, if we consider pos1v
as the index of the value v
in nums1
and pos2v
as the index of the value v
in nums2
, then a good triplet will be a set (x, y, z)
where 0 <= x, y, z <= n - 1
, such that pos1x < pos1y < pos1z
and pos2x < pos2y < pos2z
.
Return the total number of good triplets.
Example 1:
Input: nums1 = [2,0,1,3], nums2 = [0,1,2,3] Output: 1 Explanation: There are 4 triplets (x,y,z) such that pos1x < pos1y < pos1z. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3). Out of those triplets, only the triplet (0,1,3) satisfies pos2x < pos2y < pos2z. Hence, there is only 1 good triplet.
Example 2:
Input: nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3] Output: 4 Explanation: The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
Constraints:
n == nums1.length == nums2.length
3 <= n <= 105
0 <= nums1[i], nums2[i] <= n - 1
nums1
and nums2
are permutations of [0, 1, ..., n - 1]
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for counting good triplets is like exhaustively checking every possible combination of three numbers from a list. We look at each possible set of three numbers to see if they meet specific requirements. If they do, we count it as a good triplet.
Here's how the algorithm would work step-by-step:
def count_good_triplets(array, a_threshold, b_threshold, c_threshold):
list_length = len(array)
good_triplet_count = 0
for first_index in range(list_length):
for second_index in range(first_index + 1, list_length):
# Ensure second index is after first
for third_index in range(second_index + 1, list_length):
# Third index must be after the second
first_second_diff = abs(array[first_index] - array[second_index])
second_third_diff = abs(array[second_index] - array[third_index])
first_third_diff = abs(array[first_index] - array[third_index])
# Check that the triplet meets all conditions
if (first_second_diff <= a_threshold and
second_third_diff <= b_threshold and
first_third_diff <= c_threshold):
good_triplet_count += 1
return good_triplet_count
The brute-force method would check every possible triplet, which is slow. A much faster approach involves focusing on each number in the input and efficiently counting the valid triplets it can form part of by considering numbers before and after it.
Here's how the algorithm would work step-by-step:
def count_good_triplets(array, first_threshold, second_threshold, third_threshold):
array_length = len(array)
good_triplet_count = 0
for j in range(1, array_length - 1):
# Count elements before array[j] that satisfy condition one.
before_count = 0
for i in range(j):
if abs(array[i] - array[j]) <= first_threshold:
before_count += 1
# Count elements after array[j] that satisfy conditions two and three.
after_count = 0
for k in range(j + 1, array_length):
if abs(array[j] - array[k]) <= second_threshold and abs(array[i] - array[k]) <= third_threshold:
after_count += 1
# Accumulate the number of good triplets found using array[j].
good_triplet_count += before_count * after_count
return good_triplet_count
Case | How to Handle |
---|---|
Null or empty input array | Return 0 if the input array is null or empty as no triplets can be formed. |
Array size less than 3 | Return 0 if the array has fewer than 3 elements because a triplet cannot be formed. |
Large array size with tight time constraints | Optimize the solution to avoid excessive nested loops (e.g., consider sorting or other data structures for efficiency). |
Identical elements in the array | The solution should correctly count triplets even when the array contains duplicate elements at different indices. |
a, b, or c are negative numbers | The absolute difference calculations must handle negative numbers correctly. |
a, b, or c are zero | The absolute difference calculations should work fine with zero values. |
Extreme values for a, b, c, a, b, or c that could cause integer overflow | Consider using larger data types (e.g., long) to avoid integer overflow when calculating absolute differences. |
No good triplets exist in the input array | The solution should correctly return 0 when no triplets satisfy the given conditions. |