[LeetCode] 1889. Minimum Space Wasted From Packaging
Posted CNoodle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 1889. 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.
装包裹的最小浪费空间。
给你 n 个包裹,你需要把它们装在箱子里,每个箱子装一个包裹。总共有 m 个供应商提供 不同尺寸 的箱子(每个规格都有无数个箱子)。如果一个包裹的尺寸 小于等于 一个箱子的尺寸,那么这个包裹就可以放入这个箱子之中。
包裹的尺寸用一个整数数组 packages 表示,其中 packages[i] 是第 i 个包裹的尺寸。供应商用二维数组 boxes 表示,其中 boxes[j] 是第 j 个供应商提供的所有箱子尺寸的数组。
你想要选择 一个供应商 并只使用该供应商提供的箱子,使得 总浪费空间最小 。对于每个装了包裹的箱子,我们定义 浪费的 空间等于 箱子的尺寸 - 包裹的尺寸 。总浪费空间 为 所有 箱子中浪费空间的总和。
比方说,如果你想要用尺寸数组为 [4,8] 的箱子装下尺寸为 [2,3,5] 的包裹,你可以将尺寸为 2 和 3 的两个包裹装入两个尺寸为 4 的箱子中,同时把尺寸为 5 的包裹装入尺寸为 8 的箱子中。总浪费空间为 (4-2) + (4-3) + (8-5) = 6 。
请你选择 最优 箱子供应商,使得 总浪费空间最小 。如果 无法 将所有包裹放入箱子中,请你返回 -1 。由于答案可能会 很大 ,请返回它对 109 + 7 取余 的结果。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-space-wasted-from-packaging
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是二分法。这道题的 input 给的是有若干供应商,每个供应商可以提供几种不同 size 的盒子,请你判断是否存在一个供应商,他提供的盒子一定能装下你所有的 package,同时浪费的空间最少。既然是二分法做,那么首先我们需要对两个input数组排序,确保供应商提供的盒子尺寸是有序的。然后我们用一个 for 循环去遍历每个供应商提供的盒子尺寸 sizes,对于每一个不同的盒子尺寸 size,我们用二分法去看每个 size 能装下多少 package。
这里有一个 corner case 是如果当前供应商能提供的最大尺寸的盒子装不下最大的 package,那么我们就无法使用这个供应商了。在能使用的供应商里,我们还需要计算每个供应商提供的盒子浪费的空间。这里的计算方式很巧妙。我们先计算所有 package 需要的尺寸,记为 sum;同时在遍历过程中,我们是有记录每个不同尺寸的盒子被用了几个的,所以这里我们可以在过程中累加得出同一个供应商提供的不同 size 的盒子总共使用了多少空间,总计为 cost。最后 waste = cost - sum。
时间O(sort boxes, sort packages, boxes * log(packages))
空间O(1)
Java实现
1 class Solution { 2 public int minWastedSpace(int[] packages, int[][] boxes) { 3 long MOD = (long) Math.pow(10, 9) + 7; 4 Arrays.sort(packages); 5 int len = packages.length; 6 7 // sum 8 long sum = 0L; 9 for (int p : packages) { 10 sum += p; 11 } 12 13 long res = Long.MAX_VALUE; 14 for (int[] sizes : boxes) { 15 Arrays.sort(sizes); 16 // corner case 17 if (sizes[sizes.length - 1] < packages[len - 1]) { 18 continue; 19 } 20 21 int start = 0; 22 int end = 0; 23 long waste = 0; 24 long cost = 0; 25 for (int size : sizes) { 26 end = helper(packages, size); 27 cost += (long) size * (end - start); 28 start = end; 29 } 30 waste = cost - sum; 31 res = Math.min(res, waste); 32 } 33 return res == Long.MAX_VALUE ? -1 : (int) (res % MOD); 34 } 35 36 private int helper(int[] packages, int size) { 37 int left = 0; 38 int right = packages.length; 39 while (left < right) { 40 int mid = left + (right - left) / 2; 41 if (packages[mid] <= size) { 42 left = mid + 1; 43 } else { 44 right = mid; 45 } 46 } 47 return left; 48 } 49 }
以上是关于[LeetCode] 1889. Minimum Space Wasted From Packaging的主要内容,如果未能解决你的问题,请参考以下文章
leetcode@ [310] Minimum Height Trees