Merge Similar Items

You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties:

  • items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the i^th item.
  • The value of each item in items is unique.

Return a 2D integer array ret where ret[i] = [valuei, weighti], with weighti being the sum of weights of all items with value valuei.

Note: ret should be returned in ascending order by value.

Example 1
Inputitems1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
Output[[1,6],[3,9],[4,5]]
The items with values 1, 3, and 4 have total weights 6, 9, and 5 respectively, so the result is returned in ascending order by value.
Example 2
Inputitems1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
Output[[1,4],[2,4],[3,4]]
Each value 1, 2, and 3 has combined weight 4 across the two arrays, so the result is returned in ascending order by value.

Constraints

  • 1 <= items1.length, items2.length <= 1000
  • items1[i].length == items2[i].length == 2
  • 1 <= valuei, weighti <= 1000
  • Each valuei in items1 is unique.
  • Each valuei in items2 is unique.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate