In the Minimum Amount of Damage Dealt to Bob problem, Bob is facing multiple enemies simultaneously. Each enemy has a certain damage they deal per second and a certain amount of health. Bob can only attack one enemy at a time, and he deals a constant power damage per second. While Bob is attacking one enemy, all other living enemies continue to deal damage. You need to find the optimal order in which to kill the enemies to minimize the total damage Bob receives.
This is a modern Minimum Amount of Damage Dealt to Bob interview question recently seen at Amazon. It is a "Job Scheduling" optimization problem. It tests the ability to derive a custom sorting criteria based on a "ratio" of costs. It's a great test of whether a candidate can use mathematical proof (exchange arguments) to justify a greedy strategy.
The Greedy with Custom Sorting interview pattern is the core here. For any two enemies A and B, should we kill A then B, or B then A?
ceil(health / power). By comparing the damage/time ratios (or using a cross-multiplication: damageA * timeB vs damageB * timeA), you can sort the enemies in the optimal order.Bob power = 10. Enemy A: damage = 5, health = 20 (Time to kill = 2s) Enemy B: damage = 10, health = 50 (Time to kill = 5s)
10 * 2 = 20.10 * 5 = 50. (Wait, Bob only takes damage from enemies alive. While killing A, both A and B hit him. While killing B, only B hits him.)(5+10)*2 + (10)*5 = 30 + 50 = 80.(5+10)*5 = 75.5 * 2 = 10.75 + 10 = 85.
Optimal is killing A first because the damage/time ratio is better.Whenever you encounter a "minimize total cost over time" problem with multiple tasks, try the Exchange Argument. Compare two tasks (A and B) and see which order is better. This will almost always reveal a Sorting interview pattern based on a specific ratio.