Welcome to Subscribe On Youtube
3229. Minimum Operations to Make Array Equal to Target
Description
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 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:
Input: 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
:
- Increment nums[0..3]
by 1, nums = [4,6,2,3]
.
- Increment nums[3..3]
by 1, nums = [4,6,2,4]
.
Example 2:
Input: nums = [1,3,2], target = [2,1,4]
Output: 5
Explanation:
We will perform the following operations to make nums
equal to target
:
- Increment nums[0..0]
by 1, nums = [2,3,2]
.
- Decrement nums[1..1]
by 1, nums = [2,2,2]
.
- Decrement nums[1..1]
by 1, nums = [2,1,2]
.
- Increment nums[2..2]
by 1, nums = [2,1,3]
.
- Increment nums[2..2]
by 1, nums = [2,1,4]
.
Constraints:
1 <= nums.length == target.length <= 105
1 <= nums[i], target[i] <= 108
Solutions
Solution 1: Dynamic Programming
We can first calculate the difference between the arrays $\textit{nums}$ and $\textit{target}$. For a difference array, we find continuous intervals where the signs of the differences are the same. For each interval, we add the absolute value of the first element to the result. For the subsequent elements, if the absolute value of the difference is greater than the absolute value of the previous difference, we add the difference of the absolute values to the result.
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
Similar problems:
-
class Solution { public long minimumOperations(int[] nums, int[] target) { long f = Math.abs(target[0] - nums[0]); for (int i = 1; i < nums.length; ++i) { long x = target[i] - nums[i]; long y = target[i - 1] - nums[i - 1]; if (x * y > 0) { long d = Math.abs(x) - Math.abs(y); if (d > 0) { f += d; } } else { f += Math.abs(x); } } return f; } }
-
class Solution { public: long long minimumOperations(vector<int>& nums, vector<int>& target) { using ll = long long; ll f = abs(target[0] - nums[0]); for (int i = 1; i < nums.size(); ++i) { long x = target[i] - nums[i]; long y = target[i - 1] - nums[i - 1]; if (x * y > 0) { ll d = abs(x) - abs(y); if (d > 0) { f += d; } } else { f += abs(x); } } return f; } };
-
class Solution: def minimumOperations(self, nums: List[int], target: List[int]) -> int: n = len(nums) f = abs(target[0] - nums[0]) for i in range(1, n): x = target[i] - nums[i] y = target[i - 1] - nums[i - 1] if x * y > 0: d = abs(x) - abs(y) if d > 0: f += d else: f += abs(x) return f
-
func minimumOperations(nums []int, target []int) int64 { f := abs(target[0] - nums[0]) for i := 1; i < len(target); i++ { x := target[i] - nums[i] y := target[i-1] - nums[i-1] if x*y > 0 { if d := abs(x) - abs(y); d > 0 { f += d } } else { f += abs(x) } } return int64(f) } func abs(x int) int { if x < 0 { return -x } return x }
-
function minimumOperations(nums: number[], target: number[]): number { const n = nums.length; let f = Math.abs(target[0] - nums[0]); for (let i = 1; i < n; ++i) { const x = target[i] - nums[i]; const y = target[i - 1] - nums[i - 1]; if (x * y > 0) { const d = Math.abs(x) - Math.abs(y); if (d > 0) { f += d; } } else { f += Math.abs(x); } } return f; }