Load Crates Onto a Truck for the Most Cargo
Implement maxCargoUnits
A truck can carry at most truckSize crates. Crates come in several types: each type has a count (how many crates of that type exist) and the units of cargo each crate of that type holds. You may load any number of crates of each type, up to its count. Find the maximum total units the truck can carry.
Every crate takes one slot, so the only thing that differs is how many units each carries. Sorting the crate types by units per crate and loading the densest ones first is optimal — no table of combinations is needed.
Example 1:
Input: crateTypes = [[4,5],[2,12],[6,3],[3,8]], truckSize = 9
Output: 68
Example 2:
Input: crateTypes = [[3,7]], truckSize = 10
Output: 21
Example 3:
Input: crateTypes = [[10,1],[1,30]], truckSize = 5
Output: 34
+ 9 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ crateTypes.length ≤ 8; each row is [count, unitsPerCrate] - ●
1 ≤ count ≤ 20 and 1 ≤ unitsPerCrate ≤ 30 - ●
1 ≤ truckSize ≤ 200 — the truck holds at most truckSize crates in total, regardless of type - ●
You may take any number of crates of each type, from none up to its count. Return the maximum total units of cargo the truck can carry
crateTypes =
truckSize =