Shortest Path Visiting All Nodes | Leetcode 847 | Live coding session 🔥🔥 | BFS + Bit Manipulation

Sdílet
Vložit
  • čas přidán 24. 02. 2022
  • 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

Komentáře • 79

  • @ayushjindal4981
    @ayushjindal4981 Před 3 dny +1

    Nice explanation !!

  • @sidhantsharma5984
    @sidhantsharma5984 Před 2 lety +12

    Bro, very nice explanation. Keep up this discipline and consistency

  • @Ganeshkanagavel
    @Ganeshkanagavel Před 10 dny

    Nicely Explained

  • @rishabhrawat7943
    @rishabhrawat7943 Před 2 lety +3

    Thanks for such simple explanation. Keep up the good work:)

  • @aaravarya4406
    @aaravarya4406 Před rokem +2

    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.

  • @theghostwhowalk
    @theghostwhowalk Před 2 lety +4

    Felt easy problem initially till realized about usual visited can’t be used. Thanks for great explanation.

  • @divanshuagarwal8236
    @divanshuagarwal8236 Před 2 lety +1

    Thanks for the solution!

  • @varunsaraf8018
    @varunsaraf8018 Před 2 lety +1

    Great Explanation!

  • @noobbot7392
    @noobbot7392 Před 2 lety +1

    Very good explanation brother. Thanks for this

  • @hoyinli7462
    @hoyinli7462 Před 2 lety +3

    love you!
    with your video, i understand the answer in just 10 minutes. It is like a magic!

  • @sayanchandra9479
    @sayanchandra9479 Před 2 lety +3

    Thank you so much , was struggling with this question since morning !!

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  Před 2 lety

      Happy to help, do watch out the graph series if u are interested in more such questions

  • @anuragchakraborty7607
    @anuragchakraborty7607 Před 2 lety +1

    very nice explnation

  • @shivam7271
    @shivam7271 Před 2 lety +2

    Finallyyyy Understood!!!

  • @harshthakkar7503
    @harshthakkar7503 Před rokem

    Crystal clear explaination : )

  • @CUTIE-PIE-KRISHIKA
    @CUTIE-PIE-KRISHIKA Před 2 lety +1

    best explanation availabel on internet for this problem.

  • @mainakmondal5228
    @mainakmondal5228 Před 6 měsíci

    Nice explanation

  • @shyamsundar214
    @shyamsundar214 Před 2 lety +1

    Epic Explanation

  • @bimurtabismoysanchi4029
    @bimurtabismoysanchi4029 Před 2 lety +5

    Another outstanding explanation. Thanks a lot :).

  • @utsavgupta746
    @utsavgupta746 Před 2 lety +1

    Thanks..🙌

  • @prakhyat2001
    @prakhyat2001 Před 11 měsíci +1

    thanks a lot

  • @manojjain3501
    @manojjain3501 Před 2 lety +1

    Awesome Bhai. I Appreciate.

  • @abhishek__anand__
    @abhishek__anand__ Před 10 měsíci

    Great Explanation

  • @Sujith0369
    @Sujith0369 Před 10 měsíci

    Crisp short to the point solution.Just Loved it!

  • @Ryan-mr9pn
    @Ryan-mr9pn Před 2 lety +2

    Best explanation ⚡

  • @commentator2407
    @commentator2407 Před 2 lety +1

    just wow.
    im in love with bitmasks now....
    great explaination.......tooo....

  • @gauravshah7888
    @gauravshah7888 Před rokem +1

    Thanks!

  • @trevorPhillips284
    @trevorPhillips284 Před 2 lety +1

    Well Explained bro, need to dry run the solution to understand it fully

  • @MrVrtex
    @MrVrtex Před 10 měsíci

    When I feel sleeplessness, I watch his videos :) Thanks man for that

  • @pareekshithachar3788
    @pareekshithachar3788 Před rokem +1

    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
      @user-fx5lk6dy3m Před 10 měsíci

      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.

  • @gaurabdas1510
    @gaurabdas1510 Před rokem +1

    Great explanation!

  • @jagdambesingh7293
    @jagdambesingh7293 Před rokem

    Could you please explain why you have used loop inside while loop ,?

  • @girikgarg8
    @girikgarg8 Před 2 lety

    Time complexity of this approach?

  • @krishan7630
    @krishan7630 Před rokem +1

    Thanks bro

  • @vishalratna89
    @vishalratna89 Před 11 měsíci

    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?

  • @mohithadiyal6083
    @mohithadiyal6083 Před 2 lety +1

    You are simply amazing 😁

  • @mandy1339
    @mandy1339 Před 2 lety +1

    You are amazing!

  • @Sauravk2107
    @Sauravk2107 Před rokem

    ver nice explanation

  • @comment.only0598
    @comment.only0598 Před rokem

    Most youtuber explained this question like "Ratta". Thanks for this amazing video and explained very well.

  • @MrACrazyHobo
    @MrACrazyHobo Před 2 lety +1

    Thanks to your videos, I managed to get a position at Pinterest! Bro you are the fking best

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  Před 2 lety

      Heartiest congratulations @Y C, let us connect on linkedin www.linkedin.com/in/coding-decoded-6809a3215/

  • @umeshhbhat
    @umeshhbhat Před 2 lety +2

    Well explained man!

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  Před 2 lety +1

      Thanks Umesh, watch out the entire series you will simply love it

    • @umeshhbhat
      @umeshhbhat Před 2 lety +1

      @@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
      @CodeWithSunchitDudeja  Před 2 lety

      @@umeshhbhat thanks for the tip man.. will come over video
      very soon

  • @kshitijgarg2609
    @kshitijgarg2609 Před 10 měsíci

    SOlid explanatory video

  • @lingasodanapalli615
    @lingasodanapalli615 Před 10 měsíci

    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

  • @nicolattu
    @nicolattu Před 2 lety +1

    Nice one to know 👌👏 is it travelling salesman problem with unweighted edges? Also how can we calculate time complexity?

  • @gangstacoder4234
    @gangstacoder4234 Před 4 měsíci

    isnt just knowing about the parent node enough to avoid indefinite loops

  • @dubeyanurag
    @dubeyanurag Před 10 měsíci

    The BFS Solution is a lot more straightforward than the DFS one.

  • @sahilattri51
    @sahilattri51 Před rokem

    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
      @sahilattri51 Před rokem

      My bad, understood the concept now. Thanks for the video.

  • @alldaysports6556
    @alldaysports6556 Před 2 lety +1

    how can we know where to use bit manipulation . i mean intuition to use bit??

    • @bloody9162
      @bloody9162 Před 2 lety +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.

  • @bikideka7880
    @bikideka7880 Před 2 lety

    Hey is Leetcode hard necessary for interviews and online coding round?

    • @MrVrtex
      @MrVrtex Před 10 měsíci

      For FAANG/MAANG yes

  • @vaibhavvashist238
    @vaibhavvashist238 Před 2 lety +1

    is this question might come in interview as the level is question is little bit high?

    • @CodeWithSunchitDudeja
      @CodeWithSunchitDudeja  Před 2 lety +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

  • @ranitripathi607
    @ranitripathi607 Před 2 lety +1

    prettiest 😢

  • @Think_Code
    @Think_Code Před 2 lety +2

    a suggestion : u can use wacom tablet with pen to write instead of mouse. That will improve your efficiency. you can check mine videos.

  • @gauravshah7888
    @gauravshah7888 Před rokem +1

    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

  • @sonicjetson6253
    @sonicjetson6253 Před 10 měsíci

    arre bhai jisne yeh ques pehle nahi dekha woh kaise solve karega interview main. total chutiyapa chal raha hain