【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: 
(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

  

题解:

Solution 1 (TLE)

class Solution {
public:
    void helper(vector<int> nums, int cur, int& res) {
        if(nums.size() == 2) {
            if(cur > res) res = cur;
            return;
        }
        for(int i=1; i<nums.size()-1; ++i) {
            int tmp = nums[i];
            cur += nums[i-1]*nums[i]*nums[i+1];
            nums.erase(nums.begin()+i);
            helper(nums, cur, res);
            nums.insert(nums.begin()+i,tmp);
            cur -= nums[i-1]*nums[i]*nums[i+1];
        }
    }
    int maxCoins(vector<int> nums) {
        nums.insert(nums.begin(),1);
        nums.push_back(1);
        int res = INT_MIN;
        helper(nums, 0, res);
        return res;    
    }
};

Solution 2 ()

class Solution {
public:
    int maxCoins(vector<int>& nums) {
        int n = nums.size();
        nums.insert(nums.begin(), 1);
        nums.push_back(1);
        vector<vector<int> > dp(nums.size(), vector<int>(nums.size() , 0));
        for (int len = 1; len <= n; ++len) {
            for (int left = 1; left <= n - len + 1; ++left) {
                int right = left + len - 1;
                for (int k = left; k <= right; ++k) {
                    dp[left][right] = max(dp[left][right], nums[left - 1] * nums[k] * nums[right + 1] + dp[left][k - 1] + dp[k + 1][right]);
                }
            }
        }
        return dp[1][n];
  /*    for (int len = 2; len <= n+1; ++len) {
            for (int left = 0; left <= n - len + 1; ++left) {
                int right = left + len;
                for (int k = left+1; k < right; ++k) {
                    dp[left][right] = max(dp[left][right], nums[left] * nums[k] * nums[right] + dp[left][k] + dp[k][right]);
                }
            }
        }
        return dp[0][n+1]; */
    }
};

 

 

Solution 3 ()

class Solution {
public:
    int maxCoins(vector<int>& nums) {
        int n = nums.size();
        nums.insert(nums.begin(), 1);
        nums.push_back(1);
        vector<vector<int> > dp(nums.size(), vector<int>(nums.size() , 0));
        return burst(nums, dp, 1 , n);
    }
    int burst(vector<int> &nums, vector<vector<int> > &dp, int left, int right) {
        if (left > right) return 0;
        if (dp[left][right] > 0) return dp[left][right];
        int res = 0;
        for (int k = left; k <= right; ++k) {
            res = max(res, nums[left - 1] * nums[k] * nums[right + 1] + burst(nums, dp, left, k - 1) + burst(nums, dp, k + 1, right));
        }
        dp[left][right] = res;
        return res;
    }
};

Solution 4 ()

class Solution {
public:
    int maxCoins(vector<int>& nums) {
        nums.insert(nums.begin(),1);
        nums.push_back(1);
        const auto N=nums.size();
        vector<int> m(N*N);
        for(size_t l=2;l<N;l++)
        {
            for(size_t i=0;i+l<N;i++)
            {
                const size_t j=i+l;
                int v=0;
                for(size_t k=i+1;k<j;k++)
                {
                    v=max(v,nums[i]*nums[k]*nums[j]+m[i*N+k]+m[k*N+j]);
                }
                m[i*N+j]=v;
            }
        }
        return m[N-1];
    }
};

 

posted @ 2017-05-07 21:25  Vincent丶丶  阅读(368)  评论(0编辑  收藏  举报