Minimum Health to Beat Game

Updated on 16 June, 2025
Minimum Health to Beat Game header image

Problem Statement

In this scenario, a player is navigating through n levels of a game, each level causing a certain amount of health damage to the player. The amount of damage taken at each level is represented by the damage[] array where damage[i] denotes the health lost at level i. To aid in surviving the game, the player is equipped with armor that can be deployed once to mitigate up to armor amount of damage at any chosen level. To successfully complete the game, the player's health must always remain above zero.

The challenge lies in determining the minimum starting health needed for the player to survive through all levels with the strategic use of the armor.

Examples

Example 1

Input:

damage = [2,7,4,3], armor = 4

Output:

13

Explanation:

One optimal way to beat the game starting at 13 health is:
On round 1, take 2 damage. You have 13 - 2 = 11 health.
On round 2, take 7 damage. You have 11 - 7 = 4 health.
On round 3, use your armor to protect you from 4 damage. You have 4 - 0 = 4 health.
On round 4, take 3 damage. You have 4 - 3 = 1 health.
Note that 13 is the minimum health you need to start with to beat the game.

Example 2

Input:

damage = [2,5,3,4], armor = 7

Output:

10

Explanation:

One optimal way to beat the game starting at 10 health is:
On round 1, take 2 damage. You have 10 - 2 = 8 health.
On round 2, use your armor to protect you from 5 damage. You have 8 - 0 = 8 health.
On round 3, take 3 damage. You have 8 - 3 = 5 health.
On round 4, take 4 damage. You have 5 - 4 = 1 health.
Note that 10 is the minimum health you need to start with to beat the game.

Example 3

Input:

damage = [3,3,3], armor = 0

Output:

10

Explanation:

One optimal way to beat the game starting at 10 health is:
On round 1, take 3 damage. You have 10 - 3 = 7 health.
On round 2, take 3 damage. You have 7 - 3 = 4 health.
On round 3, take 3 damage. You have 4 - 3 = 1 health.
Note that you did not use your armor ability.

Constraints

  • n == damage.length
  • 1 <= n <= 105
  • 0 <= damage[i] <= 105
  • 0 <= armor <= 105

Approach and Intuition

When tackling this problem, the goal is to minimize the initial health required for survival by effective use of the armor. Here's a clear breakdown of the approach:

  1. Calculate the total damage without any armor, which is simply the cumulative sum of all values in the damage[] array.
  2. For each level, compute how effective the armor would be. This is done by considering the actual damage on that level and how much of it can be blocked by the armor. The actual benefit is the lesser of the armor value or the damage at that level.
  3. Compute the maximum benefit obtained from using the armor on any given level.
  4. Calculate the required starting health by subtracting this maximum benefit from the total damage and then adding one (to ensure the health remains above zero across all levels).

This approach not only ensures that the armor is used optimally (i.e., at the level where it reduces the most damage) but also ensures that the player's health does not drop to zero or below at any level. Through this method, the computation is both efficient and straightforward, addressing the problem directly by focusing on the key variables—total damage and optimal armor use.

Solutions

  • C++
  • Java
cpp
class Solution {
public:
    long long minimumHealth(vector<int>& damage, int armor) {
        int highestDamage = 0;
        long long sumDamage = 0;
        
        for (int dmg : damage) {
            sumDamage += dmg;
            highestDamage = max(highestDamage, dmg);
        }

        return sumDamage - min(armor, highestDamage) + 1;
    }
};

The provided C++ solution computes the minimum health necessary to beat a game where damage is taken at various points. The function minimumHealth accepts two parameters: damage, a vector of integers representing the damage taken at each encounter, and armor, an integer representing the maximum amount of damage that can be reduced in a single encounter.

Here's how the code accomplishes the task:

  1. Initialize two variables: highestDamage to track the most severe single damage encounter, and sumDamage to store the cumulative damage across all encounters.
  2. Iterate over the damage vector, updating sumDamage with each encounter's damage and updating highestDamage if a current damage value exceeds the previously found values.
  3. Calculate the needed minimum health by reducing the cumulative damage, sumDamage, by the lesser of highestDamage or armor. Add 1 to account for surviving at least with 1 health point after all damage is taken.

The final result is the precise calculation of minimum health required which factors in optimal usage of the available armor against the most significant single damage instance. This method ensures efficiency by traversing the damage list only once and performing minimal comparisons and calculations, thus operating optimally for scenarios with large lists of damage instances.

java
class Solution {
    public long calculateMinimumHealth(int[] hits, int shield) {
        int peakDamage = 0;
        long sumDamage = 0;

        for (int hit : hits) {
            sumDamage += hit;
            peakDamage = Math.max(peakDamage, hit);
        }

        return sumDamage - Math.min(shield, peakDamage) + 1;
    }
}

In this solution for the "Minimum Health to Beat Game" problem using Java, the goal is to determine the minimum health required to sustain through a series of damaging hits even when a shield is utilized for maximum single-hit damage mitigation.

Assume you have an array named hits which contains integers denoting the amount of damage dealt by each consecutive hit in a game. Additionally, an integer shield signifies the maximum amount of damage a shield can absorb from a single hit.

  • First, initialize two variables:

    • peakDamage to store the maximum damage dealt in a single hit.
    • sumDamage to accumulate the total damage from all hits.
  • Iterate through the hits array:

    • Increment sumDamage with the damage of the current hit.
    • Update peakDamage to reflect the highest damage dealt in a single hit using the Math.max function.
  • After calculating the sum of damages and identifying the peak damage, compute the minimum health required to survive all hits:

    • Subtract the smallest value between the shield's capacity and the peak damage from the total sum of damages. This represents utilizing the shield optimally against the most damaging single hit to minimize overall health loss.
    • Add 1 to the result to ensure that the player survives with at least 1 health point.

Given this implementation, return the computed sumDamage adjusted for optimal shield usage plus one, to determine the minimum health necessary to beat the game. This approach efficiently aggregates total potential damage and applies the most effective mitigation using the available shield against the most extreme single hit, providing a clear understanding of the minimum threshold of health required.

Comments

No comments yet.