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:
- The
ithcustomer gets exactlyquantity[i]integers, - The integers the
ithcustomer gets are all equal, and - Every customer is satisfied.
Return true if it is possible to distribute nums according to the above conditions.
Input: nums = [1,2,3,4], quantity = [2] Output: false Explanation: The 0th customer cannot be given two different integers.
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.
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].
n == nums.length1 <= n <= 1051 <= nums[i] <= 1000m == quantity.length1 <= m <= 101 <= quantity[i] <= 105- There are at most
50unique values innums.
class Solution:
def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
def dfs(i: int) -> bool:
if i == len(quantity):
return True
for j in range(len(count)):
if count[j] >= quantity[i] and (j == 0 or count[j] != count[j - 1]):
count[j] -= quantity[i]
if dfs(i + 1):
return True
count[j] += quantity[i]
return False
count = sorted(collections.Counter(nums).values())[-len(quantity):]
if count[-1] < max(quantity) or sum(count) < sum(quantity):
return False
return dfs(0)