Back to course home
0% completed
Vote For New Content
Merge Similar Items (easy)
Problem Statement
Given two 2D arrays of item-value pairs, named items1
and items2
.
- item[i] = [value<sub>i</sub>, weight<sub>i</sub>], where each value<sub>i</sub> in these arrays is unique within its own array and is paired with a weight<sub>i</sub>.
Combine these arrays such that if a value appears in both, its weights are summed up. The final merged array should be sorted based on the value<sub>i</sub>.
Examples
-
Example 1:
- Input:
items1 = [[1,2],[4,3]]
,items2 = [[2,1],[4,3],[3,4]]
- Expected Output:
[[1,2],[2,1],[3,4],[4,6]]
- Justification: Item 1 has value 2 in
items1
and doesn't exist initems2
, item 2 has value 1 initems2
, item 4 is summed up.
- Input:
-
Example 2:
- Input:
items1 = [[5,5]]
,items2 = [[5,10]]
- Expected Output:
[[5,15]]
- Justification: Item 5 exists in both arrays, so their values are summed.
- Input:
-
Example 3:
- Input:
items1 = [[1,1],[2,2]]
,items2 = [[3,3]]
- Expected Output:
[[1,1],[2,2],[3,3]]
- Justification: All items are unique across
items1
anditems2
, so they remain unchanged.
- Input:
Constraints:
1 <= items1.length, items2.length <= 1000
items1[i].length == items2[i].length == 2
- 1 <= value<sub>i</sub>, weight<sub>i</sub> <= 1000
- Each value<sub>i</sub> in items1 is unique.
- Each value<sub>i</sub> in items2 is unique.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page
Problem Statement
Examples
Try it yourself