Distribute Repeating Integers - Problem

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

example_1.py โ€” Basic Distribution
$ Input: nums = [1,2,3,4,4,4,4], quantity = [2,3,1]
โ€บ Output: true
๐Ÿ’ก Note: Customer 0 gets 2 items of type '4', customer 1 gets 3 items of type '4', customer 2 gets 1 item of type '1'. Total: 2+3=5 items of type '4' (we have 4 available - wait, this should be false!). Let me recalculate: Customer 0 gets 2 fours, customer 1 gets 2 fours, customer 2 gets 1 of any other type.
example_2.py โ€” Impossible Distribution
$ Input: nums = [1,2,3,3], quantity = [2,2]
โ€บ Output: false
๐Ÿ’ก Note: We need 2 identical items for each of 2 customers (4 items total). We have 2 threes, 1 two, and 1 one. We can give customer 0 the 2 threes, but customer 1 needs 2 identical items and we don't have 2 of any remaining type.
example_3.py โ€” Edge Case Single Customer
$ Input: nums = [1,1,2,2], quantity = [2]
โ€บ Output: true
๐Ÿ’ก Note: Single customer needs 2 identical items. We can give them either the 2 ones or the 2 twos.

Visualization

Tap to expand
๐Ÿช Cookie Factory Distribution ProcessInventoryChocolate: 7Vanilla: 4Strawberry: 3Customer OrdersCustomer 1: 5 cookiesCustomer 2: 3 cookiesCustomer 3: 2 cookiesAssignment Resultโœ… Customer 1 โ†’ Chocolate (5)โœ… Customer 2 โ†’ Vanilla (3)โœ… Customer 3 โ†’ Chocolate (2)Remaining: Choc(0), Van(1), Str(3)Backtracking Decision TreeStartโœ“ Valid Pathโœ— Backtrack
Understanding the Visualization
1
Inventory Check
Count how many cookies of each type we have in the warehouse
2
Order Prioritization
Sort customer orders by size (largest first) to detect impossible scenarios quickly
3
Smart Assignment
Try assigning the largest order to each available cookie type that has sufficient inventory
4
Recursive Distribution
Once an assignment is made, recursively solve for the remaining customers
5
Backtrack on Failure
If a path fails, undo the assignment and try the next available cookie type
Key Takeaway
๐ŸŽฏ Key Insight: By sorting orders in descending size and using smart pruning, we can efficiently explore the assignment space without checking every possible combination. The magic happens when we realize that with at most 50 unique items and at most 10 customers, backtracking becomes very manageable!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k^m)

Same worst-case as basic backtracking, but much better average case due to pruning

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

Recursion stack plus space for counting

n
2n
โœ“ Linear Space

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
Asked in
Amazon 45 Google 38 Meta 29 Microsoft 22
28.5K Views
Medium-High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen