1681. Minimum Incompatibility


Problem Description

In this problem, you are given an array of integers nums and an integer k. Your task is to partition the array into k subsets, each containing the same number of elements, such that no subset contains any duplicate elements. The "incompatibility" of a subset is defined as the difference between the maximum and minimum elements in that subset. The goal is to minimize the sum of incompatibilities across all subsets. If it is not possible to partition the array according to these rules, you should return -1. This is a combinatorial optimization problem that requires finding an optimal grouping of the array's elements.

Intuition

Solving this problem requires considering all the possible ways to split the array into subsets. The code uses a bit-mask and dynamic programming (DP) approach to achieve this.

  1. The variable n represents the number of elements in nums, and m is the size of each subset, which is derived from n // k.

  2. An array g is created to store the incompatibility of each subset. The subset is represented using a bitmask, with each bit representing an element in nums. If the bit is set to 1, it means that the corresponding element is in the subset.

  3. The first loop populates g. It only considers subsets with size m. If a subset has duplicates, it is disregarded. Otherwise, the incompatibility of the subset is calculated and stored in g.

  4. The main DP array f indicates the minimum sum of incompatibilities so far, for each subset of elements processed. Initially, all values are set to infinity (inf in Python denotes a very large number that acts as an approximation to infinity), except f[0], which is set to 0.

  5. The second loop goes through all the possible subsets (represented by the bitmask), and attempts to extend each subset by adding a new subset to it without adding any duplicates.

  6. The extension is done by merging two bitmasks: the current subset and a new subset. The DP is updated only if the new subset is valid and does not have an incompatibility value of -1 (meaning it had duplicates or was not of size m).

  7. By the end of the DP process, f[-1] will contain the minimum possible sum of incompatibilities for all k subsets. If it's still inf, it means it's not possible to split nums into the desired k subsets, and thus the function returns -1.

This solution is a sophisticated one due to its use of bit manipulation and dynamic programming to efficiently explore the search space of possible subset splits. It captures all possible partitions while pruning those that cannot lead to an optimal solution.

Learn more about Dynamic Programming and Bitmask patterns.

Not Sure What to Study? Take the 2-min Quiz to Find Your Missing Piece

Which algorithm should you use to find a node that is close to the root of the tree?

Solution Approach

The solution is implemented using Dynamic Programming (DP) and Bitmasking. These are combined to elegantly split the given array nums into k non-overlapping valid subsets. Here is how the approach unfolds in the code:

  1. Bitmasking: We use an integer's bits to represent the presence (1) or absence (0) of an element from nums in a subset. This efficiently represents all possible subsets of nums with a single integer. There are 2^n possible subsets where n is the number of elements in nums.

  2. Pre-computation of Incompatibilities (g): The array g is computed to store the incompatibility of each valid subset represented by a bitmask. This is done by iterating over all 2^n possible combinations and checking if they form valid subsets of size m. Here, validity implies uniqueness of elements in the subset and the correct subset size.

    • Iterate for each bitmask i from 1 to 2^n - 1.
    • For each bitmask, we check if the number of set bits (bit_count) is equal to m.
    • If the condition meets, we loop through the array to check if there are duplicates in the subset.
    • Calculate the minimum (mi) and maximum (mx) for the subset and set g[i] to mx - mi.

    By the end, g holds the incompatibility scores for all valid subsets, or -1 for invalid ones.

  3. Dynamic Programming (DP) with f Array: A DP array f is then set up to calculate the minimum possible sum of incompatibilities.

    • f[0] is initialized to 0 as the base case, representing no elements chosen.
    • Iterate through all possible states i of the DP.
    • If f[i] is inf, this state is not reachable and is skipped.
    • Otherwise, try to find a new subset to add to the current combination (represented by state i). This is done by creating a mask of elements not yet chosen.
    • Iterate through all subsets of this mask using the bit trick j = (j - 1) & mask. This trick sequentially gives all smaller subsets of a set bit mask.
    • If a valid subset is found (with a known incompatibility), update the DP state f[i | j] with the minimum between the current value and f[i] + g[j].
  4. Conclusion: After the DP completions, f[-1] will hold the minimal possible sum of incompatibilities if such a partitioning is possible, otherwise, it will remain inf.

