请你将一些箱子装在 一辆卡车 上。给你一个二维数组 boxTypes
,其中 boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]
:
numberOfBoxesi
是类型i
的箱子的数量。numberOfUnitsPerBoxi
是类型i
每个箱子可以装载的单元数量。
整数 truckSize
表示卡车上可以装载 箱子 的 最大数量 。只要箱子数量不超过 truckSize
,你就可以选择任意箱子装到卡车上。
返回卡车可以装载 单元 的 最大 总数。
示例 1:
输入:boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 输出:8 解释:箱子的情况如下: - 1 个第一类的箱子,里面含 3 个单元。 - 2 个第二类的箱子,每个里面含 2 个单元。 - 3 个第三类的箱子,每个里面含 1 个单元。 可以选择第一类和第二类的所有箱子,以及第三类的一个箱子。 单元总数 = (1 * 3) + (2 * 2) + (1 * 1) = 8
示例 2:
输入:boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 输出:91
提示:
1 <= boxTypes.length <= 1000
1 <= numberOfBoxesi, numberOfUnitsPerBoxi <= 1000
1 <= truckSize <= 106
分析:题目是要求能装下的最大总数。那我们想要得到更大的,那我们肯定要选择可以装载最的单元数量越大越好的,这样子我们才能得到更大的数,我们就去排序一下,自定义排序,Arrays.sort ,然后再去相加就可以了,简单题就不多说了,直接上代码
AC代码:
class Solution {
public int maximumUnits(int[][] boxTypes, int truckSize) {
Arrays.sort(boxTypes, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[1]-o1[1] ;
}
});
int size = 0 ;
int res = 0 ;
for (int i=0 ;i<boxTypes.length; i++){
if (size+boxTypes[i][0]<= truckSize){
res += boxTypes[i][0]*boxTypes[i][1] ;
size+=boxTypes[i][0] ;
}else{
res += boxTypes[i][1]*(truckSize - size) ;
break;
}
}
return res ;
}
}