20 Longest common subsequence Memoization

  Рет қаралды 190,537

Aditya Verma

Aditya Verma

Күн бұрын

Longest Common Subsequence Problem solution using Memoization
Given two sequences, find the length of longest subsequence present in both of them.
A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous.
For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg”.
PROBLEM STATEMENT LINK:www.geeksforge....
Playlist Link: • Dynamic Programming | ... .
------------------------------------------------------------------------------------------
Here are some of the gears that I use almost everyday:
🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
👨🏻‍💻 : My Apple Macbook pro: amzn.to/3w8iZh6
💻 : My gaming laptop: amzn.to/3yjcn23
📱 : My Ipad: amzn.to/39yEMGS
✏️ : My Apple Pencil: amzn.to/3kMnKYf
🎧 : My Headphones: amzn.to/3kMOzM7
💺 : My Chair: amzn.to/385weqR
🛋 : My Table: amzn.to/3kMohtd
⏰ : My Clock: amzn.to/3slFUV3
🙋🏻‍♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.

Пікірлер: 327
@rashmikiranpandit8962
@rashmikiranpandit8962 4 жыл бұрын
Anyone who is getting confused with Top-down and bottom up, Recursion memoization is always TOP-DOWN(and not bottom up), as we take a bigger problem and recusively solve for the smaller subproblems. Whereas in tabular DP where we start filling the table from top left to bottom right is actually BOTTOM-UP because we compute dp values of smaller subproblems first and then using these values compute dp value of bigger problems. PS: Top down and Bottom up is decided by the essence of methodology and not by whether we are filling table from top to bottom or vice versa!
@tanmayshishodia1507
@tanmayshishodia1507 4 жыл бұрын
Yuppp you are right, he got confused between the two terms and in the whole series he called memoization as bottom-up and tabulation as top-down lol
@kakadiazeel
@kakadiazeel 3 жыл бұрын
Beautiful explanation
@anilsinghnegi1481
@anilsinghnegi1481 3 жыл бұрын
Thanks buddy!!
@tekbssync5727
@tekbssync5727 3 жыл бұрын
@@tanmayshishodia1507 Name doesn't matter bhai , approches samajh me aa gyi naa defination to khi se bhi padh skte ho
@MrRavi20khatri
@MrRavi20khatri 3 жыл бұрын
you are correct, I was looking for this comment.
@palakmanocha6509
@palakmanocha6509 4 жыл бұрын
You are legend! I could do it myself because u explained it so well in knapsack videos! Thanks a ton!
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Thanks palak !! Keep watching and Sharing !!
@abhishekmore5856
@abhishekmore5856 4 жыл бұрын
same here
@akshatw7866
@akshatw7866 3 жыл бұрын
+1
@suchitragiri4546
@suchitragiri4546 3 жыл бұрын
Yes me too... I also solved bottom up as well as top down just after understood the logic behind it!! that's I was looking for..:)
@saumitrashukla8969
@saumitrashukla8969 3 жыл бұрын
Exactly, First time I had to skip to the code part in his videos!!!
@vivekanandapradhan9067
@vivekanandapradhan9067 4 жыл бұрын
Aap toh prepbytes, coding blocks, coding ninja, etc ki business kharap kr doge... Thanks😍
@aakashyadav6228
@aakashyadav6228 3 жыл бұрын
prepbytes don't need others for that !!
@anandsharma5850
@anandsharma5850 3 жыл бұрын
@@aakashyadav6228 lol
@ashu3128
@ashu3128 Жыл бұрын
@@aakashyadav6228 why?
@0anant0
@0anant0 4 жыл бұрын
20 of 50 (40%) done! Starting from 4:05 for the next couple of minutes is a gem on recursion!
@Unknown-Stranger
@Unknown-Stranger 3 жыл бұрын
love ur dedication
@arnabmukherjee5840
@arnabmukherjee5840 2 жыл бұрын
following you bro..
@harshagarwal7552
@harshagarwal7552 4 жыл бұрын
Best playlist for dynamic programming. Hope reaches to every student. Thanks Aditya..!
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Thanks brother, Do subscribe and share, that keeps me motivated to do more !!
@sairamakurti4808
@sairamakurti4808 3 жыл бұрын
If you get TLE for memoization then pass arguments by referrence
@BEEMohdArbazAli
@BEEMohdArbazAli 3 жыл бұрын
thanks bro.
@sanjayvasnani988
@sanjayvasnani988 3 жыл бұрын
Wow. Thanks dude. I was worried that something is wrong in my code.
@prernagupta243
@prernagupta243 3 жыл бұрын
Thanks dude!!! Was so confused why my code was giving TLE.
@ashishkatyal
@ashishkatyal 2 жыл бұрын
thanks :)
@amandeepnokhwal2977
@amandeepnokhwal2977 2 жыл бұрын
Duniya me abhi bhi ache log baaki hai 😭😭
@adityakhandelwal7925
@adityakhandelwal7925 3 жыл бұрын
I studied your knapsack videos so dedicatedly that as you were explaining longest substring problem, I was able to visualize the choice diagram by myself and the recursion code. Once you wrote the recursion code, I immediately went to geeksforgeeks and tried to solve the problem using top down DP and bang on!!! It successfully passed in 1st attempt. Thanks a Lot :)
@aditisaxena9097
@aditisaxena9097 Жыл бұрын
Same here :)
@shivamdwivedi3534
@shivamdwivedi3534 Жыл бұрын
same here
@VishalGupta-uu5eb
@VishalGupta-uu5eb Ай бұрын
same here :)
@adarshverma013
@adarshverma013 4 жыл бұрын
you removed the fear of dp i had from me, i did the tabulation while watching memoization video, really awesome work sir!!!!!! Thanks a Lot
@adityajoshi3922
@adityajoshi3922 3 жыл бұрын
same
@nitinbaghel1073
@nitinbaghel1073 3 жыл бұрын
Aditya Verma's DP playlist with 2X speed........It's way satisfying than anything!! ❤️❤️
@nightmare_9
@nightmare_9 4 жыл бұрын
really worthy watching ur tutorials.. pls make more tutorials on algorithms ... thank you
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Thank you, I will
@TheVijaySaravana
@TheVijaySaravana 4 жыл бұрын
Bhai, youtube pe saare videos dekha iss topic mein. Koi bhi itna clear se explain na kiya! Iss topic ke upar aapka jo clarity hai vo isme dikha raha hai.. Hats off!
@piyushsrivastava5824
@piyushsrivastava5824 4 жыл бұрын
Hi, your videos are good. One thing, the 2-D array approach where we start building from base cases are bottom-up (which you call Top-down). Top-down is when you start from problem and keep breaking in smaller ones i.e. recursive plus memoization for optimization.
@rishabhyadav8269
@rishabhyadav8269 4 жыл бұрын
yesss
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Yes, you are right, I actually has mentioned that in the pinned comment of the very first video.
@piyushsrivastava5824
@piyushsrivastava5824 4 жыл бұрын
@@TheAdityaVerma apologies haven't seen that. Thanks for your great videos. Keep them coming.
@aptitudepointiit-bhu5358
@aptitudepointiit-bhu5358 2 жыл бұрын
For those wondering, how to store -1 in the DP array in platforms like gfg, where the main function is not accessible to us ?? I also had the same confusion, but finally came up with this: We can declare the dp[1001][1001] and a boolean flag, globally, i.e., outside all functions and inside the solution Class. Now, initially the flag stores true value, and we will use this in the LCS function like: if(flag==true) { memset(dp, -1, sizeof(dp)); flag = false; } This flag = false ensures that memset is only applied once, so that it can set the dp array with -1 value only once, as we did, using the main function, thus we successfully memoize the code in platforms like GFG. FULL CODE: int dp[1001][1001]; bool flag = true; public: //Function to find the length of longest common subsequence in two strings. int lcs(int x, int y, string s1, string s2) { if(flag) {memset(dp, -1, sizeof(dp)); flag = false;} if(x==0 || y==0) return 0; if(dp[x][y] != -1) return dp[x][y]; if(s1[x-1]==s2[y-1]) return dp[x][y] = (1 + lcs(x-1, y-1, s1, s2)); else { return dp[x][y] = max(lcs(x-1, y, s1, s2), lcs(x, y-1, s1, s2)); } } ALSO, REMEMBER ONE THING, IF YOU ARE PASSING A 2D ARRAY IN FUNCTION, SPECIFY THE BOUNDS OF THE SECOND VARIABLE WHILE ACCEPTING IT. EX: int func(string s1, int dp[][1001]) is the correct way to accept 2D array in any function, whereas, int func(string s1, int dp) is incorrect.
@jenilshah2231
@jenilshah2231 Ай бұрын
you could have just declared a vector of vector with -1 as default value it doesn't need the memset or anything.
@manasikasande9400
@manasikasande9400 4 жыл бұрын
Knapsack explanation was so great that I could write this code myself !
@ankurgupta4696
@ankurgupta4696 4 жыл бұрын
i already did memoization and top down dp coz i have watched all your knapsack still dont wanna miss your explanations so watching the video
@mansigoyal4796
@mansigoyal4796 3 жыл бұрын
You are the only one on youtube who made recursion , DP easy for students.....
@Phoenix-pk7uc
@Phoenix-pk7uc 5 ай бұрын
Others teach table filling and you teach DP. From the 8th video I could solve all the problems own my own because of your teaching process. Thank you for making this tutorial.
@SarangShekokar
@SarangShekokar 4 жыл бұрын
hands down the best playlist for dynamic programming
@Weeniekri5773
@Weeniekri5773 16 күн бұрын
discovered his channel recently, he is so underrated. Also i love how he concludes every video with the little summary.
@Anjalikumari-sg1rs
@Anjalikumari-sg1rs 4 жыл бұрын
Best Playlist of dp. You teach in such a way that I could really build up my concepts of dp. You just removed my fear of dp.Looking forward for more such Playlist on backtracking and graph. Please upload videos on these topics also. It would be a great help as placements are starting.
@rajgothi2633
@rajgothi2633 4 жыл бұрын
Ab pata chala ki kese recursion se memories and top down approach find karte hee... Great explanation bro
@algorithmicthinker
@algorithmicthinker 3 жыл бұрын
Hi Aditya, just a small thing that is revolving in my brain 🧠 that whether memorization is called bottom up DP or top down DP as we start solving our problem from top most state i.e. dp[m][n] and we reach till base state. Please do throw some light on it. Anyways love your work ❤️.
@racistindian5316
@racistindian5316 3 жыл бұрын
Guys if someone is getting TLE in gfg by subitting this code in c++ then pass the string by referance so that your code will not create copy of the string. Here is the code in case you need help: int helper(int x,int y,string &s1,string &s2){ if(x==0 || y==0) return 0; if(dp[x][y]!=-1) return dp[x][y]; if(s1[x-1]==s2[y-1]) return dp[x][y] = 1 + helper(x-1,y-1,s1,s2); else{ int op1 = helper(x-1,y,s1,s2); int op2 = helper(x,y-1,s1,s2); return dp[x][y] = max(op1,op2); } }
@shivanshyadu4830
@shivanshyadu4830 2 жыл бұрын
Thankyou so much bro it helped❤
@racistindian5316
@racistindian5316 2 жыл бұрын
@@shivanshyadu4830 your welcome
@VikashKumar-uo6bx
@VikashKumar-uo6bx 3 жыл бұрын
I got tle using memoization approach but it works in top down approach🙂🙂
@darshangupta3804
@darshangupta3804 3 жыл бұрын
Just pass the strings by reference
@kshitijbodhankar3668
@kshitijbodhankar3668 3 жыл бұрын
@@darshangupta3804 thanks bro. it worked!!!
@harshalshridharkallurkar4555
@harshalshridharkallurkar4555 2 жыл бұрын
@@darshangupta3804 any particular reason as to why passing strings by reference works in this case?
@shubhammahindru3563
@shubhammahindru3563 Жыл бұрын
Apart from DP explanations pen rotation is awesome.😄
@adityabhosale8461
@adityabhosale8461 Жыл бұрын
No one else teaches DP better than you. Best wishes !
@vbp123
@vbp123 Жыл бұрын
You are wrong, striver taught better than him
@rishabsinha8749
@rishabsinha8749 4 жыл бұрын
We can initialise 2d array by double pointer also like int** dp = new int*[n+1]; for(int i=0;i
@yashbhanushali8456
@yashbhanushali8456 3 жыл бұрын
Lekin Kyu. Pointer agar ap lerahe ho, to memory apko free bhi karni padegi. It's c++ and not java garbage collection ni hai yaha. Why to end up in such a big mess.
@praphulyadav4471
@praphulyadav4471 3 жыл бұрын
sir your way of teaching is so great thanks alot for making dp so easy
@bipul2138
@bipul2138 4 жыл бұрын
Bhai badiya video ek baar fir Bas ek chiz, memset use krnge initialize krne ke liye to bas -1 ya 0 kr skte h saare elements ko Aur kisi chiz se initialize krenge to garbage value show krega matrix Better to use 2-D vector Initialization : vector < vector > dp(n+1, vector (m+1, -1) ); Thanks a lot
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Yeah ofc you have only use 0, -1, true , false with memset. And we will always be initializing it with -1 to check if the value if stored or not. So I think we are good. Thanks for watching !!
@gauravagarwal8592
@gauravagarwal8592 3 жыл бұрын
Man that this is so simple u make it I read multiple times videos , nothing worked first time i am happy .YOU are the BEST BUDDY. People should call you champion. Now i am more greedy I understand graphs with algorithm but still want you to come up for that.
@gauravagarwal8592
@gauravagarwal8592 3 жыл бұрын
I understnad multiple approach , Now i would say if i complete all your videos i will crack somewhere I always rejected at this datastructure.
@saurabhsen3560
@saurabhsen3560 4 жыл бұрын
The way you teach is awesome!!!. You really have made DP easy. Thank you for making such a wonderful video. Can you make a video on LIS ...
@sukantaghosh3237
@sukantaghosh3237 4 жыл бұрын
Really great explanation. But Bottom up approach is not memorization technique, it's Tabulation. It created confusion.
@piyushsarraf4814
@piyushsarraf4814 3 жыл бұрын
Do every memoized recursion have same time complexity as compared to table form ? @ Aditya Verma And a small correction table will t[m+1][n+1] not the opposite.
@lifehustlers164
@lifehustlers164 Жыл бұрын
Well Aditya Sir , On Leetcode other than Scrambled String ,its memoization code also failled to accept (Giving Run time error on test case 46/47), So scrambled String and Longest common Subsequence codes gives stack overflow error in memoization. Till now....,BTW AWESOMMEEE VIDEOS!!!
@hqz9380
@hqz9380 2 жыл бұрын
ur knapsack video was too good, which lead to make this problems easier to understand. Thanks.
@anuraga3018
@anuraga3018 2 жыл бұрын
Really nice explaination. @aditya Verma I have followed this and wrote this program. Looks like some issue with memoization concept which i understand from your video :- def longestCommonSubsequence(text1: String, text2: String): Int = { val cache = Array.tabulate(text1.length,text2.length)((r,c) => -1) // Declaring array and filling -1 values, for memoization return helperBug(text1,text1.length-1,text2,text2.length-1,0,cache) } def helperBug(text1:String,i:Int,text2:String,j:Int,soFar:Int,cache:Array[Array[Int]]): Int = { if(i == 0 || j == 0){ soFar } else if(cache(i)(j) != -1) cache(i)(j) else { var max = 0 if(text1(i) == text2(j)) { max = max.max(helperBug(text1,i-1,text2,j-1,soFar+1,cache)) // Instead of 1+ LCS(), I have kept length in parameter = soFar } else { max = max.max(helperBug(text1,i-1,text2,j,soFar,cache)) max = max.max(helperBug(text1,i,text2,j-1,soFar,cache)) } cache(i)(j) = max; max } }
@cyborg9910
@cyborg9910 3 жыл бұрын
C++ implementation for the same: #include const int mod=1e9+7; using namespace std; // } Driver Code Ends // function to find longest common subsequence static int dp[1001][1001]; class Solution { public: int test(int x, int y, string &s1, string &s2) { if(x == 0 || y == 0) { return 0; } if(dp[x][y] != -1) { return dp[x][y]; } if(s1[x-1] == s2[y-1]) { return dp[x][y] = 1+test(x-1,y-1,s1,s2); } else { return dp[x][y] = max(test(x-1,y,s1,s2),test(x,y-1,s1,s2)); } } //Function to find the length of longest common subsequence in two strings. int lcs(int x, int y, string s1, string s2) { memset(dp,-1,sizeof(dp)); int p = test(x,y,s1,s2); return p; } }; // { Driver Code Starts. int main() { int t,n,k,x,y; cin>>t; while(t--) { cin>>x>>y; // Take size of both the strings as input string s1,s2; cin>>s1>>s2; // Take both the string as input Solution ob; cout
@yogeshkumarprajapat8147
@yogeshkumarprajapat8147 4 жыл бұрын
sir, actually ye method jo is video mai hai vo top down approach hai kyunki is approach mai hum tree ke root se niche aate hai(bigger problems ko pehle directly approcah karte hai which get divided into smaller prblms with time) i.e leaves(aapne apne explanation mai vo bataya ), aur jo next video mai hai vo bottom up approach hai kunki usme hum directly smaller problems ko approach karte hai (with the help of loop) fir finally at index [n][m] hamara required result aa jaata hai
@amitkumar-xt2ms
@amitkumar-xt2ms 3 жыл бұрын
bhai thanks, bohot jagah dhundha , ye global matrix problem de rha tha. yaha sb clear ho gya!!! aage bhi yaad rhega
@AbhishekSingh-jv6uc
@AbhishekSingh-jv6uc 4 жыл бұрын
Thanks a lot, brother!! You have made DP so easy and interesting. I have placements after 3 months and I have the confidence I needed after watching your videos. Love you, bro!!
@coldfinger3472
@coldfinger3472 3 жыл бұрын
how was your placement?
@AbhishekSingh-jv6uc
@AbhishekSingh-jv6uc 3 жыл бұрын
@@coldfinger3472 Very Good actually. Got placed as SDE in MTX Group with 52 Lakh CTC.
@vladimirputin2756
@vladimirputin2756 Жыл бұрын
public class LCSMemo { // Declare the dp array as a global variable static int[][] dp; // Function to find the length of the Longest Common Subsequence (LCS) public static int lcs(String s1, String s2, int n, int m) { // Base case: If either of the strings is empty, the LCS is 0. if (n == 0 || m == 0) return 0; // If the result for this pair of lengths is already computed, return it. if (dp[n][m] != 0) return dp[n][m]; // If the last characters of both strings match, include them in the LCS. if (s1.charAt(n - 1) == s2.charAt(m - 1)) { // Store the result in the memoization table. dp[n][m] = 1 + lcs(s1, s2, n - 1, m - 1); return dp[n][m]; } else { // If the last characters don't match, consider two cases: // 1. Excluding the last character of s1 and finding LCS with s2. // 2. Excluding the last character of s2 and finding LCS with s1. dp[n][m] = Math.max(lcs(s1, s2, n - 1, m), lcs(s1, s2, n, m - 1)); return dp[n][m]; } } public static void main(String[] args) { String s1 = "abdefjs"; String s2 = "adfs"; int n = s1.length(); int m = s2.length(); // Initialize the dp array with dimensions (n+1) x (m+1) dp = new int[n + 1][m + 1]; // Call the LCS function with the lengths of both strings. int result = lcs(s1, s2, n, m); // Print the length of the LCS. System.out.println("Length of Longest Common Subsequence: " + result); } }
@prithvirajpatil8150
@prithvirajpatil8150 4 жыл бұрын
Dude great work . I recommend this course to many of my friends
@JerichoMatt
@JerichoMatt 11 ай бұрын
All I can say is that :- you have earned yourself a subscriber ( and a fan) kudos to your efforts and way of teaching 🎉
@swapnilshivpuje3705
@swapnilshivpuje3705 7 ай бұрын
I like it Man Can't believe how some one can explain by this way
@darshantawte7435
@darshantawte7435 2 жыл бұрын
Can you please update time and space complexity of every code (recursive,memoize,tabulation) on all DP problems in the playlist. Please make a short video for each problem regarding time/space complexities or you can even specify it in the video description.
@pv2930
@pv2930 Жыл бұрын
This guy is FAAAAADDDDD Insan... Hats Off !!! MEri taraf se 21 topo ki salami.
@kuch_visahi_bahi9871
@kuch_visahi_bahi9871 2 жыл бұрын
This is the best playlist on Dp anywhere on the internet
@aayushpagare9366
@aayushpagare9366 3 жыл бұрын
swad aa gaya bhai❤️
@herculean6748
@herculean6748 Жыл бұрын
Thanks!🙌
@tanujgyan787
@tanujgyan787 3 жыл бұрын
Awesome explanation man, aise to ye baat sun sun k tum thak gaye hoge coz har koi yahi bol raha but bhai seriously kamal videos banayi hain yaar!
@ansarishadman
@ansarishadman 2 жыл бұрын
Pen ghuma ke purani yadein taza kar di bhai.. btw brilliant explaination!
@AMAR-pc6ht
@AMAR-pc6ht 2 жыл бұрын
Smae bhaiya, I too loves eriting "Recursion and the meomization" it looks really cool to me, and it gives clean code as well.
@ayushmishra3388
@ayushmishra3388 4 жыл бұрын
You teach like the way every programmer should think, just sharing my thoughts in the starting of video, in heading you have mentioned **memoization as bottom-up, but it should be top-down**, coz it uses recursion, correct me if i am wrong bro.
@sachchidanandsingh57
@sachchidanandsingh57 4 жыл бұрын
Your way of explaining is grt, Can you pls also share some video on problems based on Binary Tree, Prims Algorithm, shortest path etc..
@pranjalmishra2602
@pranjalmishra2602 3 жыл бұрын
Bhaiya, Top down = memoization and Bottom up(DP) = for loop Right? : |
@ayushbisht2689
@ayushbisht2689 3 жыл бұрын
No memoization is bottom up .
@pranjalmishra2602
@pranjalmishra2602 3 жыл бұрын
@@ayushbisht2689 To be more simple, Memoization uses the top-down approach to solve the problem i.e. it begin with core(main) problem then breaks it into sub-problems and solve these sub-problems similarly. Source - Google
@ayushbisht2689
@ayushbisht2689 3 жыл бұрын
@@pranjalmishra2602 your right bro. My bad
@mohit84604
@mohit84604 Жыл бұрын
javascript code :- let string = "abcdef" let string1 = "abuiopmnbv" let n = string.length let m = string1.length let dp = new Array(n+1).fill(new Array(m + 1).fill(-1)) function lcs(a, b, n, m,dp) { if(n == 0 || m == 0){ return 0 } if(dp[n][m] != -1){ return dp[n][m] } if(a[n-1] == b[m-1]){ return 1+lcs(a,b,n-1,m-1,dp) }else{ return Math.max(lcs(a,b,n-1,m,dp),lcs(a,b,n,m-1,dp)) } } console.log(lcs(string, string1, n, m,dp))
@priyarathore9266
@priyarathore9266 3 жыл бұрын
Best DP course on internet!
@PratapSingh-si7qo
@PratapSingh-si7qo 4 жыл бұрын
Bhai I love all your video of dp. Thanks for making my interest in coding. I request you to make video playlist on graph. It looks much complicated.
@ankitasingh2563
@ankitasingh2563 5 ай бұрын
class Solution { public int longestCommonSubsequence(String text1, String text2) { int n = text1.length(); int m = text2.length(); int[][] t = new int[n + 1][m + 1]; // Initialize the first row and first column with zeros for (int i = 0; i < n+1; i++) { for (int j = 0; j < m+1; j++) { if (i == 0 || j == 0) { t[i][j] = 0; } } } // Fill the t array with LCS values for (int i = 1; i < n+1; i++) { for (int j = 1; j < m+1; j++) { if (text1.charAt(i - 1) == text2.charAt(j - 1)) { t[i][j] = 1 + t[i - 1][j - 1]; } else { t[i][j] = Math.max(t[i][j - 1], t[i - 1][j]); } } } return t[n][m]; } } leetcode problem no : 1143
@anuragshah516
@anuragshah516 4 жыл бұрын
Aapne itne ache se padhaya knapsack ki iski recursive, memorization aur bottom up code to khud se he likh li maine 🤩. Dp halwa lag rahi hai😂.
@prathmesh4894
@prathmesh4894 2 жыл бұрын
LEETCODE : 1143. Longest Common Subsequence CODE: class Solution { public: int LCS(string &X,string &Y, int m, int n,int t[][1001]){ if(n==0 || m==0){ return 0; } if(t[m][n] != -1){ return t[m][n]; } if(X[m-1]==Y[n-1]){ return t[m][n] =1+LCS(X,Y,m-1,n-1,t); } else{ return t[m][n] = max(LCS(X,Y,m,n-1,t ),LCS(X,Y,m-1,n,t)); } return -1; } int longestCommonSubsequence(string text1, string text2) { //base condition // agar dono string 0 hai int static t[1001][1001]; memset(t,-1,sizeof(t)); int m = text1.size(); int n = text2.size(); return LCS(text1,text2,m,n ,t); return -1; } };
@shivanshyadu4830
@shivanshyadu4830 2 жыл бұрын
Bro can you explain me that why are you returning -1 in LCS & its main function????
@sharmanihal99
@sharmanihal99 2 жыл бұрын
@@shivanshyadu4830 you can omit this return statement, it is not requires as lcs function will anyway return value.
@ipsitasahoo8387
@ipsitasahoo8387 Жыл бұрын
Really nice Playlist
@karthikkallur727
@karthikkallur727 3 жыл бұрын
Wow bhaiya kya padhate ho yaar aap Sach me bohot acha samjhate ho
@debanshumajumdar4826
@debanshumajumdar4826 9 ай бұрын
One suggestion - Please add how to optimize space of a 2d array and how to think to do that. Even in Bottom-Up using Tabulization.
@suchitragiri4546
@suchitragiri4546 3 жыл бұрын
You are great!! but I have one confusion ... is that same logic we apply in top down and bottom up but why the bottom is more faster than the top down, while memory space is same?
@animeshmishra4282
@animeshmishra4282 3 жыл бұрын
🙇🏻‍♂️ Thankyou so much. 🍁 Legend of DP Aditya Verma 🔥.
@asmitshukla4649
@asmitshukla4649 Жыл бұрын
very nice explaination...mind blowing bhaiya..keep it up❤❤
@thisthatvlogs5334
@thisthatvlogs5334 2 жыл бұрын
Thank you for existing................
@tekbssync5727
@tekbssync5727 3 жыл бұрын
bhai TLE de rha hai ye memoized code gfg me, aisa kyu ??
@shantanukumar4081
@shantanukumar4081 3 жыл бұрын
Thank u bhai
@AnonymousBear-bb8pc
@AnonymousBear-bb8pc 3 ай бұрын
Could someone advise me on whether I should learn dynamic programming from Striver or Aditya Verma? Which one is better?
@samyakgoyal2958
@samyakgoyal2958 3 жыл бұрын
Can anyone please explain what is the use of static when we declare the array for memorization globally?
@divyanshukumar813
@divyanshukumar813 3 жыл бұрын
Another great video!! Thank you soooo much
@noobichesser9434
@noobichesser9434 3 жыл бұрын
Chalo apan agla lecture ka code karte hain... GOD of DP Aditya Verma
@blasttrash
@blasttrash 4 жыл бұрын
bro you should make videos in English. You have the power to topple entire algo-teaching industry. Like business models of leetcode, algoexpert, intervieweing.io etc. Hell, your videos have the potential to topple interviews in tech industry. After your videos, big tech companies might stop asking dp questions coz everyone will be really good at it. :)
@swagatdas1991
@swagatdas1991 4 жыл бұрын
@aditya verma, maano ya naa maano, this is true af. Or you can add really good subtitles, as in the introductory video.
@durgeshsahu.7
@durgeshsahu.7 2 жыл бұрын
Excellent Explanation
@hritiksingh1767
@hritiksingh1767 4 жыл бұрын
Thanks aditya verma sir...dp genius
@factoworld9527
@factoworld9527 Жыл бұрын
great work and series by you bro
@devanshrastogi6305
@devanshrastogi6305 4 жыл бұрын
great explanation.
@avert_
@avert_ 3 жыл бұрын
I tried the same logic but not all gfg cases are passing: What am i missing out? Here's the python3 code:(ignore the I/O) dp = [[-1 for i in range(101)] for j in range(101)] def lcs(m,n,X,Y): ''' :param m: length of string X :param n: length of string Y :param X: string X :param Y: string Y :return: Integer ''' # code here if m == 0 or n == 0: return 0 if dp[m][n] != -1: return dp[m][n] if X[m-1] == Y[n-1]: dp[m][n] = 1 + lcs(m-1,n-1,X,Y) return dp[m][n] else: dp[m][n] = max(lcs(m-1,n,X,Y),lcs(m,n-1,X,Y)) return dp[m][n]
@TomJerry-bp9ig
@TomJerry-bp9ig 3 жыл бұрын
Bhai pta chle to mujhe bhi btana😓
@pujakumari6391
@pujakumari6391 Жыл бұрын
Mind Blowing
@aviseklahiri3864
@aviseklahiri3864 4 жыл бұрын
Why did we not initialize the first row and first column with zero like we used to do for knap-sack ? The base condition says us to do so from the recursive forumaltion ..
@atreyadyaram850
@atreyadyaram850 4 жыл бұрын
Because that never gets called. We never call t[0][0]. Because when m=0 and n=0, the base condition returns 0.
@sharmajikaladka3011
@sharmajikaladka3011 4 жыл бұрын
Thank you bhai. you're doing great work. please make a playlist on graph and tree also.
@saurabhkanswal7954
@saurabhkanswal7954 4 жыл бұрын
one of favourite teacher after abdul bari
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 4 жыл бұрын
Thanks Aditya for such a nice explanation. I had one doubt regarding the time complexity of the memoized version , how the time complexity turns out to be O(n^2) ??????
@tanaysaraf9402
@tanaysaraf9402 4 жыл бұрын
two for loops used! recursive calls will be placed in stack and result will be stored , so complexity will become on^2
@sairamakurti4808
@sairamakurti4808 3 жыл бұрын
to be precise T.C = O(n*m) since we are filling every box of the table so it is o(n*m)
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 3 жыл бұрын
@@sairamakurti4808 Thanks
@ccarnagee7867
@ccarnagee7867 4 жыл бұрын
Is there a memory efficient method for memoization... 2d array takes a lot of memory for large inputs....
@ANUABRAHAMCS-A-
@ANUABRAHAMCS-A- 4 жыл бұрын
well i guess you can create a HashMap and so on. does the same thing and we can fetch data in O(1) complexity
@saumyagupta7400
@saumyagupta7400 Жыл бұрын
Hi I have a doubt, hum hamesha array ko 0 se hi kyo start krte hai.
@ashutoshhans
@ashutoshhans 3 жыл бұрын
As per geeksforgeeks and every where I studied memoization is top down and tabulation is bottom up. Can you please help in clearing the doubt
@satviksrivastava9504
@satviksrivastava9504 3 жыл бұрын
haan vo terms thode mixed up h playlist m hai but concept sahi h bilkul
@ashutoshhans
@ashutoshhans 3 жыл бұрын
@@satviksrivastava9504 Yep totally agree.. concepts are correct and crystal clear
@rrMaxwell
@rrMaxwell Жыл бұрын
i did the memozisation and tabulation on my own yay!!!
@ankursharma6084
@ankursharma6084 3 жыл бұрын
Memoized version is showing time limit exceeded in both gfg and leetcode. Anyone else facing the same problem.
@shivanshyadu4830
@shivanshyadu4830 2 жыл бұрын
bro do you have solution??
@022_ananyasrivastava8
@022_ananyasrivastava8 2 жыл бұрын
Aditya Sir is the god of DP
@shubhamagarwal352
@shubhamagarwal352 4 жыл бұрын
@Aditya Verma I tried it with creating an array of size t[n][m] and saving the value in t[n-1][m-1] instead of t[n][m] and it worked fine on leetcode. I did it b'coz I noticed that first row and column is not getting used. Can you please let me know if it is also a right way and may work for other memoization questions?
@gta6515
@gta6515 3 жыл бұрын
bro mera leetcode pe TLE ara h
@bite091_jamee7
@bite091_jamee7 2 жыл бұрын
amazing sirji
@sumitkulkarni3950
@sumitkulkarni3950 3 жыл бұрын
ab pata chala actually dp kaha pe use hoti hai. THANKS.
@shivamtiwari20028
@shivamtiwari20028 2 жыл бұрын
God Of DP❤
@hrithikdhanraj2919
@hrithikdhanraj2919 2 жыл бұрын
We can't get better than this.❤❤
@KanishkaDutta
@KanishkaDutta 2 жыл бұрын
excellent explanation
@kamalsingh1345
@kamalsingh1345 Жыл бұрын
Thankuuuu Sir ❤️❤️❤️🥳🥳🥳🥳😍
@sohinichaudhuri518
@sohinichaudhuri518 4 жыл бұрын
Sir, aap aur videos laayiye na ... aap bahut accha samjhaate hain ... kai din ho gaye aapke videos nahi aaye
@abhilasharaj7991
@abhilasharaj7991 3 жыл бұрын
Please make more video on dsa , by watching ur video I am able to understand and solve ques on leetcode .
21  Longest common subsequence Top down DP
25:35
Aditya Verma
Рет қаралды 190 М.
DP 41. Longest Increasing Subsequence | Memoization
24:35
take U forward
Рет қаралды 319 М.
小丑妹妹插队被妈妈教训!#小丑#路飞#家庭#搞笑
00:12
家庭搞笑日记
Рет қаралды 35 МЛН
Amazing Parenting Hacks! 👶✨ #ParentingTips #LifeHacks
00:18
Snack Chat
Рет қаралды 18 МЛН
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 98 МЛН
23  Printing Longest common subsequence
26:45
Aditya Verma
Рет қаралды 234 М.
43 Egg Dropping Problem Recursive
30:10
Aditya Verma
Рет қаралды 165 М.
14  Rod Cutting Problem
18:53
Aditya Verma
Рет қаралды 284 М.
29  Print shortest common Supersequence
23:08
Aditya Verma
Рет қаралды 168 М.
41 Scrambled String Recursive
45:48
Aditya Verma
Рет қаралды 140 М.
22  Longest Common Substring
13:00
Aditya Verma
Рет қаралды 254 М.
10 Minimum Subset Sum Difference
46:41
Aditya Verma
Рет қаралды 385 М.
40 Evaluate Expression To True Boolean Parenthesization Memoized
28:12
39  Evaluate Expression to True  Boolean Parenthesization Recursive
40:00