题目:

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() == ) {
if(cur > res) res = cur;
return;
}
for(int i=; i<nums.size()-; ++i) {
int tmp = nums[i];
cur += nums[i-]*nums[i]*nums[i+];
nums.erase(nums.begin()+i);
helper(nums, cur, res);
nums.insert(nums.begin()+i,tmp);
cur -= nums[i-]*nums[i]*nums[i+];
}
}
int maxCoins(vector<int> nums) {
nums.insert(nums.begin(),);
nums.push_back();
int res = INT_MIN;
helper(nums, , res);
return res;
}
};

Solution 2 ()

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

Solution 4 ()

class Solution {
public:
int maxCoins(vector<int>& nums) {
nums.insert(nums.begin(),);
nums.push_back();
const auto N=nums.size();
vector<int> m(N*N);
for(size_t l=;l<N;l++)
{
for(size_t i=;i+l<N;i++)
{
const size_t j=i+l;
int v=;
for(size_t k=i+;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-];
}
};

【LeetCode】312. Burst Balloons的更多相关文章

  1. 【LeetCode】312. Burst Balloons 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...

  2. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  3. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  4. 【LEETCODE】67、分治递归,medium&hard级别,题目:215、312

    我被这些题整哭了,你呢??? 日了狗啊...... 好难啊.... 按照这个样子搞,不用找工作了,回家放牛去....... package y2019.Algorithm.divideandconqu ...

  5. LN : leetcode 312 Burst Balloons

    lc 312 Burst Balloons 312 Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is pa ...

  6. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  7. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. 开始nodejs+express的学习+实践(1)

    开始nodejs+express的学习+实践(1) 开始nodejs+express的学习+实践(2) 开始nodejs+express的学习+实践(3) 开始nodejs+express的学习+实践 ...

  2. requestWindowFeature使用详解

    requestWindowFeature可以设置的值有: // 1.DEFAULT_FEATURES:系统默认状态,一般不需要指定        // 2.FEATURE_CONTEXT_MENU:启 ...

  3. 从epoll构建muduo-12 多线程入场

    mini-muduo版本号传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ve ...

  4. 编译webrtc for android库与apk

    git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git export PATH=`pwd`/depot_t ...

  5. php计算数组的维数

    function array_dim($arr){ if(!is_array($arr)) return 0; else{ $max1 = 0; foreach($arr as $item1){ $t ...

  6. 我的Android进阶之旅------>Android中可替换string的使用,getString(int resId, Object... formatArgs)

    官方文档如下描述: 地址:http://developer.android.com/reference/android/content/Context.html#getString%28int,%20 ...

  7. js添加方法和邦定事件

    function(obj,objArr){ if(($(obj).attr("type") == "checkbox" && $j(obj).p ...

  8. SAP 增强表MODSAP 和TFDIR

    2.第二代增强(基于函数模块的增强),用于SMOD和CMOD 维护 在SAP发布的版本中,使用Call customer-function 'xxx'调用函数模块的, 所以你可以通过在程序中搜索 cu ...

  9. JVM--内存区的划分

    转自:http://www.jianshu.com/p/7ebbe102c1ae Java虚拟机在执行Java程序的过程中会把它管理的内存划分为若干个不同的数据区域. java虚拟机运行时数据区 一. ...

  10. Java中的迭代迭代器Iterator与枚举器Enumeration

    Iterator 和 Enumeration区别 Iterator 和 Eumberation都是Collection集合的遍历接口,我们先看下他们的源码接口 package java.util; p ...