We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The Longest Common Subsequence
The Longest Common Subsequence
+ 2 comments Thanks for including the video link to the MIT lecture.
+ 4 comments c++ Solution:
vector<int> longestCommonSubsequence(vector<int> a, vector<int> b) { int n=a.size(); int m=b.size(); int dp[n+1][m+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ if(i==0 || j==0) dp[i][j]=0; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i-1]==b[j-1]) dp[i][j]=1+dp[i-1][j-1]; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } int i=n; int j=m; vector<int> v; while(i>0 && j>0){ if(a[i-1]==b[j-1]){ v.push_back(a[i-1]); i--; j--; } else{ if(dp[i-1][j]>dp[i][j-1]) i--; else j--; } } reverse(v.begin(),v.end()); return v; }
+ 1 comment Is there any way to report this problem ? It's checker doesnt work. I have correct code and it fails on even first test case and they are the same.
+ 1 comment i am getting correct output in sample case as 3 4 1 but getting some command error as
Error in command /custom-RB1ptiIaeNCmJhh5eUdH/solution errno = 2
Load more conversations
Sort 142 Discussions, By:
Please Login in order to post a comment