#3207
Medium Algorithms Maximum points after enemy battles
Array Greedy
33.2% acceptance
Feb 25, 2026
128
43
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn maximum_points(enemy_energies: Vec<i32>, current_energy: i32) -> i64 {
let min_e = *enemy_energies.iter().min().unwrap() as i64;
let total: i64 = enemy_energies.iter().map(|&x| x as i64).sum();
let curr = current_energy as i64;
if curr < min_e {
return 0;
}
// Gain 1 point by defeating min_e, then absorb all others, then divide by min_e
(curr + total - min_e) / min_e
}
}