Skip to content

Commit 1cc0029

Browse files
authored
Create grumpy-bookstore-owner.cpp
1 parent 5c692e0 commit 1cc0029

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

C++/grumpy-bookstore-owner.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
7+
int result = 0, max_extra = 0, extra = 0;
8+
for (auto i = 0; i < customers.size(); ++i) {
9+
result += grumpy[i] ? 0 : customers[i];
10+
extra += grumpy[i] ? customers[i] : 0;
11+
if (i >= X) {
12+
extra -= grumpy[i - X] ? customers[i - X] : 0;
13+
}
14+
max_extra = max(max_extra, extra);
15+
}
16+
return result + max_extra;
17+
}
18+
};

0 commit comments

Comments
 (0)