2
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:4
# step1. determine the larger array and the smaller array, and get the difference on sum11
larger_sum_nums = nums112
smaller_sum_nums = nums214
larger_sum_nums = nums215
smaller_sum_nums = nums117
sum_diff = abs(sum1 - sum2)19
# step2. calculate the max "gain" at each position (how much difference we can reduce if operating on that position)20
gains_in_larger_array = [num - 1 for num in larger_sum_nums]21
gains_in_smaller_array = [6 - num for num in smaller_sum_nums]23
# step3. sort the "gain" and check the least number of steps to reduce the difference to 024
gains = gains_in_larger_array + gains_in_smaller_array25
gains.sort(reverse=True)28
target_diff = sum_diff30
for i in range(len(gains)):31
target_diff -= gains[i]37
# return -1 if the difference still cannot be reduced to 0 even after operating on all positions