Medium
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:
i
, such that currentEnergy >= enemyEnergies[i]
. By choosing this option:
currentEnergy = currentEnergy - enemyEnergies[i]
.i
. By choosing this option:
currentEnergy = currentEnergy + enemyEnergies[i]
.i
is marked.Return an integer denoting the maximum points you can get in the end by optimally performing operations.
Example 1:
Input: enemyEnergies = [3,2,2], currentEnergy = 2
Output: 3
Explanation:
The following operations can be performed to get 3 points, which is the maximum:
points
increases by 1, and currentEnergy
decreases by 2. So, points = 1
, and currentEnergy = 0
.currentEnergy
increases by 3, and enemy 0 is marked. So, points = 1
, currentEnergy = 3
, and marked enemies = [0]
.points
increases by 1, and currentEnergy
decreases by 2. So, points = 2
, currentEnergy = 1
, and marked enemies = [0]
.currentEnergy
increases by 2, and enemy 2 is marked. So, points = 2
, currentEnergy = 3
, and marked enemies = [0, 2]
.points
increases by 1, and currentEnergy
decreases by 2. So, points = 3
, currentEnergy = 1
, and marked enemies = [0, 2]
.Example 2:
Input: enemyEnergies = [2], currentEnergy = 10
Output: 5
Explanation:
Performing the first operation 5 times on enemy 0 results in the maximum number of points.
Constraints:
1 <= enemyEnergies.length <= 105
1 <= enemyEnergies[i] <= 109
0 <= currentEnergy <= 109
public class Solution {
public long maximumPoints(int[] enemyEnergies, int currentEnergy) {
int n = enemyEnergies.length;
int min = enemyEnergies[0];
for (int i = 1; i < n; i++) {
min = Math.min(min, enemyEnergies[i]);
}
if (currentEnergy == 0 || currentEnergy < min) {
return 0;
}
long sum = currentEnergy;
for (int i = n - 1; i >= 0; i--) {
sum += enemyEnergies[i];
}
sum -= min;
return sum / min;
}
}