G-51. Number of Islands - II - Online Queries - DSU

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024
  • GfG-Problem Link: bit.ly/3w9REfj
    C++/Java/Codes and Notes Link: takeuforward.o...
    DP Series: • Striver's Dynamic Prog...
    SDE Sheet: takeuforward.o...
    Check out our Website for curated resources:
    Our Second Channel: / @striver_79
    In case you are thinking to buy courses, please check below:
    Code "takeuforward" for 15% off at GFG: practice.geeks...
    Code "takeuforward" for 20% off on sys-design: get.interviewr...?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/inv...
    Take 750 rs free Amazon Stock from me: indmoney.oneli...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v...
    Linkedin/Instagram/Telegram: linktr.ee/take...
    ---------------------------------------------------------------------------------------------------------------------------

ความคิดเห็น • 160

  • @takeUforward
    @takeUforward  2 ปีที่แล้ว +30

    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

    • @sukhpreetsingh5200
      @sukhpreetsingh5200 ปีที่แล้ว +1

      understood

    • @shivamkhandelwal2707
      @shivamkhandelwal2707 ปีที่แล้ว

      very hard working bhaiyaaa😮‍💨🥵

    • @RawFromCam
      @RawFromCam 4 หลายเดือนก่อน

      you are literally the best teacher anyone can get,I feared DSA so much since my basics were weak thanks a lot your videos are super addictive,I have actually started liking DSA,in college used to hate it :)

    • @KiranKumar-sb3ti
      @KiranKumar-sb3ti 3 หลายเดือนก่อน

      understood

  • @tanyacse6577
    @tanyacse6577 ปีที่แล้ว +73

    Hi striver
    I am final year student I watch your content from my second year
    turst me I follow lot of content and also purchase course on DSA and i didn't clear my concept
    when i saw your Amazing DP Playlist and graph ,recursion it just 🔥
    I just advice my junior of college to watch your content
    your content was literally bro amazing
    I just want to tell that in u way solved the various question/ problem it just clear all the topic in that way of minute
    u r amazing please never stop doing this it's really hlep alot people with such a content
    and aslo I cracked amazon recently
    Thanks to you to provide such a amazing content
    Thank you so much🔥🔥🔥🔥🔥

    • @ycwhencpp
      @ycwhencpp ปีที่แล้ว

      hey can you share the questions that were asked you during the interview rounds and in OA

    • @dhirendrasingh6071
      @dhirendrasingh6071 ปีที่แล้ว +4

      Amazon wow program...

  • @ashishkumaryadav5252
    @ashishkumaryadav5252 ปีที่แล้ว +41

    Sir, your explanations skills are exceptional, i bought an unacademy course but comparatively your explanations are far better than that paid course, really there is no sense of paying any DSA course online if your videos are available.

  • @gowthamsivam2210
    @gowthamsivam2210 11 หลายเดือนก่อน +4

    The Kind of Excitement you show in your face while explaining.

  • @Ani_86_
    @Ani_86_ ปีที่แล้ว +7

    The explanation was perfect and also this problem marks the beginning of dsu in matrices !!

  • @ayushm106
    @ayushm106 ปีที่แล้ว +5

    Understood. Thanks for laying emphasis on Code Quality

  • @debugger275
    @debugger275 ปีที่แล้ว +11

    Understood !!!
    This series is Gem Mine 🙌🙌 thank you for your hard work

  • @sanskarkumar6391
    @sanskarkumar6391 ปีที่แล้ว +21

    This question was asked to one of my friend in thoughtspot round 1 interview for SDE Intern role.

  • @vishalsinghrajpurohit6037
    @vishalsinghrajpurohit6037 ปีที่แล้ว +10

    There is one more way of doing it. By manipulating the DSU snippet. Initializing parent array with -1 and making parent[node]=node when we iterate the operators array.
    The space complexity is little lesser as we don't always need to initialize the visit and parent array of size (n*m). Instead we are initializing it with size of operators.size().
    But the way striver Bhai has written and explained is clean. Here is my code..
    class DisjointSet{

    public:
    vectorrank,parent,size;
    DisjointSet(int n){
    rank.resize(n+1,0);
    for(int i=0;i

    • @meharvansingh402
      @meharvansingh402 ปีที่แล้ว

      Thanx vishal for sharing your approach

    • @FooBar-lb5wf
      @FooBar-lb5wf หลายเดือนก่อน

      This approach feels more natural and intuitive. Since each query effectively adds one cell land mass to the graph, the total land mass after Q queries would be atmost Q (considering the possibility of duplicate queries). The adjacency of two land cells on the matrix then corresponds to edges connecting those cells (and their components in the DS). The interal state of DS also more accurately captures the true number of components at each step, as opposed to the approach presented in the video where internal state of DS treats singleton water cells as components as well.

  • @stith_pragya
    @stith_pragya 10 หลายเดือนก่อน +1

    Understood........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻.
    Before I checked the complete video, I just watched the video till 6:00 and was able to derive the complete solution by myself........😀😀😀😀😀😀...........This increases confidence.....😎😎😎😎😎😎.
    Thanks a lot Striver Brother.........You are doing a very great job......God Will surely bless you.........😇😇😇😇😇😇

  • @varunaggarwal7126
    @varunaggarwal7126 ปีที่แล้ว +2

    his explanation drew an image in my brain, I was able to code that in 1 go.

  • @yadneshkhode3091
    @yadneshkhode3091 2 ปีที่แล้ว +12

    Thank you for your relentless hardwork even on Diwali ❤️😊❤️😊
    Happy Diwali !

  • @cinime
    @cinime 2 ปีที่แล้ว +3

    Understood! Super excellent explanation as always, thank you very much!!

  • @raghavmanish24
    @raghavmanish24 2 หลายเดือนก่อน

    understood ......very close to finish this series....thanku striver

  • @rishabh_mishra3
    @rishabh_mishra3 ปีที่แล้ว

    Understood!!
    I wish I started watching your videos earlier when I was learning tree and stuff man

  • @stith_pragya
    @stith_pragya 10 หลายเดือนก่อน +2

    Thank You So Much for this wonderful video.......................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @SD-vk3ko
    @SD-vk3ko ปีที่แล้ว +3

    Thank you so much for the effort. I request to please add a lecture on Minimize Malware Spread or Minimize Malware Spread 2 problem also. These are kind of unique patterns of Union Find.

  • @priyanshkumar17
    @priyanshkumar17 4 หลายเดือนก่อน

    excellent explanation!! application of disjoint set data structure explained nicely..Thanks

  • @ajitpalsingh606
    @ajitpalsingh606 3 หลายเดือนก่อน

    Question was very logical ... congrats for 0.6 Million subscribers striver !!

  • @augustinradjou3909
    @augustinradjou3909 8 หลายเดือนก่อน

    Amazing boss....going to complete the series soon❤

  • @vamsikrishnagannamaneni912
    @vamsikrishnagannamaneni912 หลายเดือนก่อน

    The reason for using Union instead of decrementing island count on every directional match :
    If the upper direction already merged two islands into one, the left direction won't contribute further because the union(u, v) function will check the updated ultimate parents of the two cells. If they now share the same parent, it returns False, preventing any redundant merging and maintaining the correct island count.

  • @Chandraprakash-kx4ic
    @Chandraprakash-kx4ic ปีที่แล้ว

    Understood. Thank YOU, STRIVER. Immense love and respect dude💋💋

  • @Sc0tchx0P
    @Sc0tchx0P ปีที่แล้ว +4

    understanding approach is so easy but when it comes to coding it on my own I get stuck for hours

  • @prathambhushan4859
    @prathambhushan4859 4 หลายเดือนก่อน

    U r the best teacher 🙏

  • @spartan1998
    @spartan1998 ปีที่แล้ว +1

    @takeUforward please share the time complexities as you did in the previous videos.

  • @amarjeetgupta1456
    @amarjeetgupta1456 ปีที่แล้ว

    Understood.... Amazing Explanation🙌🙌

  • @chaotichally
    @chaotichally 8 หลายเดือนก่อน +1

    Can I consider only the coordiantes given in the input as nodes and count the no of connected components at each step ??

  • @raunakmishra8403
    @raunakmishra8403 ปีที่แล้ว

    i coded myself sir. such a nice feeling for me.

  • @sourabhgautam4809
    @sourabhgautam4809 ปีที่แล้ว +2

    Time complexity = O(k*4*4*alpha) where k = operators.size();

    • @techbucks7339
      @techbucks7339 ปีที่แล้ว

      N*M also for creating the grid ig

  • @panhejia
    @panhejia ปีที่แล้ว +1

    I was just stuck on "most-stones-removed-with-same-row-or-column" Ah I wish I had say your video earlier... 2d UnionFind is tough to undertstand!

  • @hardikgupta7946
    @hardikgupta7946 10 หลายเดือนก่อน

    initially, initialize the visited array by 0, and make a count variable to store the number of island groups.
    1) pickup querie that is(row,column) in the given operations.
    2) check whether Querie is visited or not visited::
    => If it is not visited mark it as visited and make the count increase by 1, then go for its neighbors
    if a neighbor is found to have 1 then connect the previous neighbor to the new neighbor by dsu,
    and decreases the count by 1 as connections form and add to our ArrayList.
    => If it is visited then store the same count in the list.initially, initialize the visited array by 0, and make a count variable to store the number of island groups.
    1) pickup querie that is(row,column) in the given operations.
    2) check whether Querie is visited or not visited::
    => If it is not visited mark it as visited and make the count increase by 1, then go for its neighbors
    if a neighbor is found to have 1 then connect the previous neighbor to the new neighbor by dsu,
    and decreases the count by 1 as connections form and add to our ArrayList.
    => If it is visited then store the same count in the list.

  • @kashbhalla4363
    @kashbhalla4363 ปีที่แล้ว

    understood great approach striver.

  • @anirudhchandan557
    @anirudhchandan557 ปีที่แล้ว

    Understood. Hats off to you

  • @rushhour4379
    @rushhour4379 5 หลายเดือนก่อน

    If the path compression is not updated according to the latest unions, it is not safe to compare parents before compressing right? It might not always return latest ultimate parent. What is the idea behind comparing without ensuring compression? Am I missing something?

  • @Saurav_Kumar514
    @Saurav_Kumar514 ปีที่แล้ว +1

    What an explanation man !👏👏

  • @shubhiagarwal4047
    @shubhiagarwal4047 6 หลายเดือนก่อน +1

    understood

  • @omlondhe
    @omlondhe ปีที่แล้ว

    We can also use arr[pos/rows][pos%rows]

  • @naveenkumawat4571
    @naveenkumawat4571 ปีที่แล้ว

    Understood, Great explanations

  • @UECAshutoshKumar
    @UECAshutoshKumar 9 หลายเดือนก่อน +1

    Thank you sir 😊

  • @sharathkumar8338
    @sharathkumar8338 2 ปีที่แล้ว

    I always eagerly wait for your videos.

  • @DevanshuAugusty
    @DevanshuAugusty ปีที่แล้ว +1

    time complexity?

  • @udaytewary3809
    @udaytewary3809 ปีที่แล้ว

    Understood bhaiya 🙏❤️

  • @majidshaikh5913
    @majidshaikh5913 2 ปีที่แล้ว +1

    Thanks Striver!

  • @lakeshkumar1252
    @lakeshkumar1252 ปีที่แล้ว

    thank you sir for a lot of effort for us

  • @rishabhgupta9846
    @rishabhgupta9846 ปีที่แล้ว

    understood,awesome explanation

  • @av21015
    @av21015 ปีที่แล้ว

    You are a GEM!! Thank you very much

  • @SatyamKumar-bw4vi
    @SatyamKumar-bw4vi 11 หลายเดือนก่อน

    Hare Krishna..! understood

  • @kushagramishra5638
    @kushagramishra5638 ปีที่แล้ว +1

    "understood" 🙂

  • @raghavagrawal9240
    @raghavagrawal9240 ปีที่แล้ว

    Amazing as always! Thanks a ton!!!

  • @asmitkumar4003
    @asmitkumar4003 ปีที่แล้ว

    Awesome explanation🔥🔥🔥

  • @dhirajthorat285
    @dhirajthorat285 ปีที่แล้ว +1

    understood sir

  • @RawFromCam
    @RawFromCam 4 หลายเดือนก่อน

    East or West , Striver is the best😍,BST Series❤‍🔥 Graph❤‍🔥

  • @saranghae3720
    @saranghae3720 ปีที่แล้ว

    OP Explanation 🔥

  • @evilpollination1916
    @evilpollination1916 4 หลายเดือนก่อน

    understood.

  • @judgebot7353
    @judgebot7353 ปีที่แล้ว

    Understood thanks a lot 👍

  • @muchmunchies43
    @muchmunchies43 2 หลายเดือนก่อน

    Understood!

  • @prateek4279
    @prateek4279 4 หลายเดือนก่อน

    this was a tough one

  • @iamnoob7593
    @iamnoob7593 11 หลายเดือนก่อน

    Superb

  • @lakshmand8413
    @lakshmand8413 2 หลายเดือนก่อน

    Understood

  • @sunilpanchal1498
    @sunilpanchal1498 ปีที่แล้ว

    Understood as always 😃

  • @kiransequeira6152
    @kiransequeira6152 ปีที่แล้ว

    Thank you 🙏

  • @aakashgoswami2356
    @aakashgoswami2356 ปีที่แล้ว

    If someone say you can't do anything in life ,show them 4:25 ,move on and work hard.

  • @vaibhavagarwal1479
    @vaibhavagarwal1479 9 หลายเดือนก่อน

    Can someone help me with this
    why here memset work
    int vis[n][m];
    memset(vis,0, sizeof vis);
    where as
    int vis[n][m] = {0};
    doesn't work for test case 23 aren't both the code serving same purpose?

  • @The_Shubham_Soni
    @The_Shubham_Soni ปีที่แล้ว

    Understood.

  • @-NagumantryRaghuveer
    @-NagumantryRaghuveer ปีที่แล้ว

    Too good
    Understood

  • @-VLaharika
    @-VLaharika ปีที่แล้ว

    Understood👍

  • @LBK3
    @LBK3 ปีที่แล้ว

    Understood ❤

  • @japkiratsingh3728
    @japkiratsingh3728 ปีที่แล้ว

    understood 💖💖💖💖💖

  • @vishalbairagi5364
    @vishalbairagi5364 3 หลายเดือนก่อน

    thanks boss

  • @herculean6748
    @herculean6748 ปีที่แล้ว

    Thanks🙌

  • @ChewyPopcorn69
    @ChewyPopcorn69 4 หลายเดือนก่อน

    understoodd

  • @Aryan-vw7lt
    @Aryan-vw7lt ปีที่แล้ว +1

    understooddddd

  • @original_gangsta_
    @original_gangsta_ ปีที่แล้ว

    Understood 🔥🔥

  • @r_uhi05
    @r_uhi05 6 หลายเดือนก่อน

    understood!

  • @akashsahu2571
    @akashsahu2571 ปีที่แล้ว

    helpful

  • @bhavya8608
    @bhavya8608 ปีที่แล้ว

    understood!!!!

  • @vishnu9134
    @vishnu9134 7 หลายเดือนก่อน

    understood🤩

  • @abhishek__anand__
    @abhishek__anand__ ปีที่แล้ว

    Nice

  • @jessepinkman2231
    @jessepinkman2231 หลายเดือนก่อน

    can we solve this question just like the most stones removed with same row or column approach if anyone knows or have an idea about it pls do reply thanks in advance

  • @mriduljain6809
    @mriduljain6809 ปีที่แล้ว

    Understood!!

  • @girikgarg8
    @girikgarg8 ปีที่แล้ว

    Link for this problem on GFG?

  • @anirbanbera5630
    @anirbanbera5630 ปีที่แล้ว

    understood💯

  • @itsaryanb9316
    @itsaryanb9316 2 ปีที่แล้ว

    Loved the explaination !!

  • @dreamyme543
    @dreamyme543 ปีที่แล้ว

    Understood:)

  • @p38_amankuldeep75
    @p38_amankuldeep75 ปีที่แล้ว

    understood🔥🔥🔥

  • @KratosProton
    @KratosProton ปีที่แล้ว

    great

  • @KapilMaan-vw9sd
    @KapilMaan-vw9sd หลายเดือนก่อน

    understood sir 🩷

  • @mirdulswarup9065
    @mirdulswarup9065 2 ปีที่แล้ว

    How many videos will be there in this graph playlist

    • @wigglesort
      @wigglesort 2 ปีที่แล้ว

      around 57 i think

  • @tessabloomx8660
    @tessabloomx8660 ปีที่แล้ว

    US

  • @gaurigupta5129
    @gaurigupta5129 10 หลายเดือนก่อน

    Striver OP

  • @pratyakshhhhhhhhhhhhhhhhhhhhh
    @pratyakshhhhhhhhhhhhhhhhhhhhh 10 หลายเดือนก่อน

    🎉🎉🎉🎉🎉🎉

  • @Abhishekkumar-im7lb
    @Abhishekkumar-im7lb ปีที่แล้ว

    Why can't we use parent array for finding parent instead of findparent function.
    When we use unionByrank or size ,we are assigning ultimate parents to nodes,then parent array should give us ultimate parent ?

    • @techbucks7339
      @techbucks7339 ปีที่แล้ว +1

      I also had the same doubt but no ! consider this ds where 1 3 5 are connected now there's another ds 6 7 so 7's parent is 6 right? but when we connect this ds with the prior ds 7's parent is still not changed the only change in the parent is of 6 and 7's parent still remains 6 . So consider the possibility that there are tons of nodes. It would fail. I hope this clears up. Please correct me if I'm wrong.

  • @krishnendu-jana
    @krishnendu-jana ปีที่แล้ว

    Nokia Algorithm - "Connecting People(Island) 😶"

  • @prawarmundra4931
    @prawarmundra4931 ปีที่แล้ว

    unionbyrank is not working in this question. if anyone know the reason please reply

    • @ayushsbhatt6145
      @ayushsbhatt6145 ปีที่แล้ว

      Union by rank is just an optimization to the disjoint set algorithm. You must be missing something. Can you post your code down here?

    • @mugambo5505
      @mugambo5505 ปีที่แล้ว

      bro code is working absolutly fine i think u made some mistake in union by rank function. drop your code maybe we can help u.

  • @HarshSharma-jd4cc
    @HarshSharma-jd4cc ปีที่แล้ว

    alternate solution using pairs: ig better to read!
    // User function Template for C++
    class Solution {
    public:
    class DisjointSet{
    public:
    vector rank;
    vector parent;
    DisjointSet(int n , int m ){
    rank = vector(n,vector(m,0));
    parent = vector(n,vector(m,{0,0}));
    for(int i =0 ; i

  • @051-avnee4
    @051-avnee4 ปีที่แล้ว

    US :)) !!!!!!

  • @KaushikSharma-c3q
    @KaushikSharma-c3q 2 หลายเดือนก่อน

    ........................

  • @mrcoder-o3g
    @mrcoder-o3g ปีที่แล้ว

    Can any one help me to figure out what mistake I have done over here
    int fun1(int u,vector&parent){
    if(u==parent[u]){
    return u;
    }
    return parent[u]=fun1(parent[u],parent);
    }
    void union1(int u,int v,vector&rank,vector&parent){
    int parentU=fun1(u,parent);
    int parentV=fun1(v,parent);
    if(parentU==parentV)
    {
    return;
    }
    else if(rank[parentU]==rank[parentV]){
    parent[parentU]=parentV;
    rank[parentV]++;
    }
    else if(rank[parentU]>rank[parentV]){
    parent[parentV]=parentU;
    }
    else
    {
    parent[parentU]=parentV;
    }
    }
    vector numOfIslands(int n, int m, vector &operators) {
    // code here
    int i,j;
    vector parent(n*m,0);
    vector rank(n*m,0);
    vector vis(n,vector(m,0));
    vector ans;
    for(i=1;i

  • @arvindjaiswal8013
    @arvindjaiswal8013 3 หลายเดือนก่อน

    When he drew with the red pen on the grid for the last two queries, what was drawn will make you laugh. 😆 😆
    Comment understood if you really did. 🤪🤪

    • @thaman701
      @thaman701 3 หลายเดือนก่อน

      Ling ban gya tha.😂

    • @thaman701
      @thaman701 3 หลายเดือนก่อน

      Bhai ek baat bata to is qs m hamne row*m + col kia h pr humne numbering to di hi ni h matrix ko to kaise pta lagega konsa cell h krke

    • @arvindjaiswal8013
      @arvindjaiswal8013 3 หลายเดือนก่อน

      @@thaman701 , aapko query me sabhi nodes ke coordinates diye hote hain... Unhi coordinates se aapko cell number nikalna hota hai

    • @thaman701
      @thaman701 3 หลายเดือนก่อน

      @@arvindjaiswal8013 bhai aap graphs ke algorithm ko revise krte ho kya or krte ho to kaise krte ho kitne kitne din m

    • @arvindjaiswal8013
      @arvindjaiswal8013 3 หลายเดือนก่อน

      ​@@thaman701, nhi main abhi seekh hi rha hun... Striver ke videos se kafi kuch seekhne ko mila... Isi ke playlist se DP bhi seekhi... Kunal Kushwaha ke videos se Tree and baki data structures seekhe... Sabhi ko local IDE pe code karke usme proper comments daal daal ke rakha hun... Abhi 2 maheene hue hain sab seekhte hue... Revise krne ka time nhi Mila abhi kyuki abhi system design bhi seekhna hai... Waise mujhe kam time lagega kyuki Mai 8 sal ka experienced hun...

  • @mayankrai1128
    @mayankrai1128 ปีที่แล้ว +1

    I tried solving this ques by a different approach . I tried to use the previous No. Of Islands problem which we did . I stored the value returned from that function after each iteration , I was not able to get the correct ans. Can anyone help??
    Here is my code :
    class Solution {
    private :
    void bfs(int row , int col, vector &vis ,vector &grid ){
    vis[row][col]=1;
    queue q;
    q.push({row,col});
    int m= grid[0].size();
    int n=grid.size();
    while(!q.empty()){
    int row=q.front().first;
    int col=q.front().second;
    q.pop();
    for(int delrow=-1;delrow