Grumpy Bookstore Owner - Leetcode 1052 - Python

NeetCodeIO · Intermediate ·⚡ Algorithms & Data Structures ·2y ago

Key Takeaways

The video solves the Grumpy Bookstore Owner problem on Leetcode using a sliding window approach, maximizing customer satisfaction by flipping grumpy scores to zero within a given time window.

Full Transcript

hey everyone welcome back and let's write some more neat code today so today let's solve the problem grumpy bookstore owner suppose we're given two arrays that look like this and the first array represents customers so we want to sort of maximize the values in this array I won't go too much into the context of the problem like minutes and all that stuff cuz it isn't really relevant it's more about there's a onetoone mapping between the first array customers and the second array grumpy the idea is that if grumpy is zero that means that the owner is not grumpy therefore the customers here will get the full satisfaction one here the owner is grumpy so this actually is going to be a zero now it's already a zero but it makes more sense if you look here where the owner is also grumpy even though there's a two here we can't really use that it's pretty much a zero as far as we're concerned so if you think of it that way just given this array we're kind of turning this into a zero this into a zero and and the value over here into a zero now if you take all of these up and compute the sum we're going to get 1 + 1 + 1 + 7 I think that's 10 now we want to maximize this score so what can we actually do about it well that's where the third parameter comes in it's minutes in this example it's three so what that means is we can pick any block of three any continuous block of three elements and basically turn the grumpiness to zero for all of those values now if we do that the only reason would be because then we get the original values from the first array so instead of this being a zero it would actually be a two and this would be a one and if you take our original result 10 plus whatever we gained by you know flipping these we're going to get this as the result now we're trying to find the window that will allow us to maximize the result and for this particular problem it's over here because we're going to get the plus one and we're going to get the plus five so that's going to leave us with a result of 16 that is the output for this problem now the brute force would be an n * m solution where M literally is this parameter and N is the length of the array can you think of that Brute Force solution well I kind of just showed you one example of it but think about every possible position this window could be in it could be over here it could be over here and every position pretty much right so for any given spot like this one we want to compute for only the positions where grumpy is a one we want to compute the sum of these values because these are the ones we're allowed to flip back so in this window we say that it's a three for the next window here we'd say that it's actually just a one because this one 's already zero this one's already zero this one is a one we flip that and we get the one back from here so pretty much every single window is going to have some score assigned to it we obviously want to maximize the result the result is the original array like after turning them into zeros so that was 10 plus the max window that we can get and the max window in our case is going to be six so that's how we're going to do it we're going to look at every single window and compute whatever score we could get from that now now if you're thinking how is this a Brute Force solution you might already know the technique I'm about to show you which is called the sliding window this is a very textbook sliding window problem I actually have a pretty good lesson on it in my Advanced algorithms course it's called the sliding fixed window where the size of the window is fixed there's also variable size which we're not really going to get into but you can see we talk all about that here so with the sliding window the idea is that we don't have to go through the window every single time we don't have to go through these values and recompute that and then when we're over here go through the same values again because when we take our window from here and then move it to the next position over here all we have to do every time we shift the window we just add a value here and remove a value here so whatever our score was with these three values we're going to now compute the score with the next value after removing this one that's pretty much the idea now let's go ahead and code this up with this sliding window optimization it's just going to be an O of n time solution where n is the size of the input and constant space because we're not really using any other data structures so let's code it up so while I feel that conceptually this problem isn't too bad I think coding it up has a few areas that can be confusing so the idea is we're going to have the sliding window we're going to have our left pointer I'm initially going to set it to zero then we're going to have the right pointer going through the length of the input array now we want to know the score of the window right like when I say score I mean how much we could get from the current window if we flipped everything in that window to a grumpy score of zero so that's why I'm not saying all of that every single time because it's a lot to say and it kind of just makes the explanation more confusing in terms of code it's pretty simple if grumpy at the current index R is one then we're going to take whatever value is at the same index in customers and we're going to add that to the current window now I also want to keep track of how many are satisfied and what I mean by that is I want to sum up all the satisfaction scores where grumpy happens to be zero so where it's not set so I can actually put this in an else condition and then I can say this add to satisfied the value of the customer at that index because this satisfaction score is like the base score score that I was talking about earlier and the example it was 10 and that's because this is how much satisfaction we have without changing anything at all so that's like the base and we have our current window there's one last variable I'm going to have and I'm going to show that to you in a second but first let's check what if our window becomes too big because we have to remove characters from the left side so the length of the window can be calculated like this R minus L + one that's how many values are in the range from R to L inclusive and so basically if this is greater than the third parameter minutes we want to do something we're going to say remove from the window the value at the left pointer and then increment the left pointer but we're only removing a value from the window if we added it to the window so make sure you have this condition here if grumpy at index left is one only then do we want to remove cuz only then we actually add that value to the window so this is just some bookkeeping to make sure we don't get any errors and everything stays consistent with our window and now lastly I'm going to have a another variable up here I'm going to call it the max window cuz we want to keep track of the current window and also the Max and you could probably do it with less variables but I want to make this mostly readable so max window is just going to keep track of what is the max window that we saw pretty straightforward at the end you probably can guess the calculation look at the variables I wrote out what is the return value going to be based on the drawing explanation you should be able to figure it out it's going to be satisfied like the base score plus the max window it could be zero it could be some other value doesn't matter we just want to return the sum of these two values and as you can see on the left we are bigger than everybody else if you found this helpful check out N.O for more thanks for watching and I'll see you soon

