1052. Grumpy Bookstore Owner LeetCode Solution
In this guide, you will get 1052. Grumpy Bookstore Owner LeetCode Solution with the best time and space complexity. The solution to Grumpy Bookstore Owner problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Grumpy Bookstore Owner solution in C++
- Grumpy Bookstore Owner solution in Java
- Grumpy Bookstore Owner solution in Python
- Additional Resources
Problem Statement of Grumpy Bookstore Owner
There is a bookstore owner that has a store open for n minutes. You are given an integer array customers of length n where customers[i] is the number of the customers that enter the store at the start of the ith minute and all those customers leave after the end of that minute.
During certain minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise.
When the bookstore owner is grumpy, the customers entering during that minute are not satisfied. Otherwise, they are satisfied.
The bookstore owner knows a secret technique to remain not grumpy for minutes consecutive minutes, but this technique can only be used once.
Return the maximum number of customers that can be satisfied throughout the day.
Example 1:
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
Output: 16
Explanation:
The bookstore owner keeps themselves not grumpy for the last 3 minutes.
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
Example 2:
Input: customers = [1], grumpy = [0], minutes = 1
Output: 1
Constraints:
n == customers.length == grumpy.length
1 <= minutes <= n <= 2 * 104
0 <= customers[i] <= 1000
grumpy[i] is either 0 or 1.
Complexity Analysis
- Time Complexity:
- Space Complexity:
1052. Grumpy Bookstore Owner LeetCode Solution in C++
class Solution {
public:
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
int satisfied = 0;
int madeSatisfied = 0;
int windowSatisfied = 0;
for (int i = 0; i < customers.size(); ++i) {
if (grumpy[i] == 0)
satisfied += customers[i];
else
windowSatisfied += customers[i];
if (i >= X && grumpy[i - X] == 1)
windowSatisfied -= customers[i - X];
madeSatisfied = max(madeSatisfied, windowSatisfied);
}
return satisfied + madeSatisfied;
}
};
/* code provided by PROGIEZ */
1052. Grumpy Bookstore Owner LeetCode Solution in Java
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
int satisfied = 0;
int madeSatisfied = 0;
int windowSatisfied = 0;
for (int i = 0; i < customers.length; ++i) {
if (grumpy[i] == 0)
satisfied += customers[i];
else
windowSatisfied += customers[i];
if (i >= X && grumpy[i - X] == 1)
windowSatisfied -= customers[i - X];
madeSatisfied = Math.max(madeSatisfied, windowSatisfied);
}
return satisfied + madeSatisfied;
}
}
// code provided by PROGIEZ
1052. Grumpy Bookstore Owner LeetCode Solution in Python
class Solution:
def maxSatisfied(
self,
customers: list[int],
grumpy: list[int],
X: int,
) -> int:
satisfied = sum(c for i, c in enumerate(customers) if grumpy[i] == 0)
madeSatisfied = 0
windowSatisfied = 0
for i, customer in enumerate(customers):
if grumpy[i] == 1:
windowSatisfied += customer
if i >= X and grumpy[i - X] == 1:
windowSatisfied -= customers[i - X]
madeSatisfied = max(madeSatisfied, windowSatisfied)
return satisfied + madeSatisfied
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.