Unique Binary Search Trees - Leetcode 96 - Python Dynamic Programming

  Рет қаралды 74,547

NeetCode

NeetCode

Күн бұрын

Пікірлер: 69
@NeetCode
@NeetCode 4 жыл бұрын
Dynamic Programming Playlist: kzbin.info/www/bejne/bWTVZH6Nnqqpr80
@TrollFreeInternet
@TrollFreeInternet 2 жыл бұрын
I think there is a mistake with the last value at 7:45...it should be numTree[3]* numTree[0]..not numTree[3]*numTree[1]... it won't make difference to the answer but it might confuse some people.
@lizziedaffofil6064
@lizziedaffofil6064 3 жыл бұрын
No one can beat you in giving such a clear explanation even to complex topics! Way to go!
@chaoluncai4300
@chaoluncai4300 2 жыл бұрын
fr dawg, neetcode is the only person that drove me solve(watch) 10+ medium+ problems in one day
@08JuHan
@08JuHan 2 жыл бұрын
Thanks for posting it! Java version in case anyone's interested class Solution { public int numTrees(int n) { int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = 1; for (int nodeCount = 2; nodeCount
@aatishjain9770
@aatishjain9770 2 жыл бұрын
Had no idea how to approach this problem after reading the problem statement. Not sure when will get over this. As always best and savior for us. Thanks a ton bro.
@AlfranAli
@AlfranAli 4 жыл бұрын
For this particular problem, if you see the pattern it's following catalan number which can be solved in O(n) time & O(1) space, just mentioning it here for future readers to go and check that out too. Nice initiative, keep up the good work! :)
@RebornAc3
@RebornAc3 3 жыл бұрын
Very helpful, thank you!
@uncleroku8962
@uncleroku8962 4 жыл бұрын
Question: at 7:40 you said that when you get to the last value (the node being 4). You'd have one node in your right subtree. Wouldn't it be zero values in your subtree when 4 is the root, since all other nodes would be smaller?
@anthonysummit3098
@anthonysummit3098 3 жыл бұрын
Yeah
@chaoluncai4300
@chaoluncai4300 2 жыл бұрын
@@anthonysummit3098 yeh
@saijannali9725
@saijannali9725 Жыл бұрын
I was wondering the same thing, makes sense
@jonaskhanwald566
@jonaskhanwald566 Ай бұрын
Nice intuition, below is the recursive solution def numTrees(self, n: int) -> int: @cache def f(n): if n == 0:return 1 res = 0 for i in range(0,n): res += (f(i) * f(n-i-1) ) return res return f(n)
@mayankkhanna9644
@mayankkhanna9644 3 жыл бұрын
If I got this problem in an interview, I would cry. But not anymore..
@mehulsolanki9435
@mehulsolanki9435 2 жыл бұрын
My recursive solution with memoization: def numTrees(self, n: int) -> int: self.trees = {} self.trees[0] = 1 self.trees[1] = 1 self.trees[2] = 2 return self.getTrees(n) def getTrees(self, n): if n in self.trees: return self.trees[n] l,r = 0,n-1 res = 0 while r>=0: res += ( self.getTrees(l) * self.getTrees(r) ) l += 1 r -= 1 self.trees[n] = res return res
@andreytamelo1183
@andreytamelo1183 2 жыл бұрын
Thanks!
@chujunlu919
@chujunlu919 2 жыл бұрын
When you change variable i, j from Leetcode's solution to something descriptive, like this video does, suddenly the underlying thinking comes through, and the code makes sense.
@NeetCode
@NeetCode 2 жыл бұрын
Glad it was helpful!
@tripathi5174
@tripathi5174 4 жыл бұрын
was struglling for one day and then i found this helpful video , very well done
@JamesBond-mq7pd
@JamesBond-mq7pd Ай бұрын
thank you. best explanation on the internet
@apoorvbedmutha457
@apoorvbedmutha457 Жыл бұрын
The explainnation is so good ! you're an absolute legend
@tanaysingh5348
@tanaysingh5348 Жыл бұрын
crisp explanation
@siningsun4160
@siningsun4160 9 ай бұрын
The best explanation I've ever seen.
@pahehepaa4182
@pahehepaa4182 4 жыл бұрын
Thanks! This was sweet. Easy to understand. Other videos are complicating stuff 😶 Subscribed.
@nikhilgoyal007
@nikhilgoyal007 Жыл бұрын
thanks! for line #13 - I would have think - root should go from 0 to node +1 ( even if left is None for root = 0 - there our some combinations that will result from the right side ?). what am i missing pls ?
@nikhilgoyal007
@nikhilgoyal007 Жыл бұрын
Got it. 1: n + 1 is n nodes. if I had done 0: n, it would have been n nodes too (plus I would have to worry about left = root - 1 out of bound error.
@LaithBasilDotNet
@LaithBasilDotNet 3 жыл бұрын
Even though the top down dynamic programming approach works, leetcode timeout for it.
@arsahilar
@arsahilar 2 жыл бұрын
@7:44 fOR LAST VALUE shouldn't it be NODE[3] in left * and NODE[0] on right
@wlcheng
@wlcheng 3 жыл бұрын
Brilliant solution. Thank you!
@polyrain
@polyrain 3 жыл бұрын
Why did you n+1 in the memo table? Just to capture numTree[0]? Great vid!
@shuvbhowmickbestin
@shuvbhowmickbestin 3 ай бұрын
Do you type with your mouse? I hear the clicking sounds.
@atanunayak6637
@atanunayak6637 Жыл бұрын
I understand everything in the solution, except for the thing that why do we just multiply the values, I mean there can be other cases as well right? Let me elaborate, If you draw the diagram for 5 nodes solution, for f(5) we have f(3)*f(1) is good, but the three on the left also have 4C3 choices right?
@samuraijosh1595
@samuraijosh1595 Жыл бұрын
yeah the code based on the combinatorics seems to work but i dont get what the fuck is combinatorics doing here at all. i wonder if youve figured it out?
@samuraijosh1595
@samuraijosh1595 Жыл бұрын
ok now i got why we multiply them. firslty, a small correction for f (5) we have f(3) on the left and f(1) on the right subtree only for root node 4. {1, 2, 3} --- 4 --- {5 } now {1, 2, 3} can be arranged in how many unique trees is calculated by f(3) and so is {5} calculated. we get f(3) = 5, f(1) = 1. good. this is a simple case to explain where we have unique three things of one kind and 1 thing of another kind. so there's only three ways to arrange these two togetehr wohtout reptions such that they both get exist at the same time. if we take a case like f(3) on the left and f(2) on the right: we have three blue balls and 2 red balls. how would you arrange 3 blue balls with 2 red balls wihtout reptitions? 3 * 2 = 6 is the way to do it.
@symbol767
@symbol767 2 жыл бұрын
Crazy how they can give you these type of problems on an interview, I would have failed hard. But now I wont thanks to you
@Saurabh2816
@Saurabh2816 Жыл бұрын
DOUBT: line 6, numTrees[3]*numTree[1]. When we pick the last node as the root node we would have 3 values in the left subtree and 1 value in the right subtree. Why? Should we have 0 values in the right subtree? As we selected the last value as root then there will be no choices to make for the right subtree it would be just a null.
@calvincruzada1016
@calvincruzada1016 3 жыл бұрын
such a clear explanation, wowzers
@ameynaik2743
@ameynaik2743 3 жыл бұрын
Can you please make another video on unique bst ii I.e listing the possibilities?
@jhonrobaon1669
@jhonrobaon1669 2 жыл бұрын
I have a question at 4:13 (kzbin.info/www/bejne/hamThZikg5iNpsk), can you please let me know why we need to multiply for combinations?
@somdutroy
@somdutroy 2 жыл бұрын
Let's say the the left side the tree has 3 combinations makes as A, B, C and the right side has two combinations D, E. So, the possible trees are A-root-D, A-root-E, B-root-D, B-root-E, C-root-D, C-root-E. Hence, 6 combinations or 3*2 in this case.
@TheAlexanderEdwards
@TheAlexanderEdwards 2 жыл бұрын
@@somdutroy Thank you for this explanation!
@tanhnguyen2025
@tanhnguyen2025 Жыл бұрын
Are you using the Bottom-up approach for this problem? Because I think you had the two base value from which u can work your way up to adding those to n 😂
@patthiccc
@patthiccc 4 жыл бұрын
How do you decide on Memo dimensions? That is always where I go wrong. I'll try to use a 2d memo or 1d memo when the opposite is needed and I get nowhere.
@NeetCode
@NeetCode 4 жыл бұрын
That's a great question because figuring out the dimensions is usually the key in DP problems. Whenever i make a mistake in difficult problems I usually look for slightly easier problems in the same category. I solve these before repeating the difficult problem. Even with practice i still occassionally make mistakes, but I hope this helps!
@abhicasm9237
@abhicasm9237 Жыл бұрын
Hey @patthiccc, I hope that you would know better than me right now, but to add my 2 cents... What I have noticed from my practice is that the dimension of the dp depends on the number of variables that are changing. In cases like the knapsack where the index and the target changes, we use 2d array to store the index and the respective target. Hope it helps.
@lindama1276
@lindama1276 2 жыл бұрын
Why is num trees 0 equal 1?
@negarvahid
@negarvahid 2 жыл бұрын
Because we only have one option which is NULL.(or None)
@JosephJostar-j2x
@JosephJostar-j2x 2 жыл бұрын
excellent explaination
@johnj171
@johnj171 5 ай бұрын
I love youi i just love you man you are great at teaching
@Assta77
@Assta77 Жыл бұрын
But where are we ignoring the similar the structures like 2->1->3 and 3->1->2 have same structures. Aren't we considering both of them?
@KidUnicefalo
@KidUnicefalo 4 жыл бұрын
Thanks a lot, amazing explanation!
@mashab9129
@mashab9129 3 жыл бұрын
wow! as always the best explanation.
@tirupatirao7521
@tirupatirao7521 2 жыл бұрын
Great one
@modreamer
@modreamer 2 жыл бұрын
hmmm, I thought it was n. Calculate once (O(n)) for for recursion and the rest can be accessed in (O(1)) => n. Where am I wrong hmmmm?
@deidarasenpai3243
@deidarasenpai3243 Жыл бұрын
can someone explain how time Complexity is O(n^2) . I think it must be n!
@hayyan612
@hayyan612 Жыл бұрын
suppose n=3 so nodes would be 1,2,3 for us to return the final answer we need to find number of unique bsts we can make with all nodes (1,2,3) as root nodes for each root node we'll have to compute the left and right child from the nodes which makes it O(n^2)
@fefefefefee32
@fefefefefee32 2 жыл бұрын
I don't understand. Forgive me but this isn't so clear to me.
@arenmkhoyan
@arenmkhoyan Жыл бұрын
Recursion is bad and slow, we have to use dp
@pawankumarmeena6737
@pawankumarmeena6737 4 жыл бұрын
💯
@euchengc
@euchengc 2 жыл бұрын
There is a mistake at line 6 I think. Thanks for the solution nevertheless
@aviralarpan7350
@aviralarpan7350 2 жыл бұрын
I am best
@RobinHistoryMystery
@RobinHistoryMystery 11 ай бұрын
I think, Im just gonna be confused about this question, cant understand this question at all, watched so many videos about it sigh... time to move on
@RobinHistoryMystery
@RobinHistoryMystery 11 ай бұрын
im back, still confused, I hope I never cross path with this question in my life
@MishkatMazumder
@MishkatMazumder 7 ай бұрын
@@RobinHistoryMystery haha
@ms-ig8pq
@ms-ig8pq Күн бұрын
I wish you found happiness
@tlz124
@tlz124 4 ай бұрын
This guy is a genius but I'm not. I wish he did a side by side of the code and the explanation
@jay-rathod-01
@jay-rathod-01 4 жыл бұрын
This sounds like a pretty simple problem😐 If it were simple why would i be here.
@NeetCode
@NeetCode 4 жыл бұрын
That's true, the seemingly simple problems are always the most challenging
@CS_n00b
@CS_n00b 10 ай бұрын
*click* noice
Unique Binary Search Trees II - Leetcode 95 - Python
12:51
NeetCodeIO
Рет қаралды 21 М.
L33. Requirements needed to construct a Unique Binary Tree | Theory
8:41
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Spiral Matrix - Microsoft Interview Question - Leetcode 54
16:46
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 805 М.
Unique Binary Search Trees 1 || #DynamicProgramming
22:56
Code with Alisha
Рет қаралды 9 М.
Insert into a Binary Search Tree - Leetcode 701 - Python
9:48
NeetCodeIO
Рет қаралды 17 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 248 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 693 М.
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН