Skip to content

Commit 099bb57

Browse files
committed
Add solution and test-cases for problem 2570
1 parent f0a9eb1 commit 099bb57

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [2570. Merge Two 2D Arrays by Summing Values][title]
2+
3+
## Description
4+
5+
You are given two **2D** integer arrays `nums1` and `nums2`.
6+
7+
- `nums1[i] = [idi, vali]` indicate that the number with the id `idi` has a value equal to `vali`.
8+
- `nums2[i] = [idi, vali]` indicate that the number with the id `idi` has a value equal to `vali`.
9+
10+
Each array contains **unique** ids and is sorted in **ascending** order by id.
11+
12+
Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:
13+
14+
- Only ids that appear in at least one of the two arrays should be included in the resulting array.
15+
- 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`.
16+
17+
Return the resulting array. The returned array must be sorted in ascending order by id.
18+
19+
**Example 1:**
20+
21+
```
22+
Input: nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]
23+
Output: [[1,6],[2,3],[3,2],[4,6]]
24+
Explanation: The resulting array contains the following:
25+
- id = 1, the value of this id is 2 + 4 = 6.
26+
- id = 2, the value of this id is 3.
27+
- id = 3, the value of this id is 2.
28+
- id = 4, the value of this id is 5 + 1 = 6.
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]
35+
Output: [[1,3],[2,4],[3,6],[4,3],[5,5]]
36+
Explanation: There are no common ids, so we just include each id with its value in the resulting list.
37+
```
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
42+
43+
[title]: https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values
44+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums1 [][]int, nums2 [][]int) [][]int {
4+
if len(nums1) == 0 {
5+
return nums2
6+
}
7+
if len(nums2) == 0 {
8+
return nums1
9+
}
10+
res := [][]int{}
11+
if nums1[0][0] == nums2[0][0] {
12+
res = append(res, []int{nums1[0][0], nums1[0][1] + nums2[0][1]})
13+
nums1 = nums1[1:]
14+
nums2 = nums2[1:]
15+
} else {
16+
if nums1[0][0] < nums2[0][0] {
17+
res = append(res, nums1[0])
18+
nums1 = nums1[1:]
19+
} else {
20+
res = append(res, nums2[0])
21+
nums2 = nums2[1:]
22+
}
23+
}
24+
res = append(res, Solution(nums1, nums2)...)
25+
return res
526
}

leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n1, n2 [][]int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 2}, {2, 3}, {4, 5}}, [][]int{{1, 4}, {3, 2}, {4, 1}}, [][]int{{1, 6}, {2, 3}, {3, 2}, {4, 6}}},
17+
{"TestCase2", [][]int{{2, 4}, {3, 6}, {5, 5}}, [][]int{{1, 3}, {4, 3}}, [][]int{{1, 3}, {2, 4}, {3, 6}, {4, 3}, {5, 5}}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.n1, c.n2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.n1, c.n2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)