Imagine you're running a warehouse distribution center with a unique challenge! You have an array of n integers representing your inventory, where each integer represents a specific product type. The key constraint is that there are at most 50 unique product types in your entire inventory.
You receive m customer orders represented by the quantity array, where quantity[i] indicates how many items the i-th customer wants to order. Here's the catch: each customer must receive exactly the same product type for their entire order.
Your mission is to determine if you can satisfy all customers simultaneously by distributing your inventory such that:
- The i-th customer gets exactly
quantity[i]items - All items given to the i-th customer are identical (same product type)
- Every single customer is completely satisfied
Return true if this distribution is possible, false otherwise.
Example: If you have inventory [1,2,3,4,4,4,4] and orders [2,3,1], you could give customer 0 two items of type '4', customer 1 three items of type '4', and customer 2 one item of any remaining type.
Input & Output
Visualization
Time & Space Complexity
Same worst-case as basic backtracking, but much better average case due to pruning
Recursion stack plus space for counting
Constraints
- n == nums.length
- 1 โค n โค 105
- 1 โค nums[i] โค 1000
- m == quantity.length
- 1 โค m โค 10
- 1 โค quantity[i] โค 105
- At most 50 unique values in nums