Divide Nodes Into the Maximum Number of Groups | Super Detailed | Leetcode 2493 | codestorywithMIK

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.พ. 2025

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

  • @gui-codes
    @gui-codes 8 วันที่ผ่านมา +11

    Ohhho, aaj fir ek Hard problem Halwa banega . Legend MIK.
    Thanks a lot for putting details ❣❣❣

  • @universalcosmologist3675
    @universalcosmologist3675 8 วันที่ผ่านมา +7

    bro maine first try me kar liya using bfs for each node in a component and summing up max groups for each component and used dfs for component sepration and stored elements in map according to their component thnx ❣❣❤

  • @sakshamsharma4713
    @sakshamsharma4713 8 วันที่ผ่านมา +26

    Legend Mik's video >>>>>>>>>> Netflix, Prime

  • @madmaxgaming5864
    @madmaxgaming5864 8 วันที่ผ่านมา +3

    Maine bhi similar soncha tha, lekin thode testcases pass nahi ho rahe the, aapka video dekhne k baad maine chote mote tweaks kiye and mera code bhi chal gaya. Thanks bhai

  • @sushantpritmani1248
    @sushantpritmani1248 4 วันที่ผ่านมา +1

    Levels sabke nikelenge! THe OG is here.!!

  • @vikrantverma2196
    @vikrantverma2196 8 วันที่ผ่านมา +2

    all cleared...smooth explanation mik👏

  • @kashvijain4847
    @kashvijain4847 8 วันที่ผ่านมา +12

    at 32:35, it is possible to make 4 groups.
    group 1 : 1
    group 2 : 0
    group 3 : 2
    group 4 : 3,4

    • @codestorywithMIK
      @codestorywithMIK  8 วันที่ผ่านมา +10

      Ah yes yes. My bad for the silly but later in the video, i have covered the fix ❤❤❤
      And hence that’s the proof that we will have to try BFS from each node. Only trying BFS from 0 will not give best result. ❤❤
      Thanks 😇🙏

    • @shreyxnsh.14
      @shreyxnsh.14 8 วันที่ผ่านมา

      your comment is also slightly incorrect, group 3 mein 2 hoga

  • @gui-codes
    @gui-codes 8 วันที่ผ่านมา +4

    Mai bipartitte bhool gaya tha. Aaj revision kiya ache se.
    aapka graph concepts fir se revise karunga is weekend.

    • @rode_atharva
      @rode_atharva 8 วันที่ผ่านมา +1

      Same bhai😂

  • @PUSHP_GUPTAKUCP1025
    @PUSHP_GUPTAKUCP1025 8 วันที่ผ่านมา +3

    Sir, I want you to put daily questions like this continuously, this helps a lot to me. Thank you for these detailed explanations.

  • @aws_handles
    @aws_handles 8 วันที่ผ่านมา +1

    Legend ho aap 🙏🏻🙏🏻🙏🏻

  • @rahulbansal5082
    @rahulbansal5082 8 วันที่ผ่านมา +3

    explanation is amazing

  • @shauryatomer1058
    @shauryatomer1058 8 วันที่ผ่านมา +1

    thanks for this great video, I stuck on how to know which node to start BFS from in order to find maximum grouping didn't know brute force was the way lol. Grouped nodes together using Disjoint Set, then took maximum from each group

  • @monke8332
    @monke8332 8 วันที่ผ่านมา +1

    correct me if I am wrong but at 57:48 shouldn't the TC be O(V+E) as each node would be visited once same for 58:17

  • @aizad786iqbal
    @aizad786iqbal 8 วันที่ผ่านมา +5

    the quote you shared today is from Sir Alex Ferguson , former coach of Manchester United Football club

    • @codestorywithMIK
      @codestorywithMIK  8 วันที่ผ่านมา +3

      Thank you for sharing! 🙏❤️

    • @raftywate
      @raftywate 8 วันที่ผ่านมา +1

      @@codestorywithMIK And consistency beats hard work if the hard work isn't consistent.

  • @kumarbishwajeet5269
    @kumarbishwajeet5269 8 วันที่ผ่านมา +2

    Hi Mik I am using diameter of graph concept but it's giving error. Can we use this concept for finding maximum distance for each group rather than finding distance from each node

  • @Star_Sciene
    @Star_Sciene 9 ชั่วโมงที่ผ่านมา +1

    Everything was looking formal until Sir said khandani tareeka....Sir your way of teaching is amazing

    • @codestorywithMIK
      @codestorywithMIK  9 ชั่วโมงที่ผ่านมา

      Haha yes, "khandani tareeka" 😅🙏
      Thank you ❤️❤️🙏🙏

  • @amankamani9349
    @amankamani9349 8 วันที่ผ่านมา +4

    Awesome video. But having a doubt, after bipartite validation, to find the total number of groups can't we use the diameter of graph concept for each connected components and summing up them, should result in the answer. I have tried that code, but 51/55 testcases passed. Not sure, why this will not work?

    • @gui-codes
      @gui-codes 8 วันที่ผ่านมา +2

      sounds like a good idea can you share your code i want to see what is failing.

    • @amankamani9349
      @amankamani9349 8 วันที่ผ่านมา +1

      @@gui-codes Not able to put link because of youtube restriction.
      class Solution {
      public int magnificentSets(int n, int[][] edges) {
      Map graph = buildGraph(edges);
      int[] coloring = new int[n];
      boolean[] visited = new boolean[n];
      // not possible scenario
      for(int i = 0; i < n; i++) {
      if (!visited[i] && !isBiPartite(graph, i, coloring, visited)) {
      return -1;
      }
      }
      visited = new boolean[n];
      int totalGroup = 0;
      for(int i = 0; i < n; i++) {
      if (!visited[i]) {
      totalGroup += findDiameterOfGraph(graph, i, n, visited);
      }
      }
      return totalGroup;
      }
      private Map buildGraph(int[][] edges) {
      Map map = new HashMap();
      for(int[] edge: edges) {
      int u = edge[0] - 1;
      int v = edge[1] - 1;
      map.putIfAbsent(u, new ArrayList());
      map.get(u).add(v);
      map.putIfAbsent(v, new ArrayList());
      map.get(v).add(u);
      }
      return map;
      }
      private boolean isBiPartite(Map graph, int start, int[] coloring, boolean[] visited) {
      Queue q = new LinkedList();
      q.add(start);
      visited[start] = true;
      coloring[start] = 0;
      while(!q.isEmpty()) {
      int node = q.poll();
      for(int neigh: graph.getOrDefault(node, new ArrayList())) {
      if (!visited[neigh]) {
      coloring[neigh] = 1 - coloring[node]; // 0 or 1
      visited[neigh] = true;
      q.add(neigh);
      } else if (coloring[neigh] != 1 - coloring[node]) {
      return false;
      }
      }
      }
      return true;
      }
      private int findDiameterOfGraph(Map graph, int start, int n, boolean[] visited) {
      int[] pair = traverseTillLast(graph, start, visited);
      int oneEndNode = pair[0];
      pair = traverseTillLast(graph, oneEndNode, new boolean[n]);

      return pair[1]; // final distance from oneEndNode to last node
      }
      private int[] traverseTillLast(Map graph, int start, boolean[] visited) {
      Queue q = new LinkedList();
      q.add(start);
      int level = 0;
      visited[start] = true;
      int lastNode = start;
      while(!q.isEmpty()) {
      int n = q.size();

      while(n-- > 0) {
      int node = q.poll();
      lastNode = node;
      for(int neigh: graph.getOrDefault(node, new ArrayList())) {
      if (visited[neigh]) continue;
      visited[neigh] = true;
      q.add(neigh);
      }
      }
      level++;
      }
      return new int[]{lastNode, level};
      }
      }

    • @tanmaypal2003
      @tanmaypal2003 8 วันที่ผ่านมา +3

      Same😢😢, I don't know why the diameter code is not working. When we find diameter from another graph, we always get farthest node in first bfs and get diameter in second bfs, But in this 52nd test case, we are not able to find the farthest node which is 11 and 17. That is why the diameter is also coming out wrong. I tried the whole day but I am not able to understand why this is happening😢😢

    • @kumarbishwajeet5269
      @kumarbishwajeet5269 8 วันที่ผ่านมา +2

      Does anyone found why it didn't work. since morning I am not able to figure why it failed

    • @amankamani9349
      @amankamani9349 7 วันที่ผ่านมา

      @@tanmaypal2003 true

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 8 วันที่ผ่านมา +5

    Fist view 🎉❤

  • @kumarbishwajeet5269
    @kumarbishwajeet5269 8 วันที่ผ่านมา +2

    Hi MIK just one doubt. For finding maximum level in group can't we find the end node of each group(and find the level using bfs) rather than doing bfs/dfs for all node and then sum of all distance for each group.

    • @сойка-и8й
      @сойка-и8й 6 วันที่ผ่านมา +1

      It's important to find bfs of every node bcoz if you observe the answer is highest when you start from the node having the least no of children or connection

  • @naveentmyug
    @naveentmyug 8 วันที่ผ่านมา +1

    in example 1, why dont we put 5 in group 3 with 2 and 4 nodes?

    • @gui-codes
      @gui-codes 8 วันที่ผ่านมา +1

      example 1 me 5 group-3 me hi gaya tha with 2 and 4. but it will reduce the number of groups to 3. we can increase the group number, if we were able to put 5 in a separate group without breaking any rule, we did that and hence groups count increased to 4

    • @naveentmyug
      @naveentmyug 8 วันที่ผ่านมา +1

      @@gui-codes ok, got it. rules+ maximising factor need to be keep in hand.

  • @Amit_kumar03
    @Amit_kumar03 8 วันที่ผ่านมา +3

    Hey mik 👋🏻 , when will you upload 2D dp concepts video in your dp concept and question playlist....

    • @codestorywithMIK
      @codestorywithMIK  8 วันที่ผ่านมา +1

      Soon uploading more ❤❤❤

  • @Afif-d7n
    @Afif-d7n 8 วันที่ผ่านมา +1

    Vai please make a vdo on this 2 leetcode problems please
    Serialize And Deserialize Binary Tree and
    Design Twitter

  • @SagarAgrawal-vu2vd
    @SagarAgrawal-vu2vd 8 วันที่ผ่านมา

    what if a graph contains a cycle with even no of nodes?, it seems to be bipartite but will we be able to break it into groups?

    • @сойка-и8й
      @сойка-и8й 6 วันที่ผ่านมา

      It always will be bipartite

  • @darkbgm4582
    @darkbgm4582 8 วันที่ผ่านมา

    bhaiya please upload videos on weekly contest as well

  • @RishabhChatterjee-fg2gz
    @RishabhChatterjee-fg2gz 8 วันที่ผ่านมา +1

    Bhaiya bipartite word hi college mein kabhi nhi bola, to bipartite graph kyese parayenge

    • @codestorywithMIK
      @codestorywithMIK  8 วันที่ผ่านมา

      Unfortunately college professors skip such important topics. I remember i was taught Bipartite topic in Graph Theory subject.
      But no worries, I hope my Bipartite video will help you 🙏❤️ see the link in the video description

  • @shreyxnsh.14
    @shreyxnsh.14 8 วันที่ผ่านมา +1

    bro add some outro to your video, i was tryna pause it at the end, and it restarted

    • @codestorywithMIK
      @codestorywithMIK  7 วันที่ผ่านมา

      You are absolutely right! 🙏❤️ I'll add an outro to my videos in the future!

    • @shreyxnsh.14
      @shreyxnsh.14 7 วันที่ผ่านมา

      @@codestorywithMIK yeah thanks for considering

  • @sandeepn7378
    @sandeepn7378 8 วันที่ผ่านมา +3

    ❤🦸Superhero🦸of dsa coders❤

    • @codestorywithMIK
      @codestorywithMIK  8 วันที่ผ่านมา

      Means a lot Sandeep ❤️❤️🙏🙏

  • @ComputerCentre-rn2yy
    @ComputerCentre-rn2yy 8 วันที่ผ่านมา +1

    10:00

  • @DevanshGupta-io7rl
    @DevanshGupta-io7rl 8 วันที่ผ่านมา

    pichle 2 ghante se preshaan tha mai ek '&' k wajah se

  • @Abhi_008
    @Abhi_008 8 วันที่ผ่านมา

    ye dsu se bnaya jaa sakta hai ?

  • @MohitGupta-ml4st
    @MohitGupta-ml4st 8 วันที่ผ่านมา +7

    Bilkul bhi logic khud se nhi bana 😔...i need more practice in these types of questions

    • @aws_handles
      @aws_handles 8 วันที่ผ่านมา

      Same bhai. But koi nai, practice karenge aur 🔥🔥

  • @DSL-e7q
    @DSL-e7q 8 วันที่ผ่านมา +1

    Not a single video where the low battery notification is not there😅😂

  • @nandaniverma1473
    @nandaniverma1473 8 วันที่ผ่านมา +1

    sir when u reveal a face😁

  • @Aryan-cy7cu
    @Aryan-cy7cu 8 วันที่ผ่านมา +1

    I am not enough , I need to break my comfort zone ....

  • @DeepakSingh-fd2ix
    @DeepakSingh-fd2ix 8 วันที่ผ่านมา +1

    level sbke nikelenge

  • @bhupendrakalal1727
    @bhupendrakalal1727 8 วันที่ผ่านมา +1

    mik sir bipartite click hi nhi hua , feeling demotivated

    • @codestorywithMIK
      @codestorywithMIK  8 วันที่ผ่านมา +2

      Hi Bhupendra,
      Please refer to Bipartite video -
      th-cam.com/video/NeU-C1PTWB8/w-d-xo.htmlsi=e848CsDdohc5VWEe
      I am sure it will help ❤️🙏

  • @dreamy4174
    @dreamy4174 8 วันที่ผ่านมา +1

    Mujhe apka tution join karna hai 😢

  • @DeepakKumar-e2s5q
    @DeepakKumar-e2s5q 7 วันที่ผ่านมา

    Use whiteboard....Please

  • @AmitDaily_Vlogs
    @AmitDaily_Vlogs 8 วันที่ผ่านมา

    bro apna avtar change krega kya

  • @pinakvyas1779
    @pinakvyas1779 7 วันที่ผ่านมา

    how did u get intution of multi node bfs

    • @codestorywithMIK
      @codestorywithMIK  7 วันที่ผ่านมา

      I actually got a wrong answer as I mentioned in the video. Then I realised that doing BFS from only one node will not guarantee best solution. So we have to try with every node. I felt bad to have missed this case when I was solving it but it was a good learning experience ❤️

  • @yatri6329
    @yatri6329 8 วันที่ผ่านมา +4

    Google me aise Q aa skte h kya ? For experience candidate.?

    • @DeadCode_Debugs
      @DeadCode_Debugs 8 วันที่ผ่านมา +4

      haa

    • @gui-codes
      @gui-codes 8 วันที่ผ่านมา +6

      I think is tarah k to definitely aa sakte hain. Video description me dekho, jo jo concepts use hue hain all are simple, but un sab ko combine karke ek Qn bana diya gaya hai islie hard lagta hai. Google usually yahi check karta hai ki aapko foundation kitne strong hai. For example is problem me dekho, bipartite check, bfs and dfs hi likha hai bas zyada se zyada bas starting intuition lagane ki deri hai.

    • @codestorywithMIK
      @codestorywithMIK  8 วันที่ผ่านมา

      Yes ❤

    • @yatri6329
      @yatri6329 8 วันที่ผ่านมา +2

      @@gui-codes wahi to ni ho rha h starting ka thinking ...but yes bipartite sun k I got some idea, going to implement, BFS implementation

    • @DeadCode_Debugs
      @DeadCode_Debugs 8 วันที่ผ่านมา

      @ just focus on intuition and approach ,,, har ek person ka usko code mai convert krne ka tarika alag hota hai,, jab bhi intuition and logic clear haii tab coding part dekho ignore kro aur khud se hi code likhna start kro ,,,then compare ur code with mik's code mai har bar aaisa hi krta hu

  • @darkbgm4582
    @darkbgm4582 8 วันที่ผ่านมา +1

    bhaiya please upload videos on weekly contest as well

  • @darkbgm4582
    @darkbgm4582 8 วันที่ผ่านมา

    bhaiya please upload videos on weekly contest as well