Minimum Amount of Damage Dealt to Bob - Problem

You are given an integer power and two integer arrays damage and health, both having length n.

Bob has n enemies, where enemy i will deal Bob damage[i] points of damage per second while they are alive (i.e. health[i] > 0).

Every second, after the enemies deal damage to Bob, he chooses one of the enemies that is still alive and deals power points of damage to them.

Determine the minimum total amount of damage points that will be dealt to Bob before all n enemies are dead.

Input & Output

Example 1 — Basic Case
$ Input: power = 10, damage = [10,4,15], health = [80,20,40]
Output: 138
💡 Note: Enemy ratios: 10/8=1.25, 4/2=2.0, 15/4=3.75. Attack order [2,1,0] gives minimum damage of 138.
Example 2 — Equal Ratios
$ Input: power = 5, damage = [5,10], health = [10,20]
Output: 30
💡 Note: Both enemies have ratio 5/2=2.5. Either order works, total damage is 30.
Example 3 — Single Enemy
$ Input: power = 20, damage = [15], health = [30]
Output: 30
💡 Note: Only one enemy: takes 2 turns to kill, deals 15 damage per turn, total 30.

Constraints

  • 1 ≤ power ≤ 104
  • 1 ≤ n ≤ 105
  • 1 ≤ damage[i], health[i] ≤ 104

Visualization

Tap to expand
Battle Strategy: Minimize Total Damage to BobBobPower: 10Enemy 0Dmg: 10HP: 80Ratio: 1.25Enemy 1Dmg: 4HP: 20Ratio: 2.0Enemy 2Dmg: 15HP: 40Ratio: 3.75All enemies attack Bob each turnOptimal Strategy: Attack by Damage/Time RatioOrder: Enemy 2 (3.75) → Enemy 1 (2.0) → Enemy 0 (1.25)Minimum Damage: 138Using greedy priority order
Understanding the Visualization
1
Battle Setup
Bob faces multiple enemies, each dealing damage per turn
2
Turn Mechanics
Enemies attack first, then Bob attacks one enemy
3
Optimal Strategy
Attack enemies with highest damage/time ratio first
Key Takeaway
🎯 Key Insight: Prioritize enemies by damage-per-time ratio to minimize total damage taken
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
32.0K Views
Medium Frequency
~35 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen