MediumNo. 2214LeetCode Premium

Minimum Health to Beat Game — LeetCode 2214 Python Solution

Pattern
Greedy
Topics
Greedy, Array
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

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

Complexity

Time
O(n)where n is the length of the array damage
Space · auxiliary
O(1)

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

Related problems
No.ProblemDifficulty
2216Minimum Deletions to Make Array BeautifulMedium
2208Minimum Operations to Halve Array SumMedium
0011Container With Most WaterMedium
0134Gas StationMedium
0135CandyHard
0179Largest NumberMedium

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.

Free to download. Check it runs on your machine, then pick a plan.