diff --git a/src/main/java/leetcode/editor/cn/MergeSimilarItems.java b/src/main/java/leetcode/editor/cn/MergeSimilarItems.java new file mode 100644 index 0000000..a6f3a94 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/MergeSimilarItems.java @@ -0,0 +1,110 @@ +//

给你两个二维整数数组 items1 和 items2 ,表示两个物品集合。每个数组 items 有以下特质:

+// +// +// +//

请你返回一个二维数组 ret,其中 ret[i] = [valuei, weighti], weighti 是所有价值为 valuei 物品的 重量之和 。

+// +//

注意:ret 应该按价值 升序 排序后返回。

+// +//

 

+// +//

示例 1:

+// +//
+//输入:items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
+//输出:[[1,6],[3,9],[4,5]]
+//解释:
+//value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 5 ,总重量为 1 + 5 = 6 。
+//value = 3 的物品再 items1 中 weight = 8 ,在 items2 中 weight = 1 ,总重量为 8 + 1 = 9 。
+//value = 4 的物品在 items1 中 weight = 5 ,总重量为 5 。
+//所以,我们返回 [[1,6],[3,9],[4,5]] 。
+//
+// +//

示例 2:

+// +//
+//输入:items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
+//输出:[[1,4],[2,4],[3,4]]
+//解释:
+//value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 3 ,总重量为 1 + 3 = 4 。
+//value = 2 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 1 ,总重量为 3 + 1 = 4 。
+//value = 3 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
+//所以,我们返回 [[1,4],[2,4],[3,4]] 。
+// +//

示例 3:

+// +//
+//输入:items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
+//输出:[[1,7],[2,4],[7,1]]
+//解释:
+//value = 1 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 4 ,总重量为 3 + 4 = 7 。
+//value = 2 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
+//value = 7 的物品在 items2 中 weight = 1 ,总重量为 1 。
+//所以,我们返回 [[1,7],[2,4],[7,1]] 。
+//
+// +//

 

+// +//

提示:

+// +// +// +//
Related Topics
  • 数组
  • 哈希表
  • 有序集合
  • 排序

  • 👍 48
  • 👎 0
  • +package leetcode.editor.cn; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +// 2363:合并相似的物品 +public class MergeSimilarItems { + public static void main(String[] args) { + Solution solution = new MergeSimilarItems().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public List> mergeSimilarItems(int[][] items1, int[][] items2) { + List> list = new ArrayList<>(); + int xLength = items1.length + items2.length; + int[][] arr = new int[xLength][2]; + for (int i = 0; i < items1.length; i++) { + arr[i] = items1[i]; + } + int x1 = items1.length; + for (int i = 0; i < items2.length; i++) { + arr[i + x1] = items2[i]; + } + Arrays.sort(arr, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return o1[0] - o2[0]; + } + }); + int weight = arr[0][1]; + for (int i = 1; i < xLength; i++) { + if (arr[i - 1][0] == arr[i][0]) { + weight += arr[i][1]; + } else { + list.add(Arrays.asList(arr[i - 1][0], weight)); + weight = arr[i][1]; + } + } + list.add(Arrays.asList(arr[xLength - 1][0], weight)); + return list; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/MergeSimilarItems.md b/src/main/java/leetcode/editor/cn/doc/content/MergeSimilarItems.md new file mode 100644 index 0000000..38cb1ad --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/MergeSimilarItems.md @@ -0,0 +1,61 @@ +

    给你两个二维整数数组 items1 和 items2 ,表示两个物品集合。每个数组 items 有以下特质:

    + +
      +
    • items[i] = [valuei, weighti] 其中 valuei 表示第 i 件物品的 价值 ,weighti 表示第 i 件物品的 重量 。
    • +
    • items 中每件物品的价值都是 唯一的 。
    • +
    + +

    请你返回一个二维数组 ret,其中 ret[i] = [valuei, weighti], weighti 是所有价值为 valuei 物品的 重量之和 。

    + +

    注意:ret 应该按价值 升序 排序后返回。

    + +

     

    + +

    示例 1:

    + +
    +输入:items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
    +输出:[[1,6],[3,9],[4,5]]
    +解释:
    +value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 5 ,总重量为 1 + 5 = 6 。
    +value = 3 的物品再 items1 中 weight = 8 ,在 items2 中 weight = 1 ,总重量为 8 + 1 = 9 。
    +value = 4 的物品在 items1 中 weight = 5 ,总重量为 5 。
    +所以,我们返回 [[1,6],[3,9],[4,5]] 。
    +
    + +

    示例 2:

    + +
    +输入:items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
    +输出:[[1,4],[2,4],[3,4]]
    +解释:
    +value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 3 ,总重量为 1 + 3 = 4 。
    +value = 2 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 1 ,总重量为 3 + 1 = 4 。
    +value = 3 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
    +所以,我们返回 [[1,4],[2,4],[3,4]] 。
    + +

    示例 3:

    + +
    +输入:items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
    +输出:[[1,7],[2,4],[7,1]]
    +解释:
    +value = 1 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 4 ,总重量为 3 + 4 = 7 。
    +value = 2 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
    +value = 7 的物品在 items2 中 weight = 1 ,总重量为 1 。
    +所以,我们返回 [[1,7],[2,4],[7,1]] 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= items1.length, items2.length <= 1000
    • +
    • items1[i].length == items2[i].length == 2
    • +
    • 1 <= valuei, weighti <= 1000
    • +
    • items1 中每个 valuei 都是 唯一的 。
    • +
    • items2 中每个 valuei 都是 唯一的 。
    • +
    + +
    Related Topics
  • 数组
  • 哈希表
  • 有序集合
  • 排序

  • 👍 48
  • 👎 0
  • \ No newline at end of file