Clone Graph | Leetcode - 133 | Google, Facebook, Amazon | DFS+BFS | Explanation ➕ Live Coding🧑🏻‍💻

  Рет қаралды 9,174

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 69
@anuppatankar4294
@anuppatankar4294 Жыл бұрын
Graph was very tough for me but because of your graph concepts playlist today I can solve most of the graph questions easily. Thanks a Lot 😊
@codestorywithMIK
@codestorywithMIK Жыл бұрын
You made my day ❤️❤️❤️
@shivanshchauhan52
@shivanshchauhan52 Жыл бұрын
@@codestorywithMIK Keep going this channel future is very bright !!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Shivansh ❤️❤️❤️
@kumkumslab5811
@kumkumslab5811 9 ай бұрын
prooved @@shivanshchauhan52
@codestorywithMIK
@codestorywithMIK Жыл бұрын
The most detailed and deep dived explanation to this problem you will ever find. Hope you will like it. Please find all the code below : //Approach-1 (DFS) Using vector as map //Approach-2 (DFS) (Using unordered_map) //Approach-3 (BFS) Using vector as map //Approach-4 (BFS) (Using unordered_map) Link : github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
The best one bro. I am speechless
@oqant0424
@oqant0424 Жыл бұрын
Yaar bhaiya kya padhaya hai aapne ...........dil khush kar diya
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 🙏❤️😇
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Outstanding bhai! You prove every time why you are the undisputed graph king 👑💙💙
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much JJ
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
speechless with this masterpiece
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
16:59 ---> That's one of the reasons I come to your channel. I am always sure that I will understand more from your explanations.
@nk-fs8bj
@nk-fs8bj 9 ай бұрын
Awesome after listing story of 20 minutes write code by own🥳
@r.beditz3674
@r.beditz3674 Жыл бұрын
Never regret watching your explanation videos.❤ Thanks again sir. Here are the Java implementations of your BFS and DFS approaches -: BFS CODE : class Solution { Map map; public Node cloneGraph(Node node) { map = new HashMap(); if(node == null) return null; Node clone = new Node(node.val); map.put(node, clone); Queue q = new ArrayDeque(); q.add(node); while(!q.isEmpty()){ Node curr = q.poll(); Node currClone = map.get(curr); for(Node n : curr.neighbors){ if(!map.containsKey(n)){ Node c = new Node(n.val); map.put(n, c); currClone.neighbors.add(c); q.add(n); } else currClone.neighbors.add(map.get(n)); } } return clone; } } ---------------------------------------------------------------------------------------------------- DFS CODE : class Solution { Map map; public Node cloneGraph(Node node) { map = new HashMap(); if(node == null) return null; Node clone = new Node(node.val); map.put(node, clone); dfs(node, clone); return clone; } private void dfs(Node node, Node clone){ for(Node n : node.neighbors){ if(!map.containsKey(n)){ Node curr = new Node(n.val); map.put(n, curr); clone.neighbors.add(curr); dfs(n, curr); } else clone.neighbors.add(map.get(n)); } } }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so so much Means a lot to me ❤️
@HarshSharma-zz9di
@HarshSharma-zz9di Жыл бұрын
Was able to do today's question without any help, but still here just to like the video. Amazing channel and well structured videos!
@k-CE-OmkarPathak
@k-CE-OmkarPathak 11 ай бұрын
Awesome !! Worth watching 41mins
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you so much for watching 😇❤️🙏
@gauravbanerjee2898
@gauravbanerjee2898 9 ай бұрын
Solved today's GFG POTD after watching this video thank you so much bhaiya ❤❤
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
Awesome ❤️❤️
@ShivamMishra-fo1wx
@ShivamMishra-fo1wx 9 ай бұрын
Great Explanation Makes the question very easy to understand and not only graph u make easy dp also
@iamnoob7593
@iamnoob7593 19 күн бұрын
Thank you , Amazing explanation.
@level_up.1908
@level_up.1908 Жыл бұрын
bhai kyu padhate hon itna badhiya 😍
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot ❤️😇
@fshimcds
@fshimcds 3 ай бұрын
ThankU for best Explanation
@shivkumar-og4ow
@shivkumar-og4ow Жыл бұрын
thanks a lot, bhaiya, nice explanations, are easy to understand.
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
The K.I.N.G of Graphs
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
so true!
@rahularyan9808
@rahularyan9808 10 ай бұрын
Really great explanation
@udayshankar-e6v
@udayshankar-e6v 8 ай бұрын
Ye channel bahut aage jane vale h.. bahut bawal samjhata h ye bhai 😍bhai bs bata do kon sa aap use karte ho drawing ke liye .. main bhi install kar lunga..interview me bada proplem hota h diagram vagairh bana ke show karne me
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Hi there I use default notes app ❤️🙏 Thank you ☺️
@tutuimam3381
@tutuimam3381 Жыл бұрын
Thanks a lot
@souvickmaity4867
@souvickmaity4867 Жыл бұрын
Shi Haai bhai
@gui-codes
@gui-codes 6 ай бұрын
A message to new comers to this channel - "Graph is Hard until you watch codestorywithMIK"
@aryanvashishtha0001
@aryanvashishtha0001 Жыл бұрын
i love you bro
@vishalgautam-f6v
@vishalgautam-f6v Жыл бұрын
bro u are great at explaining things but u should change ur channel name to a short and simple name, so that we can also recommend ur channel to others as ur current name is slightly difficult to remember
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I really appreciate your suggestion ❤️❤️❤️ I will plan to change it to CSM (Code Story With MIK). Right now, my Instagram, linkedIn and , Facebook all have this name and hence might take time to migrate. Thank you so much for your precious suggestions and watching my videos ❤️❤️❤️
@ankitraj4179
@ankitraj4179 3 ай бұрын
Java Solution /* // Definition for a Node. class Node { public int val; public List neighbors; public Node() { val = 0; neighbors = new ArrayList(); } public Node(int _val) { val = _val; neighbors = new ArrayList(); } public Node(int _val, ArrayList _neighbors) { val = _val; neighbors = _neighbors; } } */ class Solution { public Node cloneGraph(Node node) { if(node == null) return node ; Node clone = new Node(node.val) ; HashMap map = new HashMap() ; map.put(node,clone) ; DFS(node,clone,map) ; return clone ; } public static void DFS(Node org , Node clone , HashMap map){ for(Node neigh : org.neighbors){ if(!map.containsKey(neigh)){ Node tempNode = new Node(neigh.val) ; clone.neighbors.add(tempNode) ; map.put(neigh,tempNode) ; DFS(neigh,tempNode,map) ; } else{ clone.neighbors.add(map.get(neigh)); } } } }
@ankitraj4179
@ankitraj4179 3 ай бұрын
do like if it helped.
@YashSinghal
@YashSinghal Жыл бұрын
github link in the description for this problem has a typo its a space instead of underscore so %20 instead of _
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Yash. Corrected now. github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp
@jyotishmoydeka6804
@jyotishmoydeka6804 Жыл бұрын
Bhaiya it's showing your github page is not available.. and btw thank you for wonderfull explainantion..
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sorry for the typo. Link corrected. github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp
@abhiverma5135
@abhiverma5135 Жыл бұрын
why you wrote map.clear() at the beginning of the function ? whereas we hadn't put any value in the map we just declared it globally? BTW great content ❤✌✌
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi abhi, Thanks a lot for your appreciation. Actually multiple calls for the function is made. So we don’t want stale entries in the map to be there when a new call is made to the function. But most of the time , even if you don’t clear it, the solution works. But it’s a good practice to do that.
@bhartendupant8859
@bhartendupant8859 9 ай бұрын
@satvrii
@satvrii Жыл бұрын
❤❤❤❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️🙏 Means a lot 😇
@badamadevi9497
@badamadevi9497 Жыл бұрын
Sir java ka code bhi explain krr
@riyakansal3312
@riyakansal3312 Жыл бұрын
Error is coming while opening ur github:(
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Link corrected. Can you try again please github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp
@IshanGuleria-l6b
@IshanGuleria-l6b 5 ай бұрын
iska iterative dfs ka solution h kisi k pass to please send
@vinitsinghengineer
@vinitsinghengineer 5 ай бұрын
unordered_map mp; void dfs(Node* node){ Node* temp = new Node(node->val); mp[node] = temp; for(auto it : node->neighbors){ if(mp.find(it) != mp.end()) temp->neighbors.push_back(mp[it]); else { dfs(it); temp->neighbors.push_back(mp[it]); } } } Node* cloneGraph(Node* node) { if(!node) return NULL; dfs(node); return mp[node]; }
@akshayzade9761
@akshayzade9761 Жыл бұрын
Thanks Mike for efforts :) Please find below Java Code - class Solution { Map map = new HashMap(); public Node cloneGraph(Node node) { if(node == null){ return null; } Node clone_node = new Node(node.val); map.put(node, clone_node); dfs_traversal(node, clone_node); return clone_node; } void dfs_traversal(Node node, Node clone_node){ for(Node n : node.neighbors){ if(!map.containsKey(n)){ Node clone = new Node(n.val); map.put(n, clone); clone_node.neighbors.add(clone); dfs_traversal(n, clone); } else{ clone_node.neighbors.add(map.get(n)); } } } }
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Why is my comment getting removed?😮
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
tried several times to paste the Java code but it is getting removed every time automatically 😥😥
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi JJ Can you try again. I am also not sure why this is happening
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Was waiting for your java code bro
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
@@codestorywithMIK @asimzohr2293 added, hoping it does not get removed
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
@@codestorywithMIK it got removed again it seems 😐
@kartikkk4583
@kartikkk4583 Жыл бұрын
Hogya bhai ye, recursion + map, time laga thoda dimag lagane me, @mik bhai aapne competetive programming ki hai kya : unordered_map m; Node* cloneGraph(Node* node) { if(!node)return node; Node* root= new Node(node->val); vector v; m[node]=root; for( auto x : node->neighbors){ if(m.find(x)==m.end()){ v.push_back(cloneGraph(x)); } else{ v.push_back(m[x]); } } root->neighbors=v; return root; }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi Kartik, No, i have not done much in Competitive Coding. But would love to try
@abcd76820
@abcd76820 9 ай бұрын
class Solution{ Node cloneGraph(Node node){ Node temp = new Node(node.val); //ab clone krna h recursively iske neighbours ko // saath me ek map bna lete hain taaki koi node bn gya h // to dubara nahi bnega //map ek purana node usk corresponding new node jo //hmne bnaya Map map = new HashMap(); map.put(node,temp); DFS(node,temp,map); return temp; } void DFS(Node node,Node temp,Map map) { //original node k neighbours dhund kr clone krte jaao for(Node neigh : node.neighbors) { // ab do case ya to clone hua hoga ya to nai if(!map.containsKey(neigh)) { Node cloneNeigh = new Node(neigh.val); map.put(neigh,cloneNeigh); temp.neighbors.add(cloneNeigh); DFS(neigh,cloneNeigh,map); } else { temp.neighbors.add(map.get(neigh)); } } } }
@sarthakgudwani4608
@sarthakgudwani4608 Жыл бұрын
Java Implementation class Solution { HashMap mp = new HashMap(); void DFS(Node node, Node clone_node) { for (Node n : node.neighbors) { if (!mp.containsKey(n)) { Node clone = new Node(n.val); mp.put(n, clone); clone_node.neighbors.add(clone); DFS(n, clone); } else { clone_node.neighbors.add(mp.get(n)); } } } Node cloneGraph(Node node) { if (node == null) { return null; } mp.clear(); Node clone_node = new Node(node.val); mp.put(node, clone_node); DFS(node, clone_node); return clone_node; } }
@dayashankarlakhotia4943
@dayashankarlakhotia4943 9 ай бұрын
class Solution { Node cloneGraph(Node node){ Node st=node; HashSetvis=new HashSet(); HashMapmpp=new HashMap(); dfs(st,vis,mpp); addEdges(mpp); return mpp.get(st); } public static void addEdges(HashMapmpp){ for(Map.Entery e:mpp.entrySet()){ Node u=(Node)e.getKey(); mpp.get(e.getKey()).neighbors.add(u); } } public void dfs(Node st,HashSetvis,HashMapmpp){ vis.add(st); Node copyNode=new Node(st.val); mpp.put(st,copyNode); for(Node v:st.neighbors){ if(!vis.contains(v)) dfs(v,vis,mpp); } } } 🎉❤
LRU Cache | Brute Force | Optimal | Detailed | Leetcode 146
40:32
codestorywithMIK
Рет қаралды 18 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 30 МЛН
Graph Part 2
28:51
ZeCode Community
Рет қаралды 204
Sum of Distances in Tree | Google | Leetcode 834 | codestorywithMIK
44:46
C Programming Tutorial for Beginners
3:46:13
freeCodeCamp.org
Рет қаралды 14 МЛН
Intro to Java Programming - Course for Absolute Beginners
3:48:25
freeCodeCamp.org
Рет қаралды 3,5 МЛН