LeetCode 46 - Permutations

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • If you liked this video check out my playlist...
    • Leet Code top 100

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

  • @meamail
    @meamail 4 ปีที่แล้ว +82

    This is the best recursive tree explanation I ever seen. Thank you

  • @Chloe-si2hq
    @Chloe-si2hq 2 ปีที่แล้ว +6

    Thank you for your patience and hard work for actually walking us through the recursive process. The recursion tree is clear and solid. You really know what you are teaching unlike most of the LeetCode TH-camr out there who only copy and paste code. Thanks again!

  • @raunakagrawal4230
    @raunakagrawal4230 4 ปีที่แล้ว +11

    I cannot thank you more. This is actually an interview question asked by Google. You made it look like very simple with your explanation. Tons of thank you, man!

    • @kamilnowak9750
      @kamilnowak9750 4 ปีที่แล้ว +5

      everything is simple with good approach

  • @joseulissescastro4883
    @joseulissescastro4883 ปีที่แล้ว +1

    That was the only video that helped me to really understand what happens in a backtracking solution.
    Thank you sir

  • @grantg8638
    @grantg8638 4 ปีที่แล้ว +6

    This was the first explanation of the Permutations problem I've found that makes sense. Thank you.

    • @utkarshkandpal5960
      @utkarshkandpal5960 4 ปีที่แล้ว

      answers.push([...set]) instead of this : answers.push(set) solves the problem.

    • @tharunkallati9631
      @tharunkallati9631 4 ปีที่แล้ว

      Can’t agree more!!!

  • @tonyz2203
    @tonyz2203 4 ปีที่แล้ว +4

    You are actually explaining how to approach this problem unlike most of the leetcode youtuber( they just memorize the code and type it out). Thank you very much.

  • @chamnil8666
    @chamnil8666 4 ปีที่แล้ว +4

    This is the best recursive tree video I have seen so far.Thank you so much SIR,now I can eat something,since I got it down.I was struggling to understand it for over a day.Thanks a million.

  • @soumikkarmakar7433
    @soumikkarmakar7433 2 ปีที่แล้ว +1

    No one cared to explain how recursion is working inside the for loop. But you did it thanks a lot man!!

  • @ujjwaljha845
    @ujjwaljha845 3 ปีที่แล้ว +4

    This is one of the best explanation of recursion I have come across, please keep up the good work.

  • @danxuehuang2099
    @danxuehuang2099 4 ปีที่แล้ว +16

    It's best ever!! Thank you so much for explaining step by step patiently lol. just hit subscribe but saw your latest update was 8 months ago, it would be great if you can keep going, your explanation is straightforward and super easy to understand, thank you!

  • @ShubhamKumar-sj6dp
    @ShubhamKumar-sj6dp ปีที่แล้ว

    this is the best illustration for recursion tree when called under a loop, every video just draw a two way or three way branch from beginning but this was exactly step by step , thnx

  • @Zoe-fw6kt
    @Zoe-fw6kt ปีที่แล้ว

    please start posting new videos, I watched loads of algothrim videos, yours are the ones that make more senses. and easier to understand, Thank you~~~

  • @danishuddin9752
    @danishuddin9752 2 ปีที่แล้ว

    Thank andi guru garu, ee logic ardham kaaka oka roju antha chacchi poya!

  • @prafullakh
    @prafullakh 2 ปีที่แล้ว +6

    Today marks 3 years since you posted this video. Why have you stopped posting new videos? You are a very good teacher, Sir. You have a divine gift. Recursive thanks to you.
    PLEASE START POSTING NEW VIDEOS.

  • @vibhavkushwaha5809
    @vibhavkushwaha5809 3 ปีที่แล้ว

    it was a piece of cake for you.
    And now for others too. Holy moly how well you explained, i cant describe it words, it was way to easy for us to understand what's going on.
    Thanks a ton mate. Cheers :)

  • @StyleTrick
    @StyleTrick ปีที่แล้ว

    Great solution and thinking, thanks!

  • @KarthikParthasarathy
    @KarthikParthasarathy 4 ปีที่แล้ว +1

    Time Complexity Infinity is a great concept , great channel and some brilliant work here

  • @chandrashekar8309
    @chandrashekar8309 4 ปีที่แล้ว +1

    This is it! I found the best recursion explanation. Good one 👍

  • @ansonmelakayil3281
    @ansonmelakayil3281 3 ปีที่แล้ว

    way cleaner solution than whats on leetcode rn. good stuff man. thanks

  • @xmaxfuture
    @xmaxfuture 4 ปีที่แล้ว +2

    dude, u have the best explanation for this question

  • @archanamasilamani5115
    @archanamasilamani5115 4 ปีที่แล้ว +1

    One of the best videos on recursion. Thanks!

  • @saelkhlouf1695
    @saelkhlouf1695 ปีที่แล้ว

    Man you are a life saver !

  • @cooldude3010
    @cooldude3010 4 ปีที่แล้ว +1

    Your use of filter to remove an element at an index is awesome. I've been using splice() with concat() but filter() is so much more elegant.

    • @mistercorea
      @mistercorea 2 ปีที่แล้ว

      splice should be faster than the filter.

  • @becktj
    @becktj 3 ปีที่แล้ว

    A very good explanation. I had to watch it 5 times though lol. Thank you!

  • @quochunghoang8833
    @quochunghoang8833 2 ปีที่แล้ว

    Wonderful recursive explanation !!! Thank you.

  • @justadev____7232
    @justadev____7232 3 ปีที่แล้ว

    Beautiful explanation! Been looking for something like this for a while now!!!

  • @leohippochin708
    @leohippochin708 2 ปีที่แล้ว

    This video is very well done. Good Job. Man!

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

    great explanation !

  • @navderm
    @navderm 2 ปีที่แล้ว

    the removal of one element from an array itself is an O(n) complexity. That turns your solution into O(n*n*(n!))

    • @mistercorea
      @mistercorea 2 ปีที่แล้ว

      What if you use a splice() function that will be O(1)? can you explain why it is O(n^2 * n!)?

    • @navderm
      @navderm 2 ปีที่แล้ว

      @@mistercorea How does splice work internally? How do you say that splice is an O(1) algorithm? Splice itself cannot be O(1) but at the minimum has to be O(n).

  • @admercs
    @admercs 2 ปีที่แล้ว

    Nice explanation!

  • @zidane6165
    @zidane6165 2 ปีที่แล้ว

    Read the way of solving it using GeeksForGeeks, AlgoExpert, Leetcode prime solution explanation, couple of youtube videos.
    Didn't understand it from any, but you.
    Thank you.

  • @krugerbrent4270
    @krugerbrent4270 2 ปีที่แล้ว +2

    Wow! You did a phenomenal job of explaining this in much simpler terms. Thank you so much for making this. Now, I understand this problem way better.
    I have one question though - Isn't the time complexity O(n * n!) from the tree diagram you illustrated? n times we are doing n! right, or can we reduce O(n * n!) to O(n!)?

  • @aniruddhashahapurkar9244
    @aniruddhashahapurkar9244 ปีที่แล้ว

    Best explanation!!

  • @legion_29
    @legion_29 4 ปีที่แล้ว

    Please add more LeetCode challenges.. your explanations are awesome. Thank you.

  • @TanujDhaundiyal
    @TanujDhaundiyal 4 ปีที่แล้ว

    Explained in such an easy manner. Thanks!

  • @dhillon6449
    @dhillon6449 2 ปีที่แล้ว

    the best

  • @xinhe3989
    @xinhe3989 5 ปีที่แล้ว

    The great explanation about tree and recursion.

  • @oziomaogbe8665
    @oziomaogbe8665 3 ปีที่แล้ว

    Love you bro

  • @leoc.9630
    @leoc.9630 5 ปีที่แล้ว +3

    You said we didn't use extra space in the video, but I think newNums is an extra space in each call. Space complexity will O(n*k) where n is the depth of tree and k is the extra space in each call. Please clarify.

  • @iamkarthikkeyan
    @iamkarthikkeyan 4 ปีที่แล้ว

    Thanks for the clear explanation. This is the best explanation.

  • @allenllewellynkra
    @allenllewellynkra 4 ปีที่แล้ว

    Brilliant explaination. Please do more videos

  • @vtserej
    @vtserej 3 ปีที่แล้ว

    best explanation so far

  • @mauricegoldberg7458
    @mauricegoldberg7458 4 ปีที่แล้ว +1

    Finally I get it!! thank you king

  • @roshni962
    @roshni962 3 ปีที่แล้ว

    Thanks :)

  • @poojakadiyan8790
    @poojakadiyan8790 4 ปีที่แล้ว

    very helpful recursion tree visualization

  • @joelcruz2873
    @joelcruz2873 3 ปีที่แล้ว

    Great video!

  • @tusharborole6155
    @tusharborole6155 5 ปีที่แล้ว

    you are best teacher so far

  • @jsjsjs1116
    @jsjsjs1116 3 ปีที่แล้ว

    this guy is a god

  • @ruixue6955
    @ruixue6955 2 ปีที่แล้ว

    1:22 algorithm - 1:29 recursion
    1:54 go through the recursion tree
    2:40
    11:46 code

  • @aaronargottelopez3488
    @aaronargottelopez3488 3 ปีที่แล้ว

    Thanks

  • @saumyaranjannayak2101
    @saumyaranjannayak2101 2 ปีที่แล้ว

    Awesome !

  • @apratim1919
    @apratim1919 4 ปีที่แล้ว

    Beautiful

  • @cindy__4920
    @cindy__4920 4 ปีที่แล้ว

    Thank you for your explanation! It is pretty useful!

  • @Paradise-kv7fn
    @Paradise-kv7fn 3 ปีที่แล้ว

    we don't need to create a new list every time.
    We can use :-
    def permute(self, nums: List[int]) -> List[List[int]]:
    n = len(nums)
    ans = []
    def rec(path):
    if not len(nums):
    ans.append(path[:])
    return
    for i in nums[::-1]:
    nums.remove(i)
    path.append(i)
    rec(path)
    path.pop()
    nums.append(i)
    rec([])
    return ans

  • @doug8171
    @doug8171 3 ปีที่แล้ว

    heap’s algorithm is another way I believe.

  • @thajafsal
    @thajafsal 4 ปีที่แล้ว +2

    Cracking the code interview book says the time complexity of this algorithm is not n!

    • @reelinvestor
      @reelinvestor 4 ปีที่แล้ว

      The book is right, in the video he didn't take into consideration the additional time required to copy into a new array while removing the item.

  • @user-ly1zq7qy6i
    @user-ly1zq7qy6i 3 ปีที่แล้ว

    nice explanation,thanks man.

  • @senthamilsindhu901
    @senthamilsindhu901 4 ปีที่แล้ว

    Great explanation. Thanks!

  • @ponssuarez4513
    @ponssuarez4513 5 ปีที่แล้ว

    Keep on teaching Man!

  • @piyushsharma1638
    @piyushsharma1638 5 ปีที่แล้ว

    Very well explained video, thanks.

  • @deepakranganathan9644
    @deepakranganathan9644 3 ปีที่แล้ว

    Best explanation thanks :)

  • @slymastera
    @slymastera 4 ปีที่แล้ว

    Thank you.

  • @akhilkandibanda
    @akhilkandibanda 4 ปีที่แล้ว

    dude amazing explanation thanks

  • @sandeshrana7225
    @sandeshrana7225 3 ปีที่แล้ว

    best one

  • @alex.perreras
    @alex.perreras 3 ปีที่แล้ว +1

    num.filter has O(N) time complexity - your final complexity is even worse than O(N!). Be careful using this at interview!

  • @0xSol_Invictus
    @0xSol_Invictus 4 ปีที่แล้ว +3

    These things always make sense in hindsight once it's been explained, but do normal people actually come up with these crazy-ass solutions on their own?? Need to figure out what to take away from this conceptually so I actually learn something instead of just being able to do this exact problem if I see it again.

    • @imjeffreylee
      @imjeffreylee 4 ปีที่แล้ว +2

      I guess this is how it works. Problem solving is all about reading the pattern, the more problems you solve the faster you can come up with an idea what algorithm you need to apply. You can say that we are memorizing it, but who isn't? That's why "Practice makes perfect" is a thing. I know I probably never gonna use it again when really working, unless I am on an algorithm research team, but this is how the game is played.

    • @allenalex4861
      @allenalex4861 4 ปีที่แล้ว +1

      Exactly What I've been thinking.how on Earth do these guys come up with such solutions.😂😂😂

  • @rakshith3547
    @rakshith3547 4 ปีที่แล้ว

    good explanation

  • @prakashgatiyala9869
    @prakashgatiyala9869 ปีที่แล้ว

    😇

  • @user-ou5eo1gg3z
    @user-ou5eo1gg3z 4 ปีที่แล้ว

    genius

  • @prvizpirizaditweb2324
    @prvizpirizaditweb2324 3 ปีที่แล้ว

    I am really curios about what is really going on behind the scenes , for example when for the first time Permumation returns ,the next line set.pop is executed. But what was the next step? did it return back to the previous function call in the callStack( which seems it is) or did it just increased the i in for loop?

  • @utkarshkandpal5960
    @utkarshkandpal5960 4 ปีที่แล้ว

    IN line 6 why are we doing this : answers.push([...set]) and why not simply this : answers.push(set)? set is an array and we can simply push the array into the answers array ?

  • @gurjotsinghsidhu1643
    @gurjotsinghsidhu1643 2 ปีที่แล้ว

    Very nice explanation. Can someone show how to do write this in Python?

  • @joshr9703
    @joshr9703 5 ปีที่แล้ว

    thanks m8

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

    What is the space complexity for this

  • @nutandevjoshi
    @nutandevjoshi 4 ปีที่แล้ว

    I had to play 0.75 speed. But so far its okay, will update after I complete video.

  • @markliu22
    @markliu22 3 ปีที่แล้ว +2

    Anyone know the space complexity?
    Thanks in advance!

    • @burntalmonds9598
      @burntalmonds9598 3 ปีที่แล้ว

      I believe O(n!) as well because the array containing the answers will have that many elements

    • @AJewFR0
      @AJewFR0 3 ปีที่แล้ว

      Its O(n!). The amount of permutations that you make is n!. your array then is n! large.

  • @eltonstalspets45
    @eltonstalspets45 4 ปีที่แล้ว

    Hi. I've tried to make sense of this for a long time but I am still confused. Because the base case (the if statement) is checked for before the nums array is filtered, shouldn't there be one more step of the tree for just pushing the array into the answers. In the tree showed in the video, the last step does not meet the base case because nums is not empty when the base case is checked (nums === [3]). Could someone please clarify?

  • @michaelc8876
    @michaelc8876 4 ปีที่แล้ว

    I used Java to write this code. But I get something wrong, the result would be [1,2,3] only. What's the problem with my code here? And how to write it correctly in Java?
    public List permute(int[] nums) {
    if (nums == null || nums.length == 0) return new ArrayList();
    List result = new ArrayList();
    List list = new ArrayList();
    for (int num: nums) list.add(num);
    List todo = new ArrayList();
    backtrack(list, todo, result);
    return result;
    }
    private void backtrack(List list, List todo, List result) {
    if (list.size() == 0) {
    result.add(new ArrayList(todo));
    }
    for (int i = 0; i < list.size(); i++) {
    todo.add(list.get(i));
    list.remove(i);
    backtrack(list,todo,result);
    todo.remove(todo.size() - 1);
    }
    }
    Thanks a lot!

  • @jameskanyi7376
    @jameskanyi7376 3 ปีที่แล้ว

    Would you mind explaining how this code will work in a stack frame?

  • @sonukapoor1978
    @sonukapoor1978 3 ปีที่แล้ว

    Make more videos

  • @whiteeye2121movalik
    @whiteeye2121movalik 3 ปีที่แล้ว

    is it allowed to add function parameters? not leaving just the "nums" one? I thought you are not allowed to do that....

  • @sanskarkaazi3830
    @sanskarkaazi3830 2 ปีที่แล้ว

    Can somebody do this with java. ?

  • @Dan-tg3gs
    @Dan-tg3gs 4 ปีที่แล้ว

    Good explanation but im not sure if its the best implementation in code? I've seen this problem solved using 2 functions with different # of parameters, what are the pros and cons of both implementations?

  • @Respect_Mah_Authoritah_
    @Respect_Mah_Authoritah_ 3 ปีที่แล้ว

    Why do we have to do set.pop()

  • @psettii
    @psettii 4 ปีที่แล้ว +1

    Can any one please share code for Java ?

    • @jagritbhupal5836
      @jagritbhupal5836 4 ปีที่แล้ว

      I used general backtracking logic to implement this. I don't know if its similar to what he is implementing as I only watched the logic.
      class Solution {
      List list=new ArrayList();
      public List permute(int[] nums) {
      backtrack(new ArrayList(),nums);
      return list;
      }
      public void backtrack(List curr,int[] nums)
      {
      if(curr.size()==nums.length)
      {
      list.add(new ArrayList(curr));
      }
      for(int i=0;i

    • @sannge7967
      @sannge7967 4 ปีที่แล้ว

      @@jagritbhupal5836 hey, can you please explain the code by chance?

    • @kmn1794
      @kmn1794 3 ปีที่แล้ว

      Why poke yourself in the eyes and into the brain when learning a new concept whilst coding with Java will do it for you.

  • @skkkk3105
    @skkkk3105 4 ปีที่แล้ว

    Can u please add java code?

  • @anmolcoolsinha
    @anmolcoolsinha 4 ปีที่แล้ว

    Can someone give the java version of this?

  • @MarsTheProgrammer
    @MarsTheProgrammer 3 ปีที่แล้ว

    you lost me 3 minutes in lol...