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 i is 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
Maximum Points After Enemy Battles INPUT enemyEnergies array: 3 [0] 2 [1] 4 [2] currentEnergy: 11 Initial Points: 0 Sorted: [2, 3, 4] min = 2, sum = 9 E ALGORITHM STEPS 1 Sort Array Sort ascending to find min 2 Check Initial energy >= min? 11 >= 2 OK 3 Gain All Energy Mark all except min enemy 4 Calculate Points total / min energy total = 11 + 3 + 4 = 18 min = 2 points = 18 / 2 = 9 (curr + sum - min) / min FINAL RESULT 9 Maximum Points Execution Trace: 1. Beat enemy[1]=2, pts=1 2. Mark enemy[0]=3, E=12 3. Mark enemy[2]=4, E=16 4. Beat enemy[1] 8 more times (16/2 = 8) Total: 1 + 8 = 9 points Remaining E: 0 OK - Optimal Key Insight: The greedy strategy is to first gain 1 point by beating the weakest enemy, then mark ALL other enemies to absorb their energy. Finally, repeatedly beat the weakest enemy to maximize points. Formula: points = (currentEnergy + totalEnergy - minEnergy) / minEnergy TutorialsPoint - Maximum Points After Enemy Battles | Greedy Strategy
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
8.5K Views
Medium Frequency
~25 min Avg. Time
247 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen