2179. Count Good Triplets in an Array #1562
-
Topics: You are given two 0-indexed arrays A good triplet is a set of Return the total number of good triplets. Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to count the number of good triplets (x, y, z) in two permutations such that the positions of x, y, and z are in increasing order in both permutations. A good triplet (x, y, z) must satisfy pos1[x] < pos1[y] < pos1[z] and pos2[x] < pos2[y] < pos2[z]. Approach
Let's implement this solution in PHP: 2179. Count Good Triplets in an Array <?php
class FenwickTree {
private $tree;
private $size;
/**
* @param $size
*/
public function __construct($size) {
$this->size = $size;
$this->tree = array_fill(0, $size + 1, 0); // 1-based indexing
}
/**
* @param $index
* @param $delta
* @return void
*/
public function update($index, $delta) {
$index++; // convert to 1-based index
while ($index <= $this->size) {
$this->tree[$index] += $delta;
$index += $index & -$index;
}
}
/**
* @param $index
* @return int|mixed
*/
public function query($index) {
$index++; // convert to 1-based index
$sum = 0;
while ($index > 0) {
$sum += $this->tree[$index];
$index -= $index & -$index;
}
return $sum;
}
}
/**
* @param Integer[] $nums1
* @param Integer[] $nums2
* @return Integer
*/
function goodTriplets($nums1, $nums2) {
$n = count($nums1);
$pos1 = array();
$pos2 = array();
for ($i = 0; $i < $n; $i++) {
$pos1[$nums1[$i]] = $i;
$pos2[$nums2[$i]] = $i;
}
$elements = array();
for ($x = 0; $x < $n; $x++) {
$elements[] = array('x' => $x, 'a' => $pos1[$x], 'b' => $pos2[$x]);
}
// Compute left counts
usort($elements, function($a, $b) {
return $a['a'] - $b['a'];
});
$ft_left = new FenwickTree($n);
$left = array_fill(0, $n, 0);
foreach ($elements as $elem) {
$x = $elem['x'];
$b = $elem['b'];
$left[$x] = $ft_left->query($b - 1);
$ft_left->update($b, 1);
}
// Compute right counts
usort($elements, function($a, $b) {
return $b['a'] - $a['a'];
});
$ft_right = new FenwickTree($n);
$right = array_fill(0, $n, 0);
$total = 0;
foreach ($elements as $elem) {
$x = $elem['x'];
$b = $elem['b'];
$sum = $ft_right->query($b);
$right[$x] = $total - $sum;
$ft_right->update($b, 1);
$total++;
}
// Calculate result
$result = 0;
for ($x = 0; $x < $n; $x++) {
$result += $left[$x] * $right[$x];
}
return $result;
}
// Example 1
$nums1 = [2, 0, 1, 3];
$nums2 = [0, 1, 2, 3];
echo countGoodTriplets($nums1, $nums2) . "\n"; // Output: 1
// Example 2
$nums1 = [4, 0, 1, 3, 2];
$nums2 = [4, 1, 0, 2, 3];
echo countGoodTriplets($nums1, $nums2) . "\n"; // Output: 4
?> Explanation:
This approach ensures that we efficiently count the required elements using Fenwick Trees, resulting in an overall time complexity of O(n log n), which is suitable for large input sizes up to 100,000. |
Beta Was this translation helpful? Give feedback.
We need to count the number of good triplets (x, y, z) in two permutations such that the positions of x, y, and z are in increasing order in both permutations. A good triplet (x, y, z) must satisfy pos1[x] < pos1[y] < pos1[z] and pos2[x] < pos2[y] < pos2[z].
Approach