Subsets (LeetCode 78) | Full solution with backtracking examples | Interview | Study Algorithms

  Рет қаралды 49,682

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 75
@peregrine17
@peregrine17 Жыл бұрын
Even though you explained the intuiton, but you didn't explain recursion part of the code with dry running it. You said just forget the recursion part.
@jackfrost8969
@jackfrost8969 8 ай бұрын
yeah he is trash. Never explains the code properly. If you try dry running the code on your own you will understand it though.
@nityamehrotra9436
@nityamehrotra9436 7 ай бұрын
yes he did not explain recursion part
@furryfriendfamily7550
@furryfriendfamily7550 5 ай бұрын
he said forget the recursion part for the time being, but instead he forgot😂
@ownagejuice1394
@ownagejuice1394 3 ай бұрын
this is what happens when you're being taught by a KZbin influencer lmfao, I bet he sucks on an impromptu algo exam
@kchemutai3483
@kchemutai3483 2 ай бұрын
Exactly, we need to understand how the recursion is building up the result because it is the tricky part for learners
@chia6964
@chia6964 6 ай бұрын
Rarely do I comment on videos, but I must in this case. This video is beyond excellent. It is a clear and detailed explanation that covers not just the code solution, but the thinking behind it. Well done, and thank you
@uday9132
@uday9132 Жыл бұрын
Nice bro!! The perfect path is followed to make sure that everyone can understand. Explaining the code will not fully help to understand recursion, dry run is the crux to completely get aware of the process. Thanks for the video!!
@nikoo28
@nikoo28 Жыл бұрын
glad you liked it.
@manojtate9604
@manojtate9604 7 ай бұрын
backtracking part top notch explanation !
@prashantsingh-w2u7b
@prashantsingh-w2u7b 10 ай бұрын
i am very lucky because i found this chennal thanks sir u are osm
@techLife-21
@techLife-21 7 ай бұрын
todays leetcode problem, so went to this channel
@eminentm1655
@eminentm1655 2 жыл бұрын
This was really helping Sir, thanks so much.
@BINARAI-q4r
@BINARAI-q4r Жыл бұрын
loved it!!Thankyou bhaiya
@debjanidas5786
@debjanidas5786 5 ай бұрын
finally understood after watching your video
@jritzeku
@jritzeku 11 ай бұрын
I feel that this explanation could be improved compared to all your other material. There is a big disconnect between the intuition and the code. I think you said 'forget about recursion' then never got back to explaining it. Based on the tree diagram, it leads us to believe that 15 func calls are made but in reality only 8 func. calls are made? I added a 'funcCallsCount' and incremented each time we enter a function. Suggestion: -I think that using a debugger along with this would have helped; especially, after we remove item from 'tempSet', what happens next. Perhaps that might seem like a lot but it will be much more beneficial. In the case of time, i think u can just explain the left subtree and that should be good enough.
@nikoo28
@nikoo28 10 ай бұрын
I understand the confusion...and I will try re-iterate over this code at some point in the future. Thanks for your feedback
@anusree8707
@anusree8707 6 ай бұрын
felt the same about the video
@satyamgupta6030
@satyamgupta6030 Жыл бұрын
thanks alot bhaiya very nice solution very easy to understand. Please clear my only doubt I have why ---> resultSet.add(new ArrayList(tempSet)) why this statement execute for {1,2,3} subset for the first time instead of storing { empty }, then {1} then {1,2} then finally {1,2,3} why that above statement got skipped when we call the backtrack( ) function with start value 0,1,2 and it got execute for the first time when the start value was 3 why is that so??? Please tell me where I'm making mistake in understanding this part it will be really helpful bhaiya if u can help me with that.
@raj_kundalia
@raj_kundalia 2 жыл бұрын
thanks for the amazing explanation!
@vishveshwarduraiswamy
@vishveshwarduraiswamy Жыл бұрын
How come sir the backtracking is calling that last line everytime even if the condition gets failed on the loop. I have faced this same confusion in the permutation problem as well. When the loop value ends how come the last line gets executed after that instead of breaking the entire loop. Actually if the recursive method is getting called on top of that last line the last line should not even get executed right even for once. Can you explain that part alone in detail.?
@nikoo28
@nikoo28 Жыл бұрын
it does not break. it resumes to the initial call where the recursion actually started.
@kolhesatish
@kolhesatish Жыл бұрын
Nice Explanation
@sumitmalpure944
@sumitmalpure944 21 күн бұрын
Can you please create playlist based on algorithm, explain technique and leetcode problems based on algorithm
@AmanAshutoshBCS
@AmanAshutoshBCS Жыл бұрын
Sir, you said to watch previous backtracking videos. Where is the link of previous videos. I don't even find any playlist for backtracking in your channel. Would you please help me in mastering the concept of backtracking???
@nikoo28
@nikoo28 Жыл бұрын
check the video description for more links. Here is the link to backtracking concept video: kzbin.info/www/bejne/a2K9qmSLgcaoZ9U
@010101dddm
@010101dddm Жыл бұрын
excellent explanation
@helloworld4788
@helloworld4788 Жыл бұрын
thanks brother for ur explanation
@bigjofa
@bigjofa Ай бұрын
Great explanation, thank you!!!
@kashafnasir1430
@kashafnasir1430 7 ай бұрын
Where is recursion part ?
@1murkeybadmayn
@1murkeybadmayn Жыл бұрын
Can never find someone who explains recursion well. Order of operations? If you read the loop the way it looks, it's like it does resultList = [[], [1], [1,2], [1.2.3]] and then in the last line, removes 3 to leave tempSet = [1,2]. Then the entire code stops because i = 3, condition breaks. So how are the other four subsets got? Also, how do you not end with two of [] in the result?
@nikoo28
@nikoo28 Жыл бұрын
the condition does not break, you return to the last point where the function was called. Visualizing recursion is always very tricky, it is hard to say but you need to get a feel of where the call would return. Watch my video on recursion based algorithm paradigms...that gives a little food for thought: kzbin.info/www/bejne/fIW3eZ6jo9utoq8
@spacelovertelugu44spacelov46
@spacelovertelugu44spacelov46 Жыл бұрын
For in recurssion I'm not able to understand it
@nikoo28
@nikoo28 Жыл бұрын
what part are you facing a problem with?
@pranithreddy
@pranithreddy 8 ай бұрын
Nice video sir
@Reddyshivasai789
@Reddyshivasai789 2 ай бұрын
above code is confusing little bit due to for loop during backtracing. Please see below code , simple to understand and easy to backtrack also good basecase. public class PowerSet { public static void main(String[] args) { int[] nums = {1, 2, 3}; List result = subsets(nums); System.out.println(result); } public static List subsets(int[] nums) { List result = new ArrayList(); generateSubsets(0, nums, new ArrayList(), result); return result; } private static void generateSubsets(int index, int[] nums, List current, List result) { if (index == nums.length) { result.add(new ArrayList(current)); return; } // Include the current element current.add(nums[index]); generateSubsets(index + 1, nums, current, result); // Exclude the current element current.remove(current.size() - 1); generateSubsets(index + 1, nums, current, result); } }
@parthanuj8611
@parthanuj8611 Жыл бұрын
yur voice is damn cool love you bro
@krishchiwhane2144
@krishchiwhane2144 Жыл бұрын
In the for loop, when we remove the number in the " // Case of not including number", why do we not call backtracking again? In order to append a subset to the resultSet, we must call backtracking. EDIT: I got the answer. The case of not including a number is already added to the resultSet from the resultSet.add(tempSet) that comes before the for-loop.
@nikoo28
@nikoo28 Жыл бұрын
glad you worked it out 😄
@manojnandhan8677
@manojnandhan8677 Жыл бұрын
Great Video thanks
@nikoo28
@nikoo28 Жыл бұрын
Glad you enjoyed it
@siddharthh9932
@siddharthh9932 Жыл бұрын
your explaination is very clear and understanble sir,Am so thankful for that.But I feel very difficult to apply recursion even after watching many youtube videos,I am still not able to understand how data is stored and returned in recursion.Could you please give some suggestion to learn sir?
@nikoo28
@nikoo28 Жыл бұрын
recursion is never easy to understand. Even I face problems with it. Only thing I can say is that the more problems you solve, the more patterns you will see, and that will help you. Also, every recursive problem can be solved iteratively ;)
@siddharthh9932
@siddharthh9932 Жыл бұрын
@@nikoo28 ok sir thank you
@akhilathatikonda319
@akhilathatikonda319 7 ай бұрын
i was not understood the recursive part clearly
@shravankalyankar9194
@shravankalyankar9194 2 жыл бұрын
really man your explaination seems good, but voice is so low, that I can't hear anything clearly.
@nikoo28
@nikoo28 2 жыл бұрын
thanks for the feedback. I have been improving on it. Can you try listening to my latest video and tell if that quality is ok?
@subee128
@subee128 Жыл бұрын
thank you
@Ramires2
@Ramires2 7 ай бұрын
Misunderstood the code! My previous comment was a bit unpolite and hateful! I hope, man, you're ok with such negative comments! thanks for the explanation. I spent couple of hours to resolve these backtracking algorithms. And this was emotional comment! And thanks for the positive reply. ps initial comment is updated
@nikoo28
@nikoo28 7 ай бұрын
Why do you feel the code does not work? It passes all the test cases on LeetCode. Did you have a look at the full code on Github? It is available in video description. Please let me know
@025-harshbindal7
@025-harshbindal7 7 ай бұрын
@@nikoo28 Bro ignore him and chill💫
@Ramires2
@Ramires2 7 ай бұрын
@@nikoo28 this is my fault! thanks for the reply! I must to admit that it works!!! Will remove comment!
@nikoo2805
@nikoo2805 7 ай бұрын
@@Ramires2no worries, always receptive of critical feedback 🙂 my best wishes with your coding journey
@spacelovertelugu44spacelov46
@spacelovertelugu44spacelov46 Жыл бұрын
Super
@satyaprakashjena1301
@satyaprakashjena1301 10 ай бұрын
bro u did not explain the recursive part its confusing please make another video
@nikoo28
@nikoo28 9 ай бұрын
i will make a general video on how to approach such problems. They all have a similar pattern.
@satyaprakashjena1301
@satyaprakashjena1301 9 ай бұрын
@@nikoo28 thank you 😊
@daruiraikage
@daruiraikage 2 жыл бұрын
Your code and diagram do not match. The diagram that you have shown, we will only add to the powerset at the last level. But in your code, you keep adding to your powerset at every level. According to your code, in your diagram, there should be 3 children of the root node. [ ] / l \ [1] [2] [3]
@nikoo28
@nikoo28 2 жыл бұрын
That is not the case, I do not add all 3 child nodes to the root in the beginning. If you look at the code again you add one number to the set and then call the backtrack method again.
@daruiraikage
@daruiraikage 2 жыл бұрын
@@nikoo28 Thanks for the quick reply, yes, that is what my point is. According to your code, you are adding ALL the nodes of euler tree to the powerSet. This means there will be duplicates in your powerset. See for example the first 3 levels: [ ] { [ ] } / \ [ 1 ] [ ] { [ ], [1], [ ] } /\ /\ [1,2] [1] [2] [ ] final pwrset = { [ ], [1], [ 1, 2 ], [1], [ ], [2], [ ] } At the last level, you wil have added 15 total subsets in your powerSet(from all levels) as opposed to the 8 subsets(only from the last level). You are confusing the recursive approach where you add to the powerset only at the LAST level once the current subset has been fully generated. vs. the backtracking approach where you add at EACH level(each node) and never repeat the same node once it has already been exhausted by deleting it. Please refer to this leetcode comment for the actual euler tree your code corresponds with: leetcode.com/problems/subsets/discuss/27281/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)/26331 Thanks for your time and discussion, let me know your thoughts EDIT: You could test it out by maintaining a counter in your method signature. Increment the counter each time you visit a node(recursion call) Using your code, counter would = 8 But in your diagram, counter will be = 15.
@nikoo28
@nikoo28 2 жыл бұрын
I am still failing to understand...in my diagram at 7:33, I pick up the element, and doing the same in code....after that in the diagram....I backtrack...and then chose not to pick the element at 7:37...the same is reflected in the code as well. So I am unable to understand why you say that the code and diagram do not match. I understand the count part...yes there will be duplicates in the final result set, but for this particular question it is fine.
@daruiraikage
@daruiraikage 2 жыл бұрын
@@nikoo28 You can not traverse breadth first in a recursive call stack. So your idea of selecting the left branch, then going to the right branch is wrong. You will go depth first, and at the leaf node, you will add it the currentSubset to the powerSet. As I said, in your code, you are only generating 2^n( =8 in this case) nodes in the tree, it is not even a binary tree like in your diagram. But it is a tree as shown in the diagram I have linked you. In your recursive diagram, it is a binary tree with total of 2^n * 2 - 1 nodes. (=15 in this case) According to your code, you are adding every node with resultSet.add() on EVERY recursive call, meaning you will add 15 subsets in your result set. To have a diagram and logic that you are using, your code should have been something like this(pardon the formatting and pseudoness, I'm on phone): powerSet(){ if( idx == nums.length ) { resultSet.add() return; } //selecting currentSubset.add(nums[idx]); powerSet( idx +1, nums, resultSet, currentSubset); //removing the added element for "backtracking" currentSubset.remove(size-1) //"backtracking" powerSet( idx +1, nums, resultSet, currentSubset); } }
@mohanramana282
@mohanramana282 Жыл бұрын
yes exactly that is what happening when i try running the code. it is printing all the nodes resulting in duplicate values.
@gokulakannan3664
@gokulakannan3664 Жыл бұрын
📌📌📌📌when the element enter into the for loop, it always enter into the recursion part... it never enter at the temp.remove part ......... can u plz explain, how this loop works..... in the video, you said ....now, don't see the recursion part , just see what happening in the remove part... but at last u can't explain it
@nikoo28
@nikoo28 Жыл бұрын
The temp.remove part will execute once the recursion has finished. There will be a case when you will recurse..and you will not enter the for loop. The value of “start” will be equal to nums.length. That is when a recursion ends and you will reach the remove part.
@gokulakannan3664
@gokulakannan3664 Жыл бұрын
@@nikoo28 Remove part is present inside the loop, when the loops fails, it will not enter into the loop... then how the elements access the remove part? ?? 🤔🤔🤔🤔🤔🤔
@nikoo2805
@nikoo2805 Жыл бұрын
@@gokulakannan3664 exactly…when it will not enter the for loop…this is when the previous recursion call ends…and then the remove statement from the previous call gets executed
@shravankalyankar9194
@shravankalyankar9194 2 жыл бұрын
your voice is so low, bro, could even hear you without headphones.
@aviraj5950
@aviraj5950 5 күн бұрын
neetcode solution is good
@mazharuddin3647
@mazharuddin3647 11 ай бұрын
Not efficient enough.
Subsets - Backtracking - Leetcode 78
8:47
NeetCode
Рет қаралды 305 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
Subsets - Leetcode 78 - Recursive Backtracking (Python)
11:51
Greg Hogg
Рет қаралды 12 М.
The N Queens Problem using Backtracking/Recursion - Explained
14:29
Back To Back SWE
Рет қаралды 139 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 777 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 758 М.