[LeetCode] Minimum Amount of Damage Dealt to Bob

3273. Minimum Amount of Damage Dealt to Bob

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
long long minDamage(int k, vector<int>& capacity, vector<int>& health) {
long long res = 0, op = 0;
vector<array<long double, 3>> S;
for(int i = 0; i < capacity.size(); i++) {
long double cnt = (health[i] + k - 1) / k, mul = cnt * capacity[i];
S.push_back({capacity[i] / cnt, 1. * capacity[i], cnt});
}
sort(rbegin(S), rend(S));
for(auto& [_, cap, cnt] : S) {
op += cnt;
res += cap * op;
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/01/PS/LeetCode/minimum-amount-of-damage-dealt-to-bob/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.