Minimum Space Wasted From Packaging - Problem

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 i-th package. The suppliers are given as a 2D integer array boxes, where boxes[j] is an array of box sizes that the j-th 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.

For example: if you have to fit packages with sizes [2,3,5] and the supplier offers boxes of sizes [4,8], you can fit the packages of size-2 and size-3 into two boxes of size-4 and the package with size-5 into a box of size-8. This would result in a waste of (4-2) + (4-3) + (8-5) = 6.

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.

Input & Output

Example 1 — Basic Case
$ Input: packages = [2,3,5], boxes = [[4,8],[2,3,4],[1,4]]
Output: 6
💡 Note: Best supplier is [4,8]: package 2→box 4 (waste 2), package 3→box 4 (waste 1), package 5→box 8 (waste 3). Total waste = 6.
Example 2 — Impossible Case
$ Input: packages = [2,3,5], boxes = [[1,4],[2,3]]
Output: -1
💡 Note: No supplier can fit package 5. First supplier's largest box is 4, second supplier's largest is 3, both < 5.
Example 3 — Perfect Fit
$ Input: packages = [3,5], boxes = [[3,5]]
Output: 0
💡 Note: Perfect match: package 3→box 3 (waste 0), package 5→box 5 (waste 0). Total waste = 0.

Constraints

  • 1 ≤ packages.length ≤ 105
  • 1 ≤ boxes.length ≤ 105
  • 1 ≤ boxes[j].length ≤ 105
  • 1 ≤ packages[i], boxes[j][k] ≤ 109
  • Sum of boxes[j].length ≤ 105

Visualization

Tap to expand
Minimum Space Wasted From Packaging INPUT packages = [2, 3, 5] 2 3 5 Suppliers (boxes): S0: 4 8 S1: 2 3 4 S2: 1 4 Package fits if: pkg_size <= box_size Waste = box - package Goal: Minimize total waste ALGORITHM STEPS 1 Sort packages [2,3,5] (already sorted) 2 Sort each supplier's boxes Binary search optimization 3 Try each supplier Use prefix sums for waste 4 Calculate waste per supplier Return minimum Waste Calculation: S0: max=8 < 5? NO (4-2)+(4-3)+(8-5)=2+1+3=6 S1: (2-2)+(3-3)+(4-5)? NO 4<5, can't fit pkg 5! S2: max=4 < 5? can't fit! FINAL RESULT Optimal: Supplier 0 Package to Box Assignment: 2 --> 4 waste=2 3 --> 4 waste=1 5 --> 8 waste=3 Total = 2 + 1 + 3 Output: 6 OK - Minimum waste found! S1, S2 cannot fit all pkgs S0 is only valid option Key Insight: Sort packages and use binary search to find the smallest box that fits each package. Use prefix sums to calculate total waste efficiently: waste = sum(boxes used) - sum(packages). Time: O(n log n + m * k log k) where n=packages, m=suppliers, k=avg boxes per supplier. TutorialsPoint - Minimum Space Wasted From Packaging | Optimal Solution
Asked in
Amazon 45 Google 32 Microsoft 28
23.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen