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
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
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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code