作者oin1104 (是oin的說)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Fri Jun 21 12:26:53 2024
題目 :
給你一串陣列customers
是在第 i 分鐘 會來的customers[i] 個客人
還有grumpy
是在第 i 分鐘 1會生氣 或0不會生氣的老闆
老闆生氣的話客人就哭哭跑掉
沒生氣的話客人就可以買東西
你可以痛扁老闆一次
讓他不要生氣持續minutes 分鐘
請問最多有多少客人可以買到東西
思路 :
把所有客人 乘上那時候的1-grumpy[i]
這就是不痛扁老闆會買東西的客人
都先看不痛扁老闆會買東西的客人加起來
然後不會買的 用sliding window 記下來
取最大 再加回去
return 結束
```cpp
class Solution {
public:
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes)
{
int len = customers.size();
int res = 0;
int more = 0;
int maxmore = 0;
for(int i = 0 ; i < minutes ; i ++)
{
res += customers[i] * (1-grumpy[i]);
more += grumpy[i] * customers[i];
}
maxmore = max(maxmore , more);
for(int i = minutes ; i < len ; i ++)
{
res += customers[i] * (1-grumpy[i]);
more += grumpy[i] * customers[i];
more -= grumpy[i-minutes] * customers[i-minutes];
maxmore = max(maxmore , more);
}
return res + maxmore;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.53.5 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1718944015.A.000.html
推 Furina: 大師06/21 12:27
→ CanIndulgeMe: 技术大牛06/21 12:27
※ 編輯: oin1104 (1.162.53.5 臺灣), 06/21/2024 12:35:20