Mid/SeniorArrayGreedy

Maximum Points After Enemy Battles

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:

  • Choose an unmarked enemy i such that currentEnergy >= enemyEnergies[i]. By choosing this option:
  • You gain 1 point.
  • Your energy is reduced by the enemy's energy, i.e. currentEnergy = currentEnergy - enemyEnergies[i].
  • If you have at least 1 point, you can choose an unmarked enemy i. By choosing this option:
  • Your energy increases by the enemy's energy, i.e. currentEnergy = currentEnergy + enemyEnergies[i].
  • The enemy i is marked.

Return an integer denoting the maximum points you can get in the end by optimally performing operations.

Example 1
InputenemyEnergies = [3,2,2], currentEnergy = 2
Output3
The described sequence of operations reaches 3 points, which is the maximum possible.
Example 2
InputenemyEnergies = [2], currentEnergy = 10
Output5
Performing the first operation 5 times on enemy 0 results in the maximum number of points.

Constraints

  • 1 <= enemyEnergies.length <= 10^5
  • 1 <= enemyEnergies[i] <= 10^9
  • 0 <= currentEnergy <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate