2363. Merge Similar Items LeetCode Solution
In this guide, you will get 2363. Merge Similar Items LeetCode Solution with the best time and space complexity. The solution to Merge Similar Items problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Merge Similar Items solution in C++
- Merge Similar Items solution in Java
- Merge Similar Items solution in Python
- Additional Resources
Problem Statement of 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 ith 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:
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
Each valuei in items1 is unique.
Each valuei in items2 is unique.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1000) = O(1)
2363. Merge Similar Items LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> mergeSimilarItems(vector<vector<int>>& items1,
vector<vector<int>>& items2) {
constexpr int kMax = 1000;
vector<vector<int>> ans;
vector<int> count(kMax + 1);
for (const vector<int>& item : items1)
count[item[0]] += item[1];
for (const vector<int>& item : items2)
count[item[0]] += item[1];
for (int i = 1; i <= kMax; ++i)
if (count[i])
ans.push_back({i, count[i]});
return ans;
}
};
/* code provided by PROGIEZ */
2363. Merge Similar Items LeetCode Solution in Java
class Solution {
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
final int kMax = 1000;
List<List<Integer>> ans = new ArrayList<>();
int[] count = new int[kMax + 1];
for (int[] item : items1)
count[item[0]] += item[1];
for (int[] item : items2)
count[item[0]] += item[1];
for (int i = 1; i <= kMax; ++i)
if (count[i] > 0)
ans.add(Arrays.asList(i, count[i]));
return ans;
}
}
// code provided by PROGIEZ
2363. Merge Similar Items LeetCode Solution in Python
class Solution:
def mergeSimilarItems(self, items1: list[list[int]],
items2: list[list[int]]) -> list[list[int]]:
return sorted(
(Counter(dict(items1)) + collections.Counter(dict(items2))).items())
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.