Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to distribute repeating integers in Python
When distributing items to customers, we need to ensure each customer gets exactly the quantity they ordered, with all items being identical. This problem involves checking if we can satisfy all customer demands using available items.
The approach uses backtracking with frequency counting. We count how many times each frequency appears, then try to allocate items to customers in descending order of demand.
Algorithm Steps
Here's how the solution works ?
Count frequency of each unique number in the array
Count frequency of frequencies (how many numbers appear X times)
Sort customer quantities in descending order for optimization
Use backtracking to try allocating each quantity demand
Complete Solution
from collections import Counter
def solve(nums, quantity):
def util(i, cntr):
if i == len(quantity):
return True
temp_counter = cntr.copy()
for cnt in cntr:
if cnt >= quantity[i]:
temp_counter[cnt] -= 1
if temp_counter[cnt] == 0:
temp_counter.pop(cnt)
rem = cnt - quantity[i]
if rem > 0:
temp_counter[rem] = temp_counter.get(rem, 0) + 1
if util(i + 1, temp_counter):
return True
# Backtrack
if rem > 0:
temp_counter[rem] -= 1
if temp_counter[rem] == 0:
temp_counter.pop(rem)
temp_counter[cnt] = temp_counter.get(cnt, 0) + 1
return False
# Count frequency of each number
freq = Counter(nums)
# Count frequency of frequencies
freq_count = Counter(freq.values())
# Sort quantities in descending order for better pruning
quantity.sort(reverse=True)
return util(0, freq_count)
# Test the solution
nums = [5, 1, 2, 2, 3, 4, 4, 3, 3]
quantity = [2, 2, 3]
print(solve(nums, quantity))
True
How It Works
Let's trace through the example ?
from collections import Counter
nums = [5, 1, 2, 2, 3, 4, 4, 3, 3]
quantity = [2, 2, 3]
# Step 1: Count frequency of each number
freq = Counter(nums)
print("Number frequencies:", dict(freq))
# Step 2: Count frequency of frequencies
freq_count = Counter(freq.values())
print("Frequency of frequencies:", dict(freq_count))
# Step 3: Sort quantities
quantity.sort(reverse=True)
print("Sorted quantities:", quantity)
Number frequencies: {5: 1, 1: 1, 2: 2, 3: 3, 4: 2}
Frequency of frequencies: {1: 2, 2: 2, 3: 1}
Sorted quantities: [3, 2, 2]
Step-by-Step Allocation
The algorithm tries to satisfy each customer in order ?
Customer 1 (needs 3): Can use frequency 3 (number 3 appears 3 times)
Customer 2 (needs 2): Can use frequency 2 (number 2 or 4 appears 2 times)
Customer 3 (needs 2): Can use remaining frequency 2
Key Points
We work with frequency counts rather than actual numbers for efficiency
Sorting quantities in descending order improves pruning
Backtracking explores all possible allocations
Time complexity is exponential but optimized for the constraint of at most 50 unique values
Conclusion
This solution uses backtracking with frequency optimization to efficiently check if items can be distributed to satisfy all customer demands. The key insight is counting frequencies of frequencies to reduce the search space.
