Minimum Health to Beat Game — LeetCode 2214 Python Solution

MediumLeetCode PremiumGreedyArray
Problem
#2214
Pattern
Greedy
Reading time
2 min

The problem

You are playing a game that has n levels numbered from 0 to n - 1. You are given a 0-indexed integer array damage where damage[i] is the amount of health you will lose to complete the ith level.

Example

Input
damage = [2,7,4,3], armor = 4
Output
13
Explanation
One optimal way to beat the game starting at 13 health is:

Python solution

Python
class Solution:
    def minimumHealth(self, damage: List[int], armor: int) -> int:
        return sum(damage) - min(max(damage), armor) + 1

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array \textit{damage}
SpaceO(1) auxiliary

Pattern: Greedy

Take the locally best option every time — when you can prove that never costs you later. LeetCode 2214. Minimum Health to Beat Game is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.

The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2214. Minimum Health to Beat Game?
LeetCode 2214. Minimum Health to Beat Game is rated Medium on LeetCode.
What is the time complexity of LeetCode 2214. Minimum Health to Beat Game?
The Python solution on this page runs in O(n), where n is the length of the array \textit{damage}.
What is the space complexity of LeetCode 2214. Minimum Health to Beat Game?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2214. Minimum Health to Beat Game cover?
LeetCode 2214. Minimum Health to Beat Game is tagged Greedy and Array on LeetCode.
Is LeetCode 2214. Minimum Health to Beat Game a premium problem?
Yes. LeetCode 2214. Minimum Health to Beat Game is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview