We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Maximum Points After Enemy Battles

Number: 3264

Difficulty: Medium

Paid? No

Companies: Quantbox


Problem Description

You are given an array of enemy energies and an initial amount of energy. Two operations are allowed:

  1. If your current energy is at least the enemy’s energy, you can battle that enemy to lose energy equal to its value and gain 1 point.
  2. If you have at least 1 point, you can “sacrifice” a point to gain energy equal to an enemy’s energy; however, that enemy then becomes marked (cannot be used again for other operations). The goal is to maximize the number of points you can collect by performing these operations optimally.

Key Insights

  • Sorting enemy energies helps to process the weakest enemies first when battling.
  • A two-pointer strategy can be used:
    • One pointer (lo) to pick the enemy with the least energy for a battle (gaining a point).
    • Another pointer (hi) to pick the enemy with the most energy to regain energy (at the expense of a point) when the current energy is insufficient.
  • Greedy choices work here; always battle the weakest enemy if possible, otherwise use one point to gain energy from the strongest remaining enemy when needed.
  • Maintain a running record of the maximum points achieved throughout the process.

Space and Time Complexity

Time Complexity: O(n log n) due to sorting the enemy energies. Space Complexity: O(n) if the sorting algorithm requires additional space; otherwise O(1) excluding the input.


Solution

The solution begins by sorting the enemy energies. Two pointers, lo and hi, are initialized where lo starts at the beginning (smallest energy) and hi at the end (largest energy).
While there are enemies left between lo and hi:

  • If the current energy is sufficient to defeat the enemy pointed by lo, attack that enemy, reducing energy by its amount and incrementing the score.
  • Otherwise, if you have at least one point, sacrifice one point to regain energy by “converting” the enemy at hi (which gives the maximum energy boost) and mark that enemy.
  • Keep track of the maximum points achieved during the process. The greedy strategy ensures that you are maximizing points when you can and judiciously use points to recover energy when necessary.

Code Solutions

# Python solution for Maximum Points After Enemy Battles
def max_points_after_enemy_battles(enemyEnergies, currentEnergy):
    # Sort the enemy energies array
    enemyEnergies.sort()
    lo, hi = 0, len(enemyEnergies) - 1
    points = 0
    max_points = 0

    # Process enemies with two pointers
    while lo <= hi:
        # If you can battle the enemy at lo, do so
        if currentEnergy >= enemyEnergies[lo]:
            currentEnergy -= enemyEnergies[lo]
            points += 1
            lo += 1
            max_points = max(max_points, points)  # update maximum points reached
        # If not enough energy to battle but have at least one point, sacrifice a point to regain energy
        elif points >= 1:
            currentEnergy += enemyEnergies[hi]
            points -= 1
            hi -= 1
        else: 
            # Neither operation is possible; break out of the loop
            break
    return max_points

# Example usage:
# enemyEnergies = [3, 2, 2]
# currentEnergy = 2
# Expected output: 3
print(max_points_after_enemy_battles([3, 2, 2], 2))
← Back to All Questions