Class Solution

java.lang.Object
g2301_2400.s2363_merge_similar_items.Solution

public class Solution extends Object
2363 - Merge Similar Items.<p>Easy</p> <p>You are given two 2D integer arrays, <code>items1</code> and <code>items2</code>, representing two sets of items. Each array <code>items</code> has the following properties:</p> <ul> <li><code>items[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code> where <code>value<sub>i</sub></code> represents the <strong>value</strong> and <code>weight<sub>i</sub></code> represents the <strong>weight</strong> of the <code>i<sup>th</sup></code> item.</li> <li>The value of each item in <code>items</code> is <strong>unique</strong>.</li> </ul> <p>Return <em>a 2D integer array</em> <code>ret</code> <em>where</em> <code>ret[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code><em>,</em> <em>with</em> <code>weight<sub>i</sub></code> <em>being the <strong>sum of weights</strong> of all items with value</em> <code>value<sub>i</sub></code>.</p> <p><strong>Note:</strong> <code>ret</code> should be returned in <strong>ascending</strong> order by value.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]</p> <p><strong>Output:</strong> [[1,6],[3,9],[4,5]]</p> <p><strong>Explanation:</strong> The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6.</p> <p>The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9.</p> <p>The item with value = 4 occurs in items1 with weight = 5, total weight = 5.</p> <p>Therefore, we return [[1,6],[3,9],[4,5]].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]</p> <p><strong>Output:</strong> [[1,4],[2,4],[3,4]]</p> <p><strong>Explanation:</strong> The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4.</p> <p>The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4.</p> <p>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]].</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]</p> <p><strong>Output:</strong> [[1,7],[2,4],[7,1]]</p> <p><strong>Explanation:</strong></p> <p>The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7.</p> <p>The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.</p> <p>The item with value = 7 occurs in items2 with weight = 1, total weight = 1.</p> <p>Therefore, we return [[1,7],[2,4],[7,1]].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= items1.length, items2.length <= 1000</code></li> <li><code>items1[i].length == items2[i].length == 2</code></li> <li><code>1 <= value<sub>i</sub>, weight<sub>i</sub> <= 1000</code></li> <li>Each <code>value<sub>i</sub></code> in <code>items1</code> is <strong>unique</strong>.</li> <li>Each <code>value<sub>i</sub></code> in <code>items2</code> is <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • mergeSimilarItems

      public List<List<Integer>> mergeSimilarItems(int[][] arr1, int[][] arr2)