Greedy Algorithms - Maximum Units on a Truck
Examine the following code snippet for the Maximum Units on a Truck problem:
What is the primary bug in this code?
def maxUnits(boxTypes, truckSize):
boxTypes.sort(key=lambda x: x[1]) # Sort ascending
totalUnits = 0
for count, units in boxTypes:
if truckSize == 0:
break
boxes_to_take = min(count, truckSize)
totalUnits += boxes_to_take * units
truckSize -= boxes_to_take
return totalUnitsWhat is the primary bug in this code?
