Minimum Health to Beat Game - 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 i-th level.

You are also given an integer armor. You may use your armor ability at most once during the game on any level which will protect you from at most armor damage.

You must complete the levels in order and your health must be greater than 0 at all times to beat the game.

Return the minimum health you need to start with to beat the game.

Input & Output

Example 1 — Basic Case
$ Input: damage = [2,7,4,3], armor = 4
Output: 10
💡 Note: Use armor on level 1 (damage 7), reducing it to 3. Total damage becomes [2,3,4,3]. Starting with 10 health: 10→8→5→1→(-2), but we need health > 0, so minimum is 10.
Example 2 — High Armor Value
$ Input: damage = [2,5,3,4], armor = 7
Output: 10
💡 Note: Use armor on level 1 (damage 5), reducing it to 0. Total damage becomes [2,0,3,4]. Starting with 10 health: 10→8→8→5→1. Minimum health needed is 10.
Example 3 — Small Armor
$ Input: damage = [3,3,3], armor = 1
Output: 8
💡 Note: Use armor on any level to reduce one 3 to 2. Total damage becomes [2,3,3] or similar. Starting with 8 health: 8→6→3→0, but need > 0, so minimum is 8.

Constraints

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

Visualization

Tap to expand
Minimum Health to Beat GameFind optimal armor placement to minimize starting healthGame Levels2743🛡️Armor: 4Optimal StrategyUse armor on level 1 (damage 7 → 3)Effective damage: [2, 3, 4, 3] = 12 totalMinimum Starting Health: 10
Understanding the Visualization
1
Input
Array of damage per level and armor value
2
Strategy
Choose optimal level to use armor for maximum benefit
3
Output
Minimum health needed to survive all levels
Key Takeaway
🎯 Key Insight: Use armor on the level with maximum damage to minimize total damage and starting health requirement
Asked in
Amazon 15 Google 12
32.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen