You are given two positive integer arrays nums
and target
, of the same length.
In a single operation, you can select any subarray of nums
and increment each element within that subarray by 1 or decrement each element within that subarray by 1.
Return the minimum number of operations required to make nums
equal to the array target
.
Example 1:
nums = [3,5,1,2], target = [4,6,2,4]
Output: 2
Explanation:
We will perform the following operations to make nums
equal to target
:
nums[0..3]
by 1, nums = [4,6,2,3]
.nums[3..3]
by 1, nums = [4,6,2,4]
.Example 2:
nums = [1,3,2], target = [2,1,4]
Output: 5
Explanation:
We will perform the following operations to make nums
equal to target
:
nums[0..0]
by 1, nums = [2,3,2]
.nums[1..1]
by 1, nums = [2,2,2]
.nums[1..1]
by 1, nums = [2,1,2]
.nums[2..2]
by 1, nums = [2,1,3]
.nums[2..2]
by 1, nums = [2,1,4]
.Constraints:
1 <= nums.length == target.length <= 10^5
1 <= nums[i], target[i] <= 10^8
The most straightforward approach is to iterate through the arrays and, for each index i
, if nums[i]
is not equal to target[i]
, increment or decrement nums[i]
until it matches target[i]
. This approach considers each element independently and doesn't take advantage of potential subarray operations.
def min_operations_naive(nums, target):
operations = 0
for i in range(len(nums)):
operations += abs(nums[i] - target[i])
return operations
This problem can be solved more efficiently by considering the differences between adjacent elements. The key insight is that the number of operations is related to the sum of absolute differences between adjacent elements in the diff
array (where diff[i] = target[i] - nums[i]
). We can calculate the initial difference, and then accumulate the absolute changes in the differences between each subsequent element.
Here's a step-by-step breakdown:
diff[i] = target[i] - nums[i]
.diff
array.diff
array starting from the second element.def min_operations_optimal(nums, target):
n = len(nums)
diff = [target[i] - nums[i] for i in range(n)]
operations = abs(diff[0])
for i in range(1, n):
operations += abs(diff[i] - diff[i-1])
return operations
Let's walk through Example 1: nums = [3,5,1,2], target = [4,6,2,4]
diff = [1, 1, 1, 2]
operations = abs(1) = 1
i = 1
: operations += abs(1 - 1) = 1 + 0 = 1
i = 2
: operations += abs(1 - 1) = 1 + 0 = 1
i = 3
: operations += abs(2 - 1) = 1 + 1 = 2
2
The optimal approach iterates through the arrays once to calculate the difference array and then iterates through the difference array once to compute the operations. Both iterations take O(n) time, where n is the length of the arrays. Therefore, the overall time complexity is O(n).
The optimal approach creates a difference array of size n. Thus, the space complexity is O(n).
nums
and target
can be up to 108. The difference between them could also be large. Ensure that the data type used to store the differences and operations can handle these large values to prevent overflow.def min_operations(nums, target):
if not nums or not target:
return 0
if len(nums) != len(target):
raise ValueError("Arrays must have the same length")
n = len(nums)
diff = [target[i] - nums[i] for i in range(n)]
operations = abs(diff[0])
for i in range(1, n):
operations += abs(diff[i] - diff[i-1])
return operations