Maximum Points After Enemy Battles - Problem
Maximum Points After Enemy Battles

You are a warrior facing multiple enemies in battle! Each enemy has a certain energy value that represents how much energy it takes to defeat them.

You start with some currentEnergy and 0 points. All enemies are initially unmarked (alive).

You can perform two types of operations:

๐Ÿ—ก๏ธ Attack Operation: Choose an unmarked enemy with energy โ‰ค your current energy
โ€ข Gain 1 point
โ€ข Lose energy equal to the enemy's energy value

๐Ÿ”‹ Absorb Operation: If you have at least 1 point, choose any unmarked enemy
โ€ข Gain energy equal to the enemy's energy value
โ€ข Mark the enemy (it's absorbed and can't be used again)

Your goal is to find the maximum points you can earn by optimally combining these operations!

Example: With enemies [3,2,2] and currentEnergy=2
โ€ข You can't attack the first enemy (need 3 energy, have 2)
โ€ข Attack second enemy: gain 1 point, energy becomes 0
โ€ข Since you have 1 point, absorb first enemy: gain 3 energy (total 3)
โ€ข Attack third enemy: gain 1 point, energy becomes 1
โ€ข Result: 2 points maximum

Input & Output

example_1.py โ€” Basic Strategy
$ Input: enemyEnergies = [3,2,2], currentEnergy = 2
โ€บ Output: 3
๐Ÿ’ก Note: Attack second enemy (energy 2): gain 1 point, energy becomes 0. Since we have 1 point, absorb first enemy (energy 3): energy becomes 3. Now attack third enemy: gain 1 point, energy becomes 1. Absorb the already defeated second enemy isn't possible, so attack third enemy again: not possible. Wait, we can absorb the third enemy for energy 2, then attack the already defeated enemies. Actually, optimal is: attack enemy with energy 2, absorb all others, then attack repeatedly. Total energy after absorbing = 0 + 3 + 2 = 5. Additional attacks = 5/2 = 2. Total points = 1 + 2 = 3.
example_2.py โ€” Insufficient Energy
$ Input: enemyEnergies = [4,4], currentEnergy = 3
โ€บ Output: 0
๐Ÿ’ก Note: We cannot attack any enemy since our current energy (3) is less than the minimum enemy energy (4). Therefore, we cannot gain any points.
example_3.py โ€” Large Scale Battle
$ Input: enemyEnergies = [1,1,1,1], currentEnergy = 1
โ€บ Output: 7
๐Ÿ’ก Note: Attack first enemy (energy 1): gain 1 point, energy becomes 0. Absorb remaining three enemies: energy becomes 0 + 1 + 1 + 1 = 3. Now we can attack the remaining enemies (all energy 1) three more times: 3/1 = 3. But wait, we need to be more careful. We attack one enemy of energy 1, then absorb the other 3 enemies. Total energy = 0 + 3 = 3. Additional attacks = 3/1 = 3. But we have 4 enemies total, so after first attack and 3 absorbs, we can attack the minimum energy (1) again: 3/1 = 3 more times. Total = 1 + 3 + 3 = 7 points.

Constraints

  • 1 โ‰ค enemyEnergies.length โ‰ค 105
  • 1 โ‰ค enemyEnergies[i] โ‰ค 109
  • 0 โ‰ค currentEnergy โ‰ค 109
  • All enemy energies are positive integers

Visualization

Tap to expand
YOUE: 4MINE: 2E: 5E: 3ATTACKABSORBPOWEREDYOUE: 10, P: 1MINร—5Optimal Strategy: Min Enemy Focus๐ŸŽฏ Key: Attack weakest โ†’ Absorb others โ†’ Repeat attacks on weakest
Understanding the Visualization
1
Survey the Battlefield
Look at all enemies and identify the weakest one (minimum energy required)
2
Prove Yourself
Attack the weakest enemy to gain your first point and unlock absorption ability
3
Power Up
Absorb all remaining enemies to maximize your energy reserves
4
Maximize Rewards
Repeatedly attack the weakest enemy type to maximize total points earned
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because absorbing high-energy enemies gives us more 'fuel' to repeatedly attack the most efficient target (lowest energy enemy), maximizing our point generation rate.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
11.3K Views
Medium Frequency
~15 min Avg. Time
487 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