3229. Minimum Operations to Make Array Equal to Target LeetCode Solution

In this guide, you will get 3229. Minimum Operations to Make Array Equal to Target LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make Array Equal to Target problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Make Array Equal to Target solution in C++
  4. Minimum Operations to Make Array Equal to Target solution in Java
  5. Minimum Operations to Make Array Equal to Target solution in Python
  6. Additional Resources
3229. Minimum Operations to Make Array Equal to Target LeetCode Solution image

Problem Statement of Minimum Operations to Make Array Equal to Target

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:

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].

See also  1022. Sum of Root To Leaf Binary Numbers LeetCode Solution

Constraints:

1 <= nums.length == target.length <= 105
1 <= nums[i], target[i] <= 108

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3229. Minimum Operations to Make Array Equal to Target LeetCode Solution in C++

class Solution {
 public:
  // Similar to 1526. Minimum Number of Increments on Subarrays to Form a Target
  // Array
  long long minimumOperations(vector<int>& nums, vector<int>& target) {
    long ans = abs(nums[0] - target[0]);

    for (int i = 1; i < nums.size(); ++i) {
      const int currDiff = target[i] - nums[i];
      const int prevDiff = target[i - 1] - nums[i - 1];
      if (currDiff >= 0 && prevDiff >= 0)
        ans += max(0, currDiff - prevDiff);
      else if (currDiff <= 0 && prevDiff <= 0)
        ans += max(0, abs(currDiff) - abs(prevDiff));
      else
        ans += abs(currDiff);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3229. Minimum Operations to Make Array Equal to Target LeetCode Solution in Java

class Solution {
  // Similar to 1526. Minimum Number of Increments on Subarrays to Form a Target Array
  public long minimumOperations(int[] nums, int[] target) {
    long ans = Math.abs((long) nums[0] - target[0]);

    for (int i = 1; i < nums.length; ++i) {
      final int currDiff = target[i] - nums[i];
      final int prevDiff = target[i - 1] - nums[i - 1];
      if (currDiff >= 0 && prevDiff >= 0)
        ans += Math.max(0, currDiff - prevDiff);
      else if (currDiff <= 0 && prevDiff <= 0)
        ans += Math.max(0, Math.abs((long) currDiff) - Math.abs((long) prevDiff));
      else
        ans += Math.abs((long) currDiff);
    }

    return ans;
  }
}
// code provided by PROGIEZ

3229. Minimum Operations to Make Array Equal to Target LeetCode Solution in Python

class Solution:
  # Similar to 1526. Minimum Number of Increments on Subarrays to Form a Target Array
  def minimumOperations(self, nums: list[int], target: list[int]) -> int:
    ans = abs(nums[0] - target[0])

    for (prevNum, prevTarget), (currNum, currTarget) in (
        itertools.pairwise(zip(nums, target))
    ):
      currDiff = currTarget - currNum
      prevDiff = prevTarget - prevNum
      if currDiff >= 0 and prevDiff >= 0:
        ans += max(0, currDiff - prevDiff)
      elif currDiff <= 0 and prevDiff <= 0:
        ans += max(0, abs(currDiff) - abs(prevDiff))
      else:
        ans += abs(currDiff)

    return ans
# code by PROGIEZ

Additional Resources

See also  3035. Maximum Palindromes After Operations LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.