Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too,. Do follow me on Instagram: striver_79
@harshkesharwani5112 Жыл бұрын
mx=max(mx,ds.size[ds.findUparent(0)]); This much code in the last section of code is sufficient to satisfy the case -> (1's at all places in grid)....and it is running safe...
@adarshmv2615 ай бұрын
Please clarify this doubt striver: while we do the union of all adj cells in a component why we are not maintaining a visited matrix?? Are we not doing redundant union-Ing without a visited??? Please clarify 🙏🏼🙏🏼🙏🏼🙏🏼
@codecrafts52639 ай бұрын
such a great explanation for graph HARD question. I was always afraid of graph and could not even code basic graphs, but this series made me so comfortable with graphs that I was able to guess the answer to edge cases and other stuff intutively. Thanks for this striver!
@NiranjanDandekar-sz3jw5 ай бұрын
As usual a great explanatory video. Just a diiferent approach : instead of the last for loop, we can use conditional return where we can return max element if the graph does not have all 1s, else we can return max element in the size array as it will always be the ultimate parent.
@yashaggarwal2949 Жыл бұрын
I was able to solve this problem on my own. Thanks to you for making the concepts crystal clear.
@aakashgoswami23569 ай бұрын
Khao bhagwan kasam.
@abhisheknag19297 ай бұрын
@@aakashgoswami2356 bro there are many genius people who solves much much higher level questions Check one time codeforces accounts you will get to know
@satendra62006 ай бұрын
@@aakashgoswami2356 🤣🤣
@mohanmanis2 жыл бұрын
Awesome 👏 solution but a nitpick, for the case where every cell has value as 1 and we have a large component then instead of looping through each cell and looking for size of parent. Instead, We can just return the number of cells. It is just to save some time. There won’t be any impact on time complexity. int mx = -1; //initialise return mx == -1 ? n * n : mx; //at last check,
@muditkhanna8164 Жыл бұрын
or we can return the size of the ultimate parent of any node because if all are 1s the ultimate parent will have that size, no need to loop.
@siddhantmishra4944 Жыл бұрын
@@muditkhanna8164 True.
@priyanshkumar175 ай бұрын
Thanks for this comment. I also returned n * n, if mx was still -1
@adarshnegi47853 ай бұрын
Thanks Bro
@AyushKumar-uu8vc2 жыл бұрын
this is what called quality content...loved this!
@shriyanshjain4444 Жыл бұрын
I solved Max Area of Island on leetcode using DSU in 10 minutes by myself. Thank You so much for the free resources Raj bhaiya!
@krishnananddubey2870 Жыл бұрын
For the case where every cell has value as 1 and we have a large component then instead of looping through each cell and looking for size of parent we can simply find size of 0th element it will give same answer as its connected to all. Just a suggestion :)
@rishavsingh5568 Жыл бұрын
Indeed
@iamnoob7593 Жыл бұрын
Indeed
@prakharpandey77458 ай бұрын
Indeed
@anchitbhushan61727 ай бұрын
We can straight away say that the largest component size is n*n if there are no 0s.
@yashveersingh14355 ай бұрын
Indeed
@DivineVision201 Жыл бұрын
I am truly a fan of your teaching Style. lots of thanks for all your efforts. You are doing amazing! Thanks for the content 💌
@dummydum-ih4mt2 ай бұрын
Solved it myself !!!! Had this amazing feeling coming up with this solution !.
@shubhanshugupta97542 жыл бұрын
thanks striver, I was not able to figure out the edge case
@sannareddymonesh75982 жыл бұрын
Instead of adding all the components and calculating their sizes afterwards we can just check whether we have seen this component already or not using unordered set and if not seen then we can increase the size.
@netviz8673Ай бұрын
exactly to do that we are using the set. How else are you going to know the visited components? You check that by finding the ultimate parent of the adjacent node. Once you find that ultimate parent lets say it is 5 how will you know that 5 has already been counted or not? You can do so by putting 5 in the set. Or the other method can be to just collect all the ultimate parents and have that many variables or array that could act like a hash and in those variables or array you could mark if that ultimate parent was visited or not.
@souvik33and372 ай бұрын
so glad i coded this by myself after the explanation, thanks striver :) thanks for existing
@KeerthiReddyKolan9 күн бұрын
Greattttt Explanation!!! You brain works on a different level I assume. Thank you for the video
@raghavmanish243 ай бұрын
understood .....very close to finish .....thanku striver
@amanrazz20913 ай бұрын
for the case where all cells are 1 , instead of the last n*n loop to find the maxsize we can simply return max(ans,ds.size[0]) ; The intuition begin here is that even there exists even a single cell marked with 0, then our optimal answer would be in our ans variable and if there exists no cells with 0 then it means our first cell that 0,0 is also a part of the maximum size connected componenet;
@nikhilkoushik9083 Жыл бұрын
15:50 Excellent logic bro🙌🙌🙌
@AbhishekPandey-dj2eo9 ай бұрын
One of the best explaination by striver ❤
@stith_pragya11 ай бұрын
Thank You So Much for this wonderful video..............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@lakeemangal Жыл бұрын
Bhaiya hmko yha third for loop ki jrurt ni pdti because ham second bale loop mai 0 se jo connected uska max le lia hai. but suppose ki array mai koi 0 ni hoga to max milega ni mean saare array mai 1 aur sbhi 1 ko hmne first loop mai connected kr dia to ham directly max ko ans=max(ans,ds.size.get(0)) krke return kr skte hai. And it is working fine for me. Thank you
@kishoregowda8210Ай бұрын
For the edge case where all the cells are already 1, we dont need the last for loop. we can simply return the size of 0th element. code snippet below. return result == 0 ? graph.size[graph.findBy(0)] : result;
@uavishal777 Жыл бұрын
understood bhaiya....How can a person so much clarity ..You are a legend🔥🔥🔥🔥🔥
@navinsaikaarthik37902 жыл бұрын
we can use a count variable inside any of the two for loops, and can get the count of no.of one's or zeroes, in the matrix. If the count of zeroes is 0. Then we can return n*n. Or if the count of one's equals to n*n, then we can return n*n. Hence, we can skip the last for loop.
@piyushacharya76962 жыл бұрын
Niceeee!😎😎
@ayushjain3862 жыл бұрын
You teach in a way. I am able to solve this question by myself. Thank you
@vishious14 Жыл бұрын
Thanks a lot for the previous lectures striver. I was able to solve this on my own. My approach is totally like yours
@santoshb7776 Жыл бұрын
Bro, can't we use just a simple DFS algo for this problem ?
@vishious14 Жыл бұрын
Dfs works but you'll get a lot of complications in the code and a possible TLE.
@psurya30532 жыл бұрын
thankyou sir , you are simply amazing . Your enthusiasim and passion for this subject is phenomenal . god bless u.
@cinime2 жыл бұрын
Understood! So wonderful explanation as always, thank you very much!!
@beinghappy9223 Жыл бұрын
One of the most amazing problem and explanation
@chiragPatel22c Жыл бұрын
bro got so excited 2:34 😅 btw I am following your dsa sheet. finished till here.
@Chandraprakash-kx4ic Жыл бұрын
Understood...thank you dude...lots of love
@abhavgoel9390 Жыл бұрын
we can skip the last loop,by setting mx=INT_MIN, then while returning we can check it its still INT_MIN then return the size of matrix, else mx
@harshwardhanpatki846 Жыл бұрын
Thank you sir for such valuable content ❤️ 🔥🙇♂️ Understood!
@UECAshutoshKumar11 ай бұрын
Thank you sir 😁
@lucygaming972610 ай бұрын
Was able to code the approach where we would do union in step 2. Realized from the video, that we don't need to do it, we can just take the count. Was stuck again when the size was getting counted twice, using set was an excellent approach. Truly a challenging hard problem, where just the knowledge of Disjoint Set was not enough :)
@ASHUTOSHMAURYA-tz9xd Жыл бұрын
Just Awesome Bro... speechless.. best graph series ever...thanks man...
@amansiddiqui32183 ай бұрын
At the end of the code, instead of running loop for n*n, alternatively we can find the size of the 0th node and can compare with mx. Thus, we can save extra n*n time complexity. mx = max(mx, dsu.size[dsu.findUltPar(0)]);
@bumbada13 ай бұрын
damn cool. genius. I was also thinking something similar but yours is better
@abhirupbasu3386 Жыл бұрын
Loved the problem.Excellent explanation
@bvnandhan3073 Жыл бұрын
Thats some awesome contenttt Mannnn , DSA == Striver
@udaytewary3809 Жыл бұрын
Understood bhaiya 🙏❤️
@mohakhiphop2 жыл бұрын
At the last , instead of traversing the whole matrix like - for(int cellno = 0; cellno < n*n ; cellno++) { ans = max(ans , ds.size[ds.ultimateParent(cellno)]); } we can simply do - ans = max(ans , ds.size[ds.ultimateParent(0)]); reason : since we have connected all the 1s in the matrix , (0 is the first '1' that would exist in matrix in case of matrix of 1s) Understood 💎 gem content 💕lll
@mohitkamal4864 Жыл бұрын
Yes I was also thinking the same
@hrithik7354 Жыл бұрын
what I did is set ans = -1 & at the last checked if ans == -1? n*n: ans
@madhavgaur709623 күн бұрын
Great Problem
@lingasodanapalli615 Жыл бұрын
Understood Clearly
@akshaypatade5730 Жыл бұрын
while i was solving this problem, i found out that the given code will fail for the test case 2 1 1 1 1 Code output is giving us 5 However its correct out will be 4 So, what we could in this case is that once we have iterated over the elements in the set. We will check if the current grid element i.e. grid[i][j] = 0. If yes then mx = max(mx, sizeTotal + 1). Else mx = max(mx, sizeTotal)
@SatyamKumar-bw4vi Жыл бұрын
Jay Jagannath...!!!! 🙏🙏 understood
@gautamsaxena46472 ай бұрын
Understood bhaiya
@sambhavagarwal58712 жыл бұрын
striver this is showing tle here so in in the last loop we will use this mx=max(mx,ds.size[ds.findUPar(n*n-1)]); code:class DisjointSet { public: vector rank, parent, size; DisjointSet(int n) { rank.resize(n+1, 0); parent.resize(n+1); size.resize(n+1); for(int i = 0;i=0&&adjr=0&&adjc
@kirtisachapra90305 ай бұрын
Wonderful explanation!!
@sal2054 Жыл бұрын
Great explanation! Reminds me of Minesweeper game. 4:38 🤭
@amanbhadani88402 жыл бұрын
what a nice question and crystal clear, whity milk like pure explanation💥
@lakshsinghania Жыл бұрын
wtf "whity milk" bruh sus
@abhishekkuntare46402 жыл бұрын
Striver you are the life saver !
@mohakhiphop2 жыл бұрын
1 am coders gang 👩💻
@saichandu81788 ай бұрын
for the last part, if a matrix contains all 1's, then its just enough to find `mx = max(mx, size[findUParent[0]])`
@r_uhi057 ай бұрын
Or we can return n*m
@rishabhgupta9846 Жыл бұрын
understood,great content
@samikshyasahoo9010Ай бұрын
I wish one day I'll be as good as you.
@sekhharr Жыл бұрын
could code after your hints Thanks for this aweesome playlist
@Rajat_maurya2 жыл бұрын
Last for loop can be replaced by mx=max(mx,ds.size[ds.findPar(n*n-1)]); small think which i observed :)
@krishanpratap32862 жыл бұрын
hah yes this will also work which year u bro?
@Rajat_maurya2 жыл бұрын
@@krishanpratap3286 4th🥲
@raunakkumar6144 Жыл бұрын
how was ur placements
@natansh055 ай бұрын
Legendary 🫡🫡
@DevanshuAugusty Жыл бұрын
if every element in grid is 1 than we don't need to use for loop of n*n, we just need to find the size of one ultimate parent: max_size = max(max_size, ds.size[ds.findUPar(0)]);
@lakshmand84133 ай бұрын
If every cell is 1 then the mx value stays zero so if its the case no need to check for every cell again right , simply return n*n that will be the size of the ultimate parent . Correct me if i am wrong :)
@avdharna1978 Жыл бұрын
sir last loop can be replace by if(mxSize==0){ return n*n; } return mxSize;
@The_Shubham_Soni Жыл бұрын
Understood.
@-VLaharika Жыл бұрын
Understood 👍
@learnwithayush7838 Жыл бұрын
Understaand I have fallen in love with this guy
@abhinavbhardwaj3372 Жыл бұрын
understood sir
@AryanMathur-gh6df Жыл бұрын
UNDERSTOOD!!!
@NarendraYadav-h2u3 ай бұрын
Hey RAJ sir, just one genuine question!🥱🥱🥱🥱 When you solve a problem for the first time in your life, how do you get this kind of intuition or logic?
@vaibhavsoni2437 Жыл бұрын
If all the cells contains 1, then all the cells will get connected (unionBySize) and make 0 the ultimate parent. Hence, size[0] will be the maximum if all cells contain 1.
@vishnubanjara5209 Жыл бұрын
hey striver i saw that if i declare setst outside of those loops than code is not working and if i declare set inside for loops than it runs fine why it is happening .... plz reply
@mriduljain6809 Жыл бұрын
Understood😊😊
@MMNayem-dq4kd Жыл бұрын
understood.
@bhavya8608 Жыл бұрын
awesome explanationss!!
@AdityaKumar-be7hx Жыл бұрын
Understood!
@suryakiran2970 Жыл бұрын
Understood❤
@zaiem6981Ай бұрын
If every cell is one than there will be only one ultimate parent. we can just return ds.size[0], or size of any other index.
@pulkitchausali1354 Жыл бұрын
able to code myself #thanks striver for this wonderful series😇
@rounaq_khandelwal Жыл бұрын
Love you Striver!
@hiteshnagothu8872 жыл бұрын
Understood. Got tips to make the code work in one go?
@chandrachurmukherjeejucse5816 Жыл бұрын
Understood!!
@dolbyagarwal93162 жыл бұрын
Hey Striver, thanks for the videos. Can you please ask GFG to include Python language as well for these questions. For some I only see C and java.
@adarshmv2615 ай бұрын
Please clarify this doubt striver: while we do the union of all adj cells in a component why we are not maintaining a visited matrix?? Are we not doing redundant union-Ing without a visited??? Please clarify
@gangsta_coder_12 Жыл бұрын
Understood 🔥🔥
@manishpawar5918 Жыл бұрын
Understood
@tirthpatel44985 ай бұрын
wonderfull explanation ever hats off 🫡🔥🔥
@akashsahu2571 Жыл бұрын
thaks
@pratyakshhhhhhhhhhhhhhhhhhhhh11 ай бұрын
In the last point when you run the loop through all cells saying on the basis of 'what if the Matrix contains all ones'. Why we are doing that? We just had to compare it with the size of the parent of the cell 0. Am I right?
@adebisisheriff15910 ай бұрын
Thanks striver!!!
@ACUCSPRADEEPB-up9ne Жыл бұрын
Understood ✌️
@tirthdoshi133711 ай бұрын
Great video !
@anshulgoel1940 Жыл бұрын
Instead of converting row and columns to their respective index and then use it in parent array, can't we define two parent arrays one for row and one for column. This will increase space complexity from O(NXN) to O(2*NXN) but simplify the comparisons. Just a thought.
@sunilpanchal1498 Жыл бұрын
Understood 🤩
@netviz8673Ай бұрын
attention here again: Graph is changing dynamically hence always think of Disjoint Set
@herculean6748 Жыл бұрын
Thanks🙌
@iamnoob7593 Жыл бұрын
superb
@rishabhraj82334 ай бұрын
striver makes tough concepts easy
@nishantdehariya57697 ай бұрын
very nice
@saseerak87632 жыл бұрын
understood!
@Aryan-vw7lt Жыл бұрын
understoooddd
@akarshpandey5721 Жыл бұрын
In the last step, where we are checking for the case when all grid values are 1, we can directly do it by if(mx==0) // means no 0 encountered in the grid { mx= n*n; // bcz in that case, the maximum size will always be the total number of nodes in the grid i.e. n*n } return mx;