Skip to content

Commit 78faf32

Browse files
authored
Update 1052-grumpy-bookstore-owner.js
1 parent b126d79 commit 78faf32

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

1052-grumpy-bookstore-owner.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,28 @@ const maxSatisfied = function(customers, grumpy, X) {
2424

2525
return totalSatisfiedCustomers + max
2626
}
27+
28+
// another
29+
30+
/**
31+
* @param {number[]} customers
32+
* @param {number[]} grumpy
33+
* @param {number} X
34+
* @return {number}
35+
*/
36+
const maxSatisfied = function (customers, grumpy, X) {
37+
let satisfied = 0,
38+
maxMakeSatisfied = 0
39+
for (let i = 0, winOfMakeSatisfied = 0; i < grumpy.length; ++i) {
40+
if (grumpy[i] === 0) {
41+
satisfied += customers[i]
42+
} else {
43+
winOfMakeSatisfied += customers[i]
44+
}
45+
if (i >= X) {
46+
winOfMakeSatisfied -= grumpy[i - X] * customers[i - X]
47+
}
48+
maxMakeSatisfied = Math.max(winOfMakeSatisfied, maxMakeSatisfied)
49+
}
50+
return satisfied + maxMakeSatisfied
51+
}

0 commit comments

Comments
 (0)