1889. Minimum Space Wasted From Packaging LeetCode Solution
In this guide, you will get 1889. Minimum Space Wasted From Packaging LeetCode Solution with the best time and space complexity. The solution to Minimum Space Wasted From Packaging problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Space Wasted From Packaging solution in C++
- Minimum Space Wasted From Packaging solution in Java
- Minimum Space Wasted From Packaging solution in Python
- Additional Resources

Problem Statement of Minimum Space Wasted From Packaging
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.
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 109 + 7.
Example 1:
Input: packages = [2,3,5], boxes = [[4,8],[2,8]]
Output: 6
Explanation: It is optimal to choose the first supplier, using two size-4 boxes and one size-8 box.
The total waste is (4-2) + (4-3) + (8-5) = 6.
Example 2:
Input: packages = [2,3,5], boxes = [[1,4],[2,3],[3,4]]
Output: -1
Explanation: There is no box that the package of size 5 can fit in.
Example 3:
Input: packages = [3,5,8,10,11,12], boxes = [[12],[11,9],[10,5,14]]
Output: 9
Explanation: It is optimal to choose the third supplier, using two size-5 boxes, two size-10 boxes, and two size-14 boxes.
The total waste is (5-3) + (5-5) + (10-8) + (10-10) + (14-11) + (14-12) = 9.
Constraints:
n == packages.length
m == boxes.length
1 <= n <= 105
1 <= m <= 105
1 <= packages[i] <= 105
1 <= boxes[j].length <= 105
1 <= boxes[j][k] <= 105
sum(boxes[j].length) <= 105
The elements in boxes[j] are distinct.
Complexity Analysis
- Time Complexity: O(\texttt{sort}(n) + \texttt{sort}(m) + m\log n), where n = |\texttt{packages}| and m = |\texttt{boxes}|
- Space Complexity: O(\texttt{sort}(n) + \texttt{sort}(m))
1889. Minimum Space Wasted From Packaging LeetCode Solution in C++
class Solution {
public:
int minWastedSpace(vector<int>& packages, vector<vector<int>>& boxes) {
constexpr int kMod = 1'000'000'007;
constexpr long kInf = 1e11;
const long packagesSum = accumulate(packages.begin(), packages.end(), 0L);
long minBoxesSum = kInf;
ranges::sort(packages);
for (vector<int>& box : boxes) {
ranges::sort(box);
if (box.back() < packages.back())
continue;
long accu = 0;
long i = 0;
for (const int b : box) {
const long j = firstGreaterEqual(packages, b + 1);
accu += b * (j - i);
i = j;
}
minBoxesSum = min(minBoxesSum, accu);
}
return minBoxesSum == kInf ? -1 : (minBoxesSum - packagesSum) % kMod;
}
private:
int firstGreaterEqual(const vector<int>& arr, int target) {
return ranges::lower_bound(arr, target) - arr.begin();
}
};
/* code provided by PROGIEZ */
1889. Minimum Space Wasted From Packaging LeetCode Solution in Java
class Solution {
public int minWastedSpace(int[] packages, int[][] boxes) {
final int kMod = 1_000_000_007;
final long kInf = (long) 1e11;
final long packagesSum = Arrays.stream(packages).mapToLong(p -> p).sum();
long minBoxesSum = kInf;
Arrays.sort(packages);
for (int[] box : boxes) {
Arrays.sort(box);
if (box[box.length - 1] < packages[packages.length - 1])
continue;
long accu = 0;
long i = 0;
for (final int b : box) {
final long j = firstGreaterEqual(packages, b + 1);
accu += b * (j - i);
i = j;
}
minBoxesSum = Math.min(minBoxesSum, accu);
}
return minBoxesSum == kInf ? -1 : (int) ((minBoxesSum - packagesSum) % kMod);
}
private int firstGreaterEqual(int[] arr, int target) {
int l = 0;
int r = arr.length;
while (l < r) {
final int m = (l + r) / 2;
if (arr[m] >= target)
r = m;
else
l = m + 1;
}
return l;
}
}
// code provided by PROGIEZ
1889. Minimum Space Wasted From Packaging LeetCode Solution in Python
class Solution:
def minWastedSpace(self, packages: list[int], boxes: list[list[int]]) -> int:
ans = math.inf
packages.sort()
for box in boxes:
box.sort()
if box[-1] < packages[-1]:
continue
accu = 0
i = 0
for b in box:
j = bisect.bisect(packages, b, i)
accu += b * (j - i)
i = j
ans = min(ans, accu)
return -1 if ans == math.inf else (ans - sum(packages)) % int(1e9 + 7)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.