2214. Minimum Health to Beat Game

armor 可以減輕攻擊

T: O(n)

S: O(1)

class Solution {
    public long minimumHealth(int[] damage, int armor) {
       
        long health = 1;
        long max = 0;
        for (int d : damage) {
            health += d;
            max = Math.max(d, max);
        }
        return health - Math.min(max, armor); // if armor > max, just protect max damage
    }
}

/*
case 1: max damage >= armor, so at last -armor
[2,7,4,3], armor = 4

case 2: max damage < armor, so at last. - max
*/

Last updated