Subsets - Leetcode 78 - Recursive Backtracking (Python)

Поділитися
Вставка
  • Опубліковано 6 лют 2025
  • Master Data Structures & Algorithms for FREE at AlgoMap.io/
    Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: github.com/gah...
    Complete DSA Pathway Zero to Hero: • Data Structures & Algo...
    Please check my playlists for free DSA problem solutions:
    • Fundamental DSA Theory
    • Array & String Questions
    • 2 Pointers Questions
    • Sliding Window Questions
    • Binary Search Questions
    • Stack Questions
    • Linked List Questions
    • Tree Questions
    • Heap Questions
    • Recursive Backtracking...
    • Graph Questions
    • Dynamic Programming (D...
    My Data Science & ML UA-cam Playlist: • Greg's Path to Become ...
    Learn Python and Data Science FASTER at mlnow.ai :)
    Support the content: / @greghogg
    Follow me on Instagram: / greghogg5
    Connect with me on LinkedIn: / greghogg
    Follow me on TikTok: / greghogg5
    Coursera Plus: imp.i384100.ne...
    My Favorite Courses:
    Data Structures & Algorithms:
    UCalifornia San Diego DSA: imp.i384100.ne...
    Stanford Algorithms: imp.i384100.ne...
    Python Data Structures: imp.i384100.ne...
    Meta Coding Interview Prep: imp.i384100.ne...
    Python:
    UMichigan Python for Everybody: imp.i384100.ne...
    Python Mastery from MLNOW.ai: mlnow.ai/cours...
    Google IT Automation w/ Python: imp.i384100.ne...
    Web Dev / Full Stack:
    Meta Front-End Developer: imp.i384100.ne...
    IBM Full Stack Developer: imp.i384100.ne...
    Meta Back-End Developer: imp.i384100.ne...
    John Hopkins HTML, CSS & JS: imp.i384100.ne...
    IBM DevOps: imp.i384100.ne...
    Cloud Development:
    AWS Fundamentals: imp.i384100.ne...
    GCP Cloud Engineer: imp.i384100.ne...
    Microsoft Azure Fundamentals: imp.i384100.ne...
    Game Development:
    Michigan State Unity Development: imp.i384100.ne...
    UColorado C++ for Unreal Engine: www.coursera.o...
    SQL & Data Science:
    SQL by MLNOW.ai: mlnow.ai/cours...
    Python for Data Science by MLNOW.ai: mlnow.ai/cours...
    Google Data Analytics: imp.i384100.ne...
    IBM Data Science: imp.i384100.ne...
    IBM Data Engineer: imp.i384100.ne...
    Machine Learning & AI:
    ML Mastery at MLNOW.ai: mlnow.ai/cours...
    ML w/ Andrew Ng: www.coursera.o...
    Deep Learning w/ Andrew Ng: imp.i384100.ne...

