Sliding window used for solution

This commit is contained in:
Jacob Delgado 2024-06-20 18:01:08 -07:00
parent 57878d980d
commit 2443eba370
2 changed files with 47 additions and 1 deletions

View 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)

View File

@ -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