Maximum Units on a Truck - Problem

Imagine you're a logistics coordinator tasked with optimizing truck loading! You have one truck with limited capacity and multiple types of boxes, each containing different numbers of valuable units.

Given a 2D array boxTypes where boxTypes[i] = [numberOfBoxes, unitsPerBox]:

  • numberOfBoxes - how many boxes of type i are available
  • unitsPerBox - value/units contained in each box of type i

You're also given truckSize - the maximum number of boxes the truck can carry.

Goal: Select boxes to maximize the total units loaded onto the truck without exceeding the truck's capacity.

Example: If you have box types [[1,3], [2,2], [3,1]] and truck capacity 4, you should prioritize high-value boxes first: take 1 box with 3 units, then 2 boxes with 2 units each, then 1 box with 1 unit = 10 total units.

Input & Output

example_1.py โ€” Basic Example
$ Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
โ€บ Output: 8
๐Ÿ’ก Note: Take 1 box of type with 3 units, 2 boxes of type with 2 units each, and 1 box of type with 1 unit. Total = 1ร—3 + 2ร—2 + 1ร—1 = 8 units using exactly 4 boxes.
example_2.py โ€” Excess Capacity
$ Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
โ€บ Output: 91
๐Ÿ’ก Note: Sort by units per box: [5,10], [3,9], [4,7], [2,5]. Take all: 5ร—10 + 3ร—9 + 2ร—7 = 91 units using exactly 10 boxes.
example_3.py โ€” Limited Capacity
$ Input: boxTypes = [[1,3],[5,5],[2,4],[4,1]], truckSize = 3
โ€บ Output: 13
๐Ÿ’ก Note: Sort by units per box: [5,5], [2,4], [1,3], [4,1]. Take 3 boxes with 5 units each = 3ร—5 = 15 units, but we can only take 3 boxes total, so we get 3ร—5 = 15. Wait, let me recalculate: we have [5,5] so we can take all 5 boxes, but truck only holds 3, so 3ร—5 = 15. Actually, we take 3 out of the 5 boxes of type [5,5] = 15 units.

Constraints

  • 1 โ‰ค boxTypes.length โ‰ค 1000
  • 1 โ‰ค numberOfBoxesi, numberOfUnitsPerBoxi โ‰ค 1000
  • 1 โ‰ค truckSize โ‰ค 106
  • Box types can have duplicate unit values
  • You must respect the truck capacity exactly

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Pirate's Treasure Loading StrategyStep 1: Available Treasure Chests (Before Sorting)Chest A1 chest, 3 goldChest B2 chests, 2 goldChest C3 chests, 1 goldStep 2: Sort by Gold Per Chest (Descending)๐Ÿ‘‘ Best3 gold/chestGood2 gold/chestOkay1 gold/chest๐Ÿšข Ship Capacity: 4 Chests3221โŒ No room for more
Understanding the Visualization
1
Assess the Treasure
Line up all chest types and identify their gold-per-chest value
2
Sort by Value
Arrange chests from most valuable to least valuable per chest
3
Load Greedily
Always take the most valuable chests first until ship is full
4
Maximize Gold
This greedy strategy guarantees maximum treasure value
Key Takeaway
๐ŸŽฏ Key Insight: Greedy works because there's never a benefit to taking lower-value items when higher-value items are available - always prioritize maximum value per unit of capacity!
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 21
68.4K Views
High Frequency
~12 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen