[Swift]LeetCode1052.愛生氣的書店老闆 | Grumpy Bookstore Owner

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-nafyiuap-mc.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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.github

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.spa

Example 1:code

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:htm

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

今天,書店老闆有一家店打算試營業 customers.length 分鐘。每分鐘都有一些顧客(customers[i])會進入書店,全部這些顧客都會在那一分鐘結束後離開。blog

在某些時候,書店老闆會生氣。 若是書店老闆在第 i 分鐘生氣,那麼 grumpy[i] = 1,不然 grumpy[i] = 0。 當書店老闆生氣時,那一分鐘的顧客就會不滿意,不生氣則他們是滿意的。get

書店老闆知道一個祕密技巧,能抑制本身的情緒,能夠讓本身連續 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 }
相關文章
相關標籤/搜索