1710. 卡车上的最大单元数
1. 题目描述
2. 注意
二维数组排序
3. coding
class Solution {
public int maximumUnits(int[][] boxTypes, int truckSize) {
Arrays.sort(boxTypes, new Comparator<int[]>() {
@Override
public int compare(int[] arr1, int[] arr2) {
return arr2[1] - arr1[1];
}
});
int res = 0;
for (int arr[] : boxTypes) {
if (arr[0] <= truckSize) {
res += arr[0] * arr[1];
} else {
res += truckSize * arr[1];
break;
}
truckSize -= arr[0];
}
return res;
}
}