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 == 2
- 1 ≤ 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
715. Range Module - Detailed Explanation
Learn to solve Leetcode 715. Range Module with multiple approaches.
475. Heaters - Detailed Explanation
Learn to solve Leetcode 475. Heaters with multiple approaches.
3355. Zero Array Transformation I - Detailed Explanation
Learn to solve Leetcode 3355. Zero Array Transformation I with multiple approaches.
134. Gas Station - Detailed Explanation
Learn to solve Leetcode 134. Gas Station with multiple approaches.
2109. Adding Spaces to a String - Detailed Explanation
Learn to solve Leetcode 2109. Adding Spaces to a String with multiple approaches.
1400. Construct K Palindrome Strings - Detailed Explanation
Learn to solve Leetcode 1400. Construct K Palindrome Strings 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
(69,299 learners)
$197
New

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
(1,107 learners)
$78
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
(26,683 learners)
$78
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.