Maximum Units on a Truck
Problem statement
There are boxes.length box types. Type i has boxes[i] available boxes, each containing unitsPerBox[i] units. Every box uses one truck slot, regardless of its unit count.
Load at most capacity boxes and return the maximum total units. You may take any integer number of boxes from each type up to its availability; you may leave slots unused. For this exercise, assume no other weight or volume limit, and do not change either input array.
Function
maximumTruckUnits(boxes: int[], unitsPerBox: int[], capacity: int) → intExamples
Example 1
boxes = [1,2,3]unitsPerBox = [3,2,1]capacity = 4return = 8Take one 3-unit box, two 2-unit boxes and one 1-unit box.
Example 2
boxes = [2,3]unitsPerBox = [4,4]capacity = 4return = 16Any four available boxes have 4 units each.
Example 3
boxes = [2,1]unitsPerBox = [8,5]capacity = 0return = 0No box fits when the truck has no slots.
Constraints
0 <= boxes.length = unitsPerBox.length <= 1000.0 <= boxes[i], unitsPerBox[i] <= 1000.0 <= capacity <= 1000000.- The maximum result fits a signed 32-bit integer.