Pick the Bottle Kit With the Least Leftover Volume
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Choose the bottle kit that serves every dose request with minimal total leftover space, using sorting, prefix sums and binary search to stay efficient on large inputs.
A lab must ship a batch of liquid doses. Each dose request specifies a volume. The lab owns several bottle kits and must choose exactly one kit, then fill every request using bottles taken only from that kit.
Every kit contains bottles of certain capacities, and each capacity may be used as many times as needed.
Rules for serving a request
For a single request of volume v and a chosen kit:
- Exactly one bottle is used for the request.
- The bottle used is the one with the smallest capacity that is at least
v. If a bottle with capacity exactlyvexists, it is that one and the leftover is 0. - The leftover for the request is
capacity - v.
If a kit has no bottle with capacity >= v for some request, that kit is invalid and cannot be chosen.
Task
Among all kits, consider only valid ones (those that can serve every request). For each, the total leftover is the sum of leftovers over all requests. Return the index of the kit with the smallest total leftover. If several kits tie, return the smallest index. If no kit is valid, return -1.
Input
requirements: a list of positive integers;requirements[i]is the volume of thei-th request.num_container_sets: the number of kits, indexed0tonum_container_sets - 1.containers: a list of pairs[set_index, capacity]. Each pair says that kitset_indexcontains a bottle of thatcapacity. Pairs for the same kit appear together, kits appear in increasing index order, and within a kit capacities are in non-decreasing order. Your solution should not depend on this ordering, and duplicate capacities may occur. A kit with no pairs has no bottles and is invalid.
Output
An integer: the 0-based index of the best kit, or -1 if none can serve all requests.
Constraints
1 <= len(requirements) <= 10^51 <= num_container_sets <= 10^51 <= len(containers) <= 2 * 10^51 <= requirements[i] <= 10^91 <= capacity <= 10^9- An approach that costs
O(len(requirements) * len(containers))will be too slow.
Examples
Example 1
Input: choose_bottle_kit([4, 6, 6, 7], 3, [[0, 3], [0, 5], [0, 7], [1, 6], [1, 8], [1, 9], [2, 3], [2, 5], [2, 6]])
Output: 0
Hints
Hint 1
Serving a request with an exact match or else the next larger bottle is the same as picking the smallest capacity that is at least the request volume.
Hint 2
Flip the viewpoint: for a kit with sorted distinct capacities c1 < c2 < ..., all requests in the range (c_{i-1}, c_i] use capacity c_i. You can process kits bottle by bottle instead of request by request.
Hint 3
Sort the requests once and build prefix sums. Two binary searches per bottle give the count and the sum of requests in its range, so the leftover for that range is c * count - sum. A kit is valid only if the largest request is at most its largest capacity.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
"Exact match, otherwise the next larger bottle" means each request uses the smallest capacity that is at least its volume. So for a kit with sorted distinct capacities c1 < c2 < ... < ck, every request with volume in (c_{i-1}, c_i] (taking c0 = 0) is served by c_i. Requests larger than ck cannot be served, which makes the kit invalid.
Instead of looking at each request for each kit, we look at each bottle and handle its whole range of requests at once:
- Sort the requests once and build prefix sums.
- For a bottle of capacity
cwith previous capacityp, two binary searches (bisect_right) give the index range of requests in(p, c]. The count iscntand the volume sum comes from the prefix sums. - The leftover for that range is
c * cnt - sum.
For each kit, in index order:
- Group and sort its capacities.
- Walk through the distinct capacities, adding each range's leftover to the total.
- The kit is valid only if the largest request is at most the largest capacity (an empty kit is invalid).
- Update the best answer only when the total is strictly smaller, so the earliest index wins ties.
Return the best index, or -1 if no kit was valid.
Why it is correct
- The ranges
(c_{i-1}, c_i]partition(0, ck], so each servable request is counted in exactly one range, the one for its smallest sufficient capacity. - An exact match falls into the range ending at that capacity, since
bisect_rightincludes equal values, and contributes leftover 0. - Leftover in a range is the sum of
c - v, which equalsc * cnt - sum. - Requests above the largest capacity are never counted, but the explicit validity check rejects such kits.
- Scanning kits in increasing order with a strict comparison keeps the smallest index on ties.
Complexity
Let n be the number of requests and T the number of bottle entries.
- Time:
O(n log n + T log T + T log n). - Space:
O(n + T + num_container_sets).
Reference solution
from bisect import bisect_right
from typing import List
def choose_bottle_kit(requirements: List[int], num_container_sets: int, containers: List[List[int]]) -> int:
orders = sorted(requirements)
prefix = [0]
for amount in orders:
prefix.append(prefix[-1] + amount)
kits = [[] for _ in range(num_container_sets)]
for set_index, capacity in containers:
kits[set_index].append(capacity)
best_index = -1
best_waste = None
for set_index, capacities in enumerate(kits):
capacities.sort()
total_waste = 0
previous = 0
for capacity in capacities:
if capacity == previous:
continue
left = bisect_right(orders, previous)
right = bisect_right(orders, capacity)
total_waste += capacity * (right - left) - (prefix[right] - prefix[left])
previous = capacity
valid = bool(orders) and orders[-1] <= previous
if valid and (best_waste is None or total_waste < best_waste):
best_waste = total_waste
best_index = set_index
return best_index