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 long sum = accumulate(begin(damage), end(damage), 0LL);
    const int maxDamage = *max_element(begin(damage), end(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)