You are given two integer arrays nums1 and nums2 of the same length. Find the minimum number of moves needed to make them identical.
A move increments or decrements a single element of nums1 by 1. Reordering elements is not allowed, so nums1[i] must end up equal to nums2[i] for every index i.
Return the total minimum number of moves.
Function Signature
def min_moves(nums1: list[int], nums2: list[int]) -> int:
Constraints
-
1 <= len(nums1) <= 10^4
-
len(nums1) == len(nums2)
-
-10^9 <= nums1[i], nums2[i] <= 10^9
Examples
Input: nums1 = [1, 2, 3], nums2 = [3, 2, 1]
Output: 4
Index 0 needs |1 - 3| = 2 moves, index 1 needs |2 - 2| = 0 and index 2 needs |3 - 1| = 2, for a total of 4.
Input: nums1 = [1, 4, 7, 10], nums2 = [2, 3, 8, 5]
Output: 8
The per-index costs are 1, 1, 1 and 5, which add up to 8.
Input: nums1 = [-1000000000, 7, 1000000000], nums2 = [1000000000, -3, -1000000000]
Output: 4000000010
Index 0 and index 2 each need 2,000,000,000 moves and index 1 needs |7 - (-3)| = 10. The total is larger than a 32-bit signed integer can hold.