Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3505 - Minimum Operations to Make Elements Within K Subarrays Equal.

    Hard

    You are given an integer array nums and two integers, x and k. You can perform the following operation any number of times ( including zero ):

    • Increase or decrease any element of nums by 1.

    Return the minimum number of operations needed to have at least k non-overlapping non-empty subarrays of size exactly x in nums, where all elements within each subarray are equal.

    Example 1:

    Input: nums = 5,-2,1,3,7,3,6,4,-1, x = 3, k = 2

    Output: 8

    Explanation:

    • Use 3 operations to add 3 to nums[1] and use 2 operations to subtract 2 from nums[3]. The resulting array is [5, 1, 1, 1, 7, 3, 6, 4, -1].

    • Use 1 operation to add 1 to nums[5] and use 2 operations to subtract 2 from nums[6]. The resulting array is [5, 1, 1, 1, 7, 4, 4, 4, -1].

    • Now, all elements within each subarray [1, 1, 1] (from indices 1 to 3) and [4, 4, 4] (from indices 5 to 7) are equal. Since 8 total operations were used, 8 is the output.

    Example 2:

    Input: nums = 9,-2,-2,-2,1,5, x = 2, k = 2

    Output: 3

    Explanation:

    • Use 3 operations to subtract 3 from nums[4]. The resulting array is [9, -2, -2, -2, -2, 5].

    • Now, all elements within each subarray [-2, -2] (from indices 1 to 2) and [-2, -2] (from indices 3 to 4) are equal. Since 3 operations were used, 3 is the output.

    Constraints:

    • <code>2 <= nums.length <= 10<sup>5</sup></code>

    • <code>-10<sup>6</sup><= numsi<= 10<sup>6</sup></code>

    • 2 &lt;= x &lt;= nums.length

    • 1 &lt;= k &lt;= 15

    • 2 &lt;= k * x &lt;= nums.length

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Long minOperations(IntArray nums, Integer x, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait