1710. Maximum Units on a Truck - Detailed Explanation
Problem Statement
You’re given a list of box types boxTypes, where each element is a pair [numberOfBoxes, unitsPerBox]. You also have a truck that can carry at most truckSize boxes in total. Your goal is to load the truck so that the total number of units is maximized. Return that maximum total.
Examples
Example 1
Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
Output: 8
Explanation:
Take 1 box of type [1,3] → 1×3 = 3 units
Take 2 boxes of type [2,2] → 2×2 = 4 units
Remaining capacity = 4−(1+2)=1 box
Take 1 box of type [3,1] → 1×1 = 1 unit
Total = 3+4+1 = 8
Example 2
Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
Output: 91
Explanation:
Sort by unitsPerBox descending: [5,10],[3,9],[4,7],[2,5]
Fill in that order until truck holds 10 boxes
Example 3
Input: boxTypes = [[1,5]], truckSize = 1
Output: 5
Constraints
1 ≤ boxTypes.length ≤ 10⁵boxTypes[i].length == 21 ≤ numberOfBoxes, unitsPerBox, truckSize ≤ 10⁵
Hints
- If you want the most units per box, which box types should you load first?
- Once you pick as many as possible of the highest‑unit box, move on to the next.
- Sorting by
unitsPerBoxmakes a greedy strategy optimal.
Greedy Sorting Approach
- Sort
boxTypesin descending order ofunitsPerBox. - Initialize
remaining = truckSizeandtotalUnits = 0. - Iterate through each
[count, units]in the sorted list:- Let
take = min(count, remaining). - Add
take * unitstototalUnits. - Decrease
remainingbytake. - If
remaining == 0, break.
- Let
- Return
totalUnits.
Why This Is Correct
- At every step, you choose the box that gives the highest units per box.
- If you ever picked a lower‑unit box when a higher‑unit one was available, you’d lose out on potential units.
- Because there’s no penalty for mixing types, a simple sort + fill is globally optimal.
Step‑by‑Step Walkthrough (Example 1)
boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
1. Sort by unitsPerBox → [[1,3],[2,2],[3,1]]
2. remaining=4, total=0
- First type [1,3]: take = min(1,4)=1 → total+=1*3=3, remaining=3
- Next [2,2]: take = min(2,3)=2 → total+=2*2=4 (total=7), remaining=1
- Next [3,1]: take = min(3,1)=1 → total+=1*1=1 (total=8), remaining=0
3. remaining is 0 → stop
Result = 8
Complexity Analysis
- Time:
- Sorting takes O(m log m), where m =
boxTypes.length. - One pass to accumulate takes O(m).
- Overall: O(m log m).
- Sorting takes O(m log m), where m =
- Space:
- O(1) extra beyond the input (or O(m) if your language’s sort isn’t in‑place).
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
- Not sorting by
unitsPerBoxfirst, leading to suboptimal picks. - Forgetting to break once the truck is full (causes unnecessary work).
- Using the wrong sort order (ascending instead of descending).
Edge Cases
- truckSize larger than total boxes → you end up taking them all.
- All box types identical in units per box → any order works.
- Minimum values (one box type, one capacity) → trivial fill.
Alternative Variations
- Minimize number of boxes to reach at least a target number of units.
- Multi‑constraint knapsack where each box also has a weight and the truck has weight and count limits.
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
889. Construct Binary Tree from Preorder and Postorder Traversal - Detailed Explanation
Learn to solve Leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal with multiple approaches.
2204. Distance to a Cycle in Undirected Graph - Detailed Explanation
Learn to solve Leetcode 2204. Distance to a Cycle in Undirected Graph with multiple approaches.
3428. Maximum and Minimum Sums of at Most Size K Subsequences - Detailed Explanation
Learn to solve Leetcode 3428. Maximum and Minimum Sums of at Most Size K Subsequences with multiple approaches.
206. Reverse Linked List - Detailed Explanation
Learn to solve Leetcode 206. Reverse Linked List with multiple approaches.
2493. Divide Nodes Into the Maximum Number of Groups - Detailed Explanation
Learn to solve Leetcode 2493. Divide Nodes Into the Maximum Number of Groups with multiple approaches.
2070. Most Beautiful Item for Each Query - Detailed Explanation
Learn to solve Leetcode 2070. Most Beautiful Item for Each Query with multiple approaches.
Related Courses

Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
$197

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
$72

Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
$78
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.