Minimum Amount of Damage Dealt to Bob Problem
Description
LeetCode Problem 3273.
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.
Example 1:
Input: power = 4, damage = [1,2,3,4], health = [4,5,6,8]
Output: 39
Explanation:
- Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is 10 + 10 = 20 points.
- Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is 6 + 6 = 12 points.
- Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is 3 points.
- Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is 2 + 2 = 4 points.
Example 2:
Input: power = 1, damage = [1,1,1,1], health = [1,2,3,4]
Output: 20
Explanation:
- Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is 4 points.
- Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is 3 + 3 = 6 points.
- Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is 2 + 2 + 2 = 6 points.
- Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is 1 + 1 + 1 + 1 = 4 points.
Example 3:
Input: power = 8, damage = [40], health = [59]
Output: 320
Constraints:
1 <= power <= 10^41 <= n == damage.length == health.length <= 10^51 <= damage[i], health[i] <= 10^4
Solution
Approach: Greedy with an Exchange Argument
Enemy i needs t_i = ceil(health[i] / power) hits to die and deals damage[i] per second while alive. The total damage is the sum over enemies of damage[i] times the number of seconds it stays alive, which depends only on the order in which Bob kills them.
Compare two neighbours i then j in the order. Killing i first costs damage[j] * t_i extra, killing j first costs damage[i] * t_j extra. So i should come first when damage[i] * t_j > damage[j] * t_i, that is, when damage[i] / t_i is larger. Sort by this ratio in descending order (compare with cross-multiplication to avoid fractions).
Then walk through the order with rate, the damage per second of the enemies still alive. For each enemy add rate * t_i to the answer and subtract damage[i] from rate.
Complexity: O(n log n) time and O(n) space.
from functools import cmp_to_key
class Solution:
def minDamage(self, power: int, damage: List[int], health: List[int]) -> int:
n = len(damage)
time = [(h + power - 1) // power for h in health]
# i goes before j when damage[i] / time[i] > damage[j] / time[j]
order = sorted(range(n), key=cmp_to_key(lambda i, j: damage[j] * time[i] - damage[i] * time[j]))
rate = sum(damage)
ans = 0
for i in order:
ans += rate * time[i]
rate -= damage[i]
return ans