KatInterview

Pick the Bottle Kit With the Least Leftover Volume

Heaps, Sorting & SearchAuto-graded · 21 test cases
HardAuto-gradedHeaps, Sorting & Searchbinary-searchprefix-sumssorting

Last 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:

  1. Exactly one bottle is used for the request.
  2. The bottle used is the one with the smallest capacity that is at least v. If a bottle with capacity exactly v exists, it is that one and the leftover is 0.
  3. 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 the i-th request.
  • num_container_sets: the number of kits, indexed 0 to num_container_sets - 1.
  • containers: a list of pairs [set_index, capacity]. Each pair says that kit set_index contains a bottle of that capacity. 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^5
  • 1 <= num_container_sets <= 10^5
  • 1 <= len(containers) <= 2 * 10^5
  • 1 <= requirements[i] <= 10^9
  • 1 <= 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.