The Indian Engineer

Problem 3207 Maximum Points After Enemy Battles

Posted on 5 mins

Array Greedy

Problem Statement

Link - Problem 3207

Question

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:

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:

First operation on enemy 1: points increases by 1, and currentEnergy decreases by 2.
So, points = 1, and currentEnergy = 0. Second operation on enemy 0: currentEnergy increases by 3, and enemy 0 is marked.
So, points = 1, currentEnergy = 3, and marked enemies = [0]. First operation on enemy 2: points increases by 1, and currentEnergy decreases by 2.
So, points = 2, currentEnergy = 1, and marked enemies = [0]. Second operation on enemy 2: currentEnergy increases by 2, and enemy 2 is marked.
So, points = 2, currentEnergy = 3, and marked enemies = [0, 2]. First operation on enemy 1: 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:

Solution

class Solution {
public:
   long long maximumPoints(std::vector<int>& arr, int currentEnergy) {
        sort(arr.begin(), arr.end());

        long long total = 0;
        total = std::accumulate(arr.begin(), arr.end(), 0LL);
        total -= arr[0];

        long long points = 0;
        if (currentEnergy >= arr[0]) {
            points++;
            currentEnergy -= arr[0];
        }

        if (points >= 1) {
            total += currentEnergy;
            points += total / arr[0];
        }

        return points;
    }
};

Complexity Analysis

| Algorithm             | Time Complexity | Space Complexity |
| --------------------- | --------------- | ---------------- |
| Modified Accumulation | O(n log n)      | O(1)             |

Explanation

Intial Thoughts

Sort the enemy energies array to prioritize the enemy with the least energy. Initialize points to 0. Consider the trade-off between scoring points and increasing energy by choosing the most energy-efficient enemy. Think about the benefit of marking an enemy and the potential loss of energy from fighting them.

Intuitive Analysis

Imagine you’re playing a game where you can choose to fight or boost your energy. You want to fight the enemies that require the least energy to beat and then use their energy to boost your own. The initial energy requirement is the lowest energy enemy, and then you can use the remaining enemies to boost your energy. This creates a snowball effect where you can fight and boost your energy repeatedly to maximize points.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

The problem can be solved greedily.

Mark all the others except the smallest one first.

Use the smallest one to increase the energy.

Note that the initial energy should be no less than the smallest enemy.