Permutations (LeetCode 46) | Full solution with backtracking examples | Interview | Study Algorithms

  Рет қаралды 44,530

Nikhil Lohia

Nikhil Lohia

Күн бұрын

To see more videos like this, you can buy me a coffee: www.buymeacoffee.com/studyalg...
A very famous backtracking problem. Given an array of unique elements, find all the possible permutations. This video shows how to identify the logic which creates all different permutations. Once we know how to find permutations, we can map it to the backtracking algorithmic paradigm. The concept is super simplified using diagrams and visuals of state space tree. You will also find a dry-run of the code in JAVA.
The actual problem on LeetCode: leetcode.com/problems/permuta...
Chapters:
00:00 - Intro
01:13 - Understanding the problem
02:51 - Logic to find permutations
06:43 - Using Backtracking to find permutations
11:52 - Dry-run of Code
15:36 - Final Thoughts
📚 Links to topics I talk about in the video:
Brute Force Paradigm: • Brute Force algorithms...
Backtracking: • Backtracking made easy...
Similar problem (Subsets): • Subsets (LeetCode 78) ...
Other problems on LeetCode: • Leetcode Solutions
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #backtracking #interview

Пікірлер: 73
@srin42
@srin42 Жыл бұрын
when I was college I could not understand it. Spent hours, days reading about never understood it over a decade of time. I was so much depressed when some people understood it and I was not able to understand. But today I understood. Man , you best talent to explain concepts with 1) with simple example 2) simple words 3) with actual core concept instead of making people to mug up steps. Your awesome. There are many people from around the world who lack enough money to get good quality of education. Please keep doing Algo questions and leetcode questions. I also salute your parents for raising such patient and cool attitude, talking. Thankyou
@arisahmed1105
@arisahmed1105 2 жыл бұрын
what simple easy explanation please don't stop making videos the way u explain its incredible TRUE LEGEND
@mrboyban
@mrboyban Жыл бұрын
Most of people that makes the videos on the KZfaq don't actually understand the fundamental of the subject, hence why it is poorly explained and makes everyone else confused as well. You're clearly know your stuff very well. Thanks for sharing your knowledge with us.
@rajeshpaithari7320
@rajeshpaithari7320 3 ай бұрын
I watched 10's of videos on backtracking. Though I understood the logic, I wasn't able to put that into code. Finally, I made it and related this logic to phone pad permutations, permutations 2, subsets. Thanks man!
@poorpanda9033
@poorpanda9033 10 ай бұрын
Thank you so much I spend 4 hours trying to find a good solution that explains with recursive tree. Found your video & I'm really happy... Thank you !
@abhipraychavan
@abhipraychavan 11 ай бұрын
Thank you for creating such an amazing KZfaq channel! Your explanation on solving LeetCode problem with backtracking is incredibly clear and understandable. I've learned a lot from your video, and I'll definitely be following your channel from now on to learn new approaches and algorithms whenever I encounter challenging problems. Keep up the fantastic work!
@nikoo28
@nikoo28 11 ай бұрын
thanks for the support. :)
@srikrishna_ss8263
@srikrishna_ss8263 2 жыл бұрын
Your Way Of Teaching Extreme Level And Simply Understandable
@AlexTopGT
@AlexTopGT 9 ай бұрын
one of the most clear and great explanation on this problem, thanks.
@TONY-ht5tf
@TONY-ht5tf Жыл бұрын
Excellent explanation, I finally understand backtracking. Thank you so much, sir!
@harshdhamecha5301
@harshdhamecha5301 Ай бұрын
I spent hours on this and ended up here. This motivates me to solve more problems of this type!
@ORagnar
@ORagnar Жыл бұрын
This was fantastic! The thing that clarified it for me almost immediately was the tree diagram you gave in the beginning. This allows you to visualize the problem and solution. Great job! 6, 14 -:
@nikoo28
@nikoo28 Жыл бұрын
Thanks for the lovely feedback.
@coderoad3581
@coderoad3581 5 ай бұрын
Really appreciate your efforts, man.
@mrboyban
@mrboyban Жыл бұрын
That was a masterclass. Many thanks!
@user-fg9uw9bm9l
@user-fg9uw9bm9l 8 ай бұрын
finally a video where approach is explained rather than some pre-written code. keep it up.
@nikoo28
@nikoo28 8 ай бұрын
i really believe in understanding the logic, rather than just solving it.. :)
@udayrajvadeghar8555
@udayrajvadeghar8555 5 ай бұрын
Thank you so much sir . A savior!!
@Aditya-wy4ci
@Aditya-wy4ci 5 ай бұрын
Wow sir, what a great code and an excellent explanation..
@bhargavijethva7958
@bhargavijethva7958 6 ай бұрын
Thank you so much sir, very good explanation, Please sir, make such kind of videos.
@Goodday-nm7zp
@Goodday-nm7zp 9 ай бұрын
I've been struggling to understand backtracking but this vid is 10/10 great explanation thanks so much!
@nikoo28
@nikoo28 8 ай бұрын
Great to hear!
@smtechsolutions3527
@smtechsolutions3527 11 ай бұрын
You made it very easy to understand
@pawankumarpandit1822
@pawankumarpandit1822 Жыл бұрын
love you bhaiya your amazing solution as well as amazing approach
@rahulratra6672
@rahulratra6672 Жыл бұрын
It was very well explained , thanks a lot :)
@user-kl2uu7lj4b
@user-kl2uu7lj4b 5 ай бұрын
Best explanation ever ! Better than neetcode and all as well!
@anshumansolanki6775
@anshumansolanki6775 10 ай бұрын
we do temp.remove(temp.size() - 1); because we remove the recently added number, so its going to be the last number in the temp array, for anyone wondering
@010101dddm
@010101dddm Жыл бұрын
Excellent explanation
@sakshisah4988
@sakshisah4988 5 күн бұрын
thank you soo much! u explained is so nicely ^_^
@alokvardhan
@alokvardhan 4 ай бұрын
best explanation out there
@MrShubhamenx
@MrShubhamenx Жыл бұрын
I dont agree on one point here when you say in comment " // Go back to try", at 13:35 this is misleading term "and go back" as anyone can think this is backtracking. When you remove the element, that is when actual backtracking takes place. Calling backtrack method recursively is not when backtracking happens. When you remove an element from a list, that is when you backtrack to previous path and then you wither add or call backtrack again to remove more elements to backtrack further up the path in the tree.
@manidixit4186
@manidixit4186 Жыл бұрын
Hi , you explained it very nicely. Just want to say that your text based explanation link is taking to the Longest common prefix problem and not this permutations one , so please correct it , if you can :)
@aditikaushik3334
@aditikaushik3334 3 ай бұрын
very very nice explanation
@SayheeKim
@SayheeKim 8 ай бұрын
u are a godsend
@shabazalam2374
@shabazalam2374 3 ай бұрын
Thanku bhai
@shrirambalaji2915
@shrirambalaji2915 Жыл бұрын
Thank you brother
@srin42
@srin42 Жыл бұрын
Good one
@lalanabiridi8358
@lalanabiridi8358 8 ай бұрын
simply the best
@abdalrahmanaldammad8866
@abdalrahmanaldammad8866 7 ай бұрын
You are the best
@sujithsaikalakonda4863
@sujithsaikalakonda4863 Жыл бұрын
Great explanation sir.
@nikoo28
@nikoo28 Жыл бұрын
Thanks and welcome
@rajendrabrahmbhatt2585
@rajendrabrahmbhatt2585 3 ай бұрын
I have a question, wouldn’t the time complexity of contains method be counted which is O(n)?
@nikoo28
@nikoo28 2 ай бұрын
With a hashset, the contains method works in O(1)
@saksham2331
@saksham2331 9 ай бұрын
please make full series on recursion , you are dammmmmm good bro
@nikoo28
@nikoo28 9 ай бұрын
gonna compile it very soon
@user-kl2uu7lj4b
@user-kl2uu7lj4b 5 ай бұрын
Just one thing backtrack method is really for going back and considering different choice OR it is taken care by pop() and then incrementing for loop further?
@nikoo28
@nikoo28 4 ай бұрын
backtrack method kind of helps you to explore the complete state tree. Watch my video on backtracking to understand more
@lalithamadhu2453
@lalithamadhu2453 6 ай бұрын
one question, how do one make sure that after selecting one element from array, the previous and next numbers are included? loop makes sure next numbers in array are included. how are previous numbers from selected array are included in backtracking?
@nikoo28
@nikoo28 6 ай бұрын
that is how recursion and backtracking work. when a function call ends, it will go back and resume where the recursive call started. Bactracking: kzfaq.info/get/bejne/a5eKrJSGscfZYac.html Recursion: kzfaq.info/get/bejne/fLqEe86e09zenIE.html
@hitesh8965
@hitesh8965 Жыл бұрын
Sir a little correction. You said it will add 1 then backtrack and remove 1 and add 2 and then repeat to add 3. but according to your code it will first add 1 and then find out all possible permutations with 1 and then remove 1. I tried dry running it bcz i got confused that how it is adding 1 and then removing it before finding all the possible combinations. Am i correct?
@pravat13panda
@pravat13panda Жыл бұрын
Yes, backtracking is DFS, it will first go deep on one node then will backtrack when all options are over and continue to next branch.
@LearnTravelTeachTrain
@LearnTravelTeachTrain 2 жыл бұрын
in last video of subsets i did not understand how i we got [1,3] please do write the recursive tree once
@nikoo28
@nikoo28 2 жыл бұрын
In subsets, if you watch 8:29...we get a state of choosing the 2nd element. Extend it for the 3rd element at 8:52
@chetannain4867
@chetannain4867 Жыл бұрын
Thanks!
@nikoo28
@nikoo28 Жыл бұрын
you are welcome!!
@Dineshkumar_dk
@Dineshkumar_dk 11 ай бұрын
I'm very weak in backtracking and dp, How can i become good in those things? what are the basic things to learn for becoming good in backtracking and dp?
@nikoo28
@nikoo28 11 ай бұрын
practice, practice, practice......and a lot lot of practice...there is no other shortcut my friend. :)
@sree-vn3cb
@sree-vn3cb 5 ай бұрын
please make video for "smallest good base --> leetcode =483"....🙏🙏🙏🙏
@nikoo28
@nikoo28 5 ай бұрын
that is not a good quality problem. watch this video on selecting problems: kzfaq.info/get/bejne/hsmfn92mp5PKgpc.html
@LearnTravelTeachTrain
@LearnTravelTeachTrain 2 жыл бұрын
rest all r good
@AkashkumarYadav-r4b
@AkashkumarYadav-r4b 3 күн бұрын
NIce
@pradeeps9633
@pradeeps9633 Жыл бұрын
Bro make videos to solve matrices problems plzz.
@nikoo28
@nikoo28 Жыл бұрын
I will add them to my pipeline. Do you have a particular problem in mind?
@pradeeps9633
@pradeeps9633 Жыл бұрын
i understood the question but dont no the code.plzzz exapin it broo😢😢
@nikoo28
@nikoo28 Жыл бұрын
@@pradeeps9633 which part are you facing a problem with? May be I can clarify even more.
@pradeeps9633
@pradeeps9633 Жыл бұрын
@@nikoo28 bro i have no idea to solve matrice type question.Every time i see it looks like a new one.
@curtdudeanmol9393
@curtdudeanmol9393 Жыл бұрын
volume was really very low
@nikoo28
@nikoo28 Жыл бұрын
Sorry for the inconvenience…I have fixed it in my recent videos. Please check them out and let me know if it is still an issue.
@vineetchaurasia7460
@vineetchaurasia7460 Жыл бұрын
Please fix your audio
@nikoo28
@nikoo28 Жыл бұрын
which part did you face a problem with?
@user-wj4og2gz9g
@user-wj4og2gz9g 2 ай бұрын
Unclear explanation, we are not going back every step. We simply use DFS alhoritm, which going back only when we hit the bottom of tree. So we going back when one leaf of permutations is constructed .
@sourabh258
@sourabh258 Жыл бұрын
tempList.contains(number) - This is O(n) operation
A clash of kindness and indifference #shorts
00:17
Fabiosa Best Lifehacks
Рет қаралды 106 МЛН
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 14 МЛН
WHAT’S THAT?
00:27
Natan por Aí
Рет қаралды 13 МЛН
100❤️
00:19
MY💝No War🤝
Рет қаралды 23 МЛН
Permutations - Leetcode 46 - Recursive Backtracking (Python)
9:42
Backtracking: Permutations - Leetcode 46 - Python
9:43
NeetCode
Рет қаралды 336 М.
Next Permutation - Intuition in Detail 🔥 | Brute to Optimal
28:15
take U forward
Рет қаралды 318 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 623 М.
How To Permute A String - Generate All Permutations Of A String
28:37
Back To Back SWE
Рет қаралды 109 М.
Next Permutation | Leetcode #31
19:12
Techdose
Рет қаралды 98 М.
A clash of kindness and indifference #shorts
00:17
Fabiosa Best Lifehacks
Рет қаралды 106 МЛН