Grumpy Bookstore Owner

A bookstore owner keeps the store open for `n` minutes. In minute `i`, `customers[i]` customers come in, and they all leave once that minute ends. In some minutes the owner is grumpy: `grumpy[i]` is `1` if the owner is grumpy in minute `i` and `0` otherwise. Customers in a grumpy minute are not satisfied; customers in any other minute are. The owner knows a trick that keeps them calm for `minutes` consecutive minutes, and can use it **only once**. Return the maximum number of customers that can be satisfied during the day.

30:00

Grumpy Bookstore Owner
medium
Free written solution
Topics
Companies

A bookstore owner keeps the store open for n minutes. In minute i, customers[i] customers come in, and they all leave once that minute ends.

In some minutes the owner is grumpy: grumpy[i] is 1 if the owner is grumpy in minute i and 0 otherwise. Customers in a grumpy minute are not satisfied; customers in any other minute are.

The owner knows a trick that keeps them calm for minutes consecutive minutes, and can use it only once.

Return the maximum number of customers that can be satisfied during the day.

Example 1:
Input: {"customers":[1,0,1,2,1,1,7,5],"grumpy":[0,1,0,1,0,1,0,1],"minutes":3}
Output: 16
Constraints:
  • n=customers.length=grumpy.lengthn = \text{customers.length} = \text{grumpy.length}

  • 1≤minutes≤n≤2×1041 \leq \text{minutes} \leq n \leq 2 \times 10^4

  • 0≤customers[i]≤10000 \leq \text{customers}[i] \leq 1000

  • grumpy[i]\text{grumpy}[i] is either 00 or 11

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"customers":[1,0,1,2,1,1,7,5],"grumpy":[0,1,0,1,0,1,0,1],"minutes":3}

8 minutes of customers. Green minutes are calm and always count; red minutes are grumpy and lose 8 customers unless the 3-minute trick covers them.

customers per minute, trick lasts 3

1

0

1

2

1

1

7

5

ok

0

G

1

ok

2

G

3

ok

4

G

5

ok

6

G

7

calm (base)

rescued by trick

lost (grumpy)

base = ?

extra = -

best = -

Variables
VariableValue
minutes3
DepthFunction Call
Stack empty
0/15