LintCode 168: Burst Balloons (区间型DP 经典题!!!)

  1. Burst Balloons
    中文English
    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.

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
Given [4, 1, 5, 10]
Return 270

nums = [4, 1, 5, 10] burst 1, get coins 4 * 1 * 5 = 20
nums = [4, 5, 10] burst 5, get coins 4 * 5 * 10 = 200
nums = [4, 10] burst 4, get coins 1 * 4 * 10 = 40
nums = [10] burst 10, get coins 1 * 10 * 1 = 10

Total coins 20 + 200 + 40 + 10 = 270

解法1:这题感觉挺难的。参考的九章的思路:区间型DP。假设n个气球还有一个左边界0,和一个右边界n+1。
假设最后一个扎破的气球是i。当扎破气球i时,左边是0,右边是n+1,此时获得coins 1*nums[i]*1。而气球1 ~ i-1和i+1 ~ n都已扎破,所以相当于求两个子问题的最优解。
注意计算顺序:
dp[0][1], dp[1][2], dp[2][3], …, dp[N][N+1]
dp[0][2], dp[1][3], dp[2][4], …, dp[N-1][N+1]

dp[0][N+1]
时间复杂度O(N ^ 3),空间复杂度O(N ^ 2)

代码如下:

class Solution {
public:
    /**
     * @param nums: A list of integer
     * @return: An integer, maximum coins
     */
    int maxCoins(vector<int> &nums) {
        int N = nums.size();
        if (N == 0) return 0;
        
        vector<int> nums2;
        nums2.push_back(1);
        nums2.insert(nums2.end(), nums.begin(), nums.end());
        nums2.push_back(1);

        vector<vector<int>> dp(N + 2, vector<int>(N + 2, 0));
        
        for (int i = 0; i <= N; ++i) {
            dp[i][i + 1] = 0;
        }

        for (int i = 2; i <= N + 1; ++i) {
            for (int j = 0; i + j <= N + 1; ++j) {
                int maxV = 0;
                for (int k = j + 1; k < i + j; ++k) {
                    maxV = max(maxV, dp[j][k] + dp[k][i + j] + nums2[j] * nums2[i + j] * nums2[k]);
                }
                dp[j][i + j] = maxV;
            }
        }
        return dp[0][N + 1];
    }
};

二刷:

class Solution {
public:
    /**
     * @param nums: A list of integer
     * @return: An integer, maximum coins
     */
    int maxCoins(vector<int> &nums) {
        int n = nums.size();
        nums.insert(nums.begin(), 1);
        nums.push_back(1);
        vector<vector<int>> dp(n + 2, vector<int>(n + 2, 0));
         
        for (int len = 1; len <= n; len++) {
            for (int i = 1; i <= n - len + 1; i++) {
                int j = i + len - 1;
                for (int k = i; k <= j; k++) {
                    dp[i][j] = max(dp[i][j], nums[i - 1] * nums[k] * nums[j + 1] + dp[i][k - 1] + dp[k + 1][j]);
                }
            }
        }
        return dp[1][n];
    }
};

记得是nums[i - 1] * nums[k] * nums[j + 1],因为当计算dp[i][j]时,i…k-1和k+1…j的气球都已经爆掉了。

解法3:DFS+Memorization优化。不加Memorization (也就是那个sols数组)的话过不了。

class Solution {
public:
    /**
     * @param nums: A list of integer
     * @return: An integer, maximum coins
     */
    int maxCoins(vector<int> &nums) {
        int res = 0;
        int n = nums.size();
        nums.insert(nums.begin(), 1);
        nums.push_back(1);
        vector<vector<int>> sols(n + 2, vector<int>(n + 2, 0));
        helper(nums, 1, n, sols);
        return sols[1][n];
    }
private:
    int helper(vector<int> &nums, int start, int end, vector<vector<int>> &sols) {
        if (start > end) return 0;
        if (sols[start][end]) return sols[start][end];
     //   if (start == end) {
     //       sols[start][start] = nums[start - 1] * nums[start] * nums[start + 1];
     //       return sols[start][start];
     //   }
        
        int res = 0;
        
        for (int i = start; i <= end; i++) {
            int leftRes = helper(nums, start, i - 1, sols);
            int rightRes = helper(nums, i + 1, end, sols);
            res = max(res, nums[start - 1] * nums[i] * nums[end + 1] + leftRes + rightRes);
        }
        sols[start][end] = res;
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值