Minimum Amount of Damage Dealt to Bob - Problem

In a strategic battle simulation, Bob faces n enemies, each with unique attack power and health. Every second follows this sequence:

  1. Enemy Attack Phase: All alive enemies deal damage to Bob simultaneously
  2. Bob's Counter Phase: Bob chooses one alive enemy and deals power damage to it

You need to determine the optimal order in which Bob should defeat enemies to minimize total damage taken before all enemies are eliminated.

Input: An integer power (Bob's attack damage) and two arrays damage[] and health[] where enemy i deals damage[i] per second and has health[i] HP.

Output: The minimum total damage Bob will receive.

Input & Output

basic_example.py โ€” Python
$ Input: power = 4, damage = [1, 2, 3, 4], health = [4, 5, 6, 8]
โ€บ Output: 39
๐Ÿ’ก Note: Optimal order is [3,2,1,0] (0-indexed). Enemy 3 has highest ratio (4/2=2.0), then enemy 2 (3/2=1.5), enemy 1 (2/2=1.0), and enemy 0 (1/1=1.0). Total damage: 10ร—2 + 6ร—2 + 3ร—2 + 1ร—1 = 39.
small_example.py โ€” Python
$ Input: power = 1, damage = [2, 5], health = [3, 2]
โ€บ Output: 25
๐Ÿ’ก Note: Enemy 0 ratio: 2/3 โ‰ˆ 0.67, Enemy 1 ratio: 5/2 = 2.5. Optimal order: [1,0]. Damage: 7ร—2 + 2ร—3 = 14 + 6 = 20. Wait, let me recalculate: Kill enemy 1 first (2 turns): 7ร—2 = 14. Then kill enemy 0 (3 turns): 2ร—3 = 6. Total = 20.
edge_case.py โ€” Python
$ Input: power = 100, damage = [1], health = [1]
โ€บ Output: 1
๐Ÿ’ก Note: Single enemy killed in 1 turn. Total damage taken is just 1ร—1 = 1.

Constraints

  • 1 โ‰ค power โ‰ค 104
  • 1 โ‰ค n โ‰ค 105 where n is the length of damage and health arrays
  • 1 โ‰ค damage[i], health[i] โ‰ค 106
  • All values are positive integers

Visualization

Tap to expand
Optimal Battle Strategy VisualizationTurn0123456+Kill E21 turnKill E32 turnsKill E14 turnsCumulativeDamage71217274768FINALGreedy Strategy1. Calculate ratios:โ€ข E1: 4/(80/10) = 0.5โ€ข E2: 2/(10/10) = 2.0 โ†โ€ข E3: 1/(15/10) = 0.672. Sort: E2 โ†’ E3 โ†’ E13. Total damage: 68โœ“ Optimal solution!Key Insight: High damage/time ratio enemies should be eliminated first
Understanding the Visualization
1
Assessment Phase
Calculate how dangerous each enemy is relative to how long it takes to eliminate them
2
Priority Ranking
Sort enemies by their efficiency ratio (damage per turn / turns to kill)
3
Execution Phase
Follow the optimal order to minimize total damage accumulation
4
Result
The greedy choice leads to the globally optimal solution
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because eliminating high-efficiency enemies early reduces the cumulative damage more effectively than any other strategy. This is mathematically provable through exchange arguments.
Asked in
Google 42 Amazon 38 Meta 35 Microsoft 29
68.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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