[LeetCode] Maximum Points After Enemy Battles

3207. Maximum Points After Enemy Battles

You are given an integer array enemyEnergies denoting the energy values of various enemies.

You are also given an integer currentEnergy denoting the amount of energy you have initially.

You start with 0 points, and all the enemies are unmarked initially.

You can perform either of the following operations zero or multiple times to gain points:

  • Choose an unmarked enemy, i, such that currentEnergy >= enemyEnergies[i]. By choosing this option:

    • You gain 1 point.
    • Your energy is reduced by the enemy’s energy, i.e. currentEnergy = currentEnergy - enemyEnergies[i].
  • If you have at least 1 point, you can choose an unmarked enemy, i. By choosing this option:

    • Your energy increases by the enemy’s energy, i.e. currentEnergy = currentEnergy + enemyEnergies[i].
    • The enemy i is marked.

Return an integer denoting the maximum points you can get in the end by optimally performing operations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
long long maximumPoints(vector<int>& A, int k) {
long long res = 0;
sort(begin(A), end(A));
if(A[0] > k) return 0;
while(A.size()) {
long long x = A.back();
res += k / A[0];
k = k % A[0] + x;
A.pop_back();
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/07/07/PS/LeetCode/maximum-points-after-enemy-battles/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.