zoukankan      html  css  js  c++  java
  • 【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];
        }
    };
  • 相关阅读:
    thinkphp 3.2 服务器 session 设置时间周期失效问题 服务器是linux windows 上暂时没有发现此类问题
    php 不常见的开发模式
    js 里面的 call 方法 和 apply 方法
    PHP读取文件目录, 并显示需要的目录
    PHP 时间格式
    Python之旅.第三章.函数
    Python之旅.第三章.函数
    Python之旅.第三章.函数
    Python之旅.第三章.函数
    Python之旅.第三章.函数
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6815806.html
Copyright © 2011-2022 走看看