In this approach, the inefficiency of generating all possible subset partitions is mitigated by using bitmasking to represent subsets and utilizing dynamic programming to build up solutions incrementally, reusing already computed values. This optimizes the process and provides a method for handling such combinatorial problems.

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

How would you design a stack which has a function min that returns the minimum element in the stack, in addition to push and pop? All push, pop, min should have running time O(1).

Example Walkthrough

Let's illustrate the solution approach using a small example. Suppose we have the following:

nums = [1, 2, 2, 3] and k = 2.

We want to divide this array into k=2 subsets, each with the same number of elements and without duplicates in any subset. The subsets should each contain n // k = 4 // 2 = 2 elements, so m = 2.

  • During Step 1, the bitmasking would account for 2^4 = 16 potential subsets from the nums array (since there are 4 elements).

  • In Step 2, we pre-calculate the incompatibilities (g). For simplicity, we'll consider only a few subsets bitmasks (in binary representation):

    • g[0011] -> This represents subset [1, 2].
    • g[0101] -> This subset [1, 2] is the same as before due to the repeating '2' and thus would be g[0101] = -1 (invalid).
    • g[1100] -> Subset [2, 3] with g[1100] = 3 - 2 = 1 (valid).
  • Step 3 details the usage of the DP array f. Since 2^n subsets are possible, our array f will have 16 values, each corresponding to a subset state ranging from f[0000] to f[1111].

    • Initially, f[0000] = 0 because we haven't chosen any elements yet.
    • Our aim is to find compatible g[j] values that can be added to f[i] without causing any duplicates and updating the DP array with the minimum incompatibility scores possible.
    • As an illustration, starting with f[0000], we can add g[0011] (if it were valid) to obtain f[0011].
    • We continue to search for a non-overlapping, valid g[j] subsets that can be added to f[0011] to eventually reach f[1111] which would represent a full partition into k subsets.
  • By Step 4, if a proper partitioning is possible, f[1111] (in this case f[-1] as f is indexed from 0) will hold the minimum sum of incompatibilities. Otherwise, f[-1] will remain at its initialized inf value.

For our nums, a potential solution would have subsets {1, 2} and {2, 3}, which means the corresponding bitmasks would be 0011 and 1100. The incompatibility scores would be 1 (for {2, 3}, as {1, 2} is invalid), resulting in a total incompatibility score of 1. Thus, in the end, f[1111] would be 1 for this example.

However, since we cannot have duplicates across subsets, we would end up being unable to find a valid partition and would return -1. Thus this particular input does not allow a partition under the problem's constraints.

Solution Implementation

