2570. Merge Two 2D Arrays by Summing Values solution #1381
-
Topics: You are given two 2D integer arrays nums1 and nums2.
Each array contains unique ids and is sorted in ascending order by id. Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:
Return the resulting array. The returned array must be sorted in ascending order by id. Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem requires merging two 2D arrays, Key Points
ApproachTo solve the problem, we will utilize a two-pointer approach to traverse both arrays simultaneously. The idea is to compare the
We'll also take advantage of a dictionary (hash map) to track the sum of the values of the same Plan
Let's implement this solution in PHP: 2570. Merge Two 2D Arrays by Summing Values solution <?php
/**
* @param Integer[][] $nums1
* @param Integer[][] $nums2
* @return Integer[][]
*/
function mergeArrays(array $nums1, array $nums2): array
{
$result = [];
$i = $j = 0;
while ($i < count($nums1) || $j < count($nums2)) {
if ($j == count($nums2) || ($i < count($nums1) && $nums1[$i][0] < $nums2[$j][0])) {
if ($result && $result[count($result) - 1][0] == $nums1[$i][0]) {
$result[count($result) - 1][1] += $nums1[$i][1];
} else {
$result[] = $nums1[$i];
}
$i++;
} else {
if ($result && $result[count($result) - 1][0] == $nums2[$j][0]) {
$result[count($result) - 1][1] += $nums2[$j][1];
} else {
$result[] = $nums2[$j];
}
$j++;
}
}
return $result;
}
// Example 1
$nums1 = [[1,2],[2,3],[4,5]];
$nums2 = [[1,4],[3,2],[4,1]];
print_r(mergeArrays($nums1, $nums2)); // Output: [[1,6],[2,3],[3,2],[4,6]]
// Example 2
$nums1 = [[2,4],[3,6],[5,5]];
$nums2 = [[1,3],[4,3]];
print_r(mergeArrays($nums1, $nums2)); // Output: [[1,3],[2,4],[3,6],[4,3],[5,5]]
?> Explanation:
Example WalkthroughExample 1:Input:$nums1 = [[1,2], [2,3], [4,5]];
$nums2 = [[1,4], [3,2], [4,1]]; Step-by-Step Execution:
Output:[[1,6], [2,3], [3,2], [4,6]] Example 2:Input:$nums1 = [[2,4], [3,6], [5,5]];
$nums2 = [[1,3], [4,3]]; Step-by-Step Execution:
Output:[[1,3], [2,4], [3,6], [4,3], [5,5]] Time ComplexityThe solution processes each element once using the two-pointer technique:
Output for ExampleFor the inputs: nums1 = [[1, 2], [2, 3], [4, 5]];
nums2 = [[1, 4], [3, 2], [4, 1]]; The output is: [[1, 6], [2, 3], [3, 2], [4, 6]] For the inputs: nums1 = [[2, 4], [3, 6], [5, 5]];
nums2 = [[1, 3], [4, 3]]; The output is: [[1, 3], [2, 4], [3, 6], [4, 3], [5, 5]]
This method efficiently solves the problem while keeping the implementation simple and easy to understand. 🚀 |
Beta Was this translation helpful? Give feedback.
The problem requires merging two 2D arrays,
nums1
andnums2
, where each sub-array contains anid
and a correspondingvalue
. The goal is to combine these arrays by summing the values of the sameid
and ensuring that the final result is sorted in ascending order byid
. If anid
appears only in one array, its value is included from that array, and the other is treated as0
. The merged result must only include eachid
once and should be sorted byid
.Key Points
id
.id
should be summed.id
once, sorted in ascending order.id
is missing in one of the arrays, its value is considered0
in the…