-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwo2DArraysBySummingValues.java
More file actions
56 lines (49 loc) · 1.96 KB
/
MergeTwo2DArraysBySummingValues.java
File metadata and controls
56 lines (49 loc) · 1.96 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.ArrayList;
import java.util.List;
/*
* You are given two 2D integer arrays nums1 and nums2.
nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
Each array contains unique ids and is sorted in ascending order by id.
Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:
Only ids that appear in at least one of the two arrays should be included in the resulting array.
Each id should be included only once and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be 0.
Return the resulting array. The returned array must be sorted in ascending order by id.
*/
public class MergeTwo2DArraysBySummingValues {
class Solution {
public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
int n = nums1.length;
int m = nums2.length;
int i = 0;
int j = 0;
List<int[]> list = new ArrayList<>();
while (i < n && j < m) {
if (nums1[i][0] == nums2[j][0]) {
list.add(new int[]{nums1[i][0], nums1[i][1] + nums2[j][1]});
i++;
j++;
} else if (nums1[i][0] < nums2[j][0]) {
list.add(new int[]{nums1[i][0], nums1[i][1]});
i++;
} else {
list.add(new int[]{nums2[j][0], nums2[j][1]});
j++;
}
}
while (i < n) {
list.add(new int[]{nums1[i][0], nums1[i][1]});
i++;
}
while (j < m) {
list.add(new int[]{nums2[j][0], nums2[j][1]});
j++;
}
int[][] res = new int[list.size()][2];
for (int k = 0; k < list.size(); k++) {
res[k] = list.get(k);
}
return res;
}
}
}