2025-08-19 10:40:19 +0000 UTC

Merge Similar Items

Code

class Solution:
    def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
        freqs = [0] * 1001
        for val, weight in itertools.chain(items1, items2):
            freqs[val] += weight
        res = [(num, weight) for num, weight in enumerate(freqs) if weight != 0]
        return res