Skip to content

2214. Minimum Health to Beat Game

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
class Solution {
 public:
  long long minimumHealth(vector<int>& damage, int armor) {
    const long sum = accumulate(damage.begin(), damage.end(), 0L);
    const int maxDamage = ranges::max(damage);
    return 1 + sum - min(maxDamage, armor);
  }
};
1
2
3
4
5
6
7
class Solution {
  public long minimumHealth(int[] damage, int armor) {
    final long sum = Arrays.stream(damage).asLongStream().sum();
    final int maxDamage = Arrays.stream(damage).max().getAsInt();
    return 1 + sum - Math.min(maxDamage, armor);
  }
}
1
2
3
class Solution:
  def minimumHealth(self, damage: list[int], armor: int) -> int:
    return 1 + sum(damage) - min(max(damage), armor)