【leetcode】1052. Grumpy Bookstore Owner
题目如下:
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
, otherwisegrumpy[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的更多相关文章
- leetcode_1052. Grumpy Bookstore Owner
1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- 解决Firefox已阻止运行早期版本Adobe Flash
解决Firefox已阻止运行早期版本Adobe Flash 类别 [随笔分类]web 解决Firefox已阻止运行早期版本Adobe Flash 最近火狐浏览器不知抽什么风, ...
- mysql5.6.36 源码安装过程
参考:http://www.linuxidc.com/Linux/2015-06/119354.htm cmake编译时,提示错误 CMake Error: The source directory ...
- Vue知识整理4:v-html标签
可以在数据绑定中使用html标签,这样在变量里可以使用html标签输出结果,如下所示:
- vue中淡入淡出示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Binder进程与线程ProcessState以及IPCThreadState
ProcessState以及IPCThreadState ProcessState是负责打开Binder节点并做mmap映射,IPCThreadState是负责与Binder驱动进行具体的命令交互. ...
- 3 Vue.js基础
Vue中的过滤器.钩子函数.指令.字符串填充.以及部分方法使用的案例(操作表单) <!DOCTYPE html> <html lang="en"> < ...
- TensorFlow学习笔记5-概率与信息论
TensorFlow学习笔记5-概率与信息论 本笔记内容为"概率与信息论的基础知识".内容主要参考<Deep Learning>中文版. \(X\)表示训练集的设计矩阵 ...
- Python3的基本数据类型
2.1. Python3中六个标准的基本数据类型: Number(数字) String(字符串) Sets(集合) Tuple(元组) List(列表) Dictionary(字典) 2.2. Pyt ...
- javascript实现深浅拷贝
深浅拷贝通常是对于引用数据类型进行的(数据类型为:对象(Object).数组(Array).函数(Function)) 浅拷贝: let obj = {id: 1, name: 2}; let new ...
- 常用js方法封装使用
// 冒泡排序 export function bubbleSort(arr) { let i = arr.length - 1; while (i > 0) { let maxIndex = ...