Original Description

🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews 🧑‍💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/ 🐦 Twitter: https://twitter.com/neetcode1 ⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf Problem Link: https://leetcode.com/problems/grumpy-bookstore-owner/description/ 0:00 - Read the problem 0:30 - Drawing Explanation 4:40 - Coding Explanation leetcode 1052 #neetcode #leetcode #python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeetCodeIO · NeetCodeIO · 0 of 60

← Previous Next →
1 Leetcode 149 - Maximum Points on a Line - Python
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
2 Design Linked List - Leetcode 707 - Python
Design Linked List - Leetcode 707 - Python
NeetCodeIO
3 Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
4 Design Browser History - Leetcode 1472 - Python
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
5 Number of Good Paths - Leetcode 2421 - Python
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
6 Flip String to Monotone Increasing - Leetcode 926 - Python
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
7 Maximum Sum Circular Subarray - Leetcode 918 - Python
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
8 Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
9 Concatenated Words - Leetcode 472 - Python
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
10 Data Stream as Disjoint Intervals - Leetcode 352 - Python
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
11 LFU Cache - Leetcode 460 - Python
LFU Cache - Leetcode 460 - Python
NeetCodeIO
12 N-th Tribonacci Number - Leetcode 1137
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
13 Best Team with no Conflicts - Leetcode 1626 - Python
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
14 Greatest Common Divisor of Strings - Leetcode 1071 - Python
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
15 Shortest Path in a Binary Matrix - Leetcode 1091 - Python
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
16 Insert into a Binary Search Tree - Leetcode 701 - Python
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
17 Delete Node in a BST - Leetcode 450 - Python
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
18 Shuffle the Array (Constant Space) - Leetcode 1470 - Python
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
19 Fruits into Basket - Leetcode 904 - Python
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
20 Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
21 Naming a Company - Leetcode 2306 - Python
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
22 As Far from Land as Possible - Leetcode 1162 - Python
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
23 Shortest Path with Alternating Colors - Leetcode 1129 - Python
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
24 Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
25 Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
26 Contains Duplicate II - Leetcode 219 - Python
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
27 Path with Maximum Probability - Leetcode 1514 - Python
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
28 Add to Array-Form of Integer - Leetcode 989 - Python
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
29 Unique Paths II - Leetcode 63 - Python
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
30 Minimum Distance between BST Nodes - Leetcode 783 - Python
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
31 Design Hashmap - Leetcode 706 - Python
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
32 Range Sum Query Immutable - Leetcode 303 - Python
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
33 Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
34 Middle of the Linked List - Leetcode 876 - Python
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
35 Course Schedule IV - Leetcode 1462 - Python
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
36 Single Element in a Sorted Array - Leetcode 540 - Python
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
37 Capacity to Ship Packages - Leetcode 1011 - Python
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
38 IPO - Leetcode 502 - Python
IPO - Leetcode 502 - Python
NeetCodeIO
39 Minimize Deviation in Array - Leetcode 1675 - Python
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
40 Longest Turbulent Array - Leetcode 978 - Python
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
41 Last Stone Weight II - Leetcode 1049 - Python
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
42 Construct Quad Tree - Leetcode 427 - Python
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
43 Find Duplicate Subtrees - Leetcode 652 - Python
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
44 Sort an Array - Leetcode 912 - Python
Sort an Array - Leetcode 912 - Python
NeetCodeIO
45 Ones and Zeroes - Leetcode 474 - Python
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
46 Remove Duplicates from Sorted Array II - Leetcode 80 - Python
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
47 Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
48 Concatenation of Array - Leetcode 1929 - Python
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
49 Symmetric Tree - Leetcode 101 - Python
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
50 Check Completeness of a Binary Tree - Leetcode 958 - Python
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
51 Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
52 Find Peak Element - Leetcode 162 - Python
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
53 Accounts Merge - Leetcode 721 - Python
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
54 Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
55 Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
56 Number of Zero-Filled Subarrays - Leetcode 2348 - Python
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
57 Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
58 Sqrt(x) - Leetcode 69 - Python
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
59 Successful Pairs of Spells and Potions - Leetcode 2300 - Python
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
60 Optimal Partition of String - Leetcode 2405 - Python
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO

The video teaches how to solve the Grumpy Bookstore Owner problem using a sliding window approach, explaining the concept and providing a Python implementation.

Key Takeaways
  1. Understand the problem constraints and objectives
  2. Choose a suitable algorithmic approach
  3. Implement the sliding window technique
  4. Keep track of the maximum window score
  5. Calculate the final result by adding the base score and the maximum window score
💡 The sliding window technique allows for efficient computation of the maximum window score by only considering the current window and updating the score as the window moves.

Related Reads

Chapters (3)

Read the problem
0:30 Drawing Explanation
4:40 Coding Explanation
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →