Leetcode 1626. The best team without contradictions (DAY 50) ---- Dynamic programming learning period (the second question is directly won)

Original title

Insert picture description here



Code implementation (most self-solving first brush)

void sort(int* scores,int* ages,int scoresSize)
{
    
    
    int i,j,temp1,temp2;
    for(i=0;i<scoresSize-1;i++)
    {
    
    
        for(j=i+1;j<scoresSize;j++)
        {
    
    
            if(ages[i] > ages[j] || (ages[i] == ages[j] && scores[i] > scores[j]))
            {
    
    
                temp1 = ages[i];
                ages[i] = ages[j];
                ages[j] = temp1;
                temp2 = scores[i];
                scores[i] = scores[j];
                scores[j] = temp2;
            }
        }
    }
}


int bestTeamScore(int* scores, int scoresSize, int* ages, int agesSize){
    
    
    sort(scores,ages,scoresSize);
    int* dp = (int*)malloc(sizeof(int) * (agesSize+1)),i,j,max = scores[0];
    memset(dp,0,sizeof(int) * (agesSize+1));
    dp[0] = scores[0];
    for(i=1;i<scoresSize;i++)
    {
    
    
        dp[i] = scores[i];
        for(j=0;j<i;j++)
        {
    
    
            if(ages[i] == ages[j] || scores[i] >= scores[j])
                dp[i] = fmax(dp[i],scores[i]+dp[j]);      
        }
        if(dp[i] > max)     max = dp[i];
    }
    return max;
}

Guess you like

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