-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2918-MinimumEqualSumOfTwoArraysAfterReplacingZeros.go
More file actions
126 lines (112 loc) · 4.72 KB
/
2918-MinimumEqualSumOfTwoArraysAfterReplacingZeros.go
File metadata and controls
126 lines (112 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
// 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros
// 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: 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:
// Input: nums1 = [2,0,2,0], nums2 = [1,4]
// Output: -1
// Explanation: It is impossible to make the sum of both arrays equal.
// Constraints:
// 1 <= nums1.length, nums2.length <= 10^5
// 0 <= nums1[i], nums2[i] <= 10^6
import "fmt"
func minSum(nums1 []int, nums2 []int) int64 {
calc := func(nums []int) (int64, int64) {
zero, sum := 0, 0
for _, v := range nums {
if v == 0 { zero++ }
sum += v
}
return int64(sum + zero), int64(zero)
}
sum1, zero1 := calc(nums1)
sum2, zero2 := calc(nums2)
if sum1 < sum2 && zero1 == 0 || sum2 < sum1 && zero2 == 0 {
return -1
}
if sum1 < sum2 {
return sum2
}
return sum1
}
func minSum1(nums1 []int, nums2 []int) int64 {
calc := func(nums []int) (int64, int64) {
zero, sum := 0, 0
for _, v := range nums {
if v == 0 { zero++ }
sum += v
}
return int64(sum + zero), int64(zero)
}
sum1, zero1 := calc(nums1)
sum2, zero2 := calc(nums2)
if sum1 == sum2 { return sum1 }
if sum1 > sum2 {
if zero2 == 0 { return -1 }
return sum1
}
if zero1 == 0 { return -1 }
return sum2
}
func minSum2(nums1 []int, nums2 []int) int64 {
sum1, sum2, zeros1, zeros2 := 0, 0, 0, 0
for _, v := range nums1 {
if v == 0 { zeros1++ }
sum1 += v
}
for _, v := range nums2 {
if v == 0 { zeros2++ }
sum2 += v
}
if sum1 + zeros1 > sum2 + zeros2 { // nums1 大
if zeros2 == 0 { return -1 }
return int64(sum1 + zeros1)
} else if sum1 + zeros1 < sum2 + zeros2 { // nums2 大
if zeros1 == 0 { return -1 }
return int64(sum2 + zeros2)
} else { // sum1 + zeros1 == sum2 + zeros2
if zeros1 == 0 && zeros2 == 0 { return int64(sum1) } // 两个数组都没有 0
if zeros1 == 0 { return int64(sum2 + zeros2) }
if zeros2 == 0 { return int64(sum1 + zeros1) }
return int64(sum1 + zeros1)
}
}
func main() {
// Example 1:
// 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.
fmt.Println(minSum([]int{3,2,0,1,0}, []int{6,5,0})) // 12
// Example 2:
// Input: nums1 = [2,0,2,0], nums2 = [1,4]
// Output: -1
// Explanation: It is impossible to make the sum of both arrays equal.
fmt.Println(minSum([]int{2,0,2,0}, []int{1,4})) // -1
fmt.Println(minSum([]int{1,2,3,4,5,6,7,8,9}, []int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(minSum([]int{1,2,3,4,5,6,7,8,9}, []int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(minSum([]int{9,8,7,6,5,4,3,2,1}, []int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(minSum([]int{9,8,7,6,5,4,3,2,1}, []int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(minSum1([]int{3,2,0,1,0}, []int{6,5,0})) // 12
fmt.Println(minSum1([]int{2,0,2,0}, []int{1,4})) // -1
fmt.Println(minSum1([]int{1,2,3,4,5,6,7,8,9}, []int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(minSum1([]int{1,2,3,4,5,6,7,8,9}, []int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(minSum1([]int{9,8,7,6,5,4,3,2,1}, []int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(minSum1([]int{9,8,7,6,5,4,3,2,1}, []int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(minSum2([]int{3,2,0,1,0}, []int{6,5,0})) // 12
fmt.Println(minSum2([]int{2,0,2,0}, []int{1,4})) // -1
fmt.Println(minSum2([]int{1,2,3,4,5,6,7,8,9}, []int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(minSum2([]int{1,2,3,4,5,6,7,8,9}, []int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(minSum2([]int{9,8,7,6,5,4,3,2,1}, []int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(minSum2([]int{9,8,7,6,5,4,3,2,1}, []int{9,8,7,6,5,4,3,2,1})) // 45
}