[力扣] 算法 1052 (C#)

1052. 爱生气的书店老板

public class Solution {
    public int MaxSatisfied(int[] customers, int[] grumpy, int X) {
        var ret = 0;
        for (var i = 0; i < customers.Length; i++)
            if (grumpy[i] == 0)
                ret += customers[i];
        var sum = 0;
        for (var i = 0; i < X; i++)
            if (grumpy[i] == 1)
                sum += customers[i];
        var max = sum;
        for (var i = X; i < customers.Length; i++)
        {
            if (grumpy[i - X] == 1)
                sum -= customers[i - X];
            if (grumpy[i] == 1)
                sum += customers[i];
            if (sum > max)
                max = sum;
        }
        return ret + max;
    }
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注