Description

Given two 2D integer arrays items1 and items2 where items[i] = [valuei, weighti], merge them by value and return the result sorted by value. Sum weights for same values.

Examples

Input:items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
Output:[[1,6],[3,9],[4,5]]
Explanation:

Sum weights for same values.

Input:items1 = [[1]], items2 = [[1]]
Output:[1]
Explanation:

Both arrays have one item with the same value, so their weights are summed.

Input:items1 = [[2,3],[5,2],[7,1]], items2 = [[6,4],[2,7],[8,3],[5,1]]
Output:[[2,10],[5,3],[6,4],[7,1],[8,3]]
Explanation:

Items with value 2 appear in both lists (weights 3+7=10) and items with value 5 appear in both lists (weights 2+1=3). Items with values 6, 7, and 8 appear only in one list each, so their weights remain unchanged. Result is sorted by value.

Constraints

  • 1 ≤ items.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!