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 ith
item.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:
Input: items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]] Output: [[1,6],[3,9],[4,5]] Explanation: The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6. The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9. The item with value = 4 occurs in items1 with weight = 5, total weight = 5. Therefore, we return [[1,6],[3,9],[4,5]].
Example 2:
Input: items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]] Output: [[1,4],[2,4],[3,4]] Explanation: The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4. The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4. The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4. Therefore, we return [[1,4],[2,4],[3,4]].
Example 3:
Input: items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]] Output: [[1,7],[2,4],[7,1]] Explanation: The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7. The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4. The item with value = 7 occurs in items2 with weight = 1, total weight = 1. Therefore, we return [[1,7],[2,4],[7,1]].
Constraints:
1 <= items1.length, items2.length <= 1000
items1[i].length == items2[i].length == 2
1 <= valuei, weighti <= 1000
valuei
in items1
is unique.valuei
in items2
is unique.The problem asks to merge two lists of items, where each item is represented by a value and a weight. Items with the same value should have their weights summed. The result should be a sorted list of unique items with their combined weights.
The most efficient approach involves using a hash table (or a similar data structure like a dictionary in Python or a map in C++/Java) to store the total weight for each unique item value. This allows for fast lookups and updates as we iterate through both input lists.
Here's a breakdown of the steps:
Initialization: Create a hash table (or array if the value range is known and small). The keys will be the item values, and the values will be their cumulative weights.
Iteration: Iterate through both items1
and items2
. For each item [value, weight]
:
value
exists as a key in the hash table.weight
to the current weight associated with that value
.value
as a key to the hash table with the weight
as its value.Result Construction: Create a list to store the result. Iterate through the hash table (or array) and add each [value, weight]
pair to the result list. Sort the list by value in ascending order.
Return: Return the sorted result list.
Time Complexity: The algorithm iterates through items1
and items2
once each, taking O(n + m) time, where n is the length of items1
and m is the length of items2
. The sorting step takes O((n+m) log(n+m)) time in the worst case, but since the number of unique items is likely much smaller than n+m, it often performs better. The overall time complexity is dominated by the iteration and sorting, resulting in O((n+m)log(n+m)). If we use an array instead of a hash table and the range of values is small, the sorting will dominate, resulting in O((n+m)log(n+m)).
Space Complexity: The hash table (or array) requires space proportional to the number of unique item values, which is at most O(n + m) in the worst case (all values are unique). The result list also takes O(n + m) space in the worst case. Therefore, the space complexity is O(n + m).
The provided code examples in the question demonstrate this approach effectively. Note that the Python solution uses the Counter
object from the collections
module which provides a convenient way to count items. The other solutions use arrays (or similar) to accomplish the same. The choice of using an array instead of a hash table in some solutions is a tradeoff between efficiency and memory usage, optimal when the range of item values is small and known in advance.