You are given an array of n
integers, nums
, where there are at most 50
unique values in the array. You are also given an array of m
customer order quantities, quantity
, where quantity[i]
is the amount of integers the ith
customer ordered. Determine if it is possible to distribute nums
such that:
ith
customer gets exactly quantity[i]
integers,ith
customer gets are all equal, andReturn true
if it is possible to distribute nums
according to the above conditions.
Example 1:
Input: nums = [1,2,3,4], quantity = [2] Output: false Explanation: The 0th customer cannot be given two different integers.
Example 2:
Input: nums = [1,2,3,3], quantity = [2] Output: true Explanation: The 0th customer is given [3,3]. The integers [1,2] are not used.
Example 3:
Input: nums = [1,1,2,2], quantity = [2,2] Output: true Explanation: The 0th customer is given [1,1], and the 1st customer is given [2,2].
Constraints:
n == nums.length
1 <= n <= 105
1 <= nums[i] <= 1000
m == quantity.length
1 <= m <= 10
1 <= quantity[i] <= 105
50
unique values in nums
.This problem asks whether we can distribute a given array of integers (nums
) to satisfy a set of customer orders (quantity
), ensuring each customer receives the exact quantity of identical integers. The solution uses dynamic programming with bit manipulation for efficient subset management.
Approach:
Count Occurrences: First, count the occurrences of each unique integer in nums
using a hash map (or Counter
in Python). This reduces the problem to working with counts instead of individual numbers.
State Representation: The core idea is to represent subsets of customer orders using bitmasks. If there are m
customers, a bitmask of m
bits can represent all possible subsets. Each bit corresponds to a customer; a '1' means the customer is included in the subset, and '0' means they're not.
Preprocessing Subset Sums: Calculate the sum of quantities for each subset. This precomputation avoids redundant calculations in the dynamic programming step.
Dynamic Programming: The dynamic programming table f[i][j]
represents whether it's possible to satisfy the customer orders in subset j
using the counts of the first i
unique integers from nums
.
f[i][0]
is always True
because an empty subset is always satisfiable.f[i][j]
, we iterate through all subsets k
that are subsets of j
. If we can satisfy subset j XOR k
using the first i-1
integers (f[i-1][j XOR k]
), and the count of the i
-th integer is greater than or equal to the sum of quantities in subset k
(s[k]
), then f[i][j]
is True
.Final Result: f[n-1][(1 << m) - 1]
(where n
is the number of unique integers) indicates whether all customer orders can be satisfied using all unique integers.
Time Complexity: O(n * 3m), where n is the number of unique integers and m is the number of customers. The dominant factor is the nested loops iterating through subsets. Each bit in the mask can be either 0 or 1, or part of a larger subset, leading to 3m complexity.
Space Complexity: O(n * 2m) to store the dynamic programming table.
Code Explanation (Python):
class Solution:
def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
m = len(quantity)
s = [0] * (1 << m) # Precompute subset sums
for i in range(1, 1 << m):
for j in range(m):
if i >> j & 1:
s[i] = s[i ^ (1 << j)] + quantity[j]
break
cnt = Counter(nums) # Count occurrences of each integer
arr = list(cnt.values()) # Array of counts
n = len(arr)
f = [[False] * (1 << m) for _ in range(n)] # DP table
for i in range(n):
f[i][0] = True
for i, x in enumerate(arr):
for j in range(1, 1 << m):
k = j
while k:
ok1 = j == k if i == 0 else f[i - 1][j ^ k]
ok2 = s[k] <= x
if ok1 and ok2:
f[i][j] = True
break
k = (k - 1) & j
return f[-1][-1]
The other languages (Java, C++, Go, TypeScript) follow a similar structure and logic, adapting the syntax and data structures to their respective language features. The core algorithm remains consistent across all implementations.