bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1889. Minimum Space Wasted From Packaging

1889. Minimum Space Wasted From Packaging

Leetcode Solutions

Binary Search and Sorting

  1. Sort the packages array.
  2. Initialize minWastedSpace to a very large number.
  3. Iterate over each supplier's boxes array. a. Sort the boxes array. b. If the largest box is smaller than the largest package, continue to the next supplier. c. Initialize totalBoxSize to 0 and index to 0. d. Iterate over each boxSize in boxes: i. Use binary search to find the rightmost package that can fit into boxSize. ii. Update totalBoxSize by adding the product of boxSize and the number of packages that fit. iii. Update index to the position after the last package that fits. e. Calculate the wasted space for this supplier and update minWastedSpace if it's smaller.
  4. If minWastedSpace is still very large, return -1, else return minWastedSpace modulo 10^9 + 7.
UML Thumbnail

Greedy Approach with Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...