• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode1052.爱生气的书店老板|GrumpyBookstoreOwner

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。

在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。 当书店老板生气时,那一分钟的顾客就会不满意,不生气则他们是满意的。

书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 X 分钟不生气,但却只能使用一次。

请你返回这一天营业下来,最多有多少客户能够感到满意的数量。

示例:

输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
输出:16
解释:
书店老板在最后 3 分钟保持冷静。
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.

提示:

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

240ms
 1 class Solution {
 2     func maxSatisfied(_ customers: [Int], _ grumpy: [Int], _ X: Int) -> Int {
 3         var base = 0
 4         for i in 0..<customers.count where grumpy[i] == 0 {
 5             base += customers[i]
 6         }
 7         
 8         var curr = base, ans = base
 9         for i in 0..<customers.count {
10             if grumpy[i] == 1 {
11                 curr += customers[i]
12             }
13 
14             if i >= X {
15                 if grumpy[i-X] == 1 {
16                     curr -= customers[i-X]
17                 }
18             }
19             ans = max(ans, curr)
20         }
21         
22         return ans
23     }
24 }

Runtime: 244 ms

Memory Usage: 21.3 MB
 1 class Solution {
 2     func maxSatisfied(_ customers: [Int], _ grumpy: [Int], _ X: Int) -> Int {
 3         var customers:[Int] = customers
 4         var grumpy:[Int] = grumpy
 5         let n:Int = customers.count
 6         
 7         var ans:Int = 0
 8         for i in 0..<n
 9         {
10             if (grumpy[i] == 0) || i < X
11             {
12                 ans += customers[i]
13             }
14         }
15         var cur:Int = ans
16         for p in X..<n
17         {
18             let q:Int = p - X
19             if grumpy[q] != 0 {cur -= customers[q]}
20             if grumpy[p] != 0 {cur += customers[p]}
21             ans = max(ans, cur)
22         }
23         return ans
24     }
25 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Welcome-to-Swift-23访问控制(AccessControl)发布时间:2022-07-14
下一篇:
swift学习笔记发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap