Loading...
Loading...
You are given a list of integers nums and an integer groupSize. The length of nums is a multiple of groupSize.
Consider an operation where you pick one element of nums and replace it with any integer smaller than its current value. The new value doesn't have to appear anywhere else in nums. It can also be 0 or negative.
After the operations, you want to split all the elements into groups of exactly groupSize elements, where all the elements in a group are equal. Every element must be in exactly one group. Such a split is possible exactly when every value appears a multiple of groupSize times.
Return the minimum number of operations needed.
Input: nums = [4,2,4,4,7,4], groupSize = 3
Output: 2
Explanation: Change 7 to 2 and change one of the 4s to 2.
Now the values are three 4s and three 2s, which form the groups [4,4,4] and [2,2,2].
One operation is not enough: 7 and 2 each appear once, and a single change can't give both of them a count that is a multiple of 3.
Input: nums = [1,2,2,3,4,5,5,6], groupSize = 4
Output: 5
Explanation: Change 3 and 4 to 2, which makes four 2s.
Change both 5s and the 6 to 1, which makes four 1s.
Input: nums = [10,10,10,5,5,4,4,3,3], groupSize = 3
Output: 2
Explanation: The three 10s already form a group.
Change one 5 to 4 and the other 5 to 3. Now there are three 4s and three 3s.
nums.length ≤2⋅105nums[i] ≤109groupSize ≤ nums.lengthnums.length is a multiple of groupSize