Skip to content
Decoder

Grumpy Bookstore Owner

Medium

Problem

A shop is open for n minutes. customers[i] people visit during minute i. grumpy[i] is 1 if the owner is grumpy that minute (those customers leave unhappy) and 0 if not (they leave happy). The owner knows a trick to stay calm for `minutes` consecutive minutes, usable only once. Return the maximum number of happy customers over the whole day.

Example 1

customers = [1, 0, 1, 2, 1, 1, 7, 5]

Example 2

Input
grumpy = [0, 1, 0, 1, 0, 1, 0, 1], minutes = 3
Output
16

Using the trick on the last three minutes saves 1 + 5 = 6 extra customers on top of the 10 who are already happy.

Constraints

  • 1 <= minutes <= customers.length == grumpy.length <= 2 * 10^4
  • 0 <= customers[i] <= 1000
  • grumpy[i] is either 0 or 1.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.