Leetcode 1027. The longest arithmetic sequence (DAY 45) ---- Dynamic programming learning period

Original title

Insert picture description here



Code implementation (first brush small part to see the solution and most of the self-solving)

int longestArithSeqLength(int* A, int ASize){
    
    
    int* dp[ASize],i,j,max = 0,temp;
    for(i=0;i<ASize;i++)
    {
    
    
        dp[i] = (int*)malloc(sizeof(int) * 20001);
        memset(dp[i],0,sizeof(dp[i]));
    }
    for(i=1;i<ASize;i++)
    {
    
    
        for(j=0;j<i;j++)
        {
    
    
            temp = A[i] - A[j] + 10000;
            dp[i][temp] = fmax(dp[i][temp],dp[j][temp]+1);
            max = fmax(dp[i][temp],max); 
        }
    }
    return max+1;
}

Guess you like

Origin blog.csdn.net/qq_37500516/article/details/113806835