LeetCode[312. Burst Balloons] 难度[hard]

题目:

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:
(1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Given [3, 1, 5, 8]

Return 167

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
大致意思就是有n个气球按顺序排好,每个气球上有个数字,刺破第i个气球就会得到num[i-1]*num[i]*num[i+1]个硬币,num[i]是第i个气球上的数字,求最多能得到多少个硬币。

题目分析:

这个题假如暴力把做个全排列来算,时间复杂度是O(n!),显然是要超时的。既然暴力走不通,就要想别的办法,试试分治的思想。如果直接把气球分成两半,这样显然不满足分治算法的思想,因为直接分成两半以后,两边是会相互影响的,分开出的两个元素是相邻的。这里的关键点在于怎么分,才能使分成的两部分不相互影响,假如我们以最后一个刺破的气球为界,把气球分为两边,两边就互不影响了,因为那是最后才刺破的,这是这道题算法的关键,这样一来我们就可以用分治算法来做了。

做法:

首先在原来的数组首尾都插入一个1,建立二维数组record[][],record[i][j]表示第i个气球到第j个气球之间可以获得的最大硬币数。假设m是最后刺破的气球,这里我们可以得到一个递推式:record[i][j] = record[i][m]+record[m][j]+num[i]*num[m]*num[j](这里大家可能会有疑惑,本人就在这里思考了很久,为什么这里不是等于record[i][m-1]+record[m+1][j]+num[m-1]*num[m]*num[m+1]呢?我们可以这样想,m是最后一个刺破的,说明刺破m的时候,他两边都是1了,因为我们两端插入了1,所以num[i]*num[m]*num[j]是成立的。那前面的两个怎么解释呢,因为我们插入两个i是根据题意假设边界值得邻居是1,但是我们把气球分成两部分以后,边界的邻居是实际存在的,邻居就是m,所以前面是record[i][m]+record[m][j])。理清了这些以后就可以写代码了。具体怎么实现也有两个算法:

算法一:分治算法
这里的分治算法,从上至下,会有许多重复算的地方,所以用一个数组把计算的结果存起来可以提高算法的效率。

class Solution {
public:
    int maxCoins(vector<int>& nums) {
        int n = nums.size();
        int *newArray = new int[n+2];
        vector<vector<int> > record(n+2,vector<int>(n+2,0));
        newArray[0] = newArray[n+1] = 1;
        for(int i=1; i<n+1; ++i)    newArray[i] = nums[i-1];
        return getMax(newArray,record,0,n+1);
    }
private:
    int getMax(int newArray[],vector<vector<int> >&record, int low,int high){
        if(low>high)    return 0;
        if(record[low][high])   return record[low][high];
        int max = 0;
        for(int i=low+1; i<high; ++i){
            int tmp = 0;
            tmp += newArray[i]*newArray[low]*newArray[high];
            tmp += getMax(newArray,record,low,i);
            tmp += getMax(newArray,record,i,high);
            if(tmp>max) max = tmp;
        }
        record[low][high] = max;
        return max;
    }
};

算法二:动态规划
上面的分治算法是至上而下算的,用动态规划正好反过来,自下而上,这样就可以同一个值避免重复计算多次。算法时间复杂度是O(n^3)

class Solution {
public:
    int maxCoins(vector<int>& nums) {
        int n = nums.size();
        int *newArray = new int[n+2];
        vector<vector<int> > record(n+2,vector<int>(n+2,0));
        newArray[0] = newArray[n+1] = 1;
        for(int i=1; i<n+1; ++i)    newArray[i] = nums[i-1];
        for(int k=2; k<n+2; ++k){
            for(int l=0;l+k<n+2;++l){
                int h = l + k;
                for(int m=l+1; m<h; ++m){
                    int temp = newArray[l]*newArray[m]*newArray[h]+record[l][m]+record[m][h];
                    if(temp>record[l][h])
                        record[l][h] = temp;
                }
            }
        }
        return record[0][n+1];
    }
};

在LeetCode上,两个算法耗时分别为42ms和29ms,使用动态规划效率高一些。

参考链接:https://discuss.leetcode.com/topic/30746/share-some-analysis-and-explanations

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值