You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]:
numberOfBoxesi is the number of boxes of type i.
numberOfUnitsPerBoxi is the number of units in each box of the type i.
You are also given an integer truckSize, which is the maximum number of
boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.
Return themaximum total number of units that can be put on the truck.
Input: boxTypes =[[1,3],[2,2],[3,1]], truckSize =4Output: 8Explanation: There are:-1 box of the first type that contains 3 units.-2 boxes of the second type that contain 2 units each.-3 boxes of the third type that contain 1 unit each.You can take all the boxes of the first and second types, and one box of the third type.The total number of units will be =(1*3)+(2*2)+(1*1)=8.
To maximize the total units on the truck, we should prioritize box types with the highest units per box. By sorting box types in descending order of units per box, we can greedily fill the truck with the most valuable boxes first.
classSolution {
public:int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
sort(boxTypes.begin(), boxTypes.end(), [](auto& a, auto& b) {
return a[1] > b[1];
});
int ans =0;
for (auto& b : boxTypes) {
int take = min(truckSize, b[0]);
ans += take * b[1];
truckSize -= take;
if (truckSize ==0) break;
}
return ans;
}
};
classSolution {
publicintmaximumUnits(int[][] boxTypes, int truckSize) {
Arrays.sort(boxTypes, (a, b) -> b[1]- a[1]);
int ans = 0;
for (int[] b : boxTypes) {
int take = Math.min(truckSize, b[0]);
ans += take * b[1];
truckSize -= take;
if (truckSize == 0) break;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funmaximumUnits(boxTypes: Array<IntArray>, truckSize: Int): Int {
boxTypes.sortByDescending { it[1] }
var ans = 0var size = truckSize
for (b in boxTypes) {
val take = minOf(size, b[0])
ans += take * b[1]
size -= take
if (size ==0) break }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
defmaximum_units(box_types: list[list[int]], truck_size: int) -> int:
box_types.sort(key=lambda x: -x[1])
ans =0for num, units in box_types:
take = min(truck_size, num)
ans += take * units
truck_size -= take
if truck_size ==0:
breakreturn ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnmaximum_units(box_types: Vec<Vec<i32>>, truck_size: i32) -> i32 {
letmut box_types = box_types;
box_types.sort_by(|a, b| b[1].cmp(&a[1]));
letmut ans =0;
letmut size = truck_size;
for b in box_types {
let take = size.min(b[0]);
ans += take * b[1];
size -= take;
if size ==0 { break; }
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
maximumUnits(boxTypes: number[][], truckSize: number):number {
boxTypes.sort((a, b) =>b[1] -a[1]);
letans=0;
for (const [num, units] ofboxTypes) {
consttake= Math.min(truckSize, num);
ans+=take*units;
truckSize-=take;
if (truckSize===0) break;
}
returnans;
}
}