Skip to content

Commit 191905e

Browse files
committed
Add solution and test-cases for problem 2918
1 parent a01e89c commit 191905e

File tree

3 files changed

+63
-27
lines changed

3 files changed

+63
-27
lines changed

leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [2918.Minimum Equal Sum of Two Arrays After Replacing Zeros][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two arrays `nums1` and `nums2` consisting of positive integers.
5+
6+
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**.
7+
8+
Return the **minimum** equal sum you can obtain, or `-1` if it is impossible.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0]
14+
Output: 12
15+
Explanation: We can replace 0's in the following way:
16+
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
17+
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
18+
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Equal Sum of Two Arrays After Replacing Zeros
23-
```go
2423
```
25-
24+
Input: nums1 = [2,0,2,0], nums2 = [1,4]
25+
Output: -1
26+
Explanation: It is impossible to make the sum of both arrays equal.
27+
```
2628

2729
## 结语
2830

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums1 []int, nums2 []int) int64 {
4+
a, b := 0, 0
5+
a0, b0 := 0, 0
6+
for _, n := range nums1 {
7+
a += n
8+
if n == 0 {
9+
a0++
10+
}
11+
}
12+
13+
for _, n := range nums2 {
14+
b += n
15+
if n == 0 {
16+
b0++
17+
}
18+
}
19+
if a0 == 0 && b0 == 0 {
20+
if a != b {
21+
return -1
22+
}
23+
return int64(a)
24+
}
25+
26+
if a0 != 0 && b0 != 0 {
27+
return max(int64(a+a0), int64(b+b0))
28+
}
29+
if a0 == 0 {
30+
// b0 != 0
31+
if b+b0 <= a {
32+
return int64(a)
33+
}
34+
return -1
35+
}
36+
if a+a0 <= b {
37+
return int64(b)
38+
}
39+
return -1
540
}

leetcode/2901-3000/2918.Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums1, nums2 []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 2, 0, 1, 0}, []int{6, 5, 0}, 12},
17+
{"TestCase2", []int{2, 0, 2, 0}, []int{1, 4}, -1},
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.nums1, c.nums2)
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.nums1, c.nums2)
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)