Skip to content

Add solution and test-cases for problem 2918 #1200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading