请你将一些箱子装在 一辆卡车 上。给你一个二维数组 boxTypes ,其中 boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi] :
numberOfBoxesi 是类型 i 的箱子的数量。
numberOfUnitsPerBoxi 是类型 i 每个箱子可以装载的单元数量。
整数 truckSize 表示卡车上可以装载 箱子 的 最大数量 。只要箱子数量不超过 truckSize ,你就可以选择任意箱子装到卡车上。
返回卡车可以装载 单元 的 最大 总数。
class Solution:
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
box = sorted(boxTypes, key=lambda x: -x[1])
res = 0
for item in box:
truckSize -= item[0]
if truckSize > 0:
res += item[0] * item[1]
else:
res += item[1] * (truckSize + item[0])
return res
return res