Greedy Algorithms - Maximum Units on a Truck
Identify the bug in the following code snippet for the Maximum Units on a Truck problem:
def maximumUnits(boxTypes, truckSize):
boxTypes.sort(key=lambda x: x[1]) # Sort ascending instead of descending
totalUnits = 0
for boxes, units in boxTypes:
if truckSize == 0:
break
take = min(boxes, truckSize)
totalUnits += take * units
truckSize -= take
return totalUnits
