Grumpy Bookstore Owner LeetCode solution in JavaScript

1
var maxSatisfied = function (customers, grumpy, minutes) {
2
let left = 0;
3
let maxUnsatisfied = 0;
4
let curr = 0;
5
let satisfied = 0;
6

7
// finding the satisfied customers
8
for (let i = 0; i < customers.length; i++) {
9
if (grumpy[i] === 0) satisfied += customers[i];
10
}
11

12
// finding the maximum un-satisfied customers within the give timeframe
13
// classic sliding window approach
14
for (let i = 0; i < customers.length; i++) {
15
if (grumpy[i]) curr += customers[i];
16
if (i - left + 1 > minutes) {
17
if (grumpy[left]) curr -= customers[left];
18
left++;
19
}
20
if (i - left + 1 === minutes) {
21
if (curr > max) {
22
maxUnsatisfied = curr;
23
}
24
}
25
}
26

27
return satisfied + maxUnsatisfied;
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0