Maximum Units on a Truck
LeetCode 1829 | Difficulty: Easyβ
EasyProblem Descriptionβ
You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxes~i~, numberOfUnitsPerBox~i~]:
- `numberOfBoxes~i~` is the number of boxes of type `i`.
- `numberOfUnitsPerBox~i~`~ ~is the number of units in each box of the type `i`.
You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.
Return the maximum total number of units that can be put on the truck.
Example 1:
Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
Output: 8
Explanation: There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
Example 2:
Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
Output: 91
Constraints:
- `1 <= boxTypes.length <= 1000`
- `1 <= numberOfBoxes~i~, numberOfUnitsPerBox~i~ <= 1000`
- `1 <= truckSize <= 10^6`
Topics: Array, Greedy, Sorting
Approachβ
Greedyβ
At each step, make the locally optimal choice. The challenge is proving the greedy choice leads to a global optimum. Look for: can I sort by some criterion? Does choosing the best option now ever hurt future choices?
Interval scheduling, activity selection, minimum coins (certain denominations), Huffman coding.
Solutionsβ
Solution 1: C# (Best: 120 ms)β
| Metric | Value |
|---|---|
| Runtime | 120 ms |
| Memory | 41.3 MB |
| Date | 2021-12-19 |
public class Solution {
public int MaximumUnits(int[][] boxTypes, int truckSize) {
Array.Sort(boxTypes, (a,b) => {return b[1]-a[1];});
int[] boxes = new int[1001];
int n = boxTypes.Length;
int count = 0;
for (int i = 0; i < boxTypes.Length; i++)
{
boxes[boxTypes[i][1]] += boxTypes[i][0];
}
for (int i = 1000; i >= 0 && truckSize > 0; i--)
{
if(boxes[i] == 0) continue;
int min = Math.Min(truckSize, boxes[i]);
truckSize -= min;
count += min*i;
}
return count;
}
}
π 4 more C# submission(s)
Submission (2021-12-19) β 137 ms, 38.8 MBβ
public class Solution {
public int MaximumUnits(int[][] boxTypes, int truckSize) {
Array.Sort(boxTypes, (a,b) => {return b[1]-a[1];});
int[] boxes = new int[1001];
int n = boxTypes.Length;
int count = 0;
for (int i = 0; i < boxTypes.Length; i++)
{
boxes[boxTypes[i][1]] += boxTypes[i][0];
}
for (int i = 1000; i >= 0; i--)
{
if(boxes[i] == 0 || truckSize <= 0) continue;
int min = Math.Min(truckSize, boxes[i]);
truckSize -= min;
count += min*i;
}
return count;
}
}
Submission (2021-12-19) β 139 ms, 38.7 MBβ
public class Solution {
public int MaximumUnits(int[][] boxTypes, int truckSize) {
Array.Sort(boxTypes, (a,b) => {return b[1]-a[1];});
int[] boxes = new int[1001];
int n = boxTypes.Length;
int count = 0;
for (int i = 0; i < boxTypes.Length; i++)
{
boxes[boxTypes[i][1]] += boxTypes[i][0];
}
for (int i = 1000; i >= 0 && truckSize > 0; i--)
{
if(boxes[i] == 0) continue;
int min = Math.Min(truckSize, boxes[i]);
truckSize -= min;
count += min*i;
}
return count;
}
}
Submission (2021-12-19) β 160 ms, 38.5 MBβ
public class Solution {
public int MaximumUnits(int[][] boxTypes, int truckSize) {
int[] boxes = new int[1001];
int n = boxTypes.Length;
int count = 0;
for (int i = 0; i < boxTypes.Length; i++)
{
boxes[boxTypes[i][1]] += boxTypes[i][0];
}
for (int i = 1000; i >= 0 && truckSize > 0; i--)
{
if(boxes[i] == 0) continue;
int min = Math.Min(truckSize, boxes[i]);
truckSize -= min;
count += min*i;
}
return count;
}
}
Submission (2021-12-19) β 197 ms, 38.1 MBβ
public class Solution {
public int MaximumUnits(int[][] boxTypes, int truckSize) {
Array.Sort(boxTypes, (a, b) => { return a[1] - b[1];} );
int n = boxTypes.Length;
int count = 0;
for (int i = n-1 ; i >= 0 && truckSize > 0; i--)
{
int min = Math.Min(truckSize, boxTypes[i][0]);
truckSize -= min;
count += min * boxTypes[i][1];
}
return count;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Sort + Process | $O(n log n)$ | $O(1) to O(n)$ |
Interview Tipsβ
- Start by clarifying edge cases: empty input, single element, all duplicates.
- LeetCode provides 3 hint(s) for this problem β try solving without them first.
π‘ Hints
Hint 1: If we have space for at least one box, it's always optimal to put the box with the most units.
Hint 2: Sort the box types with the number of units per box non-increasingly.
Hint 3: Iterate on the box types and take from each type as many as you can.