diff --git a/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/README.md b/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/README.md index 824091e70..a74904dfc 100755 --- a/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/README.md +++ b/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/README.md @@ -1,28 +1,30 @@ # [2918.Minimum Equal Sum of Two Arrays After Replacing Zeros][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two arrays `nums1` and `nums2` consisting of positive integers. + +You have to replace **all** the 0's in both arrays with **strictly** positive integers such that the sum of elements of both arrays becomes **equal**. + +Return the **minimum** equal sum you can obtain, or `-1` if it is impossible. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0] +Output: 12 +Explanation: We can replace 0's in the following way: +- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4]. +- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1]. +Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Minimum Equal Sum of Two Arrays After Replacing Zeros -```go ``` - +Input: nums1 = [2,0,2,0], nums2 = [1,4] +Output: -1 +Explanation: It is impossible to make the sum of both arrays equal. +``` ## 结语 diff --git a/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution.go b/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution.go index d115ccf5e..1416ff98d 100644 --- a/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution.go +++ b/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution.go @@ -1,5 +1,40 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums1 []int, nums2 []int) int64 { + a, b := 0, 0 + a0, b0 := 0, 0 + for _, n := range nums1 { + a += n + if n == 0 { + a0++ + } + } + + for _, n := range nums2 { + b += n + if n == 0 { + b0++ + } + } + if a0 == 0 && b0 == 0 { + if a != b { + return -1 + } + return int64(a) + } + + if a0 != 0 && b0 != 0 { + return max(int64(a+a0), int64(b+b0)) + } + if a0 == 0 { + // b0 != 0 + if b+b0 <= a { + return int64(a) + } + return -1 + } + if a+a0 <= b { + return int64(b) + } + return -1 } diff --git a/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution_test.go b/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution_test.go index 14ff50eb4..a38ead756 100644 --- a/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution_test.go +++ b/leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums1, nums2 []int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 2, 0, 1, 0}, []int{6, 5, 0}, 12}, + {"TestCase2", []int{2, 0, 2, 0}, []int{1, 4}, -1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums1, c.nums2) 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.nums1, c.nums2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }