Advertisement

Minimum Space Wasted From Packaging - LeetCode 1889 Solution

Minimum Space Wasted From Packaging - Complete Solution Guide

Minimum Space Wasted From Packaging is LeetCode problem 1889, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 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 su

Detailed Explanation

The problem requires finding the minimum wasted space when fitting packages into boxes provided by different suppliers. Each package must go into exactly one box. Wasted space is defined as the difference between the box size and the package size. We need to choose one supplier such that the total wasted space across all packages is minimized. If no supplier can accommodate all packages, we return -1. The final result should be returned modulo 10^9 + 7.

Solution Approach

The solution iterates through each supplier, sorts their boxes, and then attempts to fit the packages into the boxes. For each supplier, the packages are iterated through (implicitly via binary search). The binary search identifies the optimal box size for fitting the packages while calculating the total wasted space. The minimum wasted space across all suppliers is tracked and updated, returning -1 if no supplier can fit all packages. The final result is modulo 10^9 + 7.

Step-by-Step Algorithm

  1. Step 1: Sort the `packages` array to enable efficient searching.
  2. Step 2: Iterate through each `supplier_boxes` array in the `boxes` array.
  3. Step 3: Sort the `supplier_boxes` array.
  4. Step 4: Check if the largest box size in `supplier_boxes` is smaller than the largest package size in `packages`. If so, skip to the next supplier (optimization).
  5. Step 5: Initialize `current_total_box_space` and `last_package_idx` to 0.
  6. Step 6: Iterate through each `box_size` in `supplier_boxes`.
  7. Step 7: Use binary search (bisect_right in Python, binarySearchRight in Java, upper_bound in C++) to find the index of the first package that cannot fit into `box_size` starting from `last_package_idx`.
  8. Step 8: Calculate the number of packages that can fit in the current `box_size` as `idx - last_package_idx`. Update `current_total_box_space` by adding `(num_packages_for_this_box * box_size)`.
  9. Step 9: Update `last_package_idx` to `idx`.
  10. Step 10: If all packages have been assigned a box (`last_package_idx == num_packages`), break out of the inner loop.
  11. Step 11: Calculate `current_waste` as `current_total_box_space - total_package_size`.
  12. Step 12: Update `min_total_waste` with the minimum between the current `min_total_waste` and `current_waste`.
  13. Step 13: After iterating through all suppliers, if `min_total_waste` remains at its initial value (infinity), return -1. Otherwise, return `min_total_waste % MOD`.

Key Insights

  • Insight 1: Sorting both packages and boxes allows for efficient searching (binary search) to find the smallest box that can fit a particular package.
  • Insight 2: Iterating through each supplier's boxes and calculating the wasted space independently allows us to compare the results and find the minimum wasted space.
  • Insight 3: Checking if the largest box of a supplier is smaller than the largest package is crucial for early exit, as it indicates that supplier cannot accommodate all packages.

Complexity Analysis

Time Complexity: O(n log n + m * (k log k + n log n))

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search, Sorting, Prefix Sum.

Companies

Asked at: Two Sigma.