FastPrepMinimum Space Wasted From Packaging

Minimum Space Wasted From Packaging

IMC Trading logoIMC Trading● HardFULLTIMEOA
Learn

Problem statement

You have packages with positive integer sizes given by packages. Each package must go into its own box whose size is at least the package size.

There are suppliers described by boxes. The row boxes[j] lists the distinct box sizes offered by supplier j. Every offered size has an unlimited supply of boxes.

Choose exactly one supplier to pack every package. For a package of size p placed into a box of size b, the wasted space is b - p. Minimize the sum of wasted space over all packages by choosing the supplier and a box for each package.

Return -1 if no single supplier can pack all packages. Otherwise, return the minimum total wasted space modulo 1,000,000,007. Find the minimum using the true total before applying the modulo. Return the amount of wasted space, not a supplier index.

Complete minWastedSpace(packages, boxes).

Function

minWastedSpace(packages: int[], boxes: int[][]) → int

Examples

Example 1

packages = [2,3,5]boxes = [[4,8],[2,8]]return = 6

The first supplier uses two boxes of size 4 and one of size 8. Waste is (4-2)+(4-3)+(8-5)=6.

Example 2

packages = [2,3,5]boxes = [[1,4],[2,3],[3,4]]return = -1

Every supplier has maximum box size below 5, so the largest package cannot be packed.

Example 3

packages = [3,5,8,10,11,12]boxes = [[12],[11,9],[10,5,14]]return = 9

The third supplier uses sizes 5,5,10,10,14,14. Waste is 2+0+2+0+3+2=9.

Constraints

  • 1 <= packages.length <= 10^5.
  • 1 <= boxes.length <= 10^5.
  • 1 <= packages[i] <= 10^5.
  • 1 <= boxes[j].length <= 10^5.
  • 1 <= boxes[j][k] <= 10^5.
  • The sum of all supplier row lengths is at most 10^5.
  • Each supplier row contains distinct sizes. Package sizes may repeat, and neither input is guaranteed to be sorted.

More IMC Trading problems

See IMC Trading hiring insights
public int minWastedSpace(int[] packages, int[][] boxes) {
    // Write your solution here.
}
packages[2,3,5]
boxes[[4,8],[2,8]]
expected6
Checking account…