Given a 2D matrix of 0s and 1s, find maximum size rectangle of all 1s in this matrix. github.com/mis... github.com/mis...
Пікірлер: 150
@freethinker9774 жыл бұрын
2020 - still the best explanation IMHO
@hitesh38593 жыл бұрын
2021 - still the best explanation IMVHO
@皓楠叶23 күн бұрын
@@hitesh3859 2024 - still the best explanation IMVHO
@sait54898 жыл бұрын
getting things very easily by listening to ur lectures... thank u :-)
@2LegHumanist6 жыл бұрын
Great explanation. I've been struggling with this one. All over the internet people just plonk their code down without explanation, but you explain it so simply.
@nikostrongioglou99413 жыл бұрын
What exactly did he explain? He has an algorithm and he is walking through the algorithm... Nothing more than that. No word on HOW he came up with the algorithm or what is the intuition behind this...
@anushree37442 жыл бұрын
@@nikostrongioglou9941 exactly. Nothing on thought process.
@VladimirDjokic6 жыл бұрын
Great explanation! You should write a book "cracking the code interview"
@nguyenhoanvule57556 жыл бұрын
No bro! He should write the book "how to make the coding interview become a funny game". I feel really comfortable about his explain
@kant.shashi7 жыл бұрын
Each row adds to values for histogram bars unless the cell value is 0 .. (i.e. bar is gone) and so at each row/level we check out maximum area under histogram (above the row)..which is the max rectangle.
@anonymousmail99323 жыл бұрын
Really best explanation
@ShabnamKhan-cj4zc4 жыл бұрын
U are always saviour for any tough question. Thanks for such simple explanation.
@enjili60623 жыл бұрын
Only a trivial personal thought for why the "histogram" strategy works: a rectangle must have its bottom width land on a row. Here, we are simulating through all the rows for to get the bottom width to make the rectangle the maximum.
@ok123ut4 жыл бұрын
an intuition to the approach would have been amazing. but great explanation!
@ashishdukare13134 жыл бұрын
Please can you explain why this approach is working? what is the intuition behind it?
@ShinAkuma7 жыл бұрын
where were you all my life. Been struggling way too hard with this problem yet it's so simple.
@dibyendusarkar68214 жыл бұрын
What I like about you, is you save a lot of time, thanks man
@karanpathak5283 жыл бұрын
Hello Tushar awesome explanation can you pls advice book/wiki for this algorithm.
@curious_haldar2 жыл бұрын
Beautifully explained
@manivannanrajah8 жыл бұрын
can we get an explanation on why this approach works?
@hardikraj94694 жыл бұрын
@@Bdjdiwhbkdk Thanks it really clear things up
@saifurrahmanbhuiyan9259 жыл бұрын
thnaks a lot tushar . i almost learnt all dp algorithm u provided. plz carry on ur great effort. and provide us more tutorial . I have learnt dynamic programmong from ur tutorial . i am indebted to you
@vaibhavchhabra8008 жыл бұрын
+Tushar Roy At time @5:44 u said that the Maximum Size Rectangle containing 1's is that (located at bottom down). How did u find that that is the box containing max 1's .Obv by luking at box v know that since max size is 8 and this box size is also 8 ,v can say that yes this box is the one , but how will a computer do it ? V know that the max size is 8, how to find the position of that box ?
@vaibhavchhabra8008 жыл бұрын
+Tushar Roy Ok thnx
@robertkasper98904 жыл бұрын
How would you modify this algorithm to return all the coordinates of the largest rectangle, or a specific corner & the width and height?
@adhishmalviya24084 жыл бұрын
we need 3 things to get co-ordinates length breadth and lower left vertex: i think now you can find it anyways Im not going to type all of that
@nguyenkien55583 жыл бұрын
Thanks for short and clear explanation!
@SaitejaPamulapati8 жыл бұрын
I think it can be solved in O(1) space complexity , if you use 1st row or 1st column instead of extra array . Since you have no further use of it
@wozhendeaa8 жыл бұрын
I think that what you are proposing will still be 0(n), where n is the width of the original array.
@karthikk58956 жыл бұрын
Agree with Billy Yang. Also, finding the largest histogram function itself might take a space of O(col/rows).
@prakritiinusa8 жыл бұрын
ya your explanations are simple , so easy to understand.. Thanks!!
@samarthsharma90196 жыл бұрын
why are we using histogram ?
@contactdi84263 жыл бұрын
@@samarthsharma9019 For that answer, you have to look at this problem: "Largest Rectangular area in Histogram"
@mateuszlewicki76168 жыл бұрын
+Tushar Roy You write this algorithm has O(rows * columns) complexity. This suggests that finding rectangle in histogram is solvable in O(1) - this feels very wrong.
@mateuszlewicki76168 жыл бұрын
sorry, You are right.
@carsonliao81897 жыл бұрын
Mateusz Lewicki I'm concerned about it too..
@93513796278 жыл бұрын
Hi Tushar, You explain every problem very well, but I dont understand why this histogram logic works here. Please explain..Thanks
@sanyamsinghal79924 жыл бұрын
for a rectangle we need two things, length and breadth, as the width is maintained by the array by summing up, then we just have to the appropriate length for given possible width so that we can decide our rectangle. So, with the help of histogram area here we are just deciding the best possible length we can have for a rectangle.
@ameyapatil11393 жыл бұрын
Looking at leetcode - Would temp[j]++ work instead of temp[j] += input[i][j]; on line 40 ?
@DhanunjayG8 жыл бұрын
Hi Tushar..first of all thanks a lot for all your efforts..I have learnt a lot from you. Can we use and expand the same procedure above to find the largest subcube in a NxNxN cube cosisting of 1x1x1 subcubes with values either 0 or 1. like adding a new dimension to it. If possible can you please suggest the way forward
@chaitanyadumpa24395 жыл бұрын
Thank you Gautam gambhir
@gyrogojo6 жыл бұрын
is this dynamic programming solution? We are not using solution to subproblems to get the final solution.
@xxbighotshotxx5 жыл бұрын
Great question. Yes, we are. In order to solve for the input that we are given, we need to build the solution up row by row
@imalive4045 жыл бұрын
@Exodus .. The histogram is formed taking into account the previous rows. The height of any bar in the histogram is dependent on the previous row . If you look closely if (i, j) == 0 then our histogram height there is 0. But if (i,j)==1 then histogram height is height(i-1, j) + 1. Formally Say, h(i, j) = height of histogram bar at i, j then if (i, j) == 1: ....h(i, j) = h(i-1, j) + 1 else: ...h(i, j) = 0 Hope this helps
@jessematty87885 жыл бұрын
nice video but how is an area of 5 a rectangle?
@kartikkumar-60092 жыл бұрын
hey it is not a dp approach so Y ur title include dp
@ethantestimon99058 жыл бұрын
Hi Tushar... I have one question: why the algorithm of maximum size square of all 1's will not work here?
@deepanshugupta52994 жыл бұрын
Every time I see his videos, I feel like its raining in the background
@Neerajkumar-xl9kx4 жыл бұрын
or raining from ground hahahahahhah
@arnavsingh04 жыл бұрын
Although you explain things pretty easily but they are to be memorized if we go by your way.
@mirnafayezboules33104 жыл бұрын
Really you are amazing .. One of the best
@seank91227 жыл бұрын
so fast yet so clear. great explanation!
@RaviMaurya-qi5ht5 жыл бұрын
Why can't I like these videos more than once!!
@YashCodesX5 жыл бұрын
This is part of product design of 'KZbin'. Feel free to contact youtube devs
@huynguyenquang19933 жыл бұрын
Great explanation
@CryingMG9 жыл бұрын
Lovely tutorials, Thanks alot Tushar! Learning so much from you!
@sayeesh32363 жыл бұрын
Good one Tushar!!!
@varunsuryan10213 жыл бұрын
You are a gem, mate.
@aprofromuk7 жыл бұрын
@Tushar any idea how to compute the largest cross please?
@ivanonyshchenko3 жыл бұрын
I am looking for explanation how to come to the described algorithm, but instead see what to do without explaining why
@aishwaryabadgujar75327 жыл бұрын
+Tushar Roy, this method works for all examples? Suppose the 5x5 2D matrix is 1 0 0 0 1 0 1 1 1 0 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0 According to your method, the answer 4. Whereas the answer should be 6 (or 8 if you consider the vertical rectangle) Please help!
@dhirajdwivedi35307 жыл бұрын
ya this algo will work for all examples, for your example too for 1st row 1 0 0 0 1 max rectangle is 1 for 2nd row 0 1 1 1 0 max rectangle is 3 i.e. largest rectangle in histogram made by this array for 3rd row 1 2 2 2 0 max rectangle is 6 for 4th row 0 0 3 3 0 max rectangle is 6 for 5th row 1 1 4 4 0 max rectangle is 8 hence your answer i hope u got it 1 1 4 4 0 all indicates the height of histogram at each index of width 1 unit and u have to find the max area in each histogram and compare with the last one
@aishwaryabadgujar75327 жыл бұрын
Oh. For the third row, you do not consider that 1 while taking the max rectangle? If you take that 1, the max rectangle will be 4. So will have to apply some algorithm to choose the elements that form the max rectangle instead of considering all the non zero elements. Thanks for your help!
@ShinAkuma7 жыл бұрын
You do consider that 1. He probably overlooked it. Row 3 = 00331. However this doesn't affect the maxArea. since max rectangle at this point will be: 111 111
@SatyanarayanaBolenedi8 жыл бұрын
You Nicely build upon algorithm which u alrdy taught us!! Thanks Tushar!!
@code_life48354 жыл бұрын
what is the Intuition behind this working?
@quirkyquester4 жыл бұрын
amazing explanation! Thank you!
@7x34hj6 жыл бұрын
Do you have any videos Maximum Distance Matrices (MDS)? If not, could you do a quick one? Thx
@singvijaya8 жыл бұрын
Could you explain how to calculate max histogram area without using the library function?
@franciscov5115 жыл бұрын
Keeping the smallest of a continuous subset of non zero values
@anubhavkumar16374 жыл бұрын
You can calculate using stack
@kkaabbccdd80807 жыл бұрын
I would have really appreciated if you could have spent 1 min on code implementation at very high top level. +1 for your efforts on explanation.
@dongreak44785 жыл бұрын
how do you find the largest multiplication value in each row?
@yernartalgatuly42528 жыл бұрын
in which video did you discuss maximum size square in matrix?
@yernartalgatuly42528 жыл бұрын
***** thanks
@wozhendeaa8 жыл бұрын
with the following case 100 010 110 if we use the largest rectangle method wouldn't his case result be 4?(2 in the first col and 2 in the second col)
@wozhendeaa8 жыл бұрын
but the actual result is 2
@brandonm10887 жыл бұрын
1] 100 MAX is 1 2] 010 MAX is 1 3] 120 MAX is 2 When a row has a zero in it, it overrides the value in that slot, if that didn't happen then row three would have returned 220, which as we know is incorrect. Hope that helps, I don't think he explicitly mentions that behavior in the video, but it's there
@mshyamkrishnan8 жыл бұрын
Is it possible to adapt this algorithm to find out biggests border with 1s ? (which means the inside of the subrectangle need not be 1) ? Great video btw!
@shubham2774 жыл бұрын
Bro, in samsung they asked me this problem to find out biggests border with 1s and I could not make any progress.
@guneettalwar26303 жыл бұрын
Whats the intuition behind this?
@torrtuganooh24844 жыл бұрын
why do we add from the top in case of 1. Can anyone please explain?
@gauravmamgain92258 жыл бұрын
You are awesome. Your videos helped me a lot . Thanks man :)
@j50313j503134 жыл бұрын
Extremely helpful!
@devendrasharma79998 жыл бұрын
Thanks sir, this video made this question very easy .
@javawicode7 жыл бұрын
how to solve problem when we want found maximum rectangle with- all of 0s
@avinashkumar8057 жыл бұрын
convert 0 to 1 and 1 to 0 in the array and then do the same process on the new matrix.
@jatinkumar44103 жыл бұрын
nice explanation
@sheetalshireen8 жыл бұрын
Hi Tushar...Thanks for helpful video..1 query - how to handle non contiguous array like 0,0,2,0,0,2.
@sheetalshireen8 жыл бұрын
+Sheetal according to your code, its giving area = 0...please correct if something is missing
@sheetalshireen8 жыл бұрын
lets take example of 1,0,1,0.. and when we run following line after reaching last 0 (i==3) else { area = arr[top] * i; }...it will give 1*3 = 3 (where arr[top] =1 & i =3)
@justanaverageguy47393 жыл бұрын
great sir great thank you
@saumya18578 жыл бұрын
Thankyou .you made DP so easy .
@tatinenimaheshbabu73448 жыл бұрын
Thanks for such a good video..
@p111calcutta19 жыл бұрын
good explanation, thanks !!
@HadisKianpour-iw2ni4 ай бұрын
why 8? i cant undrestand:(
@GinoMtzRamos6 жыл бұрын
tushar, you are my hero
@Hystrix0084 жыл бұрын
Nice explanation, thank you : )
@nuamaaniqbal63732 жыл бұрын
Thanks man!
@hobokenbloomfiled83569 жыл бұрын
Another way to find max area would be area = max ((min * length of longest consecutive array), (highestValue in the array))
@hobokenbloomfiled83569 жыл бұрын
***** so in your video the last histogram values are 0 0 3 4 2 4 According to my suggestion length of longest consecutive array is 4 min value in the consecutive array is 2 highest value is 4 area = max ((min * length of longest consecutive array), (highestValue in the array)) area = max((2*4),4) area = max(8,4) area = 8 Time complexity O(length of the histogram array) Another example histogram array values 0 6 0 2 1 0 length of longest consecutive array is 2 min value in the consecutive array is 1 highest value is 6 area = max ((min * length of longest consecutive array), (highestValue in the array)) area = max((1*2),6) area = max(2,6) area = 6 Correct me if my solution seems wrong.
@hobokenbloomfiled83569 жыл бұрын
***** Makes sense I did realize that once I posted the comment. Although we could still use the formula for each consecutive array and compare the current max to the previous max .. and at the end of the array we can get the max of the entire array. 9,0,3,3,3,3,,0,1,1,1,1,1,1,1,1 Set arrayMax = 0 First Check max(9,0) arrayMax = 9; Second Check max(9,12) arrayMax=12; Third Check max(12,8) arrayMax =12 end of the array return arrayMax; So my formula would change to arrayMax = max ((min value in consecutive array * length of consecutive array), arrayMax);
@hobokenbloomfiled83569 жыл бұрын
***** Agreed did not consider this use case. Yeah my solution would return 5 in this case . But the max area should be 6. Is that correct? I just went down the rabbit hole trying to come up with a solution to it. Thanks for all the use cases.
@SanjayKumar-jz3gf6 жыл бұрын
Is there a way to find indexes of largest rectangle?
@lakshaykapoor65596 жыл бұрын
Just store the row no. when u update the max_area first time and last time. For column no. get the indexes of the array when u calculate its area in histogram.
@andriibogachenko25378 жыл бұрын
what if rows could be changing the places ? hm....
@pavithras79363 жыл бұрын
Max sum😍😍😍😍😍
@surekhagaikwad28015 жыл бұрын
how we can do it for parallelogram?
@aatifnazar17665 жыл бұрын
how do you represent parallelogram in a data structure?
@pranjalabhishek75664 жыл бұрын
In your videos there is one issue, You should first explain the approach on the basis of test cases and slowly move towards building the algorithm. I meant to say that how we can approach a new problem that should be discussed .The solutions to problem can be found anywhere but only to understand the journey which lead to that solution we watch the videos. Thanks
@saikarthikcheedella43017 жыл бұрын
thats really cool .....nice explination.
@Ilsk8q5 жыл бұрын
You made it really easy to understand, thank you
@glacieredge63115 жыл бұрын
Nice video. And recthaangle. Haha 😀
@karangautam88327 жыл бұрын
anyone can tell me how to find starting index of rectangle in this question
@ShinAkuma7 жыл бұрын
A noob programmer like me would take these few variable. startI , startJ and run a loop from startI=0 till less than maxArea startJ=0 till less than maxArea Search if all values in this range are 1's. If you find a 0. Just shift this whole Window accordingly till you find the rectangle. Though this probably isnt the best way to do it, but I'm a noob and that's how I did it.
@rajeshpany70174 жыл бұрын
It not working for all case
@tapanjeetroy82665 жыл бұрын
Thank you sir
@1Kapachow18 жыл бұрын
First of all, thanks for your videos, you've created a very wide collection :) Either you are missing a step or I'm missing something. Here's an example: I isolated the problem to a very simple test case. Imagine that your matrix is a very simple one 10 01 your first histogram will be: 10 max area is 1 using your accumulation method, the second histogram will become 11 the max area is 2 which brings us to final answer of 2, which is obviously not a valid answer. Am I missing something here ? Any feedback is welcome :)
@1Kapachow18 жыл бұрын
+Tushar Roy Thanks, now it makes sense :)
@HarishAmarnath3 жыл бұрын
This is wrong ! If we swap index 2,4 to 1,4 we ll still get same answer , but the matrix is not all made of 1's ..check at 5:53
@himeshk1878 жыл бұрын
Awesome !
@aatifnazar17665 жыл бұрын
mind = blown!
@bryanbocao49066 жыл бұрын
Maybe a 3D histogram?
@diegocondori56733 жыл бұрын
thanks :)
@LearnAI-andML7 жыл бұрын
just Awsome
@Simoni12034 жыл бұрын
Better to explain the logics first before throwing in the solution.
@ateamcoolboy58155 жыл бұрын
Lmao love your videos and your hair
@shivendrakadam75965 жыл бұрын
I think time complexity is O(RC^2)
@aquibansari39415 жыл бұрын
Ye sahi banda h! Like comment if your think the same!
@kalytheo8 жыл бұрын
Thank you for everything! You are amazing! However, I believe this theory does not work for the following example (the number of lines is n=5 and the number of columns is m=5): 5 5 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 1 1 1
@TheBD00008 жыл бұрын
it will work.. answer will be 10
@vinayak186f33 жыл бұрын
Thanks
@vinayak186f33 жыл бұрын
Keep visiting again n again you fool 😠
@arka.outside6 жыл бұрын
You are GOD !
@namangrover19896 жыл бұрын
Time Complexity should be O( rows * cols^2)
@gautamrchavan9 жыл бұрын
@2:44, the value should be 1
@gautamrchavan9 жыл бұрын
***** sorry my bad. I didn't listen to it clearly. Btw, great set of videos. Keep up the good work.
@suniljaiswal24714 жыл бұрын
check the code here also if you want cpincpp.blogspot.com/2020/09/maximal-rectangle-in-given-matrix-with.html
@amankumar-setАй бұрын
But how to calculate max histogram area.😂
@helloeveryone27499 жыл бұрын
suppose my row value were 3 3 4 2 then max area acc to you would have been 8 but 9 is the max value.
@AmitDhiman0009 жыл бұрын
+The rock first check the histogram area calculation algorithm video, then you will come to know every thing is proper.