【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() == ) {
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的更多相关文章
- 【LeetCode】312. Burst Balloons 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- 【LEETCODE】67、分治递归,medium&hard级别,题目:215、312
我被这些题整哭了,你呢??? 日了狗啊...... 好难啊.... 按照这个样子搞,不用找工作了,回家放牛去....... package y2019.Algorithm.divideandconqu ...
- 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 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- mycat可以干什么
单纯的读写分离,此时配置最为简单,支持读写分离,主从切换 分表分库,对于超过 1000 万的表进行分片,最大支持 1000 亿的单表分片 多租户应用,每个应用一个库,但应用程序只连接 Myca ...
- warning: mysql-community-libs-5.7.11-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5
1.错误描写叙述 [root@ mysql]# rpm -ivh mysql-community-libs-5.7.11-1.el7.x86_64.rpm warning: mysql-communi ...
- 通过PHP获取文件创建与修改时间
1.获取文件创建时间示例: 1 2 $ctime=filectime("chinawinxp.txt"); echo "创建时间:".date("Y- ...
- JAVA中两个Set比较找出交集、差集、并集
当做到某些功能的时候,使用Set能够快速方便地将需要的类型以集合类型保存在一个变量中,Set是最简单的一种集合,集合中的对象不按特定的方式排序,并且没有重复对象. //两个Set比较找出交集.差集.并 ...
- 03 redis之string类型命令解析
Redis字符串类型的操作 set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , ...
- hive job oom问题
错误信息例如以下:Container [pid=26845,containerID=container_1419056923480_0212_02_000001] is running beyond ...
- maven scope runtime
https://blog.csdn.net/ningbohezhijunbl/article/details/25818069 There are 6 scopes available: compil ...
- 九度OJ 1066:字符串排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5632 解决:2299 题目描述: 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出排序后的 ...
- 九度OJ 1018:统计同成绩学生人数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8807 解决:4651 题目描述: 读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入: 测试输入包含若干测试用例,每个测试用例的 ...
- static 不被实例调用
static - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/ ...