Maximum Points After Enemy Battles - Problem
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:
Option 1: 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:
currentEnergy = currentEnergy - enemyEnergies[i]
Option 2: 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:
currentEnergy = currentEnergy + enemyEnergies[i] - The enemy
iis marked
Return an integer denoting the maximum points you can get in the end by optimally performing operations.
Input & Output
Example 1 — Basic Strategy
$
Input:
enemyEnergies = [3,2,4], currentEnergy = 11
›
Output:
9
💡 Note:
Defeat enemy with energy 2 (weakest) → gain 1 point, energy becomes 9. Absorb enemies with energy 3 and 4 → energy becomes 9+3+4=16. Defeat weakest enemy (energy 2) repeatedly: 16÷2=8 more times. Total points = 1+8 = 9.
Example 2 — Insufficient Energy
$
Input:
enemyEnergies = [4,3,5], currentEnergy = 2
›
Output:
0
💡 Note:
Current energy (2) is less than the minimum enemy energy (3), so we cannot defeat any enemy to get our first point. Result is 0 points.
Example 3 — Single Enemy
$
Input:
enemyEnergies = [5], currentEnergy = 10
›
Output:
2
💡 Note:
Only one enemy with energy 5. Defeat it once → gain 1 point, energy becomes 5. Since we now have 1 point, we can absorb the same enemy → energy becomes 5+5=10. Defeat it again: 10÷5=2 times. But wait, we already used it, so total = 1+(10-5)÷5 = 2.
Constraints
- 1 ≤ enemyEnergies.length ≤ 105
- 1 ≤ enemyEnergies[i] ≤ 109
- 0 ≤ currentEnergy ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of enemy energies and current energy
2
Strategy
Defeat weakest, absorb others, repeat defeats
3
Output
Maximum points achievable
Key Takeaway
🎯 Key Insight: Always defeat the weakest enemy first to unlock absorption, then absorb stronger enemies to maximize energy for more defeats
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code