bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1052. Grumpy Bookstore Owner

1052. Grumpy Bookstore Owner

Leetcode Solutions

Sliding Window Approach

  1. Initialize totalSatisfied to the sum of customers that are satisfied when the owner is not grumpy.
  2. Initialize maxAdditionalSatisfied and currentAdditionalSatisfied to 0.
  3. Use a sliding window of size minutes to iterate over the arrays.
  4. For each position in the sliding window, if the owner is grumpy, add the number of customers to currentAdditionalSatisfied.
  5. If the window size exceeds minutes, subtract the number of customers at the start of the window if the owner was grumpy at that minute.
  6. Update maxAdditionalSatisfied with the maximum of itself and currentAdditionalSatisfied.
  7. After the loop, add maxAdditionalSatisfied to totalSatisfied to get the final result.
  8. Return the final result.
UML Thumbnail

Prefix Sum with Sliding Window

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...