Solution Partially correct showing TLE for higher values


#1

int static t[1007][1007];
int lcs(string A, string B, int n, int m)
{
if(n==0 || m==0) return 0;
if(t[n][m]!=-1) return t[n][m];
if(A[n-1]==B[m-1])
{
return t[n][m]= 1+lcs(A,B,n-1,m-1);
}
else
{
return t[n][m]= max(lcs(A,B,n-1,m), lcs(A,B,n,m-1));
}
}
int Solution::solve(string A, string B) {
int n=A.size();
int m=B.size();
memset(t,-1,sizeof(t));
int count=lcs(A,B,n,m);
return count;
}


#2

Bro, you either have to use memoization or dynamic programming to solve this problem efficiently.