在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Today, the bookstore owner has a store open for On some minutes, the bookstore owner is grumpy. If the bookstore owner is grumpy on the i-th minute, The bookstore owner knows a secret technique to keep themselves not grumpy for 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:
今天,书店老板有一家店打算试营业 在某些时候,书店老板会生气。 如果书店老板在第 书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 请你返回这一天营业下来,最多有多少客户能够感到满意的数量。 示例: 输入: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. 提示:
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 }
|
请发表评论