[力扣] 算法 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;
    }
}

发表回复

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据