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:
- Enemy Attack Phase: All alive enemies deal damage to Bob simultaneously
- Bob's Counter Phase: Bob chooses one alive enemy and deals
powerdamage 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code