Recursive Backtracking - DSA Course in Python Lecture 14

Поделиться
HTML-код
  • Опубликовано: 29 янв 2025

Комментарии • 26

  • @GregHogg
    @GregHogg  6 месяцев назад +1

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

  • @Alex-tm5hr
    @Alex-tm5hr 3 месяца назад +3

    I appreciate you making the vid greg! vids that explain a concept/algo and then show an example are very useful, not many people doing that

  • @asagiai4965
    @asagiai4965 6 месяцев назад +7

    Nice. Explanation. I might have a question but I still have to think about it.

  • @chinmayjoshi2495
    @chinmayjoshi2495 3 месяца назад +12

    5:52 he better call 'sol'

  • @ceciljoel9577
    @ceciljoel9577 6 месяцев назад +14

    this problem came in my coding interview but instead i had to just output the number of subset so i just printed 2 to power n

  • @teostorm1
    @teostorm1 4 месяца назад +3

    I don't think anyone could explain this better fr

  • @2NormalHuman
    @2NormalHuman Месяц назад

    Thats nice, really good explanation!

  • @harshgamer6105
    @harshgamer6105 6 месяцев назад +5

    7:40 , so basically, we are doing inorder traversal right?

  • @ojxchaos
    @ojxchaos 3 месяца назад

    This solution is brilliant

  • @ClipGossip
    @ClipGossip 3 месяца назад +1

    i think the time complexity should be 2^n * n right ?

    • @devoch2031
      @devoch2031 2 месяца назад

      Total number nodes in the tree = (2^0+2^1+...+2^n) = 2^(n+1) - 1. So, Time complexity is O(2^n).

  • @pianolessonswithbless879
    @pianolessonswithbless879 3 месяца назад

    You said youve covered videos om DFS but I went through your playlist and didn't see it, please can you send me the link

  • @gordonz4762
    @gordonz4762 Месяц назад

    Great video, although I still don't understand why the space complexity is O(n). I understand that the call stack is O(n), but we're also storing the results in the res list, whose length scales with n. With the example of 3 inputs, we have 2**3 results in the results list, and each result has up to n length. Shouldn't the space complexity therefore be O(n*2^n)

    • @naveenreddy8691
      @naveenreddy8691 11 дней назад

      Because we store only n number of call stacks at max , the count will always be balanced as we also backtrack(pop the last call) and call a new stack, try referring a tree structure - you will have more insight...

    • @gordonz4762
      @gordonz4762 11 дней назад

      @naveenreddy8691 Yes the call stack is O(n), but the results list also takes space no?

    • @naveenreddy8691
      @naveenreddy8691 10 дней назад

      @@gordonz4762 thanks for editing the ask that actually resolves ,and the answer for your question is they can actually print the subset at the base condition check each time, but the way greg solved requires that extra space

  • @aakashs1806
    @aakashs1806 6 месяцев назад +4

    Very hard

  • @biavechiato
    @biavechiato 2 месяца назад

    Nobody got me like Greg Hogg got me

  • @gibs40
    @gibs40 6 месяцев назад +4

    Hardest sht in coding

    • @GregHogg
      @GregHogg  6 месяцев назад +3

      Yeah it probably is

    • @andrewtitus6839
      @andrewtitus6839 4 месяца назад +4

      Glad it's not just me who has had trouble understanding recursion. But it's a good building block for understanding harder problems, like ones involving dynamic programming.

  • @aryanpratap5853
    @aryanpratap5853 6 месяцев назад +1

    First view 😊😊

  • @barthsparks9791
    @barthsparks9791 4 месяца назад +1

    I tried this, and it didn't even work.

    • @JoatXI
      @JoatXI 3 месяца назад +1

      if you tried it in leetcode and it didn't work, make sure your coding language is Python3 and not just "Python"

    • @xingyuxiang1637
      @xingyuxiang1637 3 месяца назад +1

      @@JoatXI He probably means conceptually. I would say let the idea about selecting and unselecting be clear in the arguments or function calls. So, in this branch, it is selected. In the other branch, it is unselected. Then, there are fewer global variables to keep track of.
      Greg's approach is like backtracking for 1-tree or reversing a single-linked list by recursions that can be done easily through combinations API or reversing API. One is probably working on advanced skill sets if one is using this approach. However, global or nonlocal keywords are discouraged at the moment.
      There are indeed many hard problems. So, one just needs to be very patient to go through the code step by step or purchase a premium to use the debug feature. In my case, I would work on beginner problems mostly.