1from typing import List
2
3class Solution:
4    def minimumIncompatibility(self, nums: List[int], k: int) -> int:
5        n = len(nums)
6        subset_size = n // k
7        incompatibilities = [-1] * (1 << n)
8      
9        # Calculate incompatibility for each valid subset (of size subset_size)
10        for subset in range(1, 1 << n):
11            # Skip subsets that do not have the correct size
12            if bin(subset).count('1') != subset_size:
13                continue
14          
15            unique_nums = set()  # to check for duplicates within a subset
16            min_num, max_num = 20, 0  # outside possible values of nums elements
17          
18            # Check each element to see if it is in the current subset and update min and max
19            for idx, num in enumerate(nums):
20                if subset >> idx & 1:
21                    if num in unique_nums:
22                        break  # if num is a duplicate, we skip this subset
23                    unique_nums.add(num)
24                    min_num = min(min_num, num)
25                    max_num = max(max_num, num)
26          
27            # If the subset was without duplicates, set its incompatibility
28            if len(unique_nums) == subset_size:
29                incompatibilities[subset] = max_num - min_num
30      
31        # Initialize the dp array with infinite values except for the starting position
32        dp = [float('inf')] * (1 << n)
33        dp[0] = 0
34      
35        # Build up the solution subset by subset
36        for state in range(1 << n):
37            if dp[state] == float('inf'):
38                continue  # if this state is not reachable, skip
39          
40            unique_nums = set()
41            next_mask = 0  # to keep track of which elements we can add to our state
42          
43            # Identify the next new elements that we could add to our state
44            for idx, num in enumerate(nums):
45                if (state >> idx & 1) == 0 and num not in unique_nums:
46                    unique_nums.add(num)
47                    next_mask |= 1 << idx
48          
49            # If the potential next state would not have a full subset, skip it
50            if len(unique_nums) < subset_size:
51                continue
52          
53            # Iterate through subsets of the next_mask and update dp accordingly
54            subset = next_mask
55            while subset:
56                if incompatibilities[subset] != -1:
57                    dp[state | subset] = min(dp[state | subset], dp[state] + incompatibilities[subset])
58                subset = (subset - 1) & next_mask
59      
60        # If the last state is reachable return its value, else return -1
61        return dp[-1] if dp[-1] != float('inf') else -1
62
1class Solution {
2    public int minimumIncompatibility(int[] nums, int k) {
3        int arrayLength = nums.length;
4        int subsetSize = arrayLength / k;
5        int[] incompatibility = new int[1 << arrayLength];
6        Arrays.fill(incompatibility, -1);
7        // Precompute the incompatibility for all possible subsets of size `subsetSize`
8        for (int i = 1; i < 1 << arrayLength; ++i) {
9            if (Integer.bitCount(i) != subsetSize) {
10                continue;
11            }
12            Set<Integer> uniqueElements = new HashSet<>();
13            int minElement = 20, maxElement = 0;
14            for (int j = 0; j < arrayLength; ++j) {
15                if ((i >> j & 1) == 1) { // Check if j-th bit is set in i
16                    if (!uniqueElements.add(nums[j])) {
17                        break; // Duplicate found, break since this subset cannot be used
18                    }
19                    minElement = Math.min(minElement, nums[j]);
20                    maxElement = Math.max(maxElement, nums[j]);
21                }
22            }
23            if (uniqueElements.size() == subsetSize) {
24                incompatibility[i] = maxElement - minElement;
25            }
26        }
27
28        // Dynamic programming table to store minimum incompatibility values
29        int[] dp = new int[1 << arrayLength];
30        final int inf = 1 << 30;
31        Arrays.fill(dp, inf);
32        dp[0] = 0;
33
34        // Compute the minimum incompatibility for all combinations of subsets
35        for (int i = 0; i < 1 << arrayLength; ++i) {
36            if (dp[i] == inf) {
37                continue;
38            }
39            // Construct a mask of elements not yet included in the current combination
40            Set<Integer> processedElements = new HashSet<>();
41            int mask = 0;
42            for (int j = 0; j < arrayLength; ++j) {
43                if ((i >> j & 1) == 0 && !processedElements.contains(nums[j])) {
44                    processedElements.add(nums[j]);
45                    mask |= 1 << j; // Set the j-th bit in the mask
46                }
47            }
48            if (processedElements.size() < subsetSize) {
49                continue; // No need to proceed if we can't form a valid subset
50            }
51            // Try all possible combinations of subsets that can be formed by unused elements
52            for (int j = mask; j > 0; j = (j - 1) & mask) {
53                if (incompatibility[j] != -1) {
54                    dp[i | j] = Math.min(dp[i | j], dp[i] + incompatibility[j]);
55                }
56            }
57        }
58        // The last element in dp is the answer unless it's INF (in which case the answer is -1)
59        return dp[(1 << arrayLength) - 1] == inf ? -1 : dp[(1 << arrayLength) - 1];
60    }
61}
62
1#include <vector>
2#include <unordered_set>
3#include <cstring>
4#include <algorithm>
5
6class Solution {
7public:
8    int minimumIncompatibility(std::vector<int>& nums, int k) {
9        int n = nums.size(); // Total number of elements in the input
10        int subsetSize = n / k; // Size of each subset
11        int subsetIncompatibility[1 << n]; // Array to store incompatibility of each subset
12        memset(subsetIncompatibility, -1, sizeof(subsetIncompatibility)); // Initialize with -1
13      
14        // Calculate the incompatibility of each possible subset
15        for (int i = 1; i < (1 << n); ++i) {
16            // Only consider subsets of the correct size
17            if (__builtin_popcount(i) != subsetSize) {
18                continue;
19            }
20
21            std::unordered_set<int> elements;
22            int minElement = 20; // Placeholder for minimum; any number larger than possible element in nums
23            int maxElement = 0;  // 0 is the minimum possible element
24            // Iterate over all elements to form a subset
25            for (int j = 0; j < n; ++j) {
26                if (i >> j & 1) { // Check if the jth element is in the subset
27                    // If we already have this number, the subset isn't valid
28                    if (elements.count(nums[j])) {
29                        break;
30                    }
31                    // Insert the element into the set
32                    elements.insert(nums[j]);
33                    // Update the minimum and maximum elements of this subset
34                    minElement = std::min(minElement, nums[j]);
35                    maxElement = std::max(maxElement, nums[j]);
36                }
37            }
38            // If a valid subset is formed, calculate its incompatibility
39            if (elements.size() == subsetSize) {
40                subsetIncompatibility[i] = maxElement - minElement;
41            }
42        }
43      
44        // Initialize dp array to store minimum incompatibility of combinations
45        int dp[1 << n];
46        memset(dp, 0x3f, sizeof(dp)); // Initialize with a large number (0x3f3f3f3f)
47        dp[0] = 0; // Base case: incompatibility of an empty set is 0
48      
49        // Iterate over all combinations of subsets
50        for (int i = 0; i < (1 << n); ++i) {
51            if (dp[i] == 0x3f3f3f3f) {
52                continue; // Skip if this combination hasn't been reached yet
53            }
54          
55            std::unordered_set<int> used;
56            int mask = 0;
57            // Create a mask for the remaining elements
58            for (int j = 0; j < n; ++j) {
59                if (!(i >> j & 1) && !used.count(nums[j])) {
60                    used.insert(nums[j]);
61                    mask |= (1 << j);
62                }
63            }
64            // Skip if there aren't enough unused elements to form a subset
65            if (used.size() < subsetSize) {
66                continue;
67            }
68            // Iterate over all subsets of the remaining elements
69            for (int j = mask; j; j = (j - 1) & mask) {
70                // Update the dp array if the subset is valid
71                if (subsetIncompatibility[j] != -1) {
72                    dp[i | j] = std::min(dp[i | j], dp[i] + subsetIncompatibility[j]);
73                }
74            }
75        }
76      
77        // Return the minimum incompatibility if it's been updated; otherwise, return -1
78        return dp[(1 << n) - 1] == 0x3f3f3f3f ? -1 : dp[(1 << n) - 1];
79    }
80};
81
1function minimumIncompatibility(nums: number[], k: number): number {
2    const totalNums = nums.length;
3    const setSize = Math.floor(totalNums / k);
4    const groupIncompatibility: number[] = Array(1 << totalNums).fill(-1);
5
6    // Pre-calculate the incompatibility for each subset with exactly 'setSize' numbers
7    for (let subsetMask = 1; subsetMask < 1 << totalNums; ++subsetMask) {
8        if (bitCount(subsetMask) !== setSize) {
9            // Skip if the number of bits is not equal to the required setSize
10            continue;
11        }
12        const setNumbers: Set<number> = new Set();
13        let [minNum, maxNum] = [20, 0];
14        for (let bitIndex = 0; bitIndex < totalNums; ++bitIndex) {
15            if ((subsetMask >> bitIndex) & 1) {
16                // Check if the bit is set meaning the number is included in the subset
17                if (setNumbers.has(nums[bitIndex])) {
18                    // Duplicate number found, this subset isn't valid
19                    break;
20                }
21                // Add number to the current set and update min and max
22                setNumbers.add(nums[bitIndex]);
23                minNum = Math.min(minNum, nums[bitIndex]);
24                maxNum = Math.max(maxNum, nums[bitIndex]);
25            }
26        }
27        // Calculate incompatibility if the set is valid
28        if (setNumbers.size === setSize) {
29            groupIncompatibility[subsetMask] = maxNum - minNum;
30        }
31    }
32
33    const inf = 1e9;
34    const dp: number[] = Array(1 << totalNums).fill(inf);
35    dp[0] = 0; // The incompatibility of an empty set is zero
36
37    // Use dynamic programming to build up solutions to larger sets
38    for (let subsetMask = 0; subsetMask < 1 << totalNums; ++subsetMask) {
39        if (dp[subsetMask] === inf) {
40            // If no valid solution for current subsetMask, skip
41            continue;
42        }
43        const availableNumbers: Set<number> = new Set();
44        let availableMask = 0;
45
46        // Build the mask for available numbers (which are not yet picked)
47        for (let bitIndex = 0; bitIndex < totalNums; ++bitIndex) {
48            if (((subsetMask >> bitIndex) & 1) === 0 && !availableNumbers.has(nums[bitIndex])) {
49                availableNumbers.add(nums[bitIndex]);
50                availableMask |= 1 << bitIndex;
51            }
52        }
53
54        // Generate all subsets of available numbers and update the answer in the DP array.
55        for (let j = availableMask; j; j = (j - 1) & availableMask) {
56            if (groupIncompatibility[j] !== -1) {
57                dp[subsetMask | j] = Math.min(dp[subsetMask | j], dp[subsetMask] + groupIncompatibility[j]);
58            }
59        }
60    }
61
62    // Return the answer for the full set or -1 if no answer exists
63    return dp[(1 << totalNums) - 1] === inf ? -1 : dp[(1 << totalNums) - 1];
64}
65
66// Helper function to count bits in an integer
67function bitCount(num: number): number {
68    num = num - ((num >>> 1) & 0x55555555);
69    num = (num & 0x33333333) + ((num >>> 2) & 0x33333333);
70    num = (num + (num >>> 4)) & 0x0f0f0f0f;
71    num = num + (num >>> 8);
72    num = num + (num >>> 16);
73    return num & 0x3f;
74}
75
Not Sure What to Study? Take the 2-min Quiz

