For those unfamiliar w/ the swapping syntax: for(var j = i; j < nums.length; j++) { // swap the positions var temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; dfs(i + 1, nums); // unswap the positions as we go back up the tree nums[j] = nums[i]; nums[i] = temp; }
Nice solution, thanks! I got confused initially, about why you swapped twice😀 But, I think it would be okay to avoid mutating the original nums array. e.g for(let j=i; j< nums.length; j++){ let temp = [...nums]; [temp[i], temp[j]] = [temp[j], temp[i]]; dfs(i+1, temp); } JS can be tricky at times.
Dude, I am trying same logic before seeing this video. I am only push the nums as it is in result. I am not understand the reason behind the push as nums.slice(). please u see then replay please.
Dude, this is one of those mind-blowing situations where I struggled for hours and you explained it in 15 minutes. Thanks very much. You helped a lot.
For those unfamiliar w/ the swapping syntax:
for(var j = i; j < nums.length; j++) {
// swap the positions
var temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
dfs(i + 1, nums);
// unswap the positions as we go back up the tree
nums[j] = nums[i];
nums[i] = temp;
}
Are you referring to this part? [nums[i[, nums[j]] = [nums[j[, nums[i]]?
@@IRLVaporwave yes he is
thanks a lot
I've spent hours trying to figure out this problem. I watched your video once, and immediately understood it with your tree. Great work!!
In the tree first swap why swap 1,2,3 to 3,1,2 not 3,2,1 ?
Good catch. Yes it should be 3,2,1.
yes
@@andygala888 pin it
I was busting my brains over this, thanks for commenting
This helped me understand recursive backtracking a lot. Thanks a lot!
Interesting… this isn’t how I solved this at all (although I too used recursion) 🤯 Amazing how many different ways there are to solve a challenge!
great explanation, the visual in the beginning helped alot, got my interview on monday hope I pass
Thank you very much. I should have thought of that earlier.
Andy I really appreciate your playlist, you are amazing!
Thank you, Andy!
In backtracking, why do we have to re-swap? we already have the answers by looking at the leaf
Nice solution, thanks!
I got confused initially, about why you swapped twice😀
But, I think it would be okay to avoid mutating the original nums array.
e.g
for(let j=i; j< nums.length; j++){
let temp = [...nums];
[temp[i], temp[j]] = [temp[j], temp[i]];
dfs(i+1, temp);
}
JS can be tricky at times.
Hey Andy, I'm not understanding line 21. Why do we need the swap again??
Thanks so much for this backtracking/recursion series!
very nicely explained thankyou so much
Dude, I am trying same logic before seeing this video. I am only push the nums as it is in result. I am not understand the reason behind the push as nums.slice(). please u see then replay please.
Why is it j=i and not j=0 in the for loop?
What does it mean when he says "when we get to the leaf node, we need to scan that" ?
we will store that copy of array in our result array.
Good One!
What about two numbers? :( [1, 2]
What whiteboard is this?