ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Leetcode 1052. 爱生气的书店老板

2019-06-29 11:50:33  阅读:329  来源: 互联网

标签:sz 1052 书店 int customers sumB 爱生气 Leetcode sumA


题目

今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。

在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。 当书店老板生气时,那一分钟的顾客就会不满意,不生气则他们是满意的。

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

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

示例:

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

提示:

1 <= X <= customers.length == grumpy.length <= 20000
0 <= customers[i] <= 1000
0 <= grumpy[i] <= 1

题解

用数组sumA记录假定老板每分钟都不生气时候顾客的进入情况的前缀和,用数组sumB记录在生气情况下顾客的进入情况的前缀和,扫描数组,若老板在[i, i + X]分钟保持冷静,则该种情况下答案为sumB[customers.length - 1] + (sumA[i + X] - sumA[i]) - (sumB[i + X] - sumB[i]),获得最大的答案并返回。

代码

C++

class Solution {
public:
    int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
        int sz = customers.size();
        int* sumA = new int[sz];
        int* sumB = new int[sz];
        sumA[0] = customers[0];
        sumB[0] = customers[0] * (1 - grumpy[0]);
        for(int i = 1; i < sz; i++)
        {
            sumA[i] = customers[i] + sumA[i - 1];
            sumB[i] = customers[i] * (1 - grumpy[i]) + sumB[i - 1];
        }
        int ans = sumB[sz - 1];
        for(int i = X - 1; i < sz; i++)
        {
            int current = sumB[sz - 1];
            current = current 
                + (i >= X ? (sumA[i] - sumA[i - X]) : sumA[i])
                - (i >= X ? (sumB[i] - sumB[i - X]) : sumB[i]);
            ans = max(ans, current);
        }

        return ans;
    }
};

标签:sz,1052,书店,int,customers,sumB,爱生气,Leetcode,sumA
来源: https://blog.csdn.net/u010286160/article/details/93603384

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有