The Indian Engineer

Problem 2570 Merge Two 2D Arrays by Summing Values

Posted on 6 mins

Array Hash-Table Two-Pointers

Problem Statement

Link - Problem 2570

Question

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:

Input: nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]
Output: [[1,6],[2,3],[3,2],[4,6]]
Explanation: The resulting array contains the following:
- id = 1, the value of this id is 2 + 4 = 6.
- id = 2, the value of this id is 3.
- id = 3, the value of this id is 2.
- id = 4, the value of this id is 5 + 1 = 6.

Example 2:

Input: nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]
Output: [[1,3],[2,4],[3,6],[4,3],[5,5]]
Explanation: There are no common ids, so we just include each id with its value in the resulting list.

Constraints:


Solution

class Solution {
public:
    vector<vector<int>> mergeArrays(vector<vector<int>>& nums1, vector<vector<int>>& nums2) {
        map<int,int>mp;
        for(const auto& v: nums1){
            mp[v[0]] = v[1];
        }
        for(const auto& v:nums2){
            if(mp.find(v[0])==mp.end()){
                mp[v[0]] = v[1];
            }
            else{
                mp[v[0]] += v[1];
            }
        }
        vector<vector<int>>ans;
        for(auto i:mp){
            ans.push_back({i.first,i.second});
        }
        return ans;
    }
};

Complexity Analysis

| Algorithm        | Time Complexity     | Space Complexity |
| ---------------- | ------------------- | ---------------- |
| Hash Map Merging | O(max(nlogn,mlogm)) | O(n + m)         |

Explanation

Intial Thoughts

The problem involves merging two sorted 2D arrays based on their ids. The first step is to think about how to efficiently combine the information from both arrays, considering that each id appears only once in each array but may appear in either or both arrays. We need to consider a data structure that can handle unique ids and allow for the easy combination of values for matching ids. Using an analogy like a library catalog system can help, where each id is like a book title and the value is like the number of copies available. The goal is to merge the catalogs from two libraries. Another key point is to consider whether to iterate through both arrays simultaneously or process them separately. Additionally, considering the constraints on the input sizes and the range of ids and values can help in determining the feasibility of different approaches.

Intuitive Analysis

One intuitive way to approach this problem is to think of the process as collecting all the information from both arrays into a single, unified view, allowing for easy lookup and combination of matching ids. This can be likened to creating a ‘master catalog’ where each id and its total value are recorded. It’s intuitive to start with one array and then add information from the second array, updating the total value for each id as we go. A key realization is that since the arrays are sorted and contain unique ids, we can efficiently build our master catalog by iterating through both arrays and directly updating the catalog when we encounter a new or existing id. It’s also important to recognize the role of a data structure like a map in this process, as it naturally fits the need to look up, add, and update ids and their values efficiently.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:

Footnote

This question is rated as Easy difficulty.

Hints

Use a dictionary/hash map to keep track of the indices and their sum values.


Similar Questions:

Title URL Difficulty
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists Easy
Meeting Scheduler https://leetcode.com/problems/meeting-scheduler Medium
Merge Similar Items https://leetcode.com/problems/merge-similar-items Easy