DP C++ solution


  • 0
    W

    class Solution {
    public:
    int findLength(vector<int>& A, vector<int>& B) {
    int m = A.size(), n = B.size();

        int cc[1005][1005];//c[i][j] = maximum common suffix of A[i-1], B[j-1]
    
        int ret = 0;
        
        for (int i = 0; i <= m; ++i)
        {
            for (int j = 0; j <= n; ++j)
            {
                if (i==0 || j==0)
                    cc[i][j] == 0;
                else if (A[i-1] == B[j-1])
                {
                    cc[i][j] = cc[i-1][j-1]+1;
                    ret = max(ret, cc[i][j]);
                }
                else
                    cc[i][j] = 0;
            }
        }
        
        
        
        return ret;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.