Skip to content

Practice Merge Similar Items

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2363
LeetCode Problem

Merge Similar Items

Problem Statement

Each entry in items1 or items2 is [value, weight], and values are unique within each individual input. Combine both inputs by summing weights for equal values. Return [value, totalWeight] entries sorted by increasing value.

Source: Merge Similar Items on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
Output
[[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]]

# Example 3

Input
items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
Output
[[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.

Related LeetCode Problems

Practice more problems similar to Merge Similar Items.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.