1052. 爱生气的书店老板

今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。
在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。 当书店老板生气时,那一分钟的顾客就会不满意,不生气则他们是满意的。
书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 X 分钟不生气,但却只能使用一次。
请你返回这一天营业下来,最多有多少客户能够感到满意的数量。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/grumpy-bookstore-owner

class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int X) {
        int result = 0;
		for (int i = 0; i < grumpy.length; i++) {
			if (grumpy[i] == 0) {
				result += customers[i];// 计算所有不生气顾客数量
			}
		}
		int sum = 0;
		int max = 0;
		for (int i = 0; i < grumpy.length; i++) {
			if (i < X) {
				if (grumpy[i] == 1) {
					sum += customers[i];// 先计算最前面X个生气时顾客总数
				}; 
			}
		}
		max = sum;
		// 依次计算后面连续X个生气时顾客总数
		for (int i = X; i < grumpy.length; i++) {
			if (grumpy[i] == 1) {
				max += customers[i];// 加上生气顾客数
			} 
			if (grumpy[i-X] == 1) {
				max -= customers[i-X];// 减去生气顾客数
			}
			sum = Math.max(max, sum); // 取生气顾客数最大值 
		}
		return sum + result; // 返回最多满意的数量
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值