Leetcode 2570: Merge Two 2D Arrays by Summing Values

grid47
grid47
Exploring patterns and algorithms
Feb 24, 2024 6 min read

You are given two 2D integer arrays nums1 and nums2. Each element in nums1 and nums2 is an array of two integers: an id and a value. The arrays are sorted in ascending order by the id. Your task is to merge the two arrays into a single array, sorted by id. Each id should appear only once, and if an id appears in both arrays, its value should be the sum of the values from both arrays. If an id is present in only one array, its value remains as it is.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays nums1 and nums2. Each array contains pairs of integers, where each pair represents an id and its corresponding value.
Example: For example, nums1 = [[1, 3], [2, 5], [4, 7]] and nums2 = [[1, 4], [3, 2], [4, 3]].
Constraints:
• 1 <= nums1.length, nums2.length <= 200
• nums1[i].length == nums2[i].length == 2
• 1 <= idi, vali <= 1000
• Both arrays contain unique ids.
• Both arrays are sorted in strictly ascending order by id.
Output: Return the merged array where each id appears only once. If an id appears in both arrays, its value should be the sum of the values in both arrays. The array should be sorted in ascending order by id.
Example: For nums1 = [[1, 3], [2, 5], [4, 7]] and nums2 = [[1, 4], [3, 2], [4, 3]], the output is [[1, 7], [2, 5], [3, 2], [4, 10]].
Constraints:
• The output should be a merged array sorted by id, with each id appearing only once.
Goal: To merge two arrays of ids and values into one array, summing the values for duplicate ids and sorting the array by id.
Steps:
• 1. Use a map to store the id as the key and the sum of values as the value.
• 2. Traverse both nums1 and nums2, adding the values to the map based on the ids.
• 3. Convert the map to a vector of pairs, where each pair contains an id and its corresponding summed value.
• 4. Return the resulting vector sorted by id.
Goal: The input arrays nums1 and nums2 are both sorted in ascending order by id, and each array contains unique ids. The arrays can each contain up to 200 elements, and each id and value is between 1 and 1000.
Steps:
• 1 <= nums1.length, nums2.length <= 200
• nums1[i].length == nums2[i].length == 2
• 1 <= idi, vali <= 1000
• Both arrays contain unique ids.
• Both arrays are sorted in strictly ascending order by id.
Assumptions:
• The input arrays nums1 and nums2 are sorted in strictly ascending order by id.
• The arrays will not contain duplicate ids within each array.
Input: For nums1 = [[1, 3], [2, 5], [4, 7]] and nums2 = [[1, 4], [3, 2], [4, 3]], the output is [[1, 7], [2, 5], [3, 2], [4, 10]].
Explanation: In this case, the values for ids 1 and 4 are added because they appear in both arrays. The other ids are included as they are.

Input: For nums1 = [[3, 5], [7, 6]] and nums2 = [[2, 4], [5, 8]], the output is [[2, 4], [3, 5], [5, 8], [7, 6]].
Explanation: No ids overlap between the two arrays, so the result is a simple combination of the two arrays sorted by id.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus