2570. Merge Two 2D Arrays by Summing Values

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|1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1|1,4],[3,2],[4,1]]
Output: [[1,6],[2,3],[3,2],[4,6|1,6],[2,3],[3,2],[4,6]]
Explanation: The resulting array contains the following:

Example 2:

Input: nums1 = [[2,4],[3,6],[5,5|2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3|1,3],[4,3]]
Output: [[1,3],[2,4],[3,6],[4,3],[5,5|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.

def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
	res = []
	l1 = len(nums1)
	l2 = len(nums2)
	i = j = 0
	while i < l1 and j < l2:
		if nums1[i][0] < nums2[j][0]:
			res.append(nums1[i])
			i += 1
		elif nums1[i][0] > nums2[j][0]:
			res.append(nums2[j])
			j += 1
		else:
			res.append([nums1[i][0], nums1[i][1] + nums2[j][1]])
			i += 1
			j += 1
	while i < l1:
		res.append(nums1[i])
		i += 1
	while j < l2:
		res.append(nums2[j])
		j += 1
	return res