Unique Binary Search Trees II | Similar Problem | Recursion | Memoization | Amazon | Leetcode-95

  Рет қаралды 7,806

codestorywithMIK

codestorywithMIK

Жыл бұрын

****************Similar Qns :
Leetcode Link - leetcode.com/problems/all-pos...
KZbin Link - • All Possible Full Bina...
This is the 54th Video on our Dynamic Programming (DP) Playlist.
In this video we will try to solve a very famous DP Problem - Unique Binary Search Trees II (Leetcode-95).
We will solve it using already studied concept (similar qn video link shared above)
Trust me, this will no longer be a Medium Problem.
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : Unique Binary Search Trees II
Company Tags : Amazon
My solutions on Github : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/unique-...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Subscribe to my channel : / @codestorywithmik
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips
#interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook

Пікірлер: 127
@AbhishekGupta-if3rb
@AbhishekGupta-if3rb Жыл бұрын
Best explanation bhot video dekhi sabne bakwas explanation bataya hai...you are the best ❤.Thank you bro.Ab aaya smj m
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Sahi me yaar. Maine 2-3 videos dekhe subah se kyoki aaj late upload kara mik bhaiya ne. Kuch samajh nahi aaya. Abhi mik bhaiya ek video se ek hi Baar me sab clear, I paused the video at 7:32 and using the same concept as the similar Qn, I was able to write the entire code. Salute hai aise teaching skill ko
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
true man
@ugcwithaddi
@ugcwithaddi Жыл бұрын
True
@codestorywithMIK
@codestorywithMIK Жыл бұрын
It means a lot. Thank you so much ❤️❤️❤️
@AlishaKhan-ww3io
@AlishaKhan-ww3io Жыл бұрын
Now, I directly come to this channel and wait 😆
@thevagabond85yt
@thevagabond85yt Жыл бұрын
much more viewership in lesser time than Larry's YT/@Algorithmist .power of indian market...keep uploading
@codestorywithMIK
@codestorywithMIK Жыл бұрын
It means a lot. We Indians are 💪💪💪🇮🇳🪷 Thank you so much ❤️❤️❤️🙏🙏😇😇
@EB-ot8uu
@EB-ot8uu 4 ай бұрын
India produces best teachers of the world
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
This guys is one fire. He is simply the only guy who can make a Medium/Hard look like a cake walk. I wonder when I will be able to reach at this level
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Sabke videos dekha tha aur fir aapke dekhta tha just to compare. Now I only watch your videos. Simplest explanation Hota hai hamesha. Thanks as always
@sumitbaghel6203
@sumitbaghel6203 Жыл бұрын
Phele wali video dekhi thi pr acche se samaj nahi aaya tha pr iss video m samaj aa gaya. Thank you ❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I am so glad it helped 😇🙏
@poojabennabhaktula4883
@poojabennabhaktula4883 2 ай бұрын
Kudos to your explanation!!!
@EB-ot8uu
@EB-ot8uu 4 ай бұрын
insane level. Loved it
@user-xf3li1lk7l
@user-xf3li1lk7l Жыл бұрын
bhut pyara khela bhai tumne ...
@ganeshmula4508
@ganeshmula4508 11 ай бұрын
very good explanation sir,as others said no one didn't given a proper explanation in youtube,thank you sir🙏🙏🙏🙏
@AlishaKhan-ww3io
@AlishaKhan-ww3io Жыл бұрын
You always make things look so easy. You are the best tutor mik bhaiya
@tutuimam3381
@tutuimam3381 Жыл бұрын
Thanks a lot❤❤❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi everyone, Apologies for the delay in upload as you all know that I travel during weekends. 😇 Appreciate your patience . Java Code Below : class Solution { Map memo; public List allPossibleBST(int start, int end) { List res = new ArrayList(); if (start > end) { res.add(null); return res; } if (memo.containsKey(new Pair(start, end))) { return memo.get(new Pair(start, end)); } // Iterate through all values from start to end to construct left and right subtree recursively. for (int i = start; i
@ugcwithaddi
@ugcwithaddi Жыл бұрын
Dont apologise sir. You are the best. Please teach us also how to manage time like you 😥
@recessionriche
@recessionriche Жыл бұрын
Perfect explanation as always!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😇❤️
@chaitanya812
@chaitanya812 Жыл бұрын
Wasn't able to solve 😢 But your video made my day
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I am glad my video helped 😇❤️
@amarnathprajapati2543
@amarnathprajapati2543 Жыл бұрын
Best Explanation Sir This video is very helpful.🙏🙏
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😇😇
@146_shashanks4
@146_shashanks4 Жыл бұрын
your explanation is very neat and clean bro.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇🙏
@anuppatankar4294
@anuppatankar4294 Жыл бұрын
Great Video 👌🏻
@Thriftinghai
@Thriftinghai Жыл бұрын
Noone teaches like you man. I agree with the Pinned comment. You have the magic of making things simple.
@anantsingh3079
@anantsingh3079 5 ай бұрын
thank you 😊
@ShubhamKumarSingh6224
@ShubhamKumarSingh6224 Жыл бұрын
bhai kam views wegaira ki wajah se rukna mat bohto achha padha rhe ho aap
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you shubham. It means a lot. ❤️❤️😇😇
@humanity7880
@humanity7880 11 ай бұрын
Amazing explanation sir!!! Much much appreciated please don't stop making these videos one day this channel will hit 1 million soon❤
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Means a lot 😇❤️🙏 Hope so it reaches more people ❤️❤️
@herculean6748
@herculean6748 Жыл бұрын
very well explained!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😇
@sachinmohanty4577
@sachinmohanty4577 Жыл бұрын
🎉🎉✨✨❤️ marking attendance ... MIK can you make of beautiful arrangement 2 question as you said you will try to make it on weekend???
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hey Sachin, apologies. Actually i am travelling this weekend. Will soon try to cover that ❤️❤️❤️😇
@Prakash_8873
@Prakash_8873 Жыл бұрын
As usual best explanation. Kabhi kabhi question ka level ka credit bhi App de diya karo😂.Just kidding..... . Literally I feel Todays POTD is little bit tough question .But understand the depth of DP with binary tree concept.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
😁 sure Thank you Prakash 😇❤️
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Good explanation please make video on tabulation approach
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure thing. Will cover in DP concepts soon.
@top_g755
@top_g755 11 ай бұрын
Thankyou so much sir❤ may allah give u everything u want
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Means a lot 😇❤️
@top_g755
@top_g755 11 ай бұрын
@@codestorywithMIK Sir iss question mei aap java ke code mei arraylist function ke andar hi define krr rhe h to recursion jb hoga to baar Barr new arraylist bn jayegi n to purani values kase milegi.... Kya hum jaise mal ko globally banaye h wase list ko nhi bna skte
@top_g755
@top_g755 11 ай бұрын
Sir iss question mei aap java ke code mei arraylist function ke andar hi define krr rhe h to recursion jb hoga to baar Barr new arraylist bn jayegi n to purani values kase milegi.... Kya hum jaise mal ko globally banaye h wase list ko nhi bna skte
@Piyushraj0
@Piyushraj0 Жыл бұрын
Amazing ❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😇
@chaitanya812
@chaitanya812 Жыл бұрын
what would be the time complexity after memoise ?
@kishan.17
@kishan.17 Жыл бұрын
Thanks soo much bro.pls make a video on Pointers bro pls ❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇🙏 Can you elaborate what exactly on pointers you want the video ?
@prashantvishwakarma6100
@prashantvishwakarma6100 Жыл бұрын
I am watching you solutions from last 10 days continuously you explain everything best way i also understand it but i am not able to do it by myself... Without looking at code what i should do ? ...Please Help
@ss8273
@ss8273 11 ай бұрын
bhaiya op++
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
❤️🙏
@soumyadipmajumdar4306
@soumyadipmajumdar4306 7 ай бұрын
using a pair of integers as a key in the unordered_map, and by default, unordered_map does not have a hash function defined for pairs..Need to define our own hash
@phoddaal7130
@phoddaal7130 Жыл бұрын
Sir, why it is happening that instead of map, when i used unordered_map. the code stops working. ?? it gave these errors, Line 63: Char 31: error: call to implicitly-deleted default constructor of 'Solution' vector ret = Solution().generateTrees(param_1); return ret; Line 14: Char 54: note: default constructor of 'Solution' is implicitly deleted because field 'mp' has a deleted default constructor unordered_map mp;
@codestorywithMIK
@codestorywithMIK Жыл бұрын
C++ doesn’t allow using pair inside unordered map. Hope that helps 😇❤️
@phoddaal7130
@phoddaal7130 Жыл бұрын
@@codestorywithMIK ohhh, got it, Thank u sir
@phoddaal7130
@phoddaal7130 Жыл бұрын
@@codestorywithMIK Sir can you please tell that, is Competitive Programming Necesaary to crack Big Product Based companies or, DSA and Leetcoding can get me that. Please Tell
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Actually competitive programming can help in giving you a boost and confidence of not missing any OA qn. Because OA qns require deep understanding of any Data Structure . But, it’s not mandatory to crack product based companies. Me and many of my mates never did CP. But I believe that today due to excessive competition, sometimes OAs are tough and of CP level but it’s not always the case. So, it’s not mandatory but it’s a good to have if you have enough time.
@ashutoshpandey1639
@ashutoshpandey1639 Жыл бұрын
Bhaiya can you explain why we initialize result vector of TreeNode* inside our solve function , because if we make it inside it will just create a new result vector every time when solve function called , if i initialize it globally i gets error
@codestorywithMIK
@codestorywithMIK Жыл бұрын
(copied from a comment by @mosereview) - “Every recursive call returns list of roots of different BSTs possible from subtrees. So every recursive call must have different result variable. Keeping it global will keep updating the same result vector in recursive calls because global variables have only one copy.” Above is a good explanation why we keep local result for every recursive call. Hope that helps. 😇❤️
@ashutoshpandey1639
@ashutoshpandey1639 Жыл бұрын
@@codestorywithMIK Cleared bhaiya . thanks for reply❤
@sauravchandra10
@sauravchandra10 Жыл бұрын
Mujhe laga aaj video nahi ayegi, toh atleast 3 4 videos dekh ke samaj aaya ache se
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I hope my video helped. Apologies for delay, as I usually travel during weekends 😇🙏
@sauravchandra10
@sauravchandra10 Жыл бұрын
@@codestorywithMIK Aapki videos se kisko nhi samaj ayga 😁
@codestorywithMIK
@codestorywithMIK Жыл бұрын
😇😇🙏🙏
@sauravbiswajit8091
@sauravbiswajit8091 Жыл бұрын
Thanks a lot
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️❤️
@sauravbiswajit8091
@sauravbiswajit8091 Жыл бұрын
Is it okay to not able to crack this problem although I have solved full binary tree walA PROBLEM , like I feel ki mere se nahin hoga
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes Saurav, that’s totally fine. Don’t worry. But make sure you understand that the code structure of both problems were similar. So basically this is a pattern and there are similar other qns out there falling under such pattern. I know it’s not possible to remember all pattern. So the best way is to treat every problem as new and start solving it from scratch.
@sauravbiswajit8091
@sauravbiswajit8091 Жыл бұрын
@@codestorywithMIK yeah sure thanks sirrr
@SohelKhan-vt5ql
@SohelKhan-vt5ql Жыл бұрын
what took you so long. Been waiting for ages
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Apologies for delay Sohel. Actually i usually travel during weekends. That’s why the delay. I hope my video helped 😇🙏
@simardeepsinghmudhar7065
@simardeepsinghmudhar7065 Жыл бұрын
Great Explanation and thanx for telling about the catalan number as well.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Glad it was helpful! 😇🙏
@Rajdweep
@Rajdweep Жыл бұрын
very hard qs bro
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Indeed. For first time its tough. However if you notice the similar Qn in the Description, i had solved it before, so this qn seemed familiar
@Rajdweep
@Rajdweep Жыл бұрын
@@codestorywithMIK yaa saw that video,getting a pattern,bt have to say the pattern is very rare i have never seen this kinda qs before
@sourabhsinghbhadouriya9711
@sourabhsinghbhadouriya9711 Жыл бұрын
Bhai please also discuss the leetcode contest problems
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I will soon start that too ❤️
@prashantgupta9502
@prashantgupta9502 Жыл бұрын
Please make videos on SQL also only you teach the best way
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you Prashant. It’s first time someone has asked me for SQL. I will definitely plan SQL playlist too ❤️😇🙏
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
thanks for the video. agr algo ko dry kar detay tou aur bhi acha hota 😄
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Apologies to have missed that. Actually i travel during weekends, that’s why couldn’t get much time. But for sure I will keep this in mind from next video ❤️❤️❤️
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
@@codestorywithMIK no problem and please enjoy your weekends. it was a good practice for me. the tree got very big very soon 😂
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️😇
@ayushkhandelwal7485
@ayushkhandelwal7485 Жыл бұрын
Bro why does it give error when I use unordered_map for memoization instead of map?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Because C++ doesn’t support pair inside unordered_map. But if you want, you will have to write your own custom hash function for your map. That’s why, it’s better to choose ordered_map
@ayushkhandelwal7485
@ayushkhandelwal7485 11 ай бұрын
Got it! Thanks sir@@codestorywithMIK
@amodsings9321
@amodsings9321 Жыл бұрын
Title dena bhul gye kya bhaiya?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Just updated Thank you 😇
@RAHULSHARMA-dl1jc
@RAHULSHARMA-dl1jc Жыл бұрын
bhaiya when i=2 means pointing at 2 ,(leftbst=1,n,2,rightbst=n) then how it supposed to store the elements for(auto lefttree:leftbst){ for(auto righttree: rightbst) { TreeNode* root=new TreeNode(i); root->left=lefttree; root->right=righttree; result.push_back(root); } } please explain this bhaiya
@wakeuppeggy
@wakeuppeggy Жыл бұрын
#augustchallengewithmik Love you bhaiya
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Let’s keep it going 🔥🔥💪 My full support, love and respect to you guys 😇
@Yogesh-ei1lg
@Yogesh-ei1lg Жыл бұрын
Bhaiya aap konsi company mai ho ?
@amanmathur8602
@amanmathur8602 Жыл бұрын
plaase make a solution video on leetcode 932
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure. noted
@107_faizanalam2
@107_faizanalam2 Жыл бұрын
Sir,I have a question regarding memoization.. Since You have used Map to memoize it.But when we have to use map to memoize it and in which type of question we have to memoize with map instead of simple dp arrays. If you will answer i will be very grateful to you Thanks..
@ugcwithaddi
@ugcwithaddi Жыл бұрын
Every recursive call returns list of roots of different BSTs possible from subtrees. So every recursive call must have different result variable. Keeping it global will keep updating the same result vector in recursive calls because global variables have only one copy.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
It totally depends on the qn. When you have to store a vector or complex result in dp then choose a map as it will be difficult to maintain that in an array. But if you have to store only a value for a given state, then choose dp array
@107_faizanalam2
@107_faizanalam2 Жыл бұрын
@@codestorywithMIK Got the point. Thanks a lot sir ☺️
@utkarshsrivastava5003
@utkarshsrivastava5003 Жыл бұрын
bhai subah se wait kar rahe the yaar
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Apologies for delay. Actually i usually travel during weekends. That’s why the delay. I hope my video helped 😇🙏
@Saryupareen
@Saryupareen Жыл бұрын
can we use unordered map here?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Actually, C++ doesn’t allow using pair inside unordered map. So it won’t work. Hope that helps 😇❤️
@Saryupareen
@Saryupareen Жыл бұрын
@@codestorywithMIK got it
@aman3210
@aman3210 Жыл бұрын
Thanks+4;
@codestorywithMIK
@codestorywithMIK Жыл бұрын
😇❤️
@user-ur9zu1xn5t
@user-ur9zu1xn5t Жыл бұрын
please share notes also
@adarshdhital007
@adarshdhital007 Жыл бұрын
Eat, Code,Repeat
@codeandtalk6
@codeandtalk6 Жыл бұрын
Solved it on own ❤❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I am the happiest when i hear this 😇🙏
@MayankSinghNegi24
@MayankSinghNegi24 Жыл бұрын
Leetcode 2808 please
@AbhishekGupta-if3rb
@AbhishekGupta-if3rb Жыл бұрын
Description gayab hai bro
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Updated 😇
@jawwadumar2100
@jawwadumar2100 Жыл бұрын
isn't nth Catalan no : (2n)! / ((n + 1)! * n!)
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Actually there is an alternative expression for nth catalan number , which you can see here at the wikipedia - en.m.wikipedia.org/wiki/Catalan_number
@jawwadumar2100
@jawwadumar2100 Жыл бұрын
@@codestorywithMIK yeah okay .. thanks man
@honey6567
@honey6567 11 ай бұрын
96. Unique Binary Search Trees From Catalan series class Solution { public: int dp[20]; //catlone number prateek sir write in copy int count(int n ){ if(n==0)return 1; if(dp[n]!=-1)return dp[n]; int ans=0; for(int root=1;root
@rishabhprasad8638
@rishabhprasad8638 Жыл бұрын
what's wrong with this code?? class Solution { List result ; public List generateTrees(int n) { result = new ArrayList(); return solve(1, n); } public List solve(int start, int end){ if(start > end){ result.add(null); return result; } if(start == end){ result.add(new TreeNode(start)); return result; } for(int i=start;i
@Thriftinghai
@Thriftinghai Жыл бұрын
Every recursive call will bring new list of roots of subtree BST. You have used a global result and you are updating same result for every recursive call. Slight modification of your code fixed the issue - class Solution { public List generateTrees(int n) { return solve(1, n); } public List solve(int start, int end){ List result = new ArrayList(); if(start > end){ result.add(null); return result; } if(start == end){ result.add(new TreeNode(start)); return result; } for(int i=start;i
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 40 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 11 МЛН
Secret Experiment Toothpaste Pt.4 😱 #shorts
00:35
Mr DegrEE
Рет қаралды 38 МЛН
Unique Binary Search Trees II - Leetcode 95 - Python
12:51
NeetCodeIO
Рет қаралды 16 М.
Lecture 127: Unique Binary Search Trees || Catalan Number
20:27
CodeHelp - by Babbar
Рет қаралды 41 М.
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 40 МЛН