Class Solution
-
- All Implemented Interfaces:
public final class Solution3229 - Minimum Operations to Make Array Equal to Target\.
Hard
You are given two positive integer arrays
numsandtarget, of the same length.In a single operation, you can select any subarray of
numsand increment or decrement each element within that subarray by 1.Return the minimum number of operations required to make
numsequal to the arraytarget.Example 1:
Input: nums = 3,5,1,2, target = 4,6,2,4
Output: 2
Explanation:
We will perform the following operations to make
numsequal totarget: \- Incrementnums[0..3]by 1,nums = [4,6,2,3]. \- Incrementnums[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
numsequal totarget: \- Incrementnums[0..0]by 1,nums = [2,3,2]. \- Decrementnums[1..1]by 1,nums = [2,2,2]. \- Decrementnums[1..1]by 1,nums = [2,1,2]. \- Incrementnums[2..2]by 1,nums = [2,1,3]. \- Incrementnums[2..2]by 1,nums = [2,1,4].Constraints:
<code>1 <= nums.length == target.length <= 10<sup>5</sup></code>
<code>1 <= numsi, targeti<= 10<sup>8</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final LongminimumOperations(IntArray nums, IntArray target)-
-
Method Detail
-
minimumOperations
final Long minimumOperations(IntArray nums, IntArray target)
-
-
-
-