What is an advantages of top-down dynamic programming vs bottom-up dynamic programming?

Time and Space Complexity

Time Complexity

The time complexity of the provided algorithm can be analyzed based on its two main loops and the operations within them.

  • The first loop iterates over all possible combinations of nums elements (from 1 to 2^n - 1). It does so by generating a bitmask i for each combination, where n is the length of the nums list. For each bitmask, the algorithm computes the size of the subset (using .bit_count()), and if the subset is of size m, it checks whether it can form a valid group without any duplications and calculates the incompatibility. This operation would take O(n) for each subset, but since we only consider subsets of size m, the check is reduced to O(m) for each subset, and the total time for this loop would be O(2^n * m).

  • The second loop also iterates over all possible combinations (from 0 to 2^n - 1) of elements in nums. For each combination, it performs a series of operations including generation of new masks and a nested loop over these masks. The total number of iterations for the nested loop is O(3^n), as for each bitmask i, it iterates over all submasks j whose bits are a subset of i. Each operation inside the nested loop is constant or linear in terms of the number of elements in nums, so we consider it O(n).

Combining both loops, the overall time complexity is O(2^n * m + 2^n * 3^n * n), and since 2^n and n are dominated by 3^n for large n, the time complexity simplifies to O(3^n * n).

Space Complexity

The space complexity is determined by the storage requirements of the algorithm.

  • The array g requires O(2^n) space as it potentially stores information for every combination of elements in nums.
  • The array f also requires O(2^n) space for the dynamic programming approach storing the minimum incompatibility for each combination.
  • Auxiliary space is also used for the set s and the mask mask with up to O(m) additional space, where m is the maximum subset size determined by n // k.

Thus, the overall space complexity of the algorithm is O(2^n + m), which simplifies to O(2^n) since m is less than 2^n for all n > 1.

Learn more about how to find time and space complexity quickly using problem constraints.

Fast Track Your Learning with Our Quick Skills Quiz:

In a binary min heap, the maximum element can be found in:


Recommended Readings


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

←
↑TA đŸ‘šâ€đŸ«