leetcode_1052. Grumpy Bookstore Owner

1052. Grumpy Bookstore Owner

https://leetcode.com/problems/grumpy-bookstore-owner/

题意:每个时刻i会有customer[i]个顾客进店,在i时刻店主的情绪是grumpy[i],若grumpy[i]==1则店主脾气暴躁,那么顾客会不满意,否则顾客讲感到满意,店主会一个技巧保证连续X时间内不暴躁,但他只能使用一次。问最多可以有多少顾客满意。



解法:找到一个连续X时间段,使用技巧后,得到的收益最大。计算前缀和,然后依次遍历所有连续X时间段,找到使用技巧能使最多的顾客从不满意变为满意。

class Solution
{
public:
    int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X)
    {
        vector<int> presum(grumpy.size());
        if(grumpy[0]==1)
            presum[0]=customers[0];
        for(int i=1;i<grumpy.size();i++)
        {
            presum[i]=grumpy[i]==1?(presum[i-1]+customers[i]):presum[i-1];
        }
        int max_sum=presum[X-1],last_pos=X-1;
        for(int i=X;i<grumpy.size();i++)
        {
            int st_pos = i-X+1;
            if(max_sum<presum[i]-presum[st_pos-1])
            {
                max_sum=presum[i]-presum[st_pos-1];
                last_pos=i;
            }
        }
        int res=0;
        for(int i=0;i<grumpy.size();i++)
        {
            if(grumpy[i]==0)
                res+=customers[i];
            else if(i>=last_pos-X+1&&i<=last_pos)
                res+=customers[i];
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/jasonlixuetao/p/10925898.html

时间: 2024-08-05 05:49:27

leetcode_1052. Grumpy Bookstore Owner的相关文章

Leetcode 1052 Grumpy Bookstore Owner. (滑动窗口)

目录 问题描述 例子 方法 Leetcode 1052 问题描述 Today, the bookstore owner has a store open for customers.length minutes. Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute. On some minu

【NetApp】移除ADP分区后的disk的owner

1)移除owner,需要到node模式下,键入如下命令: Cluster1-01*> options disk.auto_assign disk.auto_assign             off        (value might be overwritten in takeover) Cluster1-01*> Cluster1-01*> disk remove_ownership 0b.00.11P1 Volumes must be taken offline. Are a

实验六:Bookstore项目测试缺陷报告

一.                 Bookstore项目测试缺陷报告 缺陷编号 01.01.0001 发现人 林臻 记录日期 2016-06-12 所属模块 购物车模块 确认人 林臻 确认日期 2016-06-12 当前状态 公开 严重度 3 优先级 3 问题概述 用户在加入购物车添加数量为0时,点击购买也能添加进购物车. 问 题 再 现 描 述 登录用户,选择图书分类,; 选择图书C++购买数量为1 ,查看购物车已添加; 选择图书Oracle购物数量为0,购买,查看购物车,书籍已添加; 图

第四次博客作业:bookstore缺陷报告

Bookstore系统集成测试缺陷报告 ------------------------------------------------------------------------------------------------------------------ 缺陷编号:01.01.01             发现人:林怡            记录日期:2016-06-12 所属模块:购物车                 确认人:林怡            确认日期:2016-06

bookstore网上书店测试缺陷报告2

Bookstore网上书店系统测试缺陷报告   缺陷编号 01.01.0002 发现人 吴赵昕 记录日期 2016-06-10 所属模块 购物车 确认人 吴赵昕 确认日期 2016-06-10 当前状态 公开 严重度 3 优先级 3 问题概述 不同用户登陆后共享同一个购物车 问 题 再 现 描 述 登录用户a,查看购物车内容; 登录用户b,查看购物车内容; 在用户b下往购物车添加书目; 登录用户a,发现购物车内容与用户b购物车内容相同. 问题隔离描述 重复登录不同用户,并且更新书目,问题依然.

SQL SERVER 属性OWNER不可用于数据库xxx。该对象可能没有此属性,也可能是访问权限不足而无法检索。

今天遇到一个案例:右键单击数据库的属性时出现下面错误提示: 属性Owner不可用于数据库xxx,该对象可能没有此属性,也可能是访问权限不足而无法检索. 使用脚本查看该数据库的Owner时发现Owner为null.具体原因是因为该数据库的Owner是一个系统管理员的账号,由于该同事离职,接手的系统管理员将该账号清除了,所以出现上面错误.此时用下面脚本查询,就会发现该Owner为null值. SELECT  d.name ,         owner_sid ,         l.name FR

如何做好一个 Product Owner? 其实只需懂得好好的 “爱” 自己

许多人都在探讨著一个问题:如何做好一个 Product Owner ? 有的人会搬出一大堆敏捷的书, 一大堆敏捷的实践来回答这个问题.事实上, 这些书.实践都是多余的, 甚至可以说都是些废话. 其实, 大家只需向 Steve Jobs 学习, 便足矣. 因为, Steve Jobs 是这世上最成功的 Product Owner. Steve Jobs 具备了以下的特质, 使他成为这世上最成功的 Product Owner: Steve Jobs 懂得好好的 "爱" 自己.而不是被世俗价

The File&#39;s Owner

The File Owner is an instantiated, runtime object that owns the contents of your nib and its outlets/actions when the nib is loaded. It can be an instance of any class you like - take a look at the identity tab of the tool palette. For example, consi

【iOS】Xib的使用与File&#39;Owner总结

一.XIB的适用范围 xib(也叫Nib)与storyboard一样是用来描述界面的. storyboard描述的是比较大型的,大范围.适合描述界面跳转等. 二.XIB的使用 Xib是小范围的的,是轻量级的.比较适合描述小块的界面. 创建xib:新建界面里面user interface -->empty. 加载xib:[NSBundle mainBudle]loadNibNamed:XXX-.]来加载xib 三.关于loadNibNameed方法 [[NSBundle mainBundle] l