Minimum Health to Beat Game — LeetCode 2214 Python Solution
- Problem
- #2214
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
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
class Solution:
def minimumHealth(self, damage: List[int], armor: int) -> int:
return sum(damage) - min(max(damage), armor) + 1Complexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array \textit{damage} |
| Space | O(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.