Minimum Incompatibility Problem
Description
LeetCode Problem 1681.
You are given an integer array nums and an integer k. You are asked to distribute this array into k subsets of equal size such that there are no two equal elements in the same subset.
A subset's incompatibility is the difference between the maximum and minimum elements in that array.
Return the minimum possible sum of incompatibilities of the k subsets after distributing the array optimally, or return -1 if it is not possible.
A subset is a group integers that appear in the array with no particular order.
Example 1:
Input: nums = [1,2,1,4], k = 2
Output: 4
Explanation: The optimal distribution of subsets is [1,2] and [1,4].
The incompatibility is (2-1) + (4-1) = 4.
Note that [1,1] and [2,4] would result in a smaller sum, but the first subset contains 2 equal elements.
Example 2:
Input: nums = [6,3,8,1,3,1,2,2], k = 4
Output: 6
Explanation: The optimal distribution of subsets is [1,2], [2,3], [6,8], and [1,3].
The incompatibility is (2-1) + (3-2) + (8-6) + (3-1) = 6.
Example 3:
Input: nums = [5,3,3,6,3,3], k = 3
Output: -1
Explanation: It is impossible to distribute nums into 3 subsets where no two elements are equal in the same subset.
Constraints:
1 <= k <= nums.length <= 16nums.lengthis divisible byk1 <= nums[i] <= nums.length
Solution
Approach: Bitmask DP Over Subsets
With n <= 16 we can describe any set of indices as a bitmask. Every subset must have m = n / k elements.
Step 1: the cost of each possible group. For every mask with exactly m bits, check that its values are all different. If they are, its cost g[mask] is the maximum value minus the minimum value; otherwise mark it as invalid (-1).
Step 2: DP over the used indices. Let f[i] be the smallest total incompatibility when the indices in mask i have already been placed into groups, with f[0] = 0. From a reachable state i, collect the unused indices, keeping only one index per distinct value (a group can hold each value once), into a mask mask. If it has fewer than m indices, no group can be formed. Otherwise try every submask j of mask that is a valid group and update f[i | j] = min(f[i | j], f[i] + g[j]).
The order of the groups does not matter, so we only try groups j that contain the lowest unused index. Each partition is then built in exactly one order, which leaves far fewer reachable states.
The answer is f[2^n - 1], or -1 if that state is never reached.
Complexity: O(3n) time in the worst case (submasks of every mask), though the lowest-index rule makes it much faster in practice, and O(2n) space.
from math import inf
class Solution:
def minimumIncompatibility(self, nums: List[int], k: int) -> int:
n = len(nums)
m = n // k
g = [-1] * (1 << n)
for i in range(1, 1 << n):
if bin(i).count('1') != m:
continue
s = set()
mi, mx = 20, 0
for j, x in enumerate(nums):
if i >> j & 1:
if x in s:
break
s.add(x)
mi = min(mi, x)
mx = max(mx, x)
if len(s) == m:
g[i] = mx - mi
f = [inf] * (1 << n)
f[0] = 0
for i in range(1 << n):
if f[i] == inf:
continue
s = set()
mask = 0
for j, x in enumerate(nums):
if (i >> j & 1) == 0 and x not in s:
s.add(x)
mask |= 1 << j
if len(s) < m:
continue
# the group must contain the lowest unused index, so each partition is built once
low = mask & -mask
j = mask
while j:
if j & low and g[j] != -1:
f[i | j] = min(f[i | j], f[i] + g[j])
j = (j - 1) & mask
return f[-1] if f[-1] != inf else -1