You are given two arrays of integers nums1
and nums2
, possibly of different lengths. The values in the arrays are between 1
and 6
, inclusive.
In one operation, you can change any integer's value in any of the arrays to any value between 1
and 6
, inclusive.
Return the minimum number of operations required to make the sum of values in nums1
equal to the sum of values in nums2
. Return -1
if it is not possible to make the sum of the two arrays equal.
Example 1:
Input: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2] Output: 3 Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed. - Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2]. - Change nums1[5] to 1. nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2]. - Change nums1[2] to 2. nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2].
Example 2:
Input: nums1 = [1,1,1,1,1,1,1], nums2 = [6] Output: -1 Explanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.
Example 3:
Input: nums1 = [6,6], nums2 = [1] Output: 3 Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed. - Change nums1[0] to 2. nums1 = [2,6], nums2 = [1]. - Change nums1[1] to 2. nums1 = [2,2], nums2 = [1]. - Change nums2[0] to 4. nums1 = [2,2], nums2 = [4].
Constraints:
1 <= nums1.length, nums2.length <= 105
1 <= nums1[i], nums2[i] <= 6
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
s1, s2 = sum(nums1), sum(nums2)
if s1 == s2:
return 0
if s1 > s2:
return self.minOperations(nums2, nums1)
freq = [0] * 6
for x in nums1:
freq[6 - x] += 1
for x in nums2:
freq[x - 1] += 1
diff = s2 - s1
ans, i = 0, 5
while i > 0 and diff > 0:
while freq[i] and diff > 0:
diff -= i
freq[i] -= 1
ans += 1
i -= 1
return -1 if diff > 0 else ans
class Solution {
public int minOperations(int[] nums1, int[] nums2) {
int s1 = sum(nums1);
int s2 = sum(nums2);
if (s1 == s2) {
return 0;
}
if (s1 > s2) {
return minOperations(nums2, nums1);
}
int[] freq = new int[6];
for (int x : nums1) {
++freq[6 - x];
}
for (int x : nums2) {
++freq[x - 1];
}
int diff = s2 - s1;
int ans = 0;
for (int i = 5; i > 0 && diff > 0; --i) {
while (freq[i] > 0 && diff > 0) {
diff -= i;
--freq[i];
++ans;
}
}
return diff > 0 ? - 1 : ans;
}
private int sum(int[] nums) {
int s = 0;
for (int x : nums) {
s += x;
}
return s;
}
}
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
int s1 = accumulate(nums1.begin(), nums1.end(), 0);
int s2 = accumulate(nums2.begin(), nums2.end(), 0);
if (s1 == s2) return 0;
if (s1 > s2) return minOperations(nums2, nums1);
vector<int> freq(6);
for (int x : nums1) ++freq[6 - x];
for (int x : nums2) ++freq[x - 1];
int diff = s2 - s1;
int ans = 0;
for (int i = 5; i > 0 && diff > 0; --i)
{
while (freq[i] && diff > 0)
{
diff -= i;
--freq[i];
++ans;
}
}
return diff > 0 ? -1 : ans;
}
};
func minOperations(nums1 []int, nums2 []int) int {
s1, s2 := sum(nums1), sum(nums2)
if s1 == s2 {
return 0
}
if s1 > s2 {
return minOperations(nums2, nums1)
}
freq := make([]int, 6)
for _, x := range nums1 {
freq[6-x]++
}
for _, x := range nums2 {
freq[x-1]++
}
diff := s2 - s1
ans := 0
for i := 5; i > 0 && diff > 0; i-- {
for freq[i] > 0 && diff > 0 {
diff -= i
freq[i]--
ans++
}
}
if diff > 0 {
return -1
}
return ans
}
func sum(nums []int) int {
s := 0
for _, x := range nums {
s += x
}
return s
}