java.lang.Object
g3701_3800.s3724_minimum_operations_to_transform_array.Solution

public class Solution extends Object
3724 - Minimum Operations to Transform Array.

Medium

You are given two integer arrays nums1 of length n and nums2 of length n + 1.

You want to transform nums1 into nums2 using the minimum number of operations.

You may perform the following operations any number of times, each time choosing an index i:

  • Increase nums1[i] by 1.
  • Decrease nums1[i] by 1.
  • Append nums1[i] to the end of the array.

Return the minimum number of operations required to transform nums1 into nums2.

Example 1:

Input: nums1 = [2,8], nums2 = [1,7,3]

Output: 4

Explanation:

StepiOperationnums1[i]Updated nums1
10Append-[2, 8, 2]
20DecrementDecreases to 1[1, 8, 2]
31DecrementDecreases to 7[1, 7, 2]
42IncrementIncreases to 3[1, 7, 3]

Thus, after 4 operations nums1 is transformed into nums2.

Example 2:

Input: nums1 = [1,3,6], nums2 = [2,4,5,3]

Output: 4

Explanation:

StepiOperationnums1[i]Updated nums1
11Append-[1, 3, 6, 3]
20IncrementIncreases to 2[2, 3, 6, 3]
31IncrementIncreases to 4[2, 4, 6, 3]
42DecrementDecreases to 5[2, 4, 5, 3]

Thus, after 4 operations nums1 is transformed into nums2.

Example 3:

Input: nums1 = [2], nums2 = [3,4]

Output: 3

Explanation:

StepiOperationnums1[i]Updated nums1
10IncrementIncreases to 3[3]
20Append-[3, 3]
31IncrementIncreases to 4[3, 4]

Thus, after 3 operations nums1 is transformed into nums2.

Constraints:

  • 1 <= n == nums1.length <= 105
  • nums2.length == n + 1
  • 1 <= nums1[i], nums2[i] <= 105
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minOperations

      public long minOperations(int[] nums1, int[] nums2)