🧠
Brute Force (Try All Combinations)
💡 This approach exists to understand the problem deeply by exploring all possible ways to pick boxes, even though it is inefficient. It helps beginners grasp the problem constraints and why greedy is better.
Intuition
Try every possible combination of boxes from all types to find the maximum units without exceeding truckSize.
Algorithm
- Generate all subsets of box types with counts that do not exceed truckSize.
- Calculate total units for each subset.
- Track the maximum units found.
- Return the maximum units after checking all subsets.
💡 This algorithm is hard to see because it involves exponential combinations and is not practical for large inputs, but it clarifies the problem's exhaustive nature.
from itertools import product
def maximumUnits(boxTypes, truckSize):
max_units = 0
n = len(boxTypes)
# Generate all possible counts for each box type from 0 to min(boxes, truckSize)
counts_ranges = [range(min(boxTypes[i][0], truckSize) + 1) for i in range(n)]
for counts in product(*counts_ranges):
total_boxes = sum(counts)
if total_boxes <= truckSize:
units = sum(counts[i] * boxTypes[i][1] for i in range(n))
if units > max_units:
max_units = units
return max_units
# Example usage
if __name__ == '__main__':
boxTypes = [[1,3],[2,2],[3,1]]
truckSize = 4
print(maximumUnits(boxTypes, truckSize)) # Output: 8
Line Notes
from itertools import productImport product to generate all combinations of box counts
counts_ranges = [range(min(boxTypes[i][0], truckSize) + 1) for i in range(n)]Create ranges for each box type from 0 to max boxes or truckSize to cover all possible counts
for counts in product(*counts_ranges):Iterate over all possible combinations of box counts across all box types
total_boxes = sum(counts)Calculate total boxes selected in current combination
if total_boxes <= truckSize:Only consider combinations that fit in the truck capacity
units = sum(counts[i] * boxTypes[i][1] for i in range(n))Calculate total units for current combination by multiplying counts with units per box
if units > max_units:Update max units if current combination yields more units
import java.util.*;
public class MaximumUnits {
public static int maximumUnits(int[][] boxTypes, int truckSize) {
int maxUnits = 0;
int n = boxTypes.length;
// Generate all combinations using recursion
maxUnits = helper(boxTypes, truckSize, 0, new int[n]);
return maxUnits;
}
private static int helper(int[][] boxTypes, int truckSize, int index, int[] counts) {
if (index == boxTypes.length) {
int totalBoxes = 0, totalUnits = 0;
for (int i = 0; i < counts.length; i++) {
totalBoxes += counts[i];
if (totalBoxes > truckSize) return 0;
totalUnits += counts[i] * boxTypes[i][1];
}
return totalUnits;
}
int maxUnits = 0;
int maxCount = Math.min(boxTypes[index][0], truckSize);
for (int c = 0; c <= maxCount; c++) {
counts[index] = c;
maxUnits = Math.max(maxUnits, helper(boxTypes, truckSize, index + 1, counts));
}
return maxUnits;
}
public static void main(String[] args) {
int[][] boxTypes = {{1,3},{2,2},{3,1}};
int truckSize = 4;
System.out.println(maximumUnits(boxTypes, truckSize)); // Output: 8
}
}
Line Notes
maxUnits = helper(boxTypes, truckSize, 0, new int[n]);Start recursive exploration of all box count combinations from index 0
if (index == boxTypes.length)Base case: all box types have been considered
for (int c = 0; c <= maxCount; c++)Try all counts from 0 to max boxes for current box type
if (totalBoxes > truckSize) return 0;Discard combinations exceeding truck capacity early
maxUnits = Math.max(maxUnits, helper(...))Keep track of maximum units found among all combinations
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int helper(const vector<vector<int>>& boxTypes, int truckSize, int index, vector<int>& counts) {
if (index == boxTypes.size()) {
int totalBoxes = 0, totalUnits = 0;
for (int i = 0; i < counts.size(); i++) {
totalBoxes += counts[i];
if (totalBoxes > truckSize) return 0;
totalUnits += counts[i] * boxTypes[i][1];
}
return totalUnits;
}
int maxUnits = 0;
int maxCount = min(boxTypes[index][0], truckSize);
for (int c = 0; c <= maxCount; c++) {
counts[index] = c;
maxUnits = max(maxUnits, helper(boxTypes, truckSize, index + 1, counts));
}
return maxUnits;
}
int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
vector<int> counts(boxTypes.size(), 0);
return helper(boxTypes, truckSize, 0, counts);
}
int main() {
vector<vector<int>> boxTypes = {{1,3},{2,2},{3,1}};
int truckSize = 4;
cout << maximumUnits(boxTypes, truckSize) << endl; // Output: 8
return 0;
}
Line Notes
if (index == boxTypes.size())Base case: all box types processed
for (int c = 0; c <= maxCount; c++)Try all possible counts for current box type from 0 to maxCount
if (totalBoxes > truckSize) return 0;Prune invalid combinations exceeding capacity early
maxUnits = max(maxUnits, helper(...))Update max units with best found so far
function maximumUnits(boxTypes, truckSize) {
let maxUnits = 0;
const n = boxTypes.length;
function helper(index, counts) {
if (index === n) {
let totalBoxes = 0, totalUnits = 0;
for (let i = 0; i < n; i++) {
totalBoxes += counts[i];
if (totalBoxes > truckSize) return 0;
totalUnits += counts[i] * boxTypes[i][1];
}
return totalUnits;
}
let maxUnitsLocal = 0;
let maxCount = Math.min(boxTypes[index][0], truckSize);
for (let c = 0; c <= maxCount; c++) {
counts[index] = c;
maxUnitsLocal = Math.max(maxUnitsLocal, helper(index + 1, counts));
}
return maxUnitsLocal;
}
return helper(0, new Array(n).fill(0));
}
// Example usage
console.log(maximumUnits([[1,3],[2,2],[3,1]], 4)); // Output: 8
Line Notes
if (index === n)Base case: all box types considered
for (let c = 0; c <= maxCount; c++)Try all counts for current box type from 0 to maxCount
if (totalBoxes > truckSize) return 0;Discard invalid combinations exceeding truck capacity
maxUnitsLocal = Math.max(maxUnitsLocal, helper(...))Track maximum units found among all combinations
TimeO((truckSize+1)^n) exponential
SpaceO(n) recursion stack and counts array
We try all combinations of counts for each box type up to truckSize, leading to exponential time.
💡 For n=3 and truckSize=4, this means checking up to 5^3=125 combinations, which grows quickly and is impractical for large inputs.
Interview Verdict: TLE
This approach is too slow for large inputs but is useful to understand the problem and motivate greedy solutions.