forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_2363.java
25 lines (23 loc) · 841 Bytes
/
_2363.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
public class _2363 {
public static class Solution1 {
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
List<List<Integer>> result = new ArrayList<>();
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int[] item : items1) {
map.put(item[0], map.getOrDefault(item[0], 0) + item[1]);
}
for (int[] item : items2) {
map.put(item[0], map.getOrDefault(item[0], 0) + item[1]);
}
for (int key : map.keySet()) {
result.add(new ArrayList<>(Arrays.asList(key, map.get(key))));
}
return result;
}
}
}