Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0cc1e49

Browse files
committed
Add solution #1681
1 parent 94ad215 commit 0cc1e49

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,470 LeetCode solutions in JavaScript
1+
# 1,471 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1295,6 +1295,7 @@
12951295
1678|[Goal Parser Interpretation](./solutions/1678-goal-parser-interpretation.js)|Easy|
12961296
1679|[Max Number of K-Sum Pairs](./solutions/1679-max-number-of-k-sum-pairs.js)|Medium|
12971297
1680|[Concatenation of Consecutive Binary Numbers](./solutions/1680-concatenation-of-consecutive-binary-numbers.js)|Medium|
1298+
1681|[Minimum Incompatibility](./solutions/1681-minimum-incompatibility.js)|Hard|
12981299
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
12991300
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13001301
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* 1681. Minimum Incompatibility
3+
* https://leetcode.com/problems/minimum-incompatibility/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array nums and an integer k. You are asked to distribute this array
7+
* into k subsets of equal size such that there are no two equal elements in the same subset.
8+
*
9+
* A subset's incompatibility is the difference between the maximum and minimum elements in that
10+
* array.
11+
*
12+
* Return the minimum possible sum of incompatibilities of the k subsets after distributing the
13+
* array optimally, or return -1 if it is not possible.
14+
*
15+
* A subset is a group integers that appear in the array with no particular order.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @param {number} k
21+
* @return {number}
22+
*/
23+
var minimumIncompatibility = function(nums, k) {
24+
const n = nums.length;
25+
const subsetSize = n / k;
26+
const freq = new Array(n + 1).fill(0);
27+
for (const num of nums) {
28+
freq[num]++;
29+
if (freq[num] > k) return -1;
30+
}
31+
32+
nums.sort((a, b) => a - b);
33+
const subsetValues = new Map();
34+
35+
computeSubsets(0, 0, 0, 0, 0, []);
36+
37+
const dp = new Array(1 << n).fill(Infinity);
38+
dp[0] = 0;
39+
40+
for (let mask = 0; mask < (1 << n); mask++) {
41+
if (dp[mask] === Infinity) continue;
42+
for (const [subsetMask, value] of subsetValues) {
43+
if ((mask & subsetMask) === 0) {
44+
const newMask = mask | subsetMask;
45+
dp[newMask] = Math.min(dp[newMask], dp[mask] + value);
46+
}
47+
}
48+
}
49+
50+
return dp[(1 << n) - 1] === Infinity ? -1 : dp[(1 << n) - 1];
51+
52+
function computeSubsets(mask, index, count, minVal, maxVal, selected) {
53+
if (count === subsetSize) {
54+
subsetValues.set(mask, maxVal - minVal);
55+
return;
56+
}
57+
if (index >= n || n - index < subsetSize - count) return;
58+
59+
computeSubsets(mask, index + 1, count, minVal, maxVal, selected);
60+
if (!selected.includes(nums[index])) {
61+
computeSubsets(mask | (1 << index), index + 1, count + 1,
62+
count === 0 ? nums[index] : minVal, nums[index], [...selected, nums[index]]);
63+
}
64+
}
65+
};

0 commit comments

Comments
 (0)