LeetCode 1052: Grumpy Bookstore Owner Solution
Master LeetCode problem 1052 (Grumpy Bookstore Owner), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1052. Grumpy Bookstore Owner
Problem Explanation
Explanation:
- The problem requires us to find the maximum number of customers that can be satisfied throughout the day by using a secret technique to remain not grumpy for
minutesconsecutive minutes. - We can solve this problem by sliding window technique.
- We will iterate through the customers array and calculate the total satisfied customers without using the secret technique.
- Then, we will use a sliding window of size
minutesto find the maximum number of additional customers that can be satisfied by using the secret technique. - The maximum satisfied customers will be the sum of total satisfied customers and the additional satisfied customers within the sliding window.
Time Complexity: O(n)
Space Complexity: O(1)
:
Solution Code
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
int n = customers.length;
int satisfied = 0;
int maxAdditionalSatisfied = 0;
int additionalSatisfied = 0;
for (int i = 0; i < n; i++) {
if (grumpy[i] == 0) {
satisfied += customers[i];
}
}
for (int i = 0; i < minutes; i++) {
if (grumpy[i] == 1) {
additionalSatisfied += customers[i];
}
}
maxAdditionalSatisfied = additionalSatisfied;
for (int i = minutes; i < n; i++) {
if (grumpy[i] == 1) {
additionalSatisfied += customers[i];
}
if (grumpy[i - minutes] == 1) {
additionalSatisfied -= customers[i - minutes];
}
maxAdditionalSatisfied = Math.max(maxAdditionalSatisfied, additionalSatisfied);
}
return satisfied + maxAdditionalSatisfied;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1052 (Grumpy Bookstore Owner)?
This page provides optimized solutions for LeetCode problem 1052 (Grumpy Bookstore Owner) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1052 (Grumpy Bookstore Owner)?
The time complexity for LeetCode 1052 (Grumpy Bookstore Owner) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1052 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1052 in Java, C++, or Python.