Skip to content

Commit 4cb038b

Browse files
authored
Create grumpy-bookstore-owner.py
1 parent 1cc0029 commit 4cb038b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Python/grumpy-bookstore-owner.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maxSatisfied(self, customers, grumpy, X):
6+
"""
7+
:type customers: List[int]
8+
:type grumpy: List[int]
9+
:type X: int
10+
:rtype: int
11+
"""
12+
result, max_extra, extra = 0, 0, 0
13+
for i in xrange(len(customers)):
14+
result += 0 if grumpy[i] else customers[i]
15+
extra += customers[i] if grumpy[i] else 0
16+
if i >= X:
17+
extra -= customers[i-X] if grumpy[i-X] else 0
18+
max_extra = max(max_extra, extra)
19+
return result + max_extra

0 commit comments

Comments
 (0)