#2570
Merge Two 2D Arrays by Summing Values
newbie · 155 · lc easy +14 · verified · 81.7% accepted · 833 likes · top 93%
Description
Given two sorted 2D arrays nums1 and nums2 where each entry is [id, value] and ids within each array are unique, merge them into one sorted array. If an id appears in both, sum their values; if it appears in only one, use that value. Return the result sorted by id in ascending order.
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.
Code
1
2
3