Dude I like your solutions because they are always so clean and the fact that you don't use "pythonic code" makes it so much more easier to understand. So many other people abuse python shortcuts that makes the code impossible to understand
Exactly, this is more like (n+1)! should have used a set() to track the parents. But since the maximum size in the problem description was 6, this makes it negligible.
@@DinhKhoaNguyen-h8d It's not. If it was O(n + n!) then it would be O(n!) but O(n*n!) cannot be reduced because the growth rate is faster than O(n!) and is in a larger complexity class
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.
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😊
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.
Thank you for the detailed explanation! Really appreciate your work. Recently, a company asked the Question Leetcode 1307 (cryptarithm) on my campus. Would you please consider providing a video solution for this question, in python?
explanations are great but the variable namings not so great. ans and sol? both mean the same thing, it's unclear which is the final one. maybe currSubset or smth like this.
Master Data Structures & Algorithms For FREE at AlgoMap.io!
Greg you have natural talent in teaching, kindly don’t stop making videos
Awe that's so sweet. Probably just practice as I've been learning and teaching for awhile now. I really appreciate it!! 😊
After watching 3 different vids for this problem, this was the only solution where the code made intuitive sense to me, thanks for this fr
Dude I like your solutions because they are always so clean and the fact that you don't use "pythonic code" makes it so much more easier to understand. So many other people abuse python shortcuts that makes the code impossible to understand
I learned backtracking thanks to you, thanks Greg!
Amazing 😍😍
such an elegant explanation. Thanks Greg!
so much better than neetcode
have to agree on that one
Yeah
I agree, Greg’s are so well explained.. best channel on RUclips
Well, Neetcode solution works for both permutation I and permuation II problem though
so true
this solution is so much more understandable! thank you
Finally got a hang of backtracking because of you 🙏
should the checking of x in sol be counted inside the time complexity? if so, this will be O(n.n!)
Exactly, this is more like (n+1)! should have used a set() to track the parents. But since the maximum size in the problem description was 6, this makes it negligible.
Agreed. As @omarllama said, a set() should have been used.
This is a really intuitive solution, thank you!
Wouldn't the time complexity be O(n! * n) since we're checking each time if x is not in sol?
guess what, n! * n is n!
@@DinhKhoaNguyen-h8d It's not. If it was O(n + n!) then it would be O(n!) but O(n*n!) cannot be reduced because the growth rate is faster than O(n!) and is in a larger complexity class
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.
Leaving a comment here as I want to also know
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😊
@@SBManukrishna-jz6udGreat explanation, thank you!😁
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.
been coding for 1-2 months and im getting into these, my brain hurts
Woow ! You made look so simple . Good job. Keep doing such videos
Thank you!
This was excellent. Thank you
Thank you for the detailed explanation! Really appreciate your work. Recently, a company asked the Question Leetcode 1307 (cryptarithm) on my campus. Would you please consider providing a video solution for this question, in python?
Wouldn't the space complexity be exponential if we consider the recursion stack?
Great explanation, thanks!
very good explanation. thanks
Great explanation.
Beautiful teaching
Thanks a ton!!!
My brain hurts.
Kind of understood it after watching many times
Isn't the time complexity N! * n? , checking if x in sol is n
or copying solution before you add it is n
bro what is if x not in sol, can you explain that I don't understand bro
If we've already used the number, we don't want to use it again
Excellent video!
How would the code change if there are duplicate numbers in the array?
Thank you! And there's actually a problem for that, might cover it at some point :)
its O(n! * n), because of the line: if x not in sol
Same for the space complexity. It should be O(n! * n)
We can actually use a Set instead of an array for "sol", then it'd be faster to check if a number is already in the current solution
good idea but i tried and the order of set is not preserved after insertion, at least for python wise
@@Techgether yes that's why use array for sol and then set for visited. Then check if num is in visited, not sol
thanks mate
Thanks
explanations are great but the variable namings not so great. ans and sol? both mean the same thing, it's unclear which is the final one. maybe currSubset or smth like this.
hell yeah