LeetCode-1052爱生气的书店老板-简单

标题:1052爱生气的书店老板-简单

题目

有一个书店老板,他的书店开了 n 分钟。每分钟都有一些顾客进入这家商店。给定一个长度为 n 的整数数组 customers ,其中 customers[i] 是在第 i 分钟开始时进入商店的顾客数量,所有这些顾客在第 i 分钟结束后离开。

在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。

当书店老板生气时,那一分钟的顾客就会不满意,若老板不生气则顾客是满意的。

书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 minutes 分钟不生气,但却只能使用一次。

请你返回 这一天营业下来,最多有多少客户能够感到满意 。

示例1

输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
输出:16
解释:书店老板在最后 3 分钟保持冷静。
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.

示例2

输入:customers = [1], grumpy = [0], minutes = 1
输出:1

提示

  • n == customers.length == grumpy.length
  • 1 <= minutes <= n <= 2 * 104
  • 0 <= customers[i] <= 1000
  • grumpy[i] == 0 or 1

代码Go

func maxSatisfied(customers []int, grumpy []int, minutes int) int {
	// allScore := 0
	oldScore := 0
	n := len(customers)
	for i := 0; i < n; i++ {
		// allScore += customers[i]
		if grumpy[i] == 0 {
			oldScore += customers[i]
		}
	}
	// 暴力法
	// for i := 0; i <= n-minutes; i++ {
	// 	tempScore := 0
	// 	for j := 0; j < minutes; j++ {
	// 		if grumpy[i+j] == 1 {
	// 			tempScore += customers[i+j]
	// 		}
	// 	}
	// 	if tempScore > resScore {
	// 		resScore = tempScore
	// 	}
	// }

	// 滑动窗口,推出最大优化值
	curScore := 0
	for i := 0; i < minutes; i++ {
		if grumpy[i] == 1 {
			curScore += customers[i]
		}
	}
	maxScore := curScore
	for i := minutes; i < n; i++ {

		if grumpy[i] == 0 && grumpy[i-minutes] == 1 {
			curScore -= customers[i-minutes]
		} else if grumpy[i] == 1 && grumpy[i-minutes] == 0 {
			curScore += customers[i]
		} else if grumpy[i] == 1 && grumpy[i-minutes] == 1 {
			curScore = curScore + customers[i] - customers[i-minutes]
		} else {
			continue
		}
		if curScore > maxScore {
			maxScore = curScore
		}

	}
	return maxScore + oldScore
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SoaringW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值