Skip to content

Commit 424aaea

Browse files
committed
Add solution #1052
1 parent d3d84bd commit 424aaea

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,121 LeetCode solutions in JavaScript
1+
# 1,122 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -857,6 +857,7 @@
857857
1048|[Longest String Chain](./solutions/1048-longest-string-chain.js)|Medium|
858858
1049|[Last Stone Weight II](./solutions/1049-last-stone-weight-ii.js)|Medium|
859859
1051|[Height Checker](./solutions/1051-height-checker.js)|Easy|
860+
1052|[Grumpy Bookstore Owner](./solutions/1052-grumpy-bookstore-owner.js)|Medium|
860861
1071|[Greatest Common Divisor of Strings](./solutions/1071-greatest-common-divisor-of-strings.js)|Easy|
861862
1072|[Flip Columns For Maximum Number of Equal Rows](./solutions/1072-flip-columns-for-maximum-number-of-equal-rows.js)|Medium|
862863
1073|[Adding Two Negabinary Numbers](./solutions/1073-adding-two-negabinary-numbers.js)|Medium|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)