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
This problem asks to find the minimum number of operations needed to make the sum of two arrays equal. An operation involves changing any integer in either array to any value between 1 and 6.
The solution employs a greedy approach. The core idea is to prioritize larger potential changes. We can efficiently achieve this by sorting the potential changes.
Algorithm:
Calculate sums: Calculate the sums (s1
, s2
) of nums1
and nums2
.
Handle trivial cases: If the sums are already equal, return 0. If s1
> s2
, recursively call the function with nums2
, nums1
to ensure s2
>= s1
.
Calculate potential changes: Create an array (arr
) containing potential changes: 6 - nums1[i]
(increase nums1[i]
to 6), and nums2[i] - 1
(decrease nums2[i]
to 1).
Sort: Sort arr
in descending order. This ensures we address the largest potential changes first.
Greedy iteration: Iterate through arr
in descending order. For each potential change, subtract it from the difference d = s2 - s1
. If d
becomes less than or equal to 0, the number of iterations performed is the minimum number of operations required.
Handle impossible cases: If the loop completes without d
becoming less than or equal to 0, it's impossible to make the sums equal. Return -1.
Time Complexity Analysis:
arr
array: O(m + n)arr
: O((m+n) log(m+n))Therefore, the overall time complexity is dominated by sorting, resulting in O((m+n) log(m+n)).
Space Complexity Analysis:
arr
array, which has a size of m + n.Thus, the space complexity is O(m + n). (The space complexity of solution 2 is O(1) since it uses a constant size counter array.)
Code Explanation (Python Solution 1):
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)
arr = [6 - v for v in nums1] + [v - 1 for v in nums2]
d = s2 - s1
for i, v in enumerate(sorted(arr, reverse=True), 1):
d -= v
if d <= 0:
return i
return -1
The Python code directly implements the algorithm described above. The enumerate
function is used to efficiently track the number of iterations. The sorted(arr, reverse=True)
ensures elements are sorted in descending order for the greedy approach. The recursive call handles the case where s1
> s2
.
The second solution uses a counting array to achieve the same result with a slightly improved space complexity of O(1). The time complexity remains the same.