Grumpy Owner's Calm Stretch
Updated
Problem statement
A shop is open for n minutes, numbered 0 to n - 1. During minute i, visitors[i] people come in, and they all leave at the end of that minute.
The owner's mood changes from minute to minute. moody[i] = 1 means the owner is in a bad mood during minute i, and every visitor of that minute leaves unhappy. moody[i] = 0 means every visitor of that minute leaves happy.
Once during the day, the owner can stay calm for exactly k minutes in a row. The calm block covers minutes s to s + k - 1 for some start s with 0 <= s <= n - k, so it cannot wrap from the end of the day back to the start. During the calm block, every visitor leaves happy, even in minutes where moody[i] = 1.
Return the largest possible number of happy visitors over the whole day.
Function
maxHappyVisitors(visitors: int[], moody: int[], k: int) → intExamples
Example 1
visitors = [2,0,5,1,3,4]moody = [0,1,1,0,1,0]k = 2return = 12Without calming down, the happy visitors come in minutes 0, 3 and 5: 2 + 1 + 4 = 7.
A calm block of 2 minutes also saves the visitors of the bad-mood minutes inside it. Starting at minute 1 saves 0 + 5 = 5, starting at minute 2 saves 5, and starting at minute 0, 3 or 4 saves 0, 3 or 3. The best block saves 5, so the answer is 7 + 5 = 12.
Example 2
visitors = [9,0,0,5,5,5]moody = [1,0,0,1,1,1]k = 3return = 15Every visitor comes in during a bad-mood minute, so nobody is happy without the calm block.
A block covering minutes 0 to 2 saves only the 9 visitors of minute 0. The block covering minutes 3 to 5 saves 5 + 5 + 5 = 15, which is the most. The busiest single minute is not inside the best block. The answer is 15.
Example 3
visitors = [3,8,2,7]moody = [0,0,0,0]k = 1return = 20The owner is never in a bad mood, so every visitor is happy and the calm block changes nothing: 3 + 8 + 2 + 7 = 20.
Constraints
n == visitors.length == moody.length1 <= k <= n <= 10^50 <= visitors[i] <= 1000moody[i]is0or1
More Nutanix Online Assessment questions
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- Number of Players (for MTS2)OA · Seen Oct 2024
- Work Schedules (for MTS2)OA · Seen Oct 2024
- Minimum Operations to Convert ArrayOA · Seen Apr 2024
- No Adjacent CharactersOA · Seen Apr 2024
- Build a BST From a ListOA · Seen Nov 2022
- Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022
Similar questions at other companies
- Count Subarrays With Product Equal to KSalesforce · OA
- Count Servers With No Requests in a Time WindowIBM · OA
- Reschedule Meetings for Maximum Free TimeFivetran · OA
- Count Subarrays Matching a Comparison PatternCapital One · OA
- Count Subarrays with at Least K Equal-Fruit PairsTikTok · OA
- Consistent LogsRippling · OA
- Minimum Team Size From Every StartCognizant · OA
- Minimum of Fixed-Window MaximaJuspay · OA
- Sliding-Window Means with IEEE Special ValuesHeadlands Technologies · OA
- Maximum Decrypted ScoreMicrosoft · OA