题目如下:

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. ORACLE动态监听

    动态监听的原理 pmon在数据库启动到mount或open时,动态从参数文件中读取service_names值.service_names可以为多值(可以有64个,其中包括两个系统的).  servi ...

  2. iOS即时通讯之CocoaAsyncSocket源码解析二

    原文 前言 本文承接上文:iOS即时通讯之CocoaAsyncSocket源码解析一 上文我们提到了GCDAsyncSocket的初始化,以及最终connect之前的准备工作,包括一些错误检查:本机地 ...

  3. leetcode 374猜数字大小

    // Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, ...

  4. django中自定义404错误页面

    自定义404页面,如下5个步骤:1)使用自定义的404页面,必须在setting文件修改DEBUG = False(即关闭debug调试模式)2)必须在setting文件修改ALLOWED_HOSTS ...

  5. EDM营销必知:电子邮件打开和点击的几组数据

    在EDM营销中,了解一下电子邮件何时被打开和点击很重要.这有助于我们在合适的时间发送邮件出去,从而带来最大化的效果. 1.邮件打开的最高峰在早上8点至9点之间,其次是下午三点到四点.因此,在这个时间发 ...

  6. thinkphp5.0学习笔记(二)API后台处理与命名空间

    命名空间 先来看命名空间吧: 命名空间是学习TP的基础, <?php namespace app\lian\c1; class yi{ public $obj = "这是第一个空间里面 ...

  7. Week 9 - 638.Shopping Offers - Medium

    638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...

  8. SSM Maven MallDemo项目为例

    一.创建maven项目 项目结构 创建一个空项目 1. mall (**pom**) 父模块,用于放置公共属性.依赖关系等. 2. mall-util (**jar**) 工具模块,用于放置常用工具类 ...

  9. 倾旋之slack主题协同

    源:https://pocketcorp.slack.com/join/shared_invite/enQtNTk2MDYwNDA4NzU0LTg3ZGVlNDE5NWUzNjJhZTc1MDQ5MT ...

  10. Lowest Common Ancestor of a Binary Tree(二叉树公共祖先)

    来源:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree Given a binary tree, find t ...