1052. Grumpy Bookstore Owner #215
-
Topics: There is a bookstore owner that has a store open for On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied. The bookstore owner knows a secret technique to keep themselves not grumpy for Return the maximum number of customers that can be satisfied throughout the day. Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem asks us to calculate the maximum number of customers that can be satisfied at a bookstore, given that the bookstore owner can use a special technique to remain non-grumpy for Key Points
ApproachTo solve this problem, we can use the sliding window technique:
Plan
Let's implement this solution in PHP: 1052. Grumpy Bookstore Owner <?php
/**
* @param Integer[] $customers
* @param Integer[] $grumpy
* @param Integer $minutes
* @return Integer
*/
function maxSatisfied($customers, $grumpy, $minutes) {
$satisfied = 0;
$madeSatisfied = 0;
$windowSatisfied = 0;
for ($i = 0; $i < count($customers); ++$i) {
if ($grumpy[$i] == 0)
$satisfied += $customers[$i];
else
$windowSatisfied += $customers[$i];
if ($i >= $minutes && $grumpy[$i - $minutes] == 1)
$windowSatisfied -= $customers[$i - $minutes];
$madeSatisfied = max($madeSatisfied, $windowSatisfied);
}
return $satisfied + $madeSatisfied;
}
// Example 1
$customers = [1,0,1,2,1,1,7,5];
$grumpy = [0,1,0,1,0,1,0,1];
$minutes = 3;
echo maxSatisfied($customers, $grumpy, $minutes); // Output: 16
// Example 2
$customers = [1];
$grumpy = [0];
$minutes = 1;
echo maxSatisfied($customers, $grumpy, $minutes); // Output: 1
?> Explanation:
Example WalkthroughExample 1:
Example 2:
Time Complexity
This problem is efficiently solved using the sliding window technique. By iterating through the array and keeping track of potential satisfied customers, we can optimize the use of the owner's non-grumpy technique. The approach ensures that we calculate the maximum possible satisfaction in linear time, making it feasible even for the upper limits of the problem constraints. |
Beta Was this translation helpful? Give feedback.
The problem asks us to calculate the maximum number of customers that can be satisfied at a bookstore, given that the bookstore owner can use a special technique to remain non-grumpy for
minutes
consecutive minutes. The goal is to find the maximum number of customers that can be satisfied during the store's opening hours. The satisfaction of customers depends on whether the bookstore owner is grumpy during each minute. If the owner is grumpy, customers entering that minute are not satisfied, but if they are not grumpy, the customers are satisfied.Key Points
customers
andgrumpy
.customers[i]
represents the number of customers entering at minutei
.grump…