Maximum area rectangle in a binary matrix | Leetcode #85 | Maximal rectangle

  Рет қаралды 88,846

Techdose

Techdose

Күн бұрын

Пікірлер: 82
@harpercfc_
@harpercfc_ 2 жыл бұрын
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
@mismis3153
@mismis3153 3 жыл бұрын
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.
@apbh
@apbh 5 ай бұрын
Best explanation! Very easy to follow and implement. THANK YOU!
@lets_see_777
@lets_see_777 2 жыл бұрын
best channel on youtube for these questions.
@wonderInNoWhere
@wonderInNoWhere 3 жыл бұрын
Not sure what kinds of person gave thumb down. Thanks for the clear explanation.
@anantsaxena5454
@anantsaxena5454 7 ай бұрын
was able to write the code on my own thankyou
@adityaojha2701
@adityaojha2701 3 жыл бұрын
Thanks for both video!!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@divyarya7122
@divyarya7122 2 жыл бұрын
Thank you! you're life saver
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@sameergirkar
@sameergirkar 4 жыл бұрын
Great Explanation!! Thanks for the video.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@allen724
@allen724 3 жыл бұрын
Thank you so much for these videos! I have learned a lot from your channel.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@jashhinger442
@jashhinger442 Жыл бұрын
Great explanation sir. Thanku so much.
@techdose4u
@techdose4u Жыл бұрын
Welcome :)
@yitingg7942
@yitingg7942 4 жыл бұрын
Second comment, I don't know what to say, just extremely grateful about the fact that you are there!!
@techdose4u
@techdose4u 4 жыл бұрын
I am glad to see you in every video :)
@yitingg7942
@yitingg7942 4 жыл бұрын
@@techdose4u I am a big fan of yours and will always support your channel lol !
@techdose4u
@techdose4u 4 жыл бұрын
😂 Thanks ❤️
@yitingg7942
@yitingg7942 3 жыл бұрын
@@techdose4u I am a little stuck to understand why that when it is 0, we just change the entire column to 0.
@JohnWick-kh7ow
@JohnWick-kh7ow 3 жыл бұрын
Sir isme kaha dp lagi. Ye samajh nhi aaya. Ye to stack vala question ho gaya.
@ShubhamKumar-fi1kp
@ShubhamKumar-fi1kp 3 жыл бұрын
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
@balakrishnanr648
@balakrishnanr648 2 жыл бұрын
​ @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??
@bchen1403
@bchen1403 2 жыл бұрын
Excellent visualization!!
@prakharagarwal6237
@prakharagarwal6237 3 жыл бұрын
Nice Explanation! Thanks!
@harshyadav5162
@harshyadav5162 3 жыл бұрын
The code link in description contains the code for the Histogram problem....
@altafmazhar7762
@altafmazhar7762 3 жыл бұрын
yes same question
@chamoli552
@chamoli552 2 жыл бұрын
Brilliant Explaination sir🙏
@K_EC_AnuragKumarMishra
@K_EC_AnuragKumarMishra 3 жыл бұрын
Best explanation🤗
@ADNANAHMED-eo5xx
@ADNANAHMED-eo5xx 4 жыл бұрын
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
@vinitraj6268 Жыл бұрын
One more thing we can add the elements till we get 1 from upper rows, in order to make histogram
@blasttrash
@blasttrash 3 жыл бұрын
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?
@techdose4u
@techdose4u 3 жыл бұрын
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 :)
@blasttrash
@blasttrash 3 жыл бұрын
@@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?
@shivverma1459
@shivverma1459 3 жыл бұрын
@@blasttrash well that is why interviews need a lot of practise beforehand.
@blasttrash
@blasttrash 3 жыл бұрын
@@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-137shubhpratapsingh4
@it-137shubhpratapsingh4 3 жыл бұрын
@@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
@jayvideos341 Жыл бұрын
Soo nice explanation
@varunmanchanda3972
@varunmanchanda3972 3 жыл бұрын
Sir, I have one question. Can we do it using Recursion?
@techdose4u
@techdose4u 3 жыл бұрын
No
@girikgarg1268
@girikgarg1268 3 жыл бұрын
What was the intuition behind converting row of matrix to histogram?
@sainikhil956
@sainikhil956 3 жыл бұрын
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
@wepafitness5612
@wepafitness5612 2 жыл бұрын
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.
@anupam6045
@anupam6045 2 жыл бұрын
wonderful explanation
@kanwarkajla
@kanwarkajla 3 жыл бұрын
u r great sir
@techdose4u
@techdose4u 3 жыл бұрын
Thanks 😊
@mondayemmanuel191
@mondayemmanuel191 3 жыл бұрын
Great explanation as usual.👌🏿
@mohitsaran9193
@mohitsaran9193 2 жыл бұрын
What is the significance or purpose of leftbound and rightbound ??
@nareshh74
@nareshh74 4 жыл бұрын
Hi Surya, Thanks for the video😀. Can you pls tell which observation made you see the problem as histogram. Thanks.
@techdose4u
@techdose4u 4 жыл бұрын
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.
@nareshh74
@nareshh74 4 жыл бұрын
@@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 ✌️.
@techdose4u
@techdose4u 4 жыл бұрын
@@nareshh74 Practice helps a lot.
@balakrishnanr648
@balakrishnanr648 2 жыл бұрын
​@@techdose4u is it DP ??? Its just a STACKS based ques right??
@krishnaprabeesh2415
@krishnaprabeesh2415 3 жыл бұрын
Good explanation
@sainikhil956
@sainikhil956 3 жыл бұрын
Sir, but how to come up with these kind of solutions in interview if we haven't seen this before?
@Thepankaz1
@Thepankaz1 3 жыл бұрын
Thats where you are right, you dont.
@snehasamuel7426
@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?
@ankitjangir01
@ankitjangir01 3 жыл бұрын
It would be better if you show the last code part zoomed in.
@techdose4u
@techdose4u 3 жыл бұрын
Sure
@kumarc4853
@kumarc4853 3 жыл бұрын
thank you for the dose :)
@tanishgotti3659
@tanishgotti3659 9 ай бұрын
Where are we using solutions of sub problems to get solution? Isn't this supposed to be DP...
@whysoserious-yj1ku
@whysoserious-yj1ku 2 жыл бұрын
How is this a dynamic programming solution?
@seelamrameshreddy702
@seelamrameshreddy702 4 жыл бұрын
Please Explain and provide Java Solutions as well
@techdose4u
@techdose4u 4 жыл бұрын
Will try for JAVA 😅
@iit_da_munda
@iit_da_munda 3 жыл бұрын
How this is a DP problem...?? ... this is purely a stack problem.
@animeshjain4594
@animeshjain4594 3 жыл бұрын
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
@PradeepKumar-eq7kj
@PradeepKumar-eq7kj 4 жыл бұрын
nice...
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@subhamkadam5594
@subhamkadam5594 3 жыл бұрын
Can someone explain line number 12 of code
@mahipalsingh-yo4jt
@mahipalsingh-yo4jt 4 жыл бұрын
1st comment;)
@techdose4u
@techdose4u 4 жыл бұрын
😀
@scchouhansanjay
@scchouhansanjay 2 жыл бұрын
👏👏👏
@vivekptl296
@vivekptl296 3 жыл бұрын
Is code language is javascript?
@Gamerschoicetechy
@Gamerschoicetechy 3 жыл бұрын
JS CODE if(matrix.length == 0 ) return 0; let finalmax = 0; let arrforhistogram = new Array(matrix[0].length).fill(0); for(let i=0; i= heights[i]){ stack.pop(); } right[i] = stack.length ==0 ? heights.length -1 : stack[stack.length -1] - 1; stack.push(i); } } for(let i=0 ; i
@danieldouglas4006
@danieldouglas4006 4 жыл бұрын
hi, what writing app do you use?
@sanjeetsingh4743
@sanjeetsingh4743 2 жыл бұрын
How is one supposed to come up with such a solution in 45 mins? 😅
@varunappriyasekar902
@varunappriyasekar902 2 жыл бұрын
please zoom in the program
Matrix Chain Multiplication idea and its pattern detection
12:53
Largest rectangle in Histogram | Leetcode #84
27:43
Techdose
Рет қаралды 112 М.
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
Maximal square | Dynamic programming | Leetcode #221
18:27
Techdose
Рет қаралды 72 М.
Coding Interview Problem: Largest Rectangle in a Histogram
16:18
Jackson Gabbard
Рет қаралды 309 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 238 М.
L13. Maximal Rectangle | Stack and Queue Playlist
12:12
take U forward
Рет қаралды 27 М.
Maximal Square - Top Down Memoization - Leetcode 221
19:50
NeetCode
Рет қаралды 75 М.
L12. Largest Rectangle in Histogram | Stack and Queue Playlist
31:42
take U forward
Рет қаралды 62 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 242 М.
you will never ask about pointers again after watching this video
8:03
The LeetCode Fallacy
6:08
NeetCode
Рет қаралды 603 М.
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН