1865. Finding Pairs With a Certain Sum #1894
-
Topics: You are given two integer arrays
Implement the
Example 1:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to efficiently handle two operations on two given arrays: adding a value to an element in the second array and counting the number of pairs from both arrays that sum to a given value. The challenge lies in optimizing these operations, especially the count operation, given the constraints. Approach
Let's implement this solution in PHP: 1865. Finding Pairs With a Certain Sum <?php
class FindSumPairs {
private $nums1;
private $nums2;
private $freq;
/**
* @param Integer[] $nums1
* @param Integer[] $nums2
*/
function __construct($nums1, $nums2) {
$this->nums1 = $nums1;
$this->nums2 = $nums2;
$this->freq = array();
foreach ($nums2 as $num) {
if (isset($this->freq[$num])) {
$this->freq[$num]++;
} else {
$this->freq[$num] = 1;
}
}
}
/**
* @param Integer $index
* @param Integer $val
* @return NULL
*/
function add($index, $val) {
$oldVal = $this->nums2[$index];
$newVal = $oldVal + $val;
$this->nums2[$index] = $newVal;
$this->freq[$oldVal]--;
if ($this->freq[$oldVal] == 0) {
unset($this->freq[$oldVal]);
}
if (isset($this->freq[$newVal])) {
$this->freq[$newVal]++;
} else {
$this->freq[$newVal] = 1;
}
}
/**
* @param Integer $tot
* @return Integer
*/
function count($tot) {
$res = 0;
foreach ($this->nums1 as $x) {
$required = $tot - $x;
if ($required < 1) {
continue;
}
if (isset($this->freq[$required])) {
$res += $this->freq[$required];
}
}
return $res;
}
}
/////// 🔽 Example usage (same as problem) 🔽 ///////
$commands = ["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"];
$params = [
[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]],
[7],
[3, 2],
[8],
[4],
[0, 1],
[1, 1],
[7]
];
$results = array();
$object = null;
foreach ($commands as $i => $cmd) {
if ($cmd == "FindSumPairs") {
$object = new FindSumPairs($params[$i][0], $params[$i][1]);
$results[] = null;
} elseif ($cmd == "add") {
$object->add($params[$i][0], $params[$i][1]);
$results[] = null;
} elseif ($cmd == "count") {
$results[] = $object->count($params[$i][0]);
}
}
print_r($results);
?> Explanation:
This approach efficiently handles both operations by leveraging the frequency map, reducing the time complexity for the count operation from O(n*m) to O(n), where n is the length of |
Beta Was this translation helpful? Give feedback.
We need to efficiently handle two operations on two given arrays: adding a value to an element in the second array and counting the number of pairs from both arrays that sum to a given value. The challenge lies in optimizing these operations, especially the count operation, given the constraints.
Approach
Initialization:
nums1
andnums2
.nums2
to keep track of how many times each number appears. This allows efficient updates and lookups.Add Operation:
val
to an element at a specific index innums2
, first retrieve the old value at that index.val
to the old value.