Skip to content

Commit 2785292

Browse files
medium: 1052. Grumpy Bookstore Owner
1 parent 4832636 commit 2785292

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @param {number[]} customers
3+
* @param {number[]} grumpy
4+
* @param {number} minutes
5+
* @return {number}
6+
*/
7+
function maxSatisfied(customers, grumpy, minutes) {
8+
let alwaysSatisfied = 0;
9+
let windowStart = 0;
10+
let extraSatisfied = 0;
11+
let maxExtraSatisfied = 0;
12+
13+
for (let windowEnd = 0; windowEnd < grumpy.length; windowEnd++) {
14+
// Add customers to alwaysSatisfied if the owner is not grumpy
15+
if (grumpy[windowEnd] === 0) {
16+
alwaysSatisfied += customers[windowEnd];
17+
}
18+
19+
// Calculate the number of extra satisfied customers within the current window
20+
if (grumpy[windowEnd] === 1) {
21+
extraSatisfied += customers[windowEnd];
22+
}
23+
24+
// Once the window exceeds the given minutes, slide the window
25+
if (windowEnd >= minutes) {
26+
if (grumpy[windowStart] === 1) {
27+
extraSatisfied -= customers[windowStart];
28+
}
29+
windowStart++;
30+
}
31+
32+
// Update the maximum extra satisfied customers
33+
maxExtraSatisfied = Math.max(maxExtraSatisfied, extraSatisfied);
34+
}
35+
36+
// The result is the always satisfied customers plus the maximum extra satisfied customers
37+
return alwaysSatisfied + maxExtraSatisfied;
38+
}
39+
40+
// Example usage
41+
console.log(maxSatisfied([1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3)); // Output: 16
42+
console.log(maxSatisfied([1], [0], 1)); // Output: 1
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [1052. Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner)
2+
3+
---
4+
5+
title: "Maximize Satisfied Customers Using Grumpy Bookstore Owner Algorithm"
6+
summary: "A detailed explanation and implementation of the algorithm to maximize satisfied customers given grumpy periods in a bookstore."
7+
date: "2024-06-21"
8+
modified_date: "2024-06-21"
9+
tags: ["algorithm", "sliding window", "JavaScript"]
10+
slug: "maximize-satisfied-customers-grumpy-bookstore"
11+
12+
---
13+
14+
![image.png](https://assets.leetcode.com/users/images/53e81608-a212-4c8e-b945-a8a055f53935_1718960976.6266136.png)
15+
16+
## Intuition
17+
18+
The initial idea is to identify how we can maximize the number of satisfied customers by considering both the always satisfied customers and those who can be satisfied during grumpy periods with an intervention.
19+
20+
## Approach
21+
22+
We use a sliding window approach to calculate the maximum number of customers that can be satisfied by making the bookstore owner not grumpy for a continuous period of given minutes. The solution involves two main parts:
23+
24+
1. Calculating the number of customers always satisfied when the owner is not grumpy.
25+
2. Using a sliding window to find the maximum number of extra customers that can be satisfied during grumpy periods within the allowed minutes.
26+
27+
## Complexity
28+
29+
- **Time complexity:**
30+
$$O(n)$$ where \( n \) is the length of the `customers` array. The sliding window traverses the array once.
31+
32+
- **Space complexity:**
33+
$$O(1)$$ as we use a constant amount of extra space for the variables.
34+
35+
## Code
36+
37+
```javascript
38+
/**
39+
* @param {number[]} customers
40+
* @param {number[]} grumpy
41+
* @param {number} minutes
42+
* @return {number}
43+
*/
44+
function maxSatisfied(customers, grumpy, minutes) {
45+
let alwaysSatisfied = 0;
46+
let windowStart = 0;
47+
let extraSatisfied = 0;
48+
let maxExtraSatisfied = 0;
49+
50+
for (let windowEnd = 0; windowEnd < grumpy.length; windowEnd++) {
51+
// Add customers to alwaysSatisfied if the owner is not grumpy
52+
if (grumpy[windowEnd] === 0) {
53+
alwaysSatisfied += customers[windowEnd];
54+
}
55+
56+
// Calculate the number of extra satisfied customers within the current window
57+
if (grumpy[windowEnd] === 1) {
58+
extraSatisfied += customers[windowEnd];
59+
}
60+
61+
// Once the window exceeds the given minutes, slide the window
62+
if (windowEnd >= minutes) {
63+
if (grumpy[windowStart] === 1) {
64+
extraSatisfied -= customers[windowStart];
65+
}
66+
windowStart++;
67+
}
68+
69+
// Update the maximum extra satisfied customers
70+
maxExtraSatisfied = Math.max(maxExtraSatisfied, extraSatisfied);
71+
}
72+
73+
// The result is the always satisfied customers plus the maximum extra satisfied customers
74+
return alwaysSatisfied + maxExtraSatisfied;
75+
}
76+
77+
// Example usage
78+
console.log(
79+
maxSatisfied([1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3)
80+
); // Output: 16
81+
console.log(maxSatisfied([1], [0], 1)); // Output: 1
82+
```

0 commit comments

Comments
 (0)