题目如下:

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 minutes, the bookstore owner is grumpy.  If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0.  When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.

The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.

Return the maximum number of customers that can be satisfied throughout the day.

Example 1:

Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
Output: 16
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.

Note:

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

解题思路:对于grumpy[i] = 0的时间点,显然客户都是满意的,遍历customers可以把所有这些客户的数量求和,同时把对于的customers[i] 设成0。例如示例1中的customers = [1,0,1,2,1,1,7,5],经过操作后变成 [0,0,0,2,0,1,0,5]。接下来就是问题就转换成求customers中长度为X的连续子数组的和的最大值,最后把第一步的中的和加上第二部操作中的最大值即为结果。

代码如下:

class Solution(object):
def maxSatisfied(self, customers, grumpy, X):
"""
:type customers: List[int]
:type grumpy: List[int]
:type X: int
:rtype: int
"""
res = 0
for i in range(len(customers)):
if grumpy[i] == 0:
res += customers[i]
customers[i] = 0
max_count = 0
val = 0
for i in range(len(customers)):
if i < X:
val += customers[i]
continue
max_count = max(val,max_count)
val -= customers[i-X]
val += customers[i]
max_count = max(val, max_count)
#print customers
#print res
return res + max_count

【leetcode】1052. Grumpy Bookstore Owner的更多相关文章

  1. leetcode_1052. Grumpy Bookstore Owner

    1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. Linux内核调试方法总结之strace

    strace [用途] strace是一个功能强大的调试.分析.诊断工具,跟踪程序或进程执行时的系统调用和所接收的信号.可将所调用的系统调用的名称.参数和返回值输出到标准输出或者输出到-o指定的文件. ...

  2. CentOS7 日常操作

    A 安装netstat1.首先配置好本机的yum源: yum repolist all2.利用netstat命令,却提示:-bash: netstat: command not found3.执行yu ...

  3. DRF 组件

    DRF组件中的认证  授权  频率限制   分页  注册器  url控件

  4. 三十七、python中的logging介绍

    A.单文件日志 import logging#定义日志文件#文件格式logging.basicConfig(filename='log.log', format='%(asctime)s-%(name ...

  5. 《图解设计模式》读书笔记5-2 decorator模式

    目录 代码演示 模式的角色和类图 思路拓展 Decorator模式即装饰器模式,就是对类进行装饰,下面通过代码说明. 代码演示 代码展示的内容 有一个类StringDisplay:表示一句话,比如he ...

  6. set_index()与reset_index()函数

    一 set_index()函数 1 主要是理解drop和append参数,注意与reset_index()参数的不同. import pandas as pd df = pd.DataFrame({' ...

  7. python3 configparser模块

    配置文件如下: import configparser conf = configparser.ConfigParser() print(type(conf)) #conf是类 conf.read(' ...

  8. R语言平均值,中位数和众数

    R语言平均值,中位数和众数 R中的统计分析通过使用许多内置函数来执行的.这些函数大部分是R基础包的一部分.这些函数将R向量与参数一起作为输入,并在执行计算后给出结果. 我们在本章中讨论的是如何求平均值 ...

  9. 利用Python进行windows系统上的图像识别与点击(Mac OS系统也可以)

    系统环境: 1.安装了python 2.安装了pyautogui模块 windows系统:无需安装依赖模块,在cmd中直接输入pip install pyautogui即可完成安装 Mac OS系统: ...

  10. Spring004--Spring AOP(mooc)

    一.Spring AOP概览 1.1.AOP(面向切面编程)是什么 1. AOP是一种编程范式,而不是编程语言. 2.解决特写问题,不能解决所有问题 3.是OOP的补充,不是替代 除了面向切面编程,还 ...