Untitled
unknown
plain_text
9 months ago
1.4 kB
13
Indexable
def chooseContainers(requirements, numContainerSets, containers):
# Group containers by set ID
container_sets = {}
for container in containers:
set_id = container[0]
size = container[1]
if set_id not in container_sets:
container_sets[set_id] = []
container_sets[set_id].append(size)
# Sort each set for efficient searching
for set_id in container_sets:
container_sets[set_id].sort()
min_waste = float('inf')
best_set = -1
# Evaluate each container set
for set_id in range(numContainerSets):
if set_id not in container_sets:
continue
sizes = container_sets[set_id]
total_waste = 0
valid = True
for req in requirements:
# Find smallest container >= req using binary search
idx = bisect.bisect_left(sizes, req)
# Check if we found a valid container
if idx < len(sizes) and sizes[idx] >= req:
total_waste += sizes[idx] - req
else:
# No container is large enough
valid = False
break
if valid:
if total_waste < min_waste:
min_waste = total_waste
best_set = set_id
return best_setEditor is loading...
Leave a Comment