Permutations - Leetcode 46 - Recursive Backtracking (Python)

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024

ความคิดเห็น • 28

  • @GregHogg
    @GregHogg  หลายเดือนก่อน +1

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

  • @shreehari2589
    @shreehari2589 4 หลายเดือนก่อน +8

    Greg you have natural talent in teaching, kindly don’t stop making videos

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน +2

      Awe that's so sweet. Probably just practice as I've been learning and teaching for awhile now. I really appreciate it!! 😊

  • @AbhishekYadav-yw8kv
    @AbhishekYadav-yw8kv 17 วันที่ผ่านมา +2

    such an elegant explanation. Thanks Greg!

  • @dabian1916
    @dabian1916 หลายเดือนก่อน +10

    so much better than neetcode

    • @GabriellBP1
      @GabriellBP1 หลายเดือนก่อน +3

      have to agree on that one

    • @shubhamdas9489
      @shubhamdas9489 6 วันที่ผ่านมา +1

      Yeah

    • @Tallonest
      @Tallonest 3 วันที่ผ่านมา

      I agree, Greg’s are so well explained.. best channel on TH-cam

  • @Zippo_1234
    @Zippo_1234 16 วันที่ผ่านมา +1

    This was excellent. Thank you

  • @christianjt7018
    @christianjt7018 หลายเดือนก่อน +1

    I learned backtracking thanks to you, thanks Greg!

    • @GregHogg
      @GregHogg  หลายเดือนก่อน

      Amazing 😍😍

  • @saurabh-11a
    @saurabh-11a หลายเดือนก่อน +1

    Hi Greg, I could not understand how the pop is working here, once we have the first solution, I dont understand how pop is happening twice in the for loop.

    • @stanleyching123
      @stanleyching123 หลายเดือนก่อน +1

      Leaving a comment here as I want to also know

    • @SBManukrishna-jz6ud
      @SBManukrishna-jz6ud 15 วันที่ผ่านมา +2

      Hello Saurabh,
      Here is how pop() does the work here.After we append the first solution to ans,the control gets back to the "sol.pop()"line and we pop 3 from sol since its the last element. At this stage note that we are in the last iteration of for loop as well since x =3. So we don't have to execute anything further with backtrack() when x=3. So now we return back the sol which is [1,2] to backtrack() when x =2. Now the next line is sol.pop().So we pop 2 from sol and hence sol becomes [1].but here is the catch... now x=2 and the loop has 1 more interation for x=3 so we check if 3 is in solution and add it into sol and sol becomes [1,3], now we call backtrack() and we compare the len of sol to n which is not equal to 3 so we start the for loop afresh once again and check if 1 is in sol. Yes 1 is in sol=[1,3],but in next iteration when x=2 since 2 is not in sol, we add it to sol and add it to ans. This is how we formulate the 2nd solution. Hope you can figure out the flow for remaining solutions and hope this helped😊

  • @jz77096
    @jz77096 26 วันที่ผ่านมา

    I think this problem is purely a test of how to code it concisely.
    Leetcoding to land a Meta/Google position.
    Gotta use code that interviewers can read... so found 3 solutions:
    Dupes
    Insertion into a growing temp linkedList
    Rotations
    Took me a couple hours to solve on my own, giving up, then looking it up.

  • @HemanthKumar-vl9oh
    @HemanthKumar-vl9oh 2 หลายเดือนก่อน

    Woow ! You made look so simple . Good job. Keep doing such videos

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      Thank you!

  • @sabaokangan
    @sabaokangan 4 หลายเดือนก่อน +1

    Beautiful teaching

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน

      Thanks a ton!!!

  • @rishabkalluri662
    @rishabkalluri662 หลายเดือนก่อน

    Isn't the time complexity N! * n? , checking if x in sol is n

    • @rishabkalluri662
      @rishabkalluri662 หลายเดือนก่อน

      or copying solution before you add it is n

  • @aswination
    @aswination 4 หลายเดือนก่อน

    Excellent video!
    How would the code change if there are duplicate numbers in the array?

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน

      Thank you! And there's actually a problem for that, might cover it at some point :)

  • @user-hu9nu8xu5g
    @user-hu9nu8xu5g 2 หลายเดือนก่อน

    bro what is if x not in sol, can you explain that I don't understand bro

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      If we've already used the number, we don't want to use it again

  • @isako1234
    @isako1234 หลายเดือนก่อน

    its O(n! * n), because of the line: if x not in sol

    • @yunusemreozvarlik2906
      @yunusemreozvarlik2906 หลายเดือนก่อน

      Same for the space complexity. It should be O(n! * n)

  • @vroomerlifts
    @vroomerlifts 2 หลายเดือนก่อน

    hell yeah