Skip to content

Commit be68ca2

Browse files
authored
Create 1052-grumpy-bookstore-owner.js
1 parent c5cb167 commit be68ca2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

1052-grumpy-bookstore-owner.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} customers
3+
* @param {number[]} grumpy
4+
* @param {number} X
5+
* @return {number}
6+
*/
7+
const maxSatisfied = function(customers, grumpy, X) {
8+
if (customers.length === 1) return customers[0]
9+
const totalSatisfiedCustomers = customers.reduce(
10+
(ac, el, idx) => ac + (grumpy[idx] === 0 ? el : 0),
11+
0
12+
)
13+
const arr = customers.map((el, idx) => (grumpy[idx] === 1 ? el : 0))
14+
const acArr = []
15+
let ac = 0
16+
for (let i = 0, len = arr.length; i < len; i++) {
17+
acArr[i] = ac = ac + arr[i]
18+
}
19+
let max = 0
20+
for (let i = X - 1, len = grumpy.length; i < len; i++) {
21+
let tmp = i - X < 0 ? 0 : acArr[i - X]
22+
if (acArr[i] - tmp > max) max = acArr[i] - tmp
23+
}
24+
25+
return totalSatisfiedCustomers + max
26+
}

0 commit comments

Comments
 (0)