Skip to content

Practice Minimum Space Wasted From Packaging

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1889
LeetCode Problem

Minimum Space Wasted From Packaging

Problem Statement

You have n packages that you are trying to place in boxes, one package in each box. There are m suppliers that each produce boxes of different sizes (with infinite supply). A package can be placed in a box if the size of the package is less than or equal to the size of the box. The package sizes are given as an integer array packages, where packages[i] is the size of the ith package. The suppliers are given as a 2D integer array boxes, where boxes[j] is an array of box sizes that the jth supplier produces. You want to choose a single supplier and use boxes from them such that the total wasted space is minimized. For each package in a box, we define the space wasted to be size of the box - size of the package. The total wasted space is the sum of the space wasted in all the boxes. Return the minimum total wasted space by choosing the box supplier optimally, or -1 if it is impossible to fit all the packages inside boxes. Since the answer may be large, return it modulo 10^9 + 7.

Source: Minimum Space Wasted From Packaging on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

# Example 3

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

Constraints

n == packages.length
m == boxes.length
1 <= n <= 10^5
1 <= m <= 10^5
1 <= packages[i] <= 10^5
1 <= boxes[j].length <= 10^5
1 <= boxes[j][k] <= 10^5
sum(boxes[j].length) <= 10^5
The elements in boxes[j] are distinct.

Related LeetCode Problems

Practice more problems similar to Minimum Space Wasted From Packaging.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.