LeetCode solutions

2707. Merge Two 2D Arrays by Summing Values

My accepted Python solution to LeetCode problem 2707, Merge Two 2D Arrays by Summing Values, running in 3ms.

  • Difficulty: Easy
  • Python
  • Runtime 3ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.5MB, accepted 2026-01-01.

python
class Solution:
    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
        from collections import defaultdict
        
        merged = defaultdict(int)
        
        for id, val in nums1:
            merged[id] += val
        
        for id, val in nums2:
            merged[id] += val
        
        return sorted([[id, val] for id, val in merged.items()])

Source