Longest Palindromic Subsequence | Recursion | Memo | Bottom Up | Leetcode 516

preview_player
Показать описание
This is the 21st Video on our Dynamic Programming (DP) Playlist.
In this video we will try to solve a very good but similar DP Problem "Longest Palindromic Subsequence " (Leetcode - 516)

Trust me, this will no longer be a Hard Problem. I will explain the intuition so easily that you will never forget.
We will solve it using Recursion + Memoization technique

We will do live coding after explanation and see if we are able to pass all the test cases.

Problem Name : Longest Palindromic Subsequence
Company Tags : Amazon, LinkedIn, Paypal, Rivigo, Uber, Google

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

I am Praying to God, we need more people like you in the world. You are helping lots of people. From your videos, I can sense how nice a person you would be in real life.

everydaygkinhindi
Автор

i am following your multiple playlist. In this video, i just watched only 2 mins till you mentioned to reverse the string.( THEN I STOPPED as i understood the whole picture ) I went to LC and wrote everything by myself and yupeee... it worked.

Still can not believe, i am able to solve DP, graph problems so smoothly.

4 to 5 months earlier, i was very scared of dp and graph but gods know how i found your channel and now i am addicted by ur contents.

pradeepranjan
Автор

Brother I wish your channel grows 1000 times. You make things so easy to understand by giving so much detail explanation which is very rare to find.

Anand
Автор

Java All Solutions:

Using LCS:Rec+Memo
class Solution {

private int solve(String s1, String s2, int dp[][], int i, int j) {

if(i == s1.length() || j == s2.length()) return 0;

if(dp[i][j] != -1) return dp[i][j];

//match
if(s1.charAt(i) == s2.charAt(j)) {

return dp[i][j] = 1 + solve(s1, s2, dp, i+1, j+1);

}

//no match
else {

return dp[i][j] = Math.max(solve(s1, s2, dp, i+1, j), solve(s1, s2, dp, i, j+1));
}

}

public int s1) {
StringBuilder s2 = new StringBuilder(s1);
s2.reverse();

int dp[][] = new
for(int[] d : dp)
Arrays.fill(d, -1);

return solve(s1, s2.toString(), dp, 0, 0);

}
}


Bottom up:
class Solution {

private int solveTab(String s1, String s2, int i, int j){
int[][] dp = new
for(int k = s1.length() - 1; k >= 0; k--){
for(int l = s2.length() - 1; l >= 0; l--){
int ans = 0;
//match
if(s1.charAt(k) == s2.charAt(l))
ans = 1 + dp[k+1][l+1];
else ans = Math.max(dp[k][l+1], dp[k+1][l]);
dp[k][l] = ans;
}
}
return dp[i][j];
}

public int s1) {
StringBuilder s2 = new StringBuilder(s1);
s2.reverse();
return solveTab(s1, s2.toString(), 0, 0);
}
}



Approach 3 : 2 pointer
class Solution {

private int solve(String s, int dp[][], int i, int j) {

if(i > j) return 0;

if(i == j) return 1;

if(dp[i][j] != -1) return dp[i][j];

//match
if(s.charAt(i) == s.charAt(j)) {

return dp[i][j] = 2 + solve(s, dp, i+1, j-1);
}

//not match
else {

return dp[i][j] = Math.max(solve(s, dp, i+1, j), solve(s, dp, i, j-1));
}

}

public int s) {
int n = s.length();
int dp[][] = new int[n+1][n+1];

for(int[] d : dp)
Arrays.fill(d, -1);

return solve(s, dp, 0, n-1);
}
}

JJ-tpdd
Автор

Awesome explanation bro. This playlist is working like a magic for me.

souravjoshi
Автор

BHAI HAR VIDEO 2 BAAR DEKHTA HUN EK BAAR SAMAJHNE KE LIYE DUSRI BAAR ORGASM KE LIYE BC MAZA HI ITNA AA RAHA HOTA HAI
😂👻👻

atifakhtar
Автор

awesome solution...and one question which tech stack to prefer nodejs or springboot for backend dev jobs

alphadrones
Автор

Bhaiya please recursion aur dp ki concept wali playlist start kr do.

turing_machine
Автор

Which topic will the next video be on in Graph concepts playlist bhai? Long weekend, we are expecting graph concepts videos over weekend! 💙

JJ-tpdd
Автор

bhaiya Matrix chain multiplication pe videos le aao plz

atifakhtar
Автор

Sir, i thought exactly same as you for the second method, but i try to do that with iterative dp not with recursive can you pls tell me that how can i do that with iterative dp?. Thank you for the wonderful session.

Bhinuthelord
Автор

Love Babbar and striver ki video chord kar aya hu

SahilKumar-rhbb
Автор

bhaiya pls beginners ke liye linked lists ki series lao na from scratch pro banado linked lists trees graphs me

alphagaming
Автор

If possible make a video on matrix chain multiplication sir

codeandtalk
Автор

Can you please make vdo on leetocode 174. Dungeon Game

abhinavhadole
Автор

sir two pointer vali approach ka bottom up kaise code kare, I tried doing it but its giving run time error

NamanSharma-zxgq
Автор

Remainder: leetcode 1171 and leetcode 92

manimanohar_