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:
i, such that currentEnergy >= enemyEnergies[i]. By choosing this option:
currentEnergy = currentEnergy - enemyEnergies[i].i. By choosing this option:
currentEnergy = currentEnergy + enemyEnergies[i].i is marked.Return an integer denoting the maximum points you can get in the end by optimally performing operations.
Example 1:
Input: enemyEnergies = [3,2,2], currentEnergy = 2
Output: 3
Explanation:
The following operations can be performed to get 3 points, which is the maximum:
points increases by 1, and currentEnergy decreases by 2. So, points = 1, and currentEnergy = 0.currentEnergy increases by 3, and enemy 0 is marked. So, points = 1, currentEnergy = 3, and marked enemies = [0].points increases by 1, and currentEnergy decreases by 2. So, points = 2, currentEnergy = 1, and marked enemies = [0].currentEnergy increases by 2, and enemy 2 is marked. So, points = 2, currentEnergy = 3, and marked enemies = [0, 2].points increases by 1, and currentEnergy decreases by 2. So, points = 3, currentEnergy = 1, and marked enemies = [0, 2].Example 2:
Input: enemyEnergies = [2], currentEnergy = 10
Output: 5
Explanation:
Performing the first operation 5 times on enemy 0 results in the maximum number of points.
Constraints:
1 <= enemyEnergies.length <= 1051 <= enemyEnergies[i] <= 1090 <= currentEnergy <= 109Problem Overview: You are given a list of enemies where each enemy requires a certain amount of energy to defeat. Defeating an enemy costs energy but earns points. Some strategies also allow trading points to regain energy. The goal is to maximize the total points after a sequence of enemy battles.
Approach 1: Greedy with Sorting and Two Pointers (O(n log n) time, O(1) space)
The optimal strategy follows a greedy idea similar to resource management problems. Sort the enemy energy requirements first. Use two pointers: one starting from the weakest enemy and one from the strongest. If you have enough energy, defeat the weakest enemy to gain a point and reduce your energy. If you cannot defeat any enemy but already have points, trade one point to regain energy by interacting with the strongest remaining enemy. This keeps weaker enemies available for cheap points while using stronger enemies as energy recovery when necessary. Sorting enables efficient selection of cheapest and most expensive enemies, making this approach O(n log n) time with constant extra space.
This approach relies on greedy decision making: always gain points using the cheapest enemy first, and only sacrifice points when it unlocks future wins. The pattern appears frequently in Greedy optimization problems and resource allocation tasks involving Array manipulation.
Approach 2: Dynamic Programming (O(n * E) time, O(n * E) space)
A dynamic programming formulation explores all valid battle sequences. Define a DP state representing the maximum points achievable using the first i enemies with a given remaining energy E. For each enemy, you can either defeat it (if energy allows) to gain a point and reduce energy, or skip/trade depending on the rules that restore energy at the cost of points. Transition states update the best achievable score while tracking energy changes.
This approach systematically evaluates every feasible decision path. It works well when constraints on energy are small enough to keep the DP table manageable. However, its state space grows with both enemy count and energy values, making it slower and more memory-intensive than the greedy solution.
The DP formulation helps verify correctness and can be useful when greedy conditions are unclear. It also demonstrates how the problem can be interpreted as a state transition problem common in Dynamic Programming.
Recommended for interviews: The greedy sorting strategy is what most interviewers expect. Recognizing that defeating weaker enemies first maximizes future flexibility is the key insight. Implementing the two-pointer technique after sorting demonstrates strong problem-solving intuition and keeps the solution clean at O(n log n) time. Discussing the DP alternative shows deeper understanding, but the greedy approach is the practical optimal solution.
Sort the enemies by their energy requirement. Try to gain a point by defeating the lowest energy enemy when possible. If you cannot defeat any enemy, lend your energy to the highest energy unmarked enemy you can mark, provided you have at least one point.
The strategy involves sorting the enemy energies. A two-pointer technique is used. The left pointer is initially at the most attainable enemy and the right pointer at the most costly enemy. Whenever the energy is sufficient to defeat the enemy at the left pointer, do so to gain a point. If not, convert some points to energy by marking an unmarked enemy at the right pointer. If both options run out, the loop ends.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) without considering input storage.
Utilize dynamic programming to keep track of achievable points given energies left or gathered. Construct a 2D DP table where row denotes use of 'current energy' and column denotes possible number of points.
The dynamic programming approach uses a 1D array to keep track of the maximum points achievable with up to the available energy. By iterating through each enemy's energy requirement, update the possible maximum points in reverse order of energies to ensure correct computations.
C++
Time Complexity: O(n * e), where e is the currentEnergy at maximum, since the DP traversal across energies is performed for each enemy.
Space Complexity: O(e) due to the DP array usage.
According to the problem description, we need to score by defeating enemies with the lowest energy value and increase our energy value by defeating enemies with the highest energy value and marking them.
Therefore, we can sort the enemies by their energy values, then start from the enemy with the highest energy value, always choose the enemy with the lowest energy value to score and consume energy. Next, we add the energy value of the enemy with the highest energy to our current energy and mark that enemy. Repeat the above steps until all enemies are marked.
The time complexity is O(n log n), and the space complexity is O(log n), where n is the number of enemies.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Greedy Approach with Sorting | Time Complexity: O(n log n) due to sorting. |
| Dynamic Programming Approach | Time Complexity: O(n * e), where e is the currentEnergy at maximum, since the DP traversal across energies is performed for each enemy. |
| Greedy + Sorting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy with Sorting and Two Pointers | O(n log n) | O(1) | Best practical solution. Efficient when enemies can be sorted and decisions depend on weakest/strongest choices. |
| Dynamic Programming | O(n * E) | O(n * E) | Useful when exploring all decision states or when greedy conditions are unclear. |
3207. Maximum Points After Enemy Battles | Sorting | Greedy • Aryan Mittal • 2,238 views views
Watch 4 more video solutions →Practice Maximum Points After Enemy Battles with our built-in code editor and test cases.
Practice on FleetCode