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[]==)
presum[]=customers[];
for(int i=;i<grumpy.size();i++)
{
presum[i]=grumpy[i]==?(presum[i-]+customers[i]):presum[i-];
}
int max_sum=presum[X-],last_pos=X-;
for(int i=X;i<grumpy.size();i++)
{
int st_pos = i-X+;
if(max_sum<presum[i]-presum[st_pos-])
{
max_sum=presum[i]-presum[st_pos-];
last_pos=i;
}
}
int res=;
for(int i=;i<grumpy.size();i++)
{
if(grumpy[i]==)
res+=customers[i];
else if(i>=last_pos-X+&&i<=last_pos)
res+=customers[i];
}
return res;
}
};

leetcode_1052. Grumpy Bookstore Owner的更多相关文章

  1. 【leetcode】1052. Grumpy Bookstore Owner

    题目如下: Today, the bookstore owner has a store open for customers.length minutes.  Every minute, some ...

  2. Weekly Contest 138

    1051. Height Checker Students are asked to stand in non-decreasing order of heights for an annual ph ...

  3. 100-days: twenty-eight

    Title: Lawrence Ferlinghetti's(劳伦斯·费林盖蒂) enduring San Francisco(旧金山) 劳伦斯·费林盖蒂心中的旧金山,历久弥新 费林盖蒂:美国垮掉的一 ...

  4. owner:轻松管理java项目配置

    前段时间,一同事说在 github 上“活捉了”一个很有趣的开源项目,它是一个超轻量级的 jar 包,能够帮助你在 java 项目中摒弃样板式的 properties 配置代码,让你轻松自如地管理和使 ...

  5. IOS 杂笔-14(被人遗忘的owner)

    *owner在开发中现在已经很少用了 有兴趣的童鞋可以看看* 我们遇到owner通常是在类似 [[[NSBundle mainBundle] loadNibNamed:@"Food" ...

  6. 【转】WPF: 自动设置Owner的ShowDialog 适用于MVVM

    原文地址:http://www.mgenware.com/blog/?p=339 WPF中的Windows的ShowDialog方法并没有提供设置Owner的参数,开发者需要在ShowDialog前设 ...

  7. Linked Server: EXECUTE permission denied on object 'xp_prop_oledb_provider', database 'master', owner 'dbo'

    问题出现环境: 使用SQL Server Management Studio 2008 连接到SQL Server 2000的数据库,点击其中一个Oracle链接服务器,单击"目录" ...

  8. Unable to determine if the owner (Domain\UserName) of job JOB_NAME has server access

    早上巡检的的时候,发现一数据库的作业报如下错误(作业名等敏感信息已经替换),该作业的OWNER为一个域账号: JOB RUN: 'JOB_NAME' was run on 2016-6-1 at 7: ...

  9. SQL Server修改数据库对象所有者(Owner)浅析

    在SQL Server数据库中如何修改数据库对象(表.视图.存储过程..)的所有者(Owner)呢?一般我们可以使用系统提供的系统存储过程sp_changeobjectowner来修改. 我们先看看s ...

随机推荐

  1. Flutter实战视频-移动电商-53.购物车_商品列表UI框架布局

    53.购物车_商品列表UI框架布局 cart_page.dart 清空原来写的持久化的代码; 添加对应的引用,stless生成一个静态的类.建议始终静态的类,防止重复渲染 纠正个错误,上图的CartP ...

  2. VC中使用GDI+实现为按钮加载Png图片

    http://blog.csdn.net/flyfish1986/article/details/5381605 VC中使用GDI+实现为按钮加载Png图片 http://www.codeprojec ...

  3. 算法学习--Day6

    题目描述 实现一个加法器,使其能够输出a+b的值. 输入描述: 输入包括两个数a和b,其中a和b的位数不超过1000位. 输出描述: 可能有多组测试数据,对于每组数据, 输出a+b的值. 示例1 输入 ...

  4. 51nod1212【最小生成树kruskal算法】

    思路: 利用破圈法. #include <bits/stdc++.h> using namespace std; typedef long long LL; const int N=1e3 ...

  5. 着色语言(Shader Language)

    摘抄"GPU Programming And Cg Language Primer 1rd Edition" 中文名"GPU编程与CG语言之阳春白雪下里巴人" ...

  6. Git的使用方法与GitHub项目托管方法

    Git的安装 Windows上安装Git 访问网址:https://git-for-windows.github.io/ 点击Download下载,下载后双击安装包进行安装,一直"下一步&q ...

  7. unicode码表和标准下载 unicode官网

  8. JavaScript 对象的原型扩展(JS面向对象中的继承)

    <script type="text/javascript"> function person(name, age) { this._name = name; this ...

  9. 【转】String hashCode 方法为什么选择数字31作为乘子

    某天,我在写代码的时候,无意中点开了 String hashCode 方法.然后大致看了一下 hashCode 的实现,发现并不是很复杂.但是我从源码中发现了一个奇怪的数字,也就是本文的主角31.这个 ...

  10. 牛客练习赛42C(枚举权值)

    传送门 思路:既然无法枚举每个情况,那就枚举每个出现过的权值,加和.那么每个权值出现了多少次呢?用总数减去一次都选不中这个数的次数即可,类似概率的方法. #include <bits/stdc+ ...