easy +10 pts

Maximize units on truck

Pack boxes greedily to maximize total units within a truck capacity.

You are given an integer `capacity` representing the maximum number of boxes you can load onto a truck. You are also given a list `box_types`, where each element is a list of two integers: `[number_of_boxes, units_per_box]`. You may take any number of boxes from each box type (up to the full count) as long as the total boxes taken does not exceed `capacity`. Your goal is to maximize the total number of units loaded. Return the maximum total units as an integer. Implement the function `maximize_units(capacity: int, box_types: list) -> int`.

Constraints

1 <= capacity <= 10^6 1 <= len(box_types) <= 10^5 0 <= number_of_boxes <= 10^4 0 <= units_per_box <= 10^4

Example

>>> maximize_units(4, [[1,3],[2,2],[3,1]])
8
>>> maximize_units(10, [[5,10],[3,5],[2,7]])
79
>>> maximize_units(0, [[5,3]])
0
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about which box types you should prioritize to maximize units.
Sort the box types by units per box in descending order.
Take as many boxes as possible from the highest-value types first until capacity is reached.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.