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
Inputnums = [3,5,1,2], target = [4,6,2,4]
Output2
Incrementing nums[0..3] by 1 and then nums[3..3] by 1 makes nums equal to target in 2 operations.
Example 2
Inputnums = [1,3,2], target = [2,1,4]
Output5
The listed single-element increments and decrements transform nums into target in 5 operations.

Constraints

  • 1 <= nums.length == target.length <= 10^5
  • 1 <= nums[i], target[i] <= 10^8

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate