【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 ...
随机推荐
- Struts2学习四----------动态方法调用
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2动态方法调用 - 默认:默认执行方法中的execute方法,若指定类中没有该方法,默认返回success <package nam ...
- Django之forms表单类
Form表单的功能 自动生成HTML表单元素 检查表单数据的合法性 如果验证错误,重新显示表单(数据不会重置) 数据类型转换(字符类型的数据转换成相应的Python类型) 1.创建Form类 from ...
- ReentrentLock重入锁
ReentrentLock lock=new ReentrentLock(); lock.lock(); //锁的代码 finally{ lock.unlock(); } ReentrentLock ...
- rtems 4.11 IRQ (arm,beagle)
arm IRQ入口在 cpukit/score/arm/arm_exec_interrupt.S 中,其中BSP最关心就是 bl bsp_interrupt_dispatch 这句,看看beagle ...
- linux SPI驱动——spi协议(一)
一:SPI简介以及应用 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在 ...
- 规范-Git打标签与版本控制
Git打标签与版本控制规范 前言 本文适用于使用Git做VCS(版本控制系统)的场景. 用过Git的程序猿,都喜欢其分布式架构带来的commit快感.不用像使用SVN这种集中式版本管理系统,每一次提交 ...
- 15 redis 之 aof恢复与rdb服务器间迁移
三:常见的问题 BGREWRITEAOF 后台进程重写AOF BGSAVE 后台保存rdb快照 SAVE 保存rdb快照 LASTSAVE 上次保存时间 Slaveof master-Host por ...
- 目标跟踪之camshift---opencv中meanshift和camshift例子的应用
在这一节中,主要讲目标跟踪的一个重要的算法Camshift,因为它是连续自使用的meanShift,所以这2个函数opencv中都有,且都很重要.为了让大家先达到一个感性认识.这节主要是看懂和运行op ...
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in range(12
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...
- C# 串口发送 陷阱,必须知道的坑
目的:间隔100毫秒持续发送指令 由于 C#串口发送为同步方式发送,发送占用时间较长,导致发送变慢, 自己写工具并手工测试两种波特率发送占用时长如下