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 RU-vidr out there who only copy and paste code. Thanks again!
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!
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.
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.
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.
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!
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
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 :)
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!)?
@@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).
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.
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.
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
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.
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.
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 ?
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?
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!
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?
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?
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