Greedy Algorithms - Maximum Units on a Truck
Given the following code and input, what is the final output printed?
def maximumUnits(boxTypes, truckSize):
boxTypes.sort(key=lambda x: x[1], reverse=True)
totalUnits = 0
for boxes, units in boxTypes:
if truckSize == 0:
break
take = min(boxes, truckSize)
totalUnits += take * units
truckSize -= take
return totalUnits
boxTypes = [[1,3],[2,2],[3,1]]
truckSize = 4
print(maximumUnits(boxTypes, truckSize))
