首页 > 代码库 > LeetCode - Burst Balloons

LeetCode - Burst Balloons

int max(int a,int b){
    if(a>b)return a;
    return b;
}
int maxCoins(int* nums, int numsSize) {
    int a[555];
    int n  = numsSize;
    int dp[555][555];
    memset(dp,0,sizeof(dp));
    for(int i = 1 ; i <= n ; i ++){
        a[i]=nums[i-1];
    }
    a[0] = a[n+1] = 1;
    for(int i = 1 ; i <= n ; i ++){
        dp[i][i] = a[i]*a[i-1]*a[i+1];
    }

    for(int k = 1 ; k <= n ; k ++){
        for(int i = 1 ; i + k <= n ; i ++){
            int l = i ;
            int r = i+k;

            for(int j = l ; j <= r ; j ++){
                dp[l][r] = max(dp[l][r],dp[i][j-1] + dp[j+1][r] + a[j]*a[l-1]*a[r+1]);
            }

        }
    }
    return dp[1][numsSize];
}

LeetCode - Burst Balloons