2570. Merge Two 2D Arrays by Summing Values LeetCode Solution

In this guide, you will get 2570. Merge Two 2D Arrays by Summing Values LeetCode Solution with the best time and space complexity. The solution to Merge Two D Arrays by Summing Values problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Merge Two D Arrays by Summing Values solution in C++
  4. Merge Two D Arrays by Summing Values solution in Java
  5. Merge Two D Arrays by Summing Values solution in Python
  6. Additional Resources
2570. Merge Two 2D Arrays by Summing Values LeetCode Solution image

Problem Statement of Merge Two D Arrays by Summing Values

You are given two 2D integer arrays nums1 and nums2.

nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.

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:

Only ids that appear in at least one of the two arrays should be included in the resulting array.
Each id should be included only once and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be 0.

See also  761. Special Binary String LeetCode Solution

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:

1 <= nums1.length, nums2.length <= 200
nums1[i].length == nums2[j].length == 2
1 <= idi, vali <= 1000
Both arrays contain unique ids.
Both arrays are in strictly ascending order by id.

Complexity Analysis

  • Time Complexity: O(|\texttt{nums1}| + |\texttt{nums2}|)
  • Space Complexity: O(1000) = O(1)

2570. Merge Two 2D Arrays by Summing Values LeetCode Solution in C++

class Solution {
 public:
  vector<vector<int>> mergeArrays(vector<vector<int>>& nums1,
                                  vector<vector<int>>& nums2) {
    constexpr int kMax = 1000;
    vector<vector<int>> ans;
    vector<int> count(kMax + 1);

    addCount(nums1, count);
    addCount(nums2, count);

    for (int i = 1; i <= kMax; ++i)
      if (count[i] > 0)
        ans.push_back({i, count[i]});

    return ans;
  }

 private:
  void addCount(const vector<vector<int>>& nums, vector<int>& count) {
    for (const vector<int>& idAndVal : nums) {
      const int id = idAndVal[0];
      const int val = idAndVal[1];
      count[id] += val;
    }
  }
};
/* code provided by PROGIEZ */

2570. Merge Two 2D Arrays by Summing Values LeetCode Solution in Java

class Solution {
  public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
    final int kMax = 1000;
    List<int[]> ans = new ArrayList<>();
    int[] count = new int[kMax + 1];

    addCount(nums1, count);
    addCount(nums2, count);

    for (int i = 1; i <= kMax; ++i)
      if (count[i] > 0)
        ans.add(new int[] {i, count[i]});

    return ans.stream().toArray(int[][] ::new);
  }

  private void addCount(int[][] nums, int[] count) {
    for (int[] idAndVal : nums) {
      final int id = idAndVal[0];
      final int val = idAndVal[1];
      count[id] += val;
    }
  }
}
// code provided by PROGIEZ

2570. Merge Two 2D Arrays by Summing Values LeetCode Solution in Python

class Solution:
  def mergeArrays(self, nums1: list[list[int]],
                  nums2: list[list[int]]) -> list[list[int]]:
    count = [0] * (1001)
    self._addCount(nums1, count)
    self._addCount(nums2, count)
    return [[i, c] for i, c in enumerate(count) if c > 0]

  def _addCount(self, nums: list[list[int]], count: list[int]) -> None:
    for id_, val in nums:
      count[id_] += val
# code by PROGIEZ

Additional Resources

See also  1937. Maximum Number of Points with Cost LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.