Merge branch 'master' of https://gitea.arufalab.com/ImAlpha/LeetCode
This commit is contained in:
commit
50e3c941ca
35
DailyQuestion/1052_Grumpy_bookstore_owner.py
Normal file
35
DailyQuestion/1052_Grumpy_bookstore_owner.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
def maxSatisfied(customers, grumpy, minutes):
|
||||||
|
"""
|
||||||
|
:type customers: List[int]
|
||||||
|
:type grumpy: List[int]
|
||||||
|
:type minutes: int
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
# Sliding Window Approach
|
||||||
|
i = 0
|
||||||
|
j = 0
|
||||||
|
sum_unsatisfied = 0
|
||||||
|
max_sum = float('-inf')
|
||||||
|
|
||||||
|
# Using sliding window
|
||||||
|
while j < len(grumpy):
|
||||||
|
if grumpy[j] == 1: # Calculation part
|
||||||
|
sum_unsatisfied += customers[j]
|
||||||
|
|
||||||
|
if j - i + 1 < minutes:
|
||||||
|
j += 1
|
||||||
|
elif j - i + 1 == minutes:
|
||||||
|
max_sum = max(max_sum, sum_unsatisfied)
|
||||||
|
if grumpy[i] == 1: # Removing calculation part using i
|
||||||
|
sum_unsatisfied -= customers[i]
|
||||||
|
i += 1 # Sliding the window
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
for k in range(len(customers)): # Now finding only satisfied customers (grumpy[k] == 0)
|
||||||
|
if grumpy[k] == 0:
|
||||||
|
max_sum += customers[k]
|
||||||
|
|
||||||
|
return max_sum
|
||||||
|
|
||||||
|
|
||||||
|
maxSatisfied([1,0,1,2,1,1,7,5], [0,1,0,1,0,1,0,1], 3)
|
||||||
@ -1,7 +1,18 @@
|
|||||||
def plusOne(digits):
|
def plusOne(digits):
|
||||||
zeros = 0
|
zeros = 0
|
||||||
if digits[-1] == 9:
|
if digits[-1] == 9:
|
||||||
digits[-1] = 1
|
if len(digits) < 2:
|
||||||
|
digits[-1] = 1
|
||||||
|
digits.append(0)
|
||||||
|
return digits
|
||||||
|
for i in range(len(digits)):
|
||||||
|
if digits[i] == 9:
|
||||||
|
zeros += 1
|
||||||
|
if digits[i-1] == 9:
|
||||||
|
digits[i] = 0
|
||||||
|
else:
|
||||||
|
digits[i] = 1
|
||||||
|
break
|
||||||
digits.append(0)
|
digits.append(0)
|
||||||
else:
|
else:
|
||||||
digits[-1] = digits[-1] + 1
|
digits[-1] = digits[-1] + 1
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user