Minimum Operations to Make Arrays Equal
Given two arrays source and target, return the minimum number of operations to make the arrays equal. If it is impossible return -1.
In 1 operation, you can select a prefix or suffix of the array and add 1 to all the elements in the selected subarray.
For example, source = [1,2,2] and target = [2,2,3]. First operation: [2,2,2]. Next operation: [2,2,3]. Answer is 2.
Complete the function minOperations in the editor.
minOperations has the following parameters:
- 1.
int[] source: an array of integers representing the source array - 2.
int[] target: an array of integers representing the target array
Returns
int: the minimum number of operations to make the arrays equal or -1 if impossible
1Example 1
First operation: select the prefix [1] and add 1 to all its elements to get [2,2,2].
Next operation: select the suffix [3] and add 1 to all its elements to get [2,2,3].
The answer is 2 operations.
Constraints
Limits and guarantees your solution can rely on.
Size of the array can be up to 105element are in the range [-1013, 1013]