Sort by

recency

|

26 Discussions

|

  • + 0 comments

    What is wrong withthe code:

    memo=[[0]*(len(s2)+1) for _ in range((len(s1)+1))]        
        for i in range(1, len(s1)+1):
            for j in range(1, len(s2)+1):          
                if s1[i-1]==s2[j-1] :          
                    memo[i][j]=memo[i-1][j-1]+1
                else:
                    memo[i][j]=max(memo[i-1][j], memo[i][j-1])
                                    
        _cur=memo[-1][-1]
        suffixes=[[0]*(len(s2)+1) for _ in range((len(s1)+1))] 
        print(memo)      
        for i in range(len(s1)-1, -1, -1):
            for j in range(len(s2)-1, -1, -1):
                print(s1[i], s2[j])
                if s1[i]==s2[j]:   
                    suffixes[i][j]=suffixes[i+1][j+1]+1
                else:
                    suffixes[i][j]=max(suffixes[i+1][j], suffixes[i][j+1]) 
        count=0
        print(suffixes)
        for i in range(len(s1)+1):
            seen=set()
            for j in range(len(s2)):
                if memo[i][j]+suffixes[i][j+1]+1>_cur:
                    count+=1
                    seen.add(s2[j])
        return count
    
  • + 0 comments

    What is wrong withthe code:

    memo=[[0]*(len(s2)+1) for _ in range((len(s1)+1))]        
        for i in range(1, len(s1)+1):
            for j in range(1, len(s2)+1):          
                if s1[i-1]==s2[j-1] :          
                    memo[i][j]=memo[i-1][j-1]+1
                else:
                    memo[i][j]=max(memo[i-1][j], memo[i][j-1])
                                    
        _cur=memo[-1][-1]
        suffixes=[[0]*(len(s2)+1) for _ in range((len(s1)+1))] 
        print(memo)      
        for i in range(len(s1)-1, -1, -1):
            for j in range(len(s2)-1, -1, -1):
                print(s1[i], s2[j])
                if s1[i]==s2[j]:   
                    suffixes[i][j]=suffixes[i+1][j+1]+1
                else:
                    suffixes[i][j]=max(suffixes[i+1][j], suffixes[i][j+1]) 
        count=0
        print(suffixes)
        for i in range(len(s1)+1):
            seen=set()
            for j in range(len(s2)):
                if memo[i][j]+suffixes[i][j+1]+1>_cur:
                    count+=1
                    seen.add(s2[j])
        return count
    
  • + 0 comments

    quadratic time O(A.size() * B.size())

    int LCSReturns(const string& A, const string& B) {
        int total = 0;
        vector<vector<int>> cache(A.size() + 1, vector<int>(B.size() + 1)), reverse(A.size() + 2, vector<int>(B.size() + 2));
        vector<vector<bool>> insertion(A.size() + 2, vector<bool>(75));
        for (int i = 1; i <= A.size(); i++) {
            for (int j = 1; j <= B.size(); j++) {
                if (A[i - 1] == B[j - 1]) cache[i][j] = cache[i - 1][j - 1] + 1;
                else cache[i][j] = max(cache[i - 1][j], cache[i][j - 1]);
            }
        }
        for (int i = A.size(); i >= 1; i--) {
            for (int j = B.size(); j >= 1; j--) {
                if (A[i - 1] == B[j - 1]) reverse[i][j] = reverse[i + 1][j + 1] + 1;
                else reverse[i][j] = max(reverse[i + 1][j], reverse[i][j + 1]);
            }
        }
        for (int i = 1; i <= A.size() + 1; i++) {
            for (int j = 1; j <= B.size(); j++) {
                if (insertion[i][B[j - 1] - '0']) continue;
                if (cache[i - 1][j - 1] + reverse[i][j + 1] == cache[A.size()][B.size()]) {
                    insertion[i][B[j - 1] - '0'] = true;
                    total++;
                }
            }
        }
        return total;
    }
    
  • + 0 comments

    Here is my solution in java, javascript, python, C ,C++, Csharp HackerRank LCS Returns Problem Solution

  • + 0 comments

    The following codes work by pypy 3

    na=len(a)
    nb=len(b)
    dp=[[0]*(nb+1) for _ in range(na+1)]
    for i in range(na):
        for j in range(nb):
            if a[i]==b[j]:
                dp[i+1][j+1]=dp[i][j]+1
            else:
                dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1])
    
    suff=[[0]*(nb+1) for _ in range(na+1)]
    for i in range(na-1,-1,-1):
        for j in range(nb-1,-1,-1):
            if a[i]==b[j]:
                suff[i][j]=suff[i+1][j+1]+1
            else:
                suff[i][j]=max(suff[i+1][j],suff[i][j+1])
    
    cur = dp[na][nb]      
    ret = 0
    for i in range(na+1):
        used=[False]*(256)
    
        for j in range(nb):
            if used[ord(b[j])]==True:
                continue
            now=dp[i][j]+ suff[i][j+1] + 1
            if now == cur+1:
                used[ord(b[j])]=True
                ret+=1
    return ret