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

  Рет қаралды 85,809

Techdose

Techdose

Күн бұрын

This video explains a very important programming interview problem which is based on dynamic programming.The problem is to find the maximal area of rectangle in a given binary matrix.In this problem, we are given a binary matrix and we are required to find the largest area of rectangle.I have explained the intuition for solving this problem by giving some examples and then I have shown diagramatically, how to solve this problem by using the same concept of dp as we used to solve the problem of finding the largest rectangle area in a histogram.I have given dry run idea and at the end of the video, I have also shown the code walkthrough.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
========================================================================
Join this channel to get access to perks:
/ @techdose4u
INSTAGRAM : / surya.pratap.k
SUPPORT OUR WORK: / techdose
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
TELEGRAM Channel LINK: t.me/codewithT...
TELEGRAM Group LINK: t.me/joinchat/...
=======================================================================
CODE LINK: techdose.co.in...
USEFUL VIDEOS:-
Largest rectangle in Histogram: • Largest rectangle in H...
Maximal Square: • Maximal square | Dynam...
#dp #dpongrid #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
@apbh
@apbh 14 күн бұрын
Best explanation! Very easy to follow and implement. THANK YOU!
@adityaojha2701
@adityaojha2701 3 жыл бұрын
Thanks for both video!!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@mismis3153
@mismis3153 2 жыл бұрын
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.
@dr_920
@dr_920 2 жыл бұрын
Not sure what kinds of person gave thumb down. Thanks for the clear explanation.
@anantsaxena5454
@anantsaxena5454 2 ай бұрын
was able to write the code on my own thankyou
@divyarya7122
@divyarya7122 2 жыл бұрын
Thank you! you're life saver
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@sameergirkar
@sameergirkar 3 жыл бұрын
Great Explanation!! Thanks for the video.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@lets_see_777
@lets_see_777 2 жыл бұрын
best channel on youtube for these questions.
@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??
@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 :)
@bchen1403
@bchen1403 2 жыл бұрын
Excellent visualization!!
@prakharagarwal6237
@prakharagarwal6237 3 жыл бұрын
Nice Explanation! Thanks!
@yitingg7942
@yitingg7942 3 жыл бұрын
Second comment, I don't know what to say, just extremely grateful about the fact that you are there!!
@techdose4u
@techdose4u 3 жыл бұрын
I am glad to see you in every video :)
@yitingg7942
@yitingg7942 3 жыл бұрын
@@techdose4u I am a big fan of yours and will always support your channel lol !
@techdose4u
@techdose4u 3 жыл бұрын
😂 Thanks ❤️
@yitingg7942
@yitingg7942 2 жыл бұрын
@@techdose4u I am a little stuck to understand why that when it is 0, we just change the entire column to 0.
@vinitraj6268
@vinitraj6268 8 ай бұрын
One more thing we can add the elements till we get 1 from upper rows, in order to make histogram
@jayvideos341
@jayvideos341 Жыл бұрын
Soo nice explanation
@JohnWick-kh7ow
@JohnWick-kh7ow 3 жыл бұрын
Sir isme kaha dp lagi. Ye samajh nhi aaya. Ye to stack vala question ho gaya.
@chamoli552
@chamoli552 2 жыл бұрын
Brilliant Explaination sir🙏
@anupam6045
@anupam6045 2 жыл бұрын
wonderful explanation
@kumarc4853
@kumarc4853 3 жыл бұрын
thank you for the dose :)
@K_EC_AnuragKumarMishra
@K_EC_AnuragKumarMishra 2 жыл бұрын
Best explanation🤗
@kanwarkajla
@kanwarkajla 2 жыл бұрын
u r great sir
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@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
@mondayemmanuel191
@mondayemmanuel191 3 жыл бұрын
Great explanation as usual.👌🏿
@harshyadav5162
@harshyadav5162 3 жыл бұрын
The code link in description contains the code for the Histogram problem....
@altafmazhar7762
@altafmazhar7762 3 жыл бұрын
yes same question
@krishnaprabeesh2415
@krishnaprabeesh2415 3 жыл бұрын
Good explanation
@ADNANAHMED-eo5xx
@ADNANAHMED-eo5xx 3 жыл бұрын
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?
@nareshh74
@nareshh74 3 жыл бұрын
Hi Surya, Thanks for the video😀. Can you pls tell which observation made you see the problem as histogram. Thanks.
@techdose4u
@techdose4u 3 жыл бұрын
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 3 жыл бұрын
@@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 3 жыл бұрын
@@nareshh74 Practice helps a lot.
@balakrishnanr648
@balakrishnanr648 2 жыл бұрын
​@@techdose4u is it DP ??? Its just a STACKS based ques right??
@thecyberexperts5612
@thecyberexperts5612 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.
@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
@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 2 жыл бұрын
@@blasttrash well that is why interviews need a lot of practise beforehand.
@blasttrash
@blasttrash 2 жыл бұрын
@@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 2 жыл бұрын
@@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.
@iit_da_munda
@iit_da_munda 3 жыл бұрын
How this is a DP problem...?? ... this is purely a stack problem.
@mohitsaran9193
@mohitsaran9193 2 жыл бұрын
What is the significance or purpose of leftbound and rightbound ??
@varunmanchanda3972
@varunmanchanda3972 3 жыл бұрын
Sir, I have one question. Can we do it using Recursion?
@techdose4u
@techdose4u 3 жыл бұрын
No
@scchouhansanjay
@scchouhansanjay 2 жыл бұрын
👏👏👏
@PradeepKumar-eq7kj
@PradeepKumar-eq7kj 3 жыл бұрын
nice...
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@ankitjangir01
@ankitjangir01 3 жыл бұрын
It would be better if you show the last code part zoomed in.
@techdose4u
@techdose4u 3 жыл бұрын
Sure
@tanishgotti3659
@tanishgotti3659 5 ай бұрын
Where are we using solutions of sub problems to get solution? Isn't this supposed to be DP...
@seelamrameshreddy702
@seelamrameshreddy702 3 жыл бұрын
Please Explain and provide Java Solutions as well
@techdose4u
@techdose4u 3 жыл бұрын
Will try for JAVA 😅
@snehasamuel7426
@snehasamuel7426 11 ай бұрын
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?
@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 2 жыл бұрын
Thats where you are right, you dont.
@whysoserious-yj1ku
@whysoserious-yj1ku 2 жыл бұрын
How is this a dynamic programming solution?
@subhamkadam5594
@subhamkadam5594 3 жыл бұрын
Can someone explain line number 12 of code
@mahipalsingh-yo4jt
@mahipalsingh-yo4jt 3 жыл бұрын
1st comment;)
@techdose4u
@techdose4u 3 жыл бұрын
😀
@animeshjain4594
@animeshjain4594 2 жыл бұрын
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
@sanjeetsingh4743
@sanjeetsingh4743 2 жыл бұрын
How is one supposed to come up with such a solution in 45 mins? 😅
@varunappriyasekar902
@varunappriyasekar902 Жыл бұрын
please zoom in the program
@danieldouglas4006
@danieldouglas4006 3 жыл бұрын
hi, what writing app do you use?
@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
Matrix Chain Multiplication idea and its pattern detection
12:53
LARGEST RECTANGLE IN HISTOGRAM - Leetcode 84 - Python
17:20
NeetCode
Рет қаралды 221 М.
У ГОРДЕЯ ПОЖАР в ОФИСЕ!
01:01
Дима Гордей
Рет қаралды 7 МЛН
GTA 5 vs GTA San Andreas Doctors🥼🚑
00:57
Xzit Thamer
Рет қаралды 26 МЛН
АЗАРТНИК 4 |СЕЗОН 2 Серия
31:45
Inter Production
Рет қаралды 849 М.
Maximal square | Dynamic programming | Leetcode #221
18:27
Techdose
Рет қаралды 70 М.
8 Max Area Rectangle in binary matrix
29:39
Aditya Verma
Рет қаралды 151 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 204 М.
Largest rectangle in Histogram | Leetcode #84
27:43
Techdose
Рет қаралды 110 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,2 МЛН
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 264 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Coding Interview Problem: Largest Rectangle in a Histogram
16:18
Jackson Gabbard
Рет қаралды 307 М.
Maximal Square - Top Down Memoization - Leetcode 221
19:50
NeetCode
Рет қаралды 66 М.