This is awesome. Tons of problem solvers told me that this one is similar to #84 but I didn't get any bit. Thank you sir now I know where the correlation is
@mismis31533 жыл бұрын
Having visuals like this makes it easy to understand. What I did was check the area of every rectangle that you can create from any point. Of course, it has some optimisations but it was still slower than most submissions.
@apbh5 ай бұрын
Best explanation! Very easy to follow and implement. THANK YOU!
@lets_see_7772 жыл бұрын
best channel on youtube for these questions.
@wonderInNoWhere3 жыл бұрын
Not sure what kinds of person gave thumb down. Thanks for the clear explanation.
@anantsaxena54547 ай бұрын
was able to write the code on my own thankyou
@adityaojha27013 жыл бұрын
Thanks for both video!!
@techdose4u3 жыл бұрын
Welcome :)
@divyarya71222 жыл бұрын
Thank you! you're life saver
@techdose4u2 жыл бұрын
Welcome 😊
@sameergirkar4 жыл бұрын
Great Explanation!! Thanks for the video.
@techdose4u4 жыл бұрын
Welcome :)
@allen7243 жыл бұрын
Thank you so much for these videos! I have learned a lot from your channel.
@techdose4u3 жыл бұрын
Welcome :)
@jashhinger442 Жыл бұрын
Great explanation sir. Thanku so much.
@techdose4u Жыл бұрын
Welcome :)
@yitingg79424 жыл бұрын
Second comment, I don't know what to say, just extremely grateful about the fact that you are there!!
@techdose4u4 жыл бұрын
I am glad to see you in every video :)
@yitingg79424 жыл бұрын
@@techdose4u I am a big fan of yours and will always support your channel lol !
@techdose4u4 жыл бұрын
😂 Thanks ❤️
@yitingg79423 жыл бұрын
@@techdose4u I am a little stuck to understand why that when it is 0, we just change the entire column to 0.
@JohnWick-kh7ow3 жыл бұрын
Sir isme kaha dp lagi. Ye samajh nhi aaya. Ye to stack vala question ho gaya.
@ShubhamKumar-fi1kp3 жыл бұрын
LeetCode Solution : class Solution { public: vector NSL(vector heights){ // Function to find indices of next smallest left element vector left; stack st; for(int i=0;i= heights[i]){ while(!st.empty() && st.top().first>=heights[i]) st.pop(); if(st.empty()) left.push_back(-1); else left.push_back(st.top().second); } st.push({heights[i],i}); } return left; } vector NSR(vector heights){ // Function to find indices of next smallest right element vector right; stack st; for(int i=heights.size()-1;i>=0;i--){ if(st.empty()) right.push_back(heights.size()); else if(!st.empty() && st.top().first=heights[i]){ while(!st.empty() && st.top().first>=heights[i]) st.pop(); if(st.empty()) right.push_back(heights.size()); else right.push_back(st.top().second); } st.push({heights[i],i}); } reverse(right.begin(),right.end()); return right; } int MAH(vector& heights) { //Function to find maximum area of histogram vector right; vector left; right = NSR(heights); left = NSL(heights); vector width; int mx = 0; for(int i=0;i
@balakrishnanr6482 жыл бұрын
@TECH DOSE is it DP ??? Its just a STACKS based ques right?? just finding NSL Nearest Smaller to Left and NSR Nearest Smaller to Right??
@bchen14032 жыл бұрын
Excellent visualization!!
@prakharagarwal62373 жыл бұрын
Nice Explanation! Thanks!
@harshyadav51623 жыл бұрын
The code link in description contains the code for the Histogram problem....
@altafmazhar77623 жыл бұрын
yes same question
@chamoli5522 жыл бұрын
Brilliant Explaination sir🙏
@K_EC_AnuragKumarMishra3 жыл бұрын
Best explanation🤗
@ADNANAHMED-eo5xx4 жыл бұрын
u took size r = matrix.size() (line 4 of code shown), but while u made same matrix but with integer numbers, u took size r+1 (line 9 of code). Why?
@vinitraj6268 Жыл бұрын
One more thing we can add the elements till we get 1 from upper rows, in order to make histogram
@blasttrash3 жыл бұрын
How did you know that you can use "largest rectangle area in the histogram"? I mean how did you come up with this thought process? How to derive this? Do you have some process or you just found it on some site like gfg or leetcode or some textbook?
@techdose4u3 жыл бұрын
You have to understand the logic for why Largest rectangle algo works. If you couldn't get it in the first go then please refer solution. It's appreciated to learn new observations and techniques rather than coming up with something new yourself everytime :)
@blasttrash3 жыл бұрын
@@techdose4u but amongst 1000s of questions, how do you remember that histogram question? It was a coincidence that you did that question before this. But what happens if you face this question in interview first before every hearing about histogram question?
@shivverma14593 жыл бұрын
@@blasttrash well that is why interviews need a lot of practise beforehand.
@blasttrash3 жыл бұрын
@@shivverma1459 essentially interviewing now has become memorization of "patterns" which is why as you said we need a lot of practice beforehand. If I was a scientist and coming up with new algorithm, I would have to read a lot of literature, run many experiments and analyze a lot of trade offs over a long period of time to come up with some of these algorithms. But to do so in a interview under the pretext of "problem solving" is just not possible. There is a hidden assumption laid by companies that they don't want problem solving skills, they want "hard work" and "memorization of patterns". essentially they are looking for skills of perseverance(since we would be spending a lot of time practicing things that might be boring or not that useful in grand scheme of things). And these are the skills that you need in corporate while building some useless features for certain products too.
@it-137shubhpratapsingh43 жыл бұрын
@@blasttrash hey does this mean, if I do more and more problems and keep revising them so that I know more new approaches it would be better than doing fewer problems on my own, like giving proper thought to the questions and trying to come up with a solution on my own.
@jayvideos341 Жыл бұрын
Soo nice explanation
@varunmanchanda39723 жыл бұрын
Sir, I have one question. Can we do it using Recursion?
@techdose4u3 жыл бұрын
No
@girikgarg12683 жыл бұрын
What was the intuition behind converting row of matrix to histogram?
@sainikhil9563 жыл бұрын
Bro, watch the previous video in this playlist u'll get it for sure that is prerequisite! in that video we have to find maximum area of rectangle in 1D array in this video we have to find maximum are of rectangle in 2D array
@wepafitness56122 жыл бұрын
Does this code convert the Binary Matrix into a Histogram? I don't see how it's doing that. No biggie I can add that on my own, just curious. Thanks for the explanation.
@anupam60452 жыл бұрын
wonderful explanation
@kanwarkajla3 жыл бұрын
u r great sir
@techdose4u3 жыл бұрын
Thanks 😊
@mondayemmanuel1913 жыл бұрын
Great explanation as usual.👌🏿
@mohitsaran91932 жыл бұрын
What is the significance or purpose of leftbound and rightbound ??
@nareshh744 жыл бұрын
Hi Surya, Thanks for the video😀. Can you pls tell which observation made you see the problem as histogram. Thanks.
@techdose4u4 жыл бұрын
First I was solving by finding area of largest rectangle ending at a cell (bottom right) corner but it was not working. After that some random trial and error methods and also I had solved this histogram problem which made life easier.
@nareshh744 жыл бұрын
@@techdose4u I too started it like the square problem. But unfortunately, I couldn't come up with the histogram perspective like you did. Needs more practice I guess ✌️.
@techdose4u4 жыл бұрын
@@nareshh74 Practice helps a lot.
@balakrishnanr6482 жыл бұрын
@@techdose4u is it DP ??? Its just a STACKS based ques right??
@krishnaprabeesh24153 жыл бұрын
Good explanation
@sainikhil9563 жыл бұрын
Sir, but how to come up with these kind of solutions in interview if we haven't seen this before?
@Thepankaz13 жыл бұрын
Thats where you are right, you dont.
@snehasamuel7426 Жыл бұрын
I have a doubt wrt to this question. I coded this problem and for the test case 5, 4 = {1011, 1011, 0101, 1111, 0001}. The output according to the logic you explained and I coded is coming 8 but the actual answer is 5. Where am i going wrong ? can someone guide me pls?
@ankitjangir013 жыл бұрын
It would be better if you show the last code part zoomed in.
@techdose4u3 жыл бұрын
Sure
@kumarc48533 жыл бұрын
thank you for the dose :)
@tanishgotti36599 ай бұрын
Where are we using solutions of sub problems to get solution? Isn't this supposed to be DP...
@whysoserious-yj1ku2 жыл бұрын
How is this a dynamic programming solution?
@seelamrameshreddy7024 жыл бұрын
Please Explain and provide Java Solutions as well
@techdose4u4 жыл бұрын
Will try for JAVA 😅
@iit_da_munda3 жыл бұрын
How this is a DP problem...?? ... this is purely a stack problem.
@animeshjain45943 жыл бұрын
Can someone tell me what is wrong in this code- I didn't used stack I just applied the while loop until I find the next element smalller than the current element in filling the left and right array. class Solution { public int maximalRectangle(char[][] matrix) { int n = matrix.length; int maxArea = Integer.MIN_VALUE; int m = matrix[0].length; int[] arr = new int[m]; for(int i=0;i=arr[i]){ j--; } if(j=0;i--){ if(arr[i+1]>=arr[i]){ int j = i+1; while(j=arr[i]){ j++; } if(j>=n-1){ right[i] = n-1; }else{ right[i] = j-1; } }else{ right[i] = i; } } int maxArea = Integer.MIN_VALUE; for(int i=0;i