2214. Minimum Health to Beat Game - Detailed Explanation

Problem Statement

You are given an integer array damage where each element represents the change in your health after completing a level:

  • A positive value means you lose that much health (damage).

  • A negative value means you gain health (healing).

You play the levels in order. Your goal is to finish all levels without your health ever dropping below 1. Determine the minimum initial health required to accomplish this.

Examples

Example 1:

  • Input: damage = [3, 4, 3]
  • Output: 11
  • Explanation:
    • Start with 11 health.
    • Level 1: 11 – 3 = 8
    • Level 2: 8 – 4 = 4
    • Level 3: 4 – 3 = 1
    • Health never drops below 1.

Example 2:

  • Input: damage = [1, 2, 3]
  • Output: 7
  • Explanation:
    • Start with 7 health.
    • Level 1: 7 – 1 = 6
    • Level 2: 6 – 2 = 4
    • Level 3: 4 – 3 = 1
    • Health never drops below 1.

Example 3:

  • Input: damage = [5, -3, 2]
  • Output: 6
  • Explanation:
    • Start with 6 health.
    • Level 1: 6 – 5 = 1
    • Level 2: 1 + 3 = 4 (healing from -3)
    • Level 3: 4 – 2 = 2
    • Health never drops below 1.

Constraints

  • 1 ≤ damage.length ≤ 10⁵
  • -10⁴ ≤ damage[i] ≤ 10⁴

Hints

  1. Cumulative Health: Think about how your health changes as you progress through each level. Compute the running (or prefix) sum of the damage array.
  2. Tracking the Lowest Point: Identify the minimum value reached by the running sum. If at any point your cumulative health is negative, that tells you how much extra initial health you need to avoid ever falling below 1.

Approach Overview

There are multiple ways to solve this problem. Let’s discuss two approaches:

Brute Force Approach

  • Idea: Start with an initial health of 1, simulate the game level by level, and check if your health ever falls below 1. If it does, increment the starting health and try again.
  • Drawback: This method requires simulating the entire game repeatedly, which results in a high time complexity. For large arrays, this approach is not feasible.

Optimal Approach Using Prefix Sum

  • Idea:

    • Compute the running sum of health changes as you progress through the levels.
    • Find the minimum value reached by this running sum.
    • If the minimum value is below 0 (say, –X), you need at least 1 + X extra health to ensure the lowest point is raised to 1.
  • How It Works:

    • Let runningSum be the cumulative sum of the damage changes when starting at 0.
    • Let minRunningSum be the minimum value of this running sum.
    • The minimum initial health required is:
      Initial Health = max(1, 1 − minRunningSum)
  • Example Walkthrough (damage = [5, -3, 2]):

    • Start with a running sum of 0.
    • After Level 1: 0 – 5 = –5
    • After Level 2: –5 + 3 = –2
    • After Level 3: –2 – 2 = –4
    • The minimum running sum is –5.
    • To ensure that even at the lowest point your health is at least 1, you need an extra 1 – (–5) = 6 initial health.
    • Therefore, the answer is 6.
  • Time Complexity: O(n)

  • Space Complexity: O(1)

Code Implementation

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time Complexity:

    • Both the Python and Java solutions iterate through the array once.
    • O(n) where n is the number of levels.
  • Space Complexity:

    • O(1) additional space is used (only a few variables are maintained).

Step-by-Step Walkthrough (Visual Example)

Let’s walk through an example with damage = [5, -3, 2]:

  1. Initialization:

    • runningSum = 0
    • minRunningSum = +∞
  2. Level 1 (damage = 5):

    • Update: runningSum = 0 - 5 = -5
    • Update: minRunningSum = min(+∞, -5) = -5
  3. Level 2 (damage = -3, healing actually):

    • Update: runningSum = -5 - (-3) = -5 + 3 = -2
    • Update: minRunningSum = min(-5, -2) = -5
  4. Level 3 (damage = 2):

    • Update: runningSum = -2 - 2 = -4
    • Update: minRunningSum = min(-5, -4) = -5
  5. Final Calculation:

    • Since minRunningSum is –5, we need extra health = 1 - (-5) = 6.
    • Therefore, the minimum initial health required is 6.

A visual chart of the running sum would show the lowest point at –5, which we “lift” to 1 by adding 6 to our starting health.

Common Mistakes & Edge Cases

  • Common Mistakes:

    • Not handling healing correctly (i.e., treating negative values as further damage).
    • Forgetting that the health must never drop below 1 (not 0).
    • Overcomplicating the simulation by trying to check every possible starting health rather than using the prefix sum approach.
  • Edge Cases:

    • When the damage array is of length 1.
    • When all values are negative (in which case the optimal starting health remains 1 since you’re only healing).
    • When the cumulative sum never goes below 1; the answer should be 1.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
204. Count Primes - Detailed Explanation
Learn to solve Leetcode 204. Count Primes with multiple approaches.
735. Asteroid Collision - Detailed Explanation
Learn to solve Leetcode 735. Asteroid Collision with multiple approaches.
542. 01 Matrix - Detailed Explanation
Learn to solve Leetcode 542. 01 Matrix with multiple approaches.
769. Max Chunks To Make Sorted - Detailed Explanation
Learn to solve Leetcode 769. Max Chunks To Make Sorted with multiple approaches.
295. Find Median from Data Stream - Detailed Explanation
Learn to solve Leetcode 295. Find Median from Data Stream with multiple approaches.
628. Maximum Product of Three Numbers - Detailed Explanation
Learn to solve Leetcode 628. Maximum Product of Three Numbers with multiple approaches.
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.