Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35a1739

Browse files
solves #2570: Merge Two 2D Arrays by Summing Values in java
1 parent 16da767 commit 35a1739

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@
802802
| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile) | [![Java](assets/java.png)](src/TakeGiftsFromTheRichestPile.java) | |
803803
| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value) | [![Java](assets/java.png)](src/FindTheArrayConcatenationValue.java) | |
804804
| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit) | [![Java](assets/java.png)](src/MaximumDifferenceByRemappingADigit.java) | |
805-
| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values) | | |
805+
| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values) | [![Java](assets/java.png)](src/MergeTwo2DArraysBySummingValues.java) | |
806806
| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences) | | |
807807
| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum) | | |
808808
| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow) | | |
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values
2+
// T: O(N + M)
3+
// S: O(N + M)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class MergeTwo2DArraysBySummingValues {
9+
public int[][] mergeArrays(int[][] array1, int[][] array2) {
10+
final List<int[]> result = new ArrayList<>();
11+
int i = 0, j = 0;
12+
while (i < array1.length && j < array2.length) {
13+
if (array1[i][0] == array2[j][0]) {
14+
result.add(new int[] {array1[i][0], array1[i][1] + array2[j][1]});
15+
i++;
16+
j++;
17+
} else if (array1[i][0] < array2[j][0]) {
18+
result.add(array1[i++]);
19+
} else {
20+
result.add(array2[j++]);
21+
}
22+
}
23+
24+
while (i < array1.length) {
25+
result.add(array1[i++]);
26+
}
27+
28+
while (j < array2.length) {
29+
result.add(array2[j++]);
30+
}
31+
32+
return toArray(result);
33+
}
34+
35+
private int[][] toArray(List<int[]> list) {
36+
final int[][] array = new int[list.size()][2];
37+
int k = 0;
38+
for (int[] slice : list) {
39+
array[k++] = slice;
40+
}
41+
return array;
42+
}
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /