[leetcode] 312. Burst Balloons

与矩阵连乘问题很相似:

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

Note:

  • You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
  • 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Input: [3,1,5,8]
Output: 167 
Explanation: nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
             coins =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167

最后一个扎破的气球要乘以原区间外相邻左右两边的值,没有时则乘以1,将nums转化为 a[1000],a[0] = a[n + 1] = 1,即可解决乘以1的问题,这题与矩阵连乘问题很相似,解决办法也很相似。设 k 是最后一个扎破的气球,dp[i][k - 1] 以及dp[k + 1][j]都是已经完全得到的具体数值,因为长度更短的区间的 dp 值都已经求得了,那么仅需加上 a[i - 1] * a[k] * a[j + 1],i , j 分别为所求区间的端点,L从1开始,经过判断,对长度为 1 的dp也可以按正确求得,注意 k = i; k <= j; 是正确的,虽然可能出现 k - 1 > i 的情况,但是由于已经初始化dp数组为0,则不会产生影响。

class Solution {
public:
    int a[1000];
    int dp[501][501] = {0};
    int maxCoins(vector<int>& nums) {
        int n = nums.size();
        for(int i = 0; i < n; i++)
            a[i + 1] = nums[i];
        a[0] = 1;
        a[n + 1] = 1;
        for(int L = 1; L <= n; L++)
        {
            for(int i = 1; i <= n - L + 1; i++)
            {
                int j = i + L - 1;
                for(int k = i; k <= j; k++)
                {
                    dp[i][j] = max(dp[i][j], dp[i][k - 1] + a[i - 1] * a[k] * a[j + 1] + dp[k + 1][j]);
                }
            }
        }
        return dp[1][n];
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值