-
Notifications
You must be signed in to change notification settings - Fork 16
/
MergeTwo2DArraysBySummingValues.java
43 lines (37 loc) · 1.17 KB
/
MergeTwo2DArraysBySummingValues.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values
// T: O(N + M)
// S: O(N + M)
import java.util.ArrayList;
import java.util.List;
public class MergeTwo2DArraysBySummingValues {
public int[][] mergeArrays(int[][] array1, int[][] array2) {
final List<int[]> result = new ArrayList<>();
int i = 0, j = 0;
while (i < array1.length && j < array2.length) {
if (array1[i][0] == array2[j][0]) {
result.add(new int[] {array1[i][0], array1[i][1] + array2[j][1]});
i++;
j++;
} else if (array1[i][0] < array2[j][0]) {
result.add(array1[i++]);
} else {
result.add(array2[j++]);
}
}
while (i < array1.length) {
result.add(array1[i++]);
}
while (j < array2.length) {
result.add(array2[j++]);
}
return toArray(result);
}
private int[][] toArray(List<int[]> list) {
final int[][] array = new int[list.size()][2];
int k = 0;
for (int[] slice : list) {
array[k++] = slice;
}
return array;
}
}