-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path1681-minimum-incompatibility.js
65 lines (57 loc) · 1.97 KB
/
1681-minimum-incompatibility.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 1681. Minimum Incompatibility
* https://leetcode.com/problems/minimum-incompatibility/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minimumIncompatibility = function(nums, k) {
const n = nums.length;
const subsetSize = n / k;
const freq = new Array(n + 1).fill(0);
for (const num of nums) {
freq[num]++;
if (freq[num] > k) return -1;
}
nums.sort((a, b) => a - b);
const subsetValues = new Map();
computeSubsets(0, 0, 0, 0, 0, []);
const dp = new Array(1 << n).fill(Infinity);
dp[0] = 0;
for (let mask = 0; mask < (1 << n); mask++) {
if (dp[mask] === Infinity) continue;
for (const [subsetMask, value] of subsetValues) {
if ((mask & subsetMask) === 0) {
const newMask = mask | subsetMask;
dp[newMask] = Math.min(dp[newMask], dp[mask] + value);
}
}
}
return dp[(1 << n) - 1] === Infinity ? -1 : dp[(1 << n) - 1];
function computeSubsets(mask, index, count, minVal, maxVal, selected) {
if (count === subsetSize) {
subsetValues.set(mask, maxVal - minVal);
return;
}
if (index >= n || n - index < subsetSize - count) return;
computeSubsets(mask, index + 1, count, minVal, maxVal, selected);
if (!selected.includes(nums[index])) {
computeSubsets(mask | (1 << index), index + 1, count + 1,
count === 0 ? nums[index] : minVal, nums[index], [...selected, nums[index]]);
}
}
};