-
Notifications
You must be signed in to change notification settings - Fork 225
/
1052-grumpy-bookstore-owner.js
51 lines (48 loc) · 1.31 KB
/
1052-grumpy-bookstore-owner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @param {number[]} customers
* @param {number[]} grumpy
* @param {number} X
* @return {number}
*/
const maxSatisfied = function(customers, grumpy, X) {
if (customers.length === 1) return customers[0]
const totalSatisfiedCustomers = customers.reduce(
(ac, el, idx) => ac + (grumpy[idx] === 0 ? el : 0),
0
)
const arr = customers.map((el, idx) => (grumpy[idx] === 1 ? el : 0))
const acArr = []
let ac = 0
for (let i = 0, len = arr.length; i < len; i++) {
acArr[i] = ac = ac + arr[i]
}
let max = 0
for (let i = X - 1, len = grumpy.length; i < len; i++) {
let tmp = i - X < 0 ? 0 : acArr[i - X]
if (acArr[i] - tmp > max) max = acArr[i] - tmp
}
return totalSatisfiedCustomers + max
}
// another
/**
* @param {number[]} customers
* @param {number[]} grumpy
* @param {number} X
* @return {number}
*/
const maxSatisfied = function (customers, grumpy, X) {
let satisfied = 0,
maxMakeSatisfied = 0
for (let i = 0, winOfMakeSatisfied = 0; i < grumpy.length; ++i) {
if (grumpy[i] === 0) {
satisfied += customers[i]
} else {
winOfMakeSatisfied += customers[i]
}
if (i >= X) {
winOfMakeSatisfied -= grumpy[i - X] * customers[i - X]
}
maxMakeSatisfied = Math.max(winOfMakeSatisfied, maxMakeSatisfied)
}
return satisfied + maxMakeSatisfied
}