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]wherevalueirepresents the value andweightirepresents the weight of theithitem.- The value of each item in
itemsis 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.
Merge Similar Items LeetCode Contest
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 <= 1000items1[i].length == items2[i].length == 21 <= valuei, weighti <= 1000- Each
valueiinitems1is unique. - Each
valueiinitems2is unique.
Solution
class Solution {
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
Map<Integer, Integer> merge = new TreeMap<>();
for (int[] iterm : items1) {
merge.merge(iterm[0], iterm[1], Integer::sum);
}
for (int[] iterm : items2) {
merge.merge(iterm[0], iterm[1], Integer::sum);
}
List<List<Integer>> result = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : merge.entrySet()) {
result.add(List.of(entry.getKey(), entry.getValue()));
}
return result;
}
}Happy Learning – If you require any further information, feel free to contact me.
![[Solved] Merge Similar Items LeetCode Contest [Solved] Merge Similar Items LeetCode Contest](https://realcoder.techss24.com/wp-content/uploads/2022/08/Solved-Merge-Similar-Items-LeetCode-Contest.png)
![[Solved] Count Unreachable Pairs of Nodes in an Undirected Graph LeetCode Contest Problem](https://realcoder.techss24.com/wp-content/uploads/2022/06/Solved-Count-Unreachable-Pairs-of-Nodes-in-an-Undirected-Graph-LeetCode-Contest-Problem-300x200.png)
![[Solved] Shifting Letters II LeetCode Contest](https://realcoder.techss24.com/wp-content/uploads/2022/08/Solved-Shifting-Letters-II-LeetCode-Contest-300x200.png)
![[Solved] First Letter to Appear Twice LeetCode Contest Problem](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-First-Letter-to-Appear-Twice-LeetCode-Contest-Problem-300x200.png)