diff --git a/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/README.md b/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/README.md new file mode 100644 index 000000000..7e1af32e6 --- /dev/null +++ b/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/README.md @@ -0,0 +1,44 @@ +# [2570. Merge Two 2D Arrays by Summing Values][title] + +## Description + +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. + +**Example 1:** + +``` +Input: nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]] +Output: [[1,6],[2,3],[3,2],[4,6]] +Explanation: The resulting array contains the following: +- id = 1, the value of this id is 2 + 4 = 6. +- id = 2, the value of this id is 3. +- id = 3, the value of this id is 2. +- id = 4, the value of this id is 5 + 1 = 6. +``` + +**Example 2:** + +``` +Input: nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]] +Output: [[1,3],[2,4],[3,6],[4,3],[5,5]] +Explanation: There are no common ids, so we just include each id with its value in the resulting list. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution.go b/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution.go index d115ccf5e..709f6aad9 100755 --- a/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution.go +++ b/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution.go @@ -1,5 +1,26 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums1 [][]int, nums2 [][]int) [][]int { + if len(nums1) == 0 { + return nums2 + } + if len(nums2) == 0 { + return nums1 + } + res := [][]int{} + if nums1[0][0] == nums2[0][0] { + res = append(res, []int{nums1[0][0], nums1[0][1] + nums2[0][1]}) + nums1 = nums1[1:] + nums2 = nums2[1:] + } else { + if nums1[0][0] < nums2[0][0] { + res = append(res, nums1[0]) + nums1 = nums1[1:] + } else { + res = append(res, nums2[0]) + nums2 = nums2[1:] + } + } + res = append(res, Solution(nums1, nums2)...) + return res } diff --git a/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution_test.go b/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution_test.go index 14ff50eb4..b1df1dff6 100755 --- a/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution_test.go +++ b/leetcode/2501-2600/2570.Merge-Two-2D-Arrays-by-Summing-Values/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n1, n2 [][]int + expect [][]int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{1, 2}, {2, 3}, {4, 5}}, [][]int{{1, 4}, {3, 2}, {4, 1}}, [][]int{{1, 6}, {2, 3}, {3, 2}, {4, 6}}}, + {"TestCase2", [][]int{{2, 4}, {3, 6}, {5, 5}}, [][]int{{1, 3}, {4, 3}}, [][]int{{1, 3}, {2, 4}, {3, 6}, {4, 3}, {5, 5}}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n1, c.n2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n1, c.n2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }