Shortest Path Visiting All Nodes | Leetcode 847 | Live coding session 🔥🔥 | BFS + Bit Manipulation
Here is the solution to "Shortest Path Visiting All Nodes" leetcode question. Hope you have a great time going through it.
🔴 Question:leetcode.com/problems/shortes...
🔥Don't click: bit.ly/32XIXbU
🔥Linkedin: bit.ly/3n65udU
🔥Insta: bit.ly/31TV5ur
👉 Solutions
Java github.com/Sunchit/Coding-Dec...
C++
github.com/Sunchit/Coding-Dec...
🔥🔥🔥🔥👇👇👇 For discussion/feedback/humour/doubts/new openings
Feel free to join discord / discord
🔴 Checkout the series: 🔥🔥🔥
👉 Leetcode Interview experiences and tips : • Playlist
👉 Leetcode contests: www.youtube.com?list=PLEI-q7w3s9gRwAEYzYkvVTsYHDi2-ffdL
👉 Leetcode System Design: • System Design
👉 Leetcode LinkedList: • Coding Decoded LinkedL...
👉 Leetcode Stack: • Coding Decoded Stack S...
👉 Leetcode Binary Search: • Coding Decoded BinaryS...
👉 Leetcode Tree: • Coding Decoded Tree Se...
👉 Leetcode Dyanmic Programming: • Coding Decoded Dynamic...
👉 Leetcode Hard: • LeetCode Hard Problems
👉 Leetcode Heap: • Coding Decoded Heap Se...
👉 Building Coding Aptitude: • Building Coding Aptitude
👉 Leetcode Map: • Coding Decoded Map Ser...
👉 Leetcode Two Pointer: • Coding Decoded Two Poi...
👉 Leetcode Backtracking: • Leetcode Backtracking ...
👉 Leetcode Graph Traversal: • Coding Decoded Graph T...
👉 Leetcode Trie: • Coding Decoded Trie Se...
👉 Leetcode Union Find: • Coding Decoded Union F...
👉 Leetcode BFS Graph Problems: • Coding Decoded BFS Ser...
👉 Leetcode Matrix Problems: • Coding Decoded Matrix ...
👉 Leetcode Easy: • Leetcode Easy Problems
👉 Leetcode Bit Manipulation: • Coding Decoded Bit Man...
🔥🔥🔥 Leetcode Monthly Contest Playlist
👉 Leetcode December 2021: www.youtube.com?list=PLEI-q7w3s9gRN3LsF0zArurVSz3HQyKZP
👉 Leetcode November 2021: www.youtube.com?list=PLEI-q7w3s9gT2IeinxSocuxyOKMB4H2zF
👉 Leetcode October 2021: www.youtube.com?list=PLEI-q7w3s9gTNU412xW8kV1xQ8upnrN0a
👉 Leetcode September 2021: www.youtube.com?list=PLEI-q7w3s9gTNU412xW8kV1xQ8upnrN0a
👉 Leetcode August2021: www.youtube.com?list=PLEI-q7w3s9gTEQxR2uR-g8DVhSTb2ZAx1
👉 Leetcode July 2021: www.youtube.com?list=PLEI-q7w3s9gRV3Bn3VO1rtgkA0wNkIm-y
👉 Leetcode June 2021: www.youtube.com?list=PLEI-q7w3s9gRGYr0jtVjqir5_8SpnQ6Og
👉 Leetcode May 2021: www.youtube.com?list=PLEI-q7w3s9gS8UNo22UA4O3_YjnQczyNp
👉 Leetcode April 2021: www.youtube.com?list=PLEI-q7w3s9gStjIegCW3i84AI9L2KjGhJ
👉 Leetcode March 2021: www.youtube.com?list=PLEI-q7w3s9gTbYRnbU7Np0_-v_GwxYGOb
👉 Leetcode Feb 2021: www.youtube.com?list=PLEI-q7w3s9gRNUjYwtb53A7SXSCQaJguT
👉 Leetcode Jan 2021: www.youtube.com?list=PLEI-q7w3s9gR8EhTsObcgM74NnIiAcRWm
👉 Leetcode December 2020: www.youtube.com?list=PLEI-q7w3s9gQIB_urBmMV04f_NBelXJEP
PS : Please increase the speed to 1.25X
Пікірлер: 78
Felt easy problem initially till realized about usual visited can’t be used. Thanks for great explanation.
Bro, very nice explanation. Keep up this discipline and consistency
Thanks for such simple explanation. Keep up the good work:)
love you! with your video, i understand the answer in just 10 minutes. It is like a magic!
@CodeWithSunchitDudeja
2 жыл бұрын
Wonderful!
Thank you so much , was struggling with this question since morning !!
@CodeWithSunchitDudeja
2 жыл бұрын
Happy to help, do watch out the graph series if u are interested in more such questions
Thanks for the solution!
Idk why people teaching in other videos feel the need to teach bit manipulation from scratch on every step and waste the time. If someone is doing this problem he should already be assumed to be knowing bit manipulation. Finally this video gives the good talk. Thanks.
best explanation availabel on internet for this problem.
Nicely Explained
Very good explanation brother. Thanks for this
Crisp short to the point solution.Just Loved it!
Great Explanation!
very nice explnation
Nice explanation
Crystal clear explaination : )
Another outstanding explanation. Thanks a lot :).
@CodeWithSunchitDudeja
2 жыл бұрын
Bimurta my frds used to call me sanchi in college .... Haha
@bimurtabismoysanchi4029
2 жыл бұрын
Good to hear 🙂
Epic Explanation
Awesome Bhai. I Appreciate.
When I feel sleeplessness, I watch his videos :) Thanks man for that
Great Explanation
Well Explained bro, need to dry run the solution to understand it fully
Thanks..🙌
just wow. im in love with bitmasks now.... great explaination.......tooo....
@CodeWithSunchitDudeja
2 жыл бұрын
Thanks 👍
Best explanation ⚡
thanks a lot
Finallyyyy Understood!!!
@CodeWithSunchitDudeja
2 жыл бұрын
Great
Thanks to your videos, I managed to get a position at Pinterest! Bro you are the fking best
@CodeWithSunchitDudeja
2 жыл бұрын
Heartiest congratulations @Y C, let us connect on linkedin www.linkedin.com/in/coding-decoded-6809a3215/
Thanks!
@CodeWithSunchitDudeja
Жыл бұрын
Thanks Gaurav
Most youtuber explained this question like "Ratta". Thanks for this amazing video and explained very well.
Great explanation!
@CodeWithSunchitDudeja
Жыл бұрын
Glad it was helpful!
can you explain how this solves the problem of choosing the shortest path?,, like how does adding ebverything to the queue initiallly and doing for loop till size of queue enssure we get the shortest path among all the paths thanks
@user-fx5lk6dy3m
10 ай бұрын
bfs does the magic here.first we move at distance 1 from each starting node ,then we move at distance 2 from each starting node,in this way the first time we have covered all nodes from a starting node it will be the shortest distance.
Suppose we would have had 3 also attached to 0. Then we would go to 2 and come back to 0, and there then we would again go back to 1 as, that state would not have visited before?
You are simply amazing 😁
@CodeWithSunchitDudeja
2 жыл бұрын
you too bro
Thanks bro
@CodeWithSunchitDudeja
Жыл бұрын
Welcome
You are amazing!
@CodeWithSunchitDudeja
2 жыл бұрын
You too!!
You said we need to apply this starting from every node right? but we are applying it for only one node to start with. Didn't get it
ver nice explanation
@CodeWithSunchitDudeja
Жыл бұрын
Thanks for liking
Could you please explain why you have used loop inside while loop ,?
Well explained man!
@CodeWithSunchitDudeja
2 жыл бұрын
Thanks Umesh, watch out the entire series you will simply love it
@umeshhbhat
2 жыл бұрын
@@CodeWithSunchitDudeja Ik bro, have been seeing your videos from quite a long time. Always understood the solution :) One feedback though, try adding your own face in the video to feel more like as if some human is teaching us! Something like pep coding.
@CodeWithSunchitDudeja
2 жыл бұрын
@@umeshhbhat thanks for the tip man.. will come over video very soon
Time complexity of this approach?
Nice one to know 👌👏 is it travelling salesman problem with unweighted edges? Also how can we calculate time complexity?
@CodeWithSunchitDudeja
2 жыл бұрын
Yes it is similar to that
@mandy1339
2 жыл бұрын
Yea it's a variation of travelling salesman.
Sorry if I missed something in the video.. if we have to do independent BFS from each node, why isn't shortest path variable reset to zero? I see you added all vertices to queue in the beginning only.. Why does it not cause any problem?
@sahilattri51
Жыл бұрын
My bad, understood the concept now. Thanks for the video.
how can we know where to use bit manipulation . i mean intuition to use bit??
@bloody9162
2 жыл бұрын
Bit Vector can be generally used in place of arrays when the problem needs the state which can vary from 0 to 2^n-1, n being the length of the array which is nodes in this case. Helps with saving space, CPU operations are also faster on bits.
SOlid explanatory video
is this question might come in interview as the level is question is little bit high?
@CodeWithSunchitDudeja
2 жыл бұрын
As an interviewer, who have interviewed more than 100 candidates I will never ask this question. In case it comes ask the interviewer can you solve it haha :)...PS : Google is an exception to this list
The BFS Solution is a lot more straightforward than the DFS one.
isnt just knowing about the parent node enough to avoid indefinite loops
Hey is Leetcode hard necessary for interviews and online coding round?
@MrVrtex
10 ай бұрын
For FAANG/MAANG yes
a suggestion : u can use wacom tablet with pen to write instead of mouse. That will improve your efficiency. you can check mine videos.
@CodeWithSunchitDudeja
2 жыл бұрын
Thanks thinkcode for the advice
prettiest 😢
C++ code, if anyone needs it class Solution { public: /* a type of multisource bfs with each node as source ->BFS traversal from all nodes ->Basic BFS while maintaining visited arr will not work because we will be revisiting a node if necessary ->we need to avoid infinite loop, for this we will maintain the visited state of nodes(in bitmask) with current visiting node in a queue. In example 1, final bitmask will look like 1111 */ int shortestPathLength(vector& graph) { int n = graph.size(); if(n==1) return 0; int final_bitState = (1
@HarshaKeVlogs
11 ай бұрын
thank you
@HarshaKeVlogs
11 ай бұрын
useful
arre bhai jisne yeh ques pehle nahi dekha woh kaise solve karega interview main. total chutiyapa chal raha hain