This a question of dp I dont know how to solve?
(http://leetcode.com/problems/longest-arithmetic-subsequence/submissions/)
Here is my solution i applied memorization technique i dont undersatnd why im getting error
the code i write

class Solution {
public:
 
 long int  dp[1033][1004];
   
int rec(vector<int>& A,int start,int end,int diff ){
 
   if(start>=A.size()){
       return 0;
   }
    if(dp[start][end]!=0){
      return dp[start][end];
    }
 int o1=0,o2;
    
   o2= rec(A,start+1,end,diff);
    
    
    
    
    if(A[start]-A[end]==diff){
    
    o1 =  1+rec(A,start+1,start,diff);
        }
 
return dp[start][end]=max(o1,o2);
    
    }
    
    
int longestArithSeqLength(vector<int>& A) {
       int maxe=0;
    memset(dp,0,sizeof(dp));
    for(auto i=0;i<A.size();i++){

        for(auto j=i+1;j<A.size();j++){
            
          int  diff=A[j]-A[i];
            if(dp[j][i]==0){
         
                dp[j][i]=2+rec(A,j+1,j,diff);
            }
        
          if(maxe<=dp[j][i]){

          maxe=dp[j][i];
          }
        
        
        }
       
                     }
        return maxe;
    
        
        
    }
};
i only pass 40 testcases out of 60 but dont know where i get error and let me know what changes should i make in order to clear whole test cases.
Comments (0)