Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c104f49

Browse files
authoredMar 2, 2025··
2570_Merge_Two_2D_Arrays_by_Summing_Values.java
1 parent 5f6b1b5 commit c104f49

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Problem Number 2570
2+
3+
// Merge Two 2D Arrays by Summing their Values.
4+
5+
class Solution {
6+
public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
7+
final int kMax = 1000;
8+
List<int[]> ans = new ArrayList<>();
9+
int[] count = new int[kMax + 1];
10+
11+
addCount(nums1, count);
12+
addCount(nums2, count);
13+
14+
for (int i = 1; i <= kMax; ++i)
15+
if (count[i] > 0)
16+
ans.add(new int[] {i, count[i]});
17+
18+
return ans.stream().toArray(int[][] ::new);
19+
}
20+
21+
private void addCount(int[][] nums, int[] count) {
22+
for (int[] idAndVal : nums) {
23+
final int id = idAndVal[0];
24+
final int val = idAndVal[1];
25+
count[id] += val;
26+
}
27+
28+
}
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.