Leetcode 1626.矛盾のない最高のチーム(50日目)----動的計画法の学習期間(2番目の質問に直接勝ちます)

元のタイトル

ここに画像の説明を挿入



コードの実装(ほとんどの自己解決型の最初のブラシ)

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;
}

おすすめ

転載: blog.csdn.net/qq_37500516/article/details/113879959