КОМЕНТАРІ • 41

  • @GregHogg
    @GregHogg  6 місяців тому

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @SHIHJUIheh
    @SHIHJUIheh 6 місяців тому +7

    Thank you for explaining drawing part in detail, especially the recursive backtracking part ! You made this concept so much easier!!!

    • @GregHogg
      @GregHogg  6 місяців тому

      You're very welcome 😎

    • @itsmenatika
      @itsmenatika 3 місяці тому

      I've started understanding backtracking by that. In only 12 minutes. Thanks

  • @NavneetKaur-x6q
    @NavneetKaur-x6q 4 місяці тому +1

    After looking at several other videos, I finally found one where all my questions were answered. Loved the simplicity of the explanation whether whiteboard, coding part or the complexity.

  • @chisomedoka5651
    @chisomedoka5651 7 місяців тому +1

    this is gold, so intuitive . Thanks for this

  • @shubhambajaj4939
    @shubhambajaj4939 6 місяців тому +1

    brilliant solution. you just got yourself a new subscriber :)

  • @anothertechguy-q9g
    @anothertechguy-q9g 5 місяців тому +1

    Thank you a lot for explaining the transitive parts of backtracking!

  • @vasachisenjubean5944
    @vasachisenjubean5944 Місяць тому

    Absolutely beautiful explanation. I loved it !!

  • @EquinoXReZ
    @EquinoXReZ Місяць тому

    How did you get so good at explaining solutions? I want to get to the point where I can communicate in interviews the way you are communicating in this video. Any tips?

  • @kenkaneki5433
    @kenkaneki5433 4 місяці тому +2

    better than stp neetcode guy explanation

  • @amanreh
    @amanreh Місяць тому

    better than the neetcode guy respectfully

  • @FZRides
    @FZRides 8 місяців тому +4

    Hi Greg,
    I found your video very intuitive. Thanks for sharing such content. Can you please make a video on "Tower of Hanoi" problem using recursion. I am unable to catch the recursive logic behind it. Can you please do it Sir.

  • @christianjt7018
    @christianjt7018 6 місяців тому +1

    thanks Greg your explanations are the best!

    • @GregHogg
      @GregHogg  6 місяців тому

      Bro I can see from the comments that you're just flying through these questions, good for you honestly!

  • @DivineEdoka
    @DivineEdoka 4 місяці тому +1

    I'll like to see how you slve subset ii with this pattern as well

  • @dusvn1484
    @dusvn1484 3 місяці тому

    Nice video,helped me a lot!

  • @sampjm1898
    @sampjm1898 9 місяців тому +1

    you can use a dp solution :
    fn(n)=fn(n-1)+{fn(n-1) and put element_n in every set that return by fn(n-1)},cache the result of f(n).,f(n-1).....
    consider you need to solve all the fn(n) you can write a bottom up dp solution,
    consider for each fn(n) only need f(n-1) you can just maintain one layer of cache
    so basic case is {[element_1],[empty]} for every element in the array add this element to each set and add this set back to the result:
    so the 2nd iteration: {[element_1],[empty],
    [element_1,element_2],[element_2]} and so on
    sorry for my English

    • @GregHogg
      @GregHogg  9 місяців тому

      I'll have to look into this. Thanks so much for sharing!

    • @supremoluminary
      @supremoluminary 8 місяців тому

      Do you have a link to this? I want to learn it. Thanks.

  • @olaf9063
    @olaf9063 8 місяців тому +3

    Great explanation, thanks. Is the time complexity not O(n * 2^n) - reason being that at each of the terminal nodes you need to copy the list, which is an O(n) operation?

    • @henryhan8838
      @henryhan8838 8 місяців тому

      You've inspired me, I was just wondering why we multiplied it by n.

    • @tauicsicsics
      @tauicsicsics 2 місяці тому

      indeed, I thing Gregg forgot the "n *" part of the time complexity, it's not O(2^n)

  • @GarouNguyen
    @GarouNguyen 7 місяців тому +1

    bro how do you move recursion left what is line 12 code back track i + 1 , why you have two backtrack(i+1) can you explain bro

  • @MysterioGamer-88
    @MysterioGamer-88 6 місяців тому +1

    what does backtrack(i +1) mean

  • @phamh79
    @phamh79 3 місяці тому

    many thanks.

  • @m.y.7230
    @m.y.7230 7 місяців тому

    thanks for explaning dfs in drawing

  • @nav213
    @nav213 6 місяців тому

    Would backtracking have to do with recursion? Is it possible to solve this in a non-recursive way? I am asking because it's really difficult to understand the recursive implementation of the code unless you memorize it. Thanks again for you awesome tutorials!!!! cheers

    • @GregHogg
      @GregHogg  6 місяців тому

      You can memorize the template for how this is done, I promise the best way is through recursion

  • @kollabalaji-q4n
    @kollabalaji-q4n 18 днів тому

    perfect !!

  • @Alex-tm5hr
    @Alex-tm5hr 3 місяці тому

    Can you pls make a vid for subsets 2?

  • @GarouNguyen
    @GarouNguyen 7 місяців тому

    bro why don't pick and pick is same backtrack(i+1) and why you recursion left i + 1 i don't understand

  • @anti-dn541
    @anti-dn541 9 місяців тому +1

    Easy to understand for noob like me 👍🏻

    • @GregHogg
      @GregHogg  9 місяців тому

      Oh that's so great to hear 😊

  • @ohmyoni
    @ohmyoni 9 місяців тому

    Great solution

    • @GregHogg
      @GregHogg  9 місяців тому +1

      Thanks so much!

  • @PhươngNguyễnMinh-n4k
    @PhươngNguyễnMinh-n4k 26 днів тому

    My solution is just calculate combinations C(n,k) where k from 1 to n

  • @hrushik10
    @hrushik10 6 місяців тому

    thanks

  • @Antinormanisto
    @Antinormanisto 8 місяців тому +1

    I don't understand(

    • @GregHogg
      @GregHogg  8 місяців тому

      Maybe try watching it again? Backtracking is REALLY confusing at first