LeetCode solutions

2447. Merge Similar Items

My accepted Python solution to LeetCode problem 2447, Merge Similar Items, running in 8ms.

  • Difficulty: Easy
  • Python
  • Runtime 8ms
  • Memory 18MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 8ms, memory 18MB, accepted 2025-12-31.

python
class Solution:
    def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
        weight_map = {}
        
        for value, weight in items1:
            weight_map[value] = weight_map.get(value, 0) + weight
        
        for value, weight in items2:
            weight_map[value] = weight_map.get(value, 0) + weight
        
        result = [[value, weight] for value, weight in weight_map.items()]
        result.sort()
        
        return result

Source