Bird
Raised Fist0

Given the final total units loaded is 14 and the truck size is 5, which of the following boxTypes could have produced this result using the greedy algorithm?

hard🔄 Reverse Engineer Q9 of Q15
Greedy Algorithms - Maximum Units on a Truck
Given the final total units loaded is 14 and the truck size is 5, which of the following boxTypes could have produced this result using the greedy algorithm?
A[[3,2],[3,3],[3,1]]
B[[1,10],[2,2],[3,1]]
C[[2,5],[3,3],[4,1]]
D[[5,1],[5,2],[5,3]]
Step-by-Step Solution
Solution:
  1. Step 1: Sort boxTypes by units descending

    [[1,10],[2,2],[3,1]] sorted: [[1,10],[2,2],[3,1]]
  2. Step 2: Pick boxes greedily until truckSize=5

    Take 1 box of 10 units -> 10 units, truckSize=4 left; take 2 boxes of 2 units -> 4 units, truckSize=2 left; total units = 14.
  3. Step 3: Check other options

    [[3,2],[3,3],[3,1]] sorted: [[3,3],[3,2],[3,1]]; pick 3 boxes of 3 units -> 9 units, truckSize=2 left; pick 2 boxes of 2 units -> 4 units, truckSize=0; total=13 units (less than 14). [[2,5],[3,3],[4,1]] sorted: [[2,5],[3,3],[4,1]]; pick 2 boxes of 5 units -> 10 units, truckSize=3 left; pick 3 boxes of 3 units -> 9 units, truckSize=0; total=19 units (more than 14). [[5,1],[5,2],[5,3]] sorted: [[5,3],[5,2],[5,1]]; pick 5 boxes of 3 units -> 15 units, truckSize=0; total=15 units (more than 14).
  4. Step 4: Confirm correct option

    [[1,10],[2,2],[3,1]] matches total units 14 with truckSize 5.
  5. Final Answer:

    Option B -> Option B
  6. Quick Check:

    [[1,10],[2,2],[3,1]] matches total units and capacity constraints [OK]
Quick Trick: Reverse simulate greedy picks to match total units [OK]
Common Mistakes:
MISTAKES
  • Not sorting before simulating picks
  • Ignoring truckSize limit
Trap Explanation:
PITFALL
  • Candidates often forget to sort or miscalculate total units when reasoning backwards.
Interviewer Note:
CONTEXT
  • Tests deep understanding by reasoning backwards from output to input.
Master "Maximum Units on a Truck" in Greedy Algorithms

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Greedy Algorithms Quizzes