classSolution { public: intminimumIncompatibility(vector<int>& nums, int k){ int n = nums.size(); vector<int> dp(1 << n, INT_MAX); dp[0] = 0; int group = n / k; unordered_map<int, int> values;
for (int mask = 1; mask < (1 << n); mask++) { if (__builtin_popcount(mask) != group) { continue; } int mn = 20, mx = 0; unordered_set<int> cur; for (int i = 0; i < n; i++) { if (mask & (1 << i)) { if (cur.count(nums[i]) > 0) { break; } cur.insert(nums[i]); mn = min(mn, nums[i]); mx = max(mx, nums[i]); } } if (cur.size() == group) { values[mask] = mx - mn; } }
for (int mask = 0; mask < (1 << n); mask++) { if (dp[mask] == INT_MAX) { continue; } unordered_map<int, int> seen; for (int i = 0; i < n; i++) { if ((mask & (1 << i)) == 0) { seen[nums[i]] = i; } } if (seen.size() < group) { continue; } int sub = 0; for (auto& pair : seen) { sub |= (1 << pair.second); } int nxt = sub; while (nxt > 0) { if (values.count(nxt) > 0) { dp[mask | nxt] = min(dp[mask | nxt], dp[mask] + values[nxt]); } nxt = (nxt - 1) & sub; } }
classSolution: defminimumIncompatibility(self, nums: List[int], k: int) -> int: n = len(nums) dp = [inf] * (1 << n) dp[0] = 0 group = n // k values = {}
for mask inrange(1 << n): if mask.bit_count() != group: continue mn = 20 mx = 0 cur = set() for i inrange(n): if mask & (1 << i) > 0: if nums[i] in cur: break cur.add(nums[i]) mn = min(mn, nums[i]) mx = max(mx, nums[i]) iflen(cur) == group: values[mask] = mx - mn
for mask inrange(1 << n): if dp[mask] == inf: continue seen = {} for i inrange(n): if mask & (1 << i) == 0: seen[nums[i]] = i iflen(seen) < group: continue sub = 0 for v in seen: sub |= 1 << seen[v] nxt = sub while nxt > 0: if nxt in values: dp[mask | nxt] = min(dp[mask | nxt], dp[mask] + values[nxt]) nxt = (nxt - 1) & sub
funcminimumIncompatibility(nums []int, k int)int { n := len(nums) group := n / k inf := math.MaxInt32 dp := make([]int, 1 << n) for i := range dp { dp[i] = inf } dp[0] = 0 values := make(map[int]int)
for mask := 1; mask < (1 << n); mask++ { if bits.OnesCount(uint(mask)) != group { continue } minVal := 20 maxVal := 0 cur := make(map[int]bool) for i := 0; i < n; i++ { if mask & (1 << i) != 0 { if cur[nums[i]] { break } cur[nums[i]] = true minVal = min(minVal, nums[i]) maxVal = max(maxVal, nums[i]) } } iflen(cur) == group { values[mask] = maxVal - minVal } }
for mask := 0; mask < (1 << n); mask++ { if dp[mask] == inf { continue } seen := make(map[int]int) for i := 0; i < n; i++ { if (mask & (1 << i)) == 0 { seen[nums[i]] = i } } iflen(seen) < group { continue } sub := 0 for _, v := range seen { sub |= (1 << v) } nxt := sub for nxt > 0 { if val, ok := values[nxt]; ok { dp[mask|nxt] = min(dp[mask|nxt], dp[mask] + val) } nxt = (nxt - 1) & sub } } if (dp[(1 << n) - 1] < inf) { return dp[(1 << n) - 1] } return-1 }
funcmin(a, b int)int { if a < b { return a } return b }
funcmax(a, b int)int { if a > b { return a } return b }
intminimumIncompatibility(int* nums, int numsSize, int k) { int n = numsSize; HashItem *values = NULL; int dp[1 << n]; for (int i = 0; i < (1 << n); i++) { dp[i] = INT_MAX; } dp[0] = 0; int group = n / k;
for (int mask = 1; mask < (1 << n); mask++) { if (__builtin_popcount(mask) != group) { continue; } int mn = 20, mx = 0; int cur[n + 1]; memset(cur, 0, sizeof(cur)); for (int i = 0; i < n; i++) { if (mask & (1 << i)) { if (cur[nums[i]] > 0) { break; } cur[nums[i]] = 1; mn = fmin(mn, nums[i]); mx = fmax(mx, nums[i]); } } int curSize = 0; for (int i = 0; i <= n; i++) { if (cur[i] > 0) { curSize++; } } if (curSize == group) { hashAddItem(&values, mask, mx - mn); } } for (int mask = 0; mask < (1 << n); mask++) { if (dp[mask] == INT_MAX) { continue; } HashItem *seen = NULL; for (int i = 0; i < n; i++) { if ((mask & (1 << i)) == 0) { hashAddItem(&seen, nums[i], i); } } if (HASH_COUNT(seen) < group) { continue; } int sub = 0; for (HashItem *pEntry = seen; pEntry; pEntry = pEntry->hh.next) { sub |= (1 << pEntry->val); } hashFree(&seen); int nxt = sub; while (nxt > 0) { if (hashFindItem(&values, nxt)) { dp[mask | nxt] = fmin(dp[mask | nxt], dp[mask] + hashGetItem(&values, nxt, 0)); } nxt = (nxt - 1) & sub; } } hashFree(&values); return (dp[(1 << n) - 1] < INT_MAX) ? dp[(1 << n) - 1] : -1; }