1710. Maximum Units on a Truck
Question
You are assigned to put some amount of boxes onto one truck. You are given a 2D array
boxTypes
, whereboxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]
:
numberOfBoxes<sub>i</sub>
is the number of boxes of typei
.numberOfUnitsPerBox<sub>i</sub>
is the number of units in each box of the typei
.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 exceedtruckSize
.Return the maximum total number of units that can be put on the truck.
Solution
根据每个箱子可以装最多单元从大到小排序。
遍历数组,如果truckSize还有剩余,则将res增加容量乘以箱子数量。
然后将truckSize减少箱子数量个。
最后返回res即可。
Code
1 | class Solution { |
1710. Maximum Units on a Truck
https://xuanhe95.github.io/2022/07/01/1710-Maximum-Units-on-a-Truck/