|
| 1 | +/** |
| 2 | + * 1052. Grumpy Bookstore Owner |
| 3 | + * https://leetcode.com/problems/grumpy-bookstore-owner/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * There is a bookstore owner that has a store open for n minutes. You are given an integer array |
| 7 | + * customers of length n where customers[i] is the number of the customers that enter the store |
| 8 | + * at the start of the ith minute and all those customers leave after the end of that minute. |
| 9 | + * |
| 10 | + * During certain minutes, the bookstore owner is grumpy. You are given a binary array grumpy |
| 11 | + * where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise. |
| 12 | + * |
| 13 | + * When the bookstore owner is grumpy, the customers entering during that minute are not satisfied. |
| 14 | + * Otherwise, they are satisfied. |
| 15 | + * |
| 16 | + * The bookstore owner knows a secret technique to remain not grumpy for minutes consecutive |
| 17 | + * minutes, but this technique can only be used once. |
| 18 | + * |
| 19 | + * Return the maximum number of customers that can be satisfied throughout the day. |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * @param {number[]} customers |
| 24 | + * @param {number[]} grumpy |
| 25 | + * @param {number} minutes |
| 26 | + * @return {number} |
| 27 | + */ |
| 28 | +var maxSatisfied = function(customers, grumpy, minutes) { |
| 29 | + let baseSatisfied = 0; |
| 30 | + let windowGain = 0; |
| 31 | + let maxGain = 0; |
| 32 | + |
| 33 | + for (let i = 0; i < customers.length; i++) { |
| 34 | + if (grumpy[i] === 0) { |
| 35 | + baseSatisfied += customers[i]; |
| 36 | + } |
| 37 | + if (i < minutes) { |
| 38 | + windowGain += grumpy[i] * customers[i]; |
| 39 | + } else { |
| 40 | + windowGain += grumpy[i] * customers[i] - grumpy[i - minutes] * customers[i - minutes]; |
| 41 | + } |
| 42 | + maxGain = Math.max(maxGain, windowGain); |
| 43 | + } |
| 44 | + |
| 45 | + return baseSatisfied + maxGain; |
| 46 | +}; |
0 commit comments