Minimum Space Wasted From Packaging
Updated
Problem statement
You have packages with positive integer sizes given by packages. Each package must go into its own box whose size is at least the package size.
There are suppliers described by boxes. The row boxes[j] lists the distinct box sizes offered by supplier j. Every offered size has an unlimited supply of boxes.
Choose exactly one supplier to pack every package. For a package of size p placed into a box of size b, the wasted space is b - p. Minimize the sum of wasted space over all packages by choosing the supplier and a box for each package.
Return -1 if no single supplier can pack all packages. Otherwise, return the minimum total wasted space modulo 1,000,000,007. Find the minimum using the true total before applying the modulo. Return the amount of wasted space, not a supplier index.
Complete minWastedSpace(packages, boxes).
Function
minWastedSpace(packages: int[], boxes: int[][]) → intExamples
Example 1
packages = [2,3,5]boxes = [[4,8],[2,8]]return = 6The first supplier uses two boxes of size 4 and one of size 8. Waste is (4-2)+(4-3)+(8-5)=6.
Example 2
packages = [2,3,5]boxes = [[1,4],[2,3],[3,4]]return = -1Every supplier has maximum box size below 5, so the largest package cannot be packed.
Example 3
packages = [3,5,8,10,11,12]boxes = [[12],[11,9],[10,5,14]]return = 9The third supplier uses sizes 5,5,10,10,14,14. Waste is 2+0+2+0+3+2=9.
Constraints
1 <= packages.length <= 10^5.1 <= boxes.length <= 10^5.1 <= packages[i] <= 10^5.1 <= boxes[j].length <= 10^5.1 <= boxes[j][k] <= 10^5.- The sum of all supplier row lengths is at most
10^5. - Each supplier row contains distinct sizes. Package sizes may repeat, and neither input is guaranteed to be sorted.
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Chain of CommandOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
Similar questions at other companies
- Minimum Cost to Make Array EqualWalmart · OAWalmart interview questions
- Use Minimum TokensAmazon · OA
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Minimum Operations to Make All Array Elements EqualIBM · OAIBM interview questions
- Calculating QuantilesIndeed · OAIndeed interview questions
- Count Lamps Lighting Each PointCapital One · OACapital One interview questions
- Maintain a Consistent-Hashing RingCohesity · ONSITE INTERVIEW
- Maintain a Consistent-Hashing RingApple · PHONE SCREENApple interview questions
- Average Prices from Broker QuotesAkuna Capital · PHONE SCREENAkuna Capital interview questions