Grumpy Bookstore Owner | Simplest Thought Process | Leetcode 1052 | codestorywithMIK
Вставка
- Опубліковано 19 чер 2024
- Whatsapp Community Link : www.whatsapp.com/channel/0029...
This is the 23rd Video of our Playlist "Sliding Window : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a good and standard Sliding Window Problem : Grumpy Bookstore Owner | Simplest Thought Process | Leetcode 1052 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Grumpy Bookstore Owner | Simplest Thought Process | Leetcode 1052 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/grumpy-...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
The approach used in the provided code to solve the problem of maximizing satisfied customers involves the following steps:
Initial Calculation:
Calculate the initial number of unsatisfied customers within the first minutes window. This is done by multiplying the number of customers by the grumpy state for the first minutes elements and summing the results.
Sliding Window Technique:
Utilize a sliding window of size minutes to determine the maximum number of unsatisfied customers that can be turned into satisfied customers by leveraging the special technique. As the window slides one position to the right:
Include the next element's contribution to unsatisfied customers if it falls within the grumpy period.
Exclude the first element of the previous window.
Update the maximum number of unsatisfied customers (maxUnsat) that can be converted to satisfied customers within any window of minutes.
Total Satisfied Customers Calculation:
Calculate the total number of satisfied customers by adding:
The maximum number of potentially converted unsatisfied customers (maxUnsat).
The customers who are already satisfied (i.e., where grumpy[i] is 0).
By combining these steps, the algorithm efficiently finds the maximum possible number of satisfied customers by utilizing a sliding window technique to optimize the period during which the special technique can be applied.
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
Pehle 6 min mei hi Moot dia mere approach par
🤣🤣🤣 are bhai
Bs bhaii🤣🤣🤣
Itna sach nhi bolna tha bhai 😂😂
😂😂😂
Hahah... Ak dam se approach badal di, example badal diya... Waqt badal diya...jazzbat badal diye😂😂😂😂
Hi Mike, today I followed your approach of writing brute force first then writing the story for optimized solution and then the code finally. I was able to solve this in first attempt. Thanks a lot for building this power in your solutions. I can now get the intuition from the questions and I am also able to solve medium level questions now on leetcode. god bless you MIK 😇 you are doing a great job!
Another way to doing this
class Solution {
public:
int maxSatisfied(vector& customers, vector& grumpy, int minutes) {
int n= customers.size();
int ans=0;
for(int i=0;i
you are so underrated bro!! thanks for making my leetcode journey easier
You are a legend.. Keep teaching like this. Also one more request. Can you please make an excel sheet for all the important DSA questions that you are teaching us ?I would be great. also we can track our progress
Amazing work sir app bhaut badiya samjhate ho varnah sabh hindi-urdu bolne wale toh angreez banne kai chakkar mai hai
I solved this by calculating which subarray will cause the maximum increase in sum rather than finding the subarray with maximum sum but it runs in O(n*m) complexity. Accept ho gaya
class Solution:
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
n = len(customers)
all_sum = 0
secret_sum_increase = -1
for i in range(n-minutes+1):
j = i+minutes
temp = 0
for k in range(i,j):
if grumpy[k] == 1:
temp += customers[k]
secret_sum_increase = max(secret_sum_increase,temp)
# O(n * minutes)
for a in range(len(customers)):
if grumpy[a] == 0:
all_sum+= customers[a]
#O(n)
return all_sum+secret_sum_increase
this is how i did it
class Solution {
public:
int maxSatisfied(vector& customers, vector& grumpy, int minutes) {
int n = customers.size();
int j = 0;
int i = 0;
int sum = 0;
int maxSum = 0;
int start = -1;
int end = -1;
while(j < n){
sum += (grumpy[j] == 1)?customers[j]:0;
while((j - i + 1) > minutes){
sum -= (grumpy[i] == 1)?customers[i]:0;
i++;
}
if(j-i+1 == minutes && maxSum < sum){
maxSum = sum;
start = i;
end = j;
}
j++;
}
int ans = 0;
for(int i = 0;i= start && i
Thought of sliding window on the first minute of thinking, all thanks to you sir
Excellent explanation!! Appreciate your daily efforts!!
Crystal clear explanation!!! Thanks bhaiya...
great explaination bhaiya
Just wow❤🔥❤🔥
just love your explaination...❤Crystal Clear
Here is the code ->
class Solution {
public:
int maxSatisfied(vector& customers, vector& grumpy, int minutes) {
// we will use sliding window approach here
// 1. maxUnsatCust findout
// 2. add this to totalSat;
int n = customers.size();
int maxUnsatCust = 0;
int curUnsatCust = 0;
for(int i=0; i O(n)
{
curUnsatCust += grumpy[i]*customers[i];
}
maxUnsatCust=curUnsatCust;
// sliding window approach below
int i=0;
int j=minutes;
while(jO(n)
curUnsatCust += customers[j]*grumpy[j]; // adding the new element in the window
curUnsatCust -= customers[i]*grumpy[i]; // removing the old element in the window
i++;
j++;
maxUnsatCust=max(maxUnsatCust,curUnsatCust);
}
int totalSat=maxUnsatCust;
for(int i=0;i O(n)
{
if(grumpy[i]==0)
totalSat+=customers[i];
}
return totalSat;
}
};
// tc-> O(n)
// sc -> O(1)
Solved this one on my own today!
did this on my own but it took 305ms. thank you so much for your solution.
wonderful explanation thanks!! I was able to do this on my own after watching the explanation!!
kya he explanation dete ho bhaiya 😍😍. Segment tree ki new video jaldi lao na please waiting for it.
well taught👏👏👏👏
class Solution {
public:
int maxSatisfied(vector& customers, vector& grumpy, int minutes) {
int n=customers.size();
int sum=0;
for(int i=0;i
Waiting for your video
Good explanation 🎉❤
Thanks 😊
Holy shit man. I was making it so complex
solved by myself today ❤
I don't believe this is same solution that i have implemented 😮
Thanks
Please upload the next lecture on segment trees. Also, sir, could you provide some LeetCode questions that we can practice?
Wowww ! and i solved this problem using DP 🤐 and i come here to watch optimized solution and w t h this is the sliding window problem so now i am going to solve it using sliding window 💝💝
and i Solved that problem using sliding window also SatisfyCustomer + maximumUnsatisfyCustomer in that minutes window
bhai dp kha se pdha
@@codecodecode1752 First pada Love Babbar then From Priyansh agarwal and abhi iss channel se
Thanks a lot bhaiya ❤❤
thanks
6:17 bhaiya maine yahi socha tha lekin phir galti mil gyi iss approach mai 🥲🥲🥲🥲
Thank you sir!
Suggestion/Request: Can you also start providing weekly leetcode contest solutions?
Today I solved this on my own, using sliding window algorithm 😊
ab tu nakuri lega aur naukar bnega🤣🤣
@@aggarwalsachin4854 शिक्षित बनो, संघर्ष करो
@@aggarwalsachin4854 शिक्षित बन, संघर्ष कर, अपने पिताजी के पैसों पर ज्यादा मत उछल
@@aggarwalsachin4854 abe yr tu bhi wahi krega kuch din bad
Can't believe,it is solved by me without watching your video 😍😍😍🤯🤯❤️❤️🎉🎉🎉
Respected MIK Sir,
May I know what motivates you to deliver such a high quality content with lot of dedication, efforts and with total selflessness for free?
🙇🙏
subscriber yesterday 52.6K and today 52.9K 👍
Bhaiya Please make a playlist on rabin karp and rolling hash problems
one suggestion sir if possible please avoid telling the hint in the start of the video like you mentioned sliding window in the starting of this video. This thing make our mind to focus on one direction only.
sir aapne bola tha *longest valid parantheses* pe video layenge weekend pe......please bana dena
bruteforce got accepted in this question
Bhaiya please please make a video on leetcode 2659. Make array empty please explain the intuition of this problem . No one has covered it properly i wasnt able to understand the intuition after searching for all resources. I beleive your story to code formula will be able to do it . Please please please bhaiya.
DSA legend 🫡
i guess you're our indian @NeetcodeIO
Feedback: All you need is to improve the thumbnails and add some intro/outro. May your channel grow to 100k soon
This means a lot. Thank you for the feedback.
Feel free to provide some suggestions on what changes should I make in the Thumbnail. Would love to hear.
Also in intro and outro, what should I add ❤️
@@codestorywithMIK
1) Your thumbnail have different components like different text, your signature image :). But all things seem to be placed/pasted in little unorganized fashion. Matlab sab milakar ek single componet nahi lagte.
2) For example you can see thumbails of other channels like anuj bhaiya, Aryan mittal, take you forward. most of them use their personal brand, image (Pattern 1- More attractive and catchier)
3) But if you dont like these types of thumbnails you can take inspiration from 3 blue one brown channel, Reducible channel. (Pattern 2- simplicity at its best
Maybe you could try more on brand building and stuff.
Intro toh aap jo motivation dete ho vohi best hai, thoda background music ke sath testing kar sakte ho
I am just a college student with some experience in designing but as your regular viewer just wanted to tell you
that I am attracted to a 1) catchy and professional thumbnail 2) the views on the video and 3) if the person is familiar to me or is famous. I know content is the power. But these small things can increase subs and views and eventually take the content to a more audience. 💖✌
Noted ❤️❤️
Did my self🎉
Segment Tree video 4, when??
Can’t we just find maximum sub array sum starting with 1?
again did this on my own , bhaiya kal vali video ke comment mai apne similar type ke q ki list di thi wo ab dikh nhi rhi comments mai ? dlt to nhi krdi?
parso vali video ke niche he
maine socha tha ki mai pahle customer array ke hisab se customer aur grumpty ko sort kardunga taki muje customers ke hisab se sorted array mil jaye.and then mai last se traverse karta hua aaung and jaha pe muje grumpy mil jae vaha se mai vo power use karlunga and mere is approach se 1st example mai ans 18 aa raha tha to koi bata sakta hai ki kyu wrong tha?
Power consecutive minutes me use honge...sort karne se minutes consecutive nahi rahenge
minutes consecutive he to sort karne se order kharab ho raha he
@@nishantkshirsagar2150 matlab is question Mai order matter karta hai?
@@IITians pls thoda sa explanation de sakte ho?
@@viholvishwassinh1709 consecutive minutes tak wo apna secret power use kar sakt ahe agar tum use sort karoge to conseuctive minutes konse he ye kaisse pata chalega?
Bahiya 12.06 me logic click ho gaya ...3ms me solve ho gaya .....but khud se kyu ho nahi ho pata😰😰😰😰😰😰
customer ko nhi, girlfriend ko satisfy kro
Size increase karke
Sir meh sirf problem statement samjhne ata hu par first page pr uska type samjh jata hai fir mazha nahi ata solve krne aap please timestamp dal sakte ho kya taki direct mein problem statement pr ja saku first slide skip kr k
Ese agr video dekh kr smjoge to kabhi question pdne ki ability improve nhi hogi.. Khud se try kro.. 1-2 baar pd kar. ..
first try reading the Qns and try understanding from the examples on your own. Try 1-2 times. If your really get stuck, then get help.
Waise mai most of the times video 1 minutes k baad se start karta hu so that start me topic ka pata na chal sake.
Please segment tree continue kijiye
Can someone explain what am i doing wrong. I used max heap for this ques
Intuition : shopkeeper wants to be non grumpy when he has more customers in his shop
better name for cnt= minute_Used_To_Make_not_grumpy
class Solution {
public:
int maxSatisfied(vector& customers, vector& grumpy, int minutes) {
int n=customers.size();
priority_queuepq;
for(int i=0;i0 && cnt=minutes)
{
if(pq.top().second==0)
satisfied=pq.top().first;
}
pq.pop();
}
return satisfied;
}
};