From 2443eba370e4a00540ecdddb728238891e9ef779 Mon Sep 17 00:00:00 2001 From: Jacob Delgado Date: Thu, 20 Jun 2024 18:01:08 -0700 Subject: [PATCH 1/3] Sliding window used for solution --- DailyQuestion/1052_Grumpy_bookstore_owner.py | 35 +++++++++++++++++++ .../Arrays_and_Hashing/66_Plus_One.py | 13 ++++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 DailyQuestion/1052_Grumpy_bookstore_owner.py diff --git a/DailyQuestion/1052_Grumpy_bookstore_owner.py b/DailyQuestion/1052_Grumpy_bookstore_owner.py new file mode 100644 index 0000000..f9eaa94 --- /dev/null +++ b/DailyQuestion/1052_Grumpy_bookstore_owner.py @@ -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) \ No newline at end of file diff --git a/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py b/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py index 16a8b58..5694f1c 100644 --- a/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py +++ b/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py @@ -1,7 +1,18 @@ def plusOne(digits): zeros = 0 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) else: digits[-1] = digits[-1] + 1 From c0b0e4f859d7ebe1b2218f3c85de114e05a20dc0 Mon Sep 17 00:00:00 2001 From: Jacob Delgado Date: Thu, 20 Jun 2024 18:02:58 -0700 Subject: [PATCH 2/3] removed conflict --- .../Arrays_and_Hashing/66_Plus_One.py | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py diff --git a/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py b/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py deleted file mode 100644 index 5694f1c..0000000 --- a/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py +++ /dev/null @@ -1,22 +0,0 @@ -def plusOne(digits): - zeros = 0 - if digits[-1] == 9: - 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) - else: - digits[-1] = digits[-1] + 1 - return digits - - -print(plusOne([1,2,3])) \ No newline at end of file From c5c82a3ba1eb37819e4251164d6a89b013b0fee6 Mon Sep 17 00:00:00 2001 From: Jacob Delgado Date: Thu, 20 Jun 2024 18:06:45 -0700 Subject: [PATCH 3/3] update with refresh credentials --- .../Arrays_and_Hashing/66_Plus_One.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py diff --git a/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py b/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py new file mode 100644 index 0000000..5694f1c --- /dev/null +++ b/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py @@ -0,0 +1,22 @@ +def plusOne(digits): + zeros = 0 + if digits[-1] == 9: + 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) + else: + digits[-1] = digits[-1] + 1 + return digits + + +print(plusOne([1,2,3])) \ No newline at end of file