#3505

Minimum Operations to Make Elements Within K Subarrays Equal

hard · 27.8% accepted · 59 likes · top 6%

array · hash table · math · dynamic programming · sliding window · heap (priority queue)

⊣ practice⊣ open on leetcode ↗

Description

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 subarrays of size exactly x in nums, where all elements within each subarray are equal.

Solution