312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示。
现在要求你戳破所有的气球。每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币。 这里的 left 和 right 代表和 i 相邻的气球的序号。 注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球。
求所能获得硬币数量的最大值。
注意:
(1) 你可以认为 nums[-1] = nums[n] = 1,但注意它们不是真实存在的所以并不能被戳破。
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
例子:
输入 [3, 1, 5, 8]
输出 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
详见:https://leetcode.com/problems/burst-balloons/description/
C++:
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];
}
};
参考:https://www.cnblogs.com/grandyang/p/5006441.html
312 Burst Balloons 戳气球的更多相关文章
- [LeetCode] 312. Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] 312. Burst Balloons 爆气球
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- 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 ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 【LeetCode】312. Burst Balloons 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
随机推荐
- cogs——21. [HAOI2005] 希望小学
21. [HAOI2005] 希望小学 ★★ 输入文件:hopeschool.in 输出文件:hopeschool.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述 ...
- Mutual Training for Wannafly Union #9
A(SPOJ NPC2016A) 题意:给一个正方形和内部一个点,要求从这个点向四边反射形成的路线的长度 分析:不断做对称,最后等价于求两个点之间的距离 B(CF480E) 题意:求01矩阵内由0组成 ...
- 洛谷 P1566 加等式
P1566 加等式 题目描述 对于一个整数集合,我们定义“加等式”如下:集合中的某一个元素可以表示成集合内其他元素之和.如集合{1,2,3}中就有一个加等式:3=1+2,而且3=1+2 和3=2+1是 ...
- MongoDB小结17 - find【查询条件$or】
我们再添加一个游泳的人,并用$in查询游泳的人 db.user.find({"hobby":{"$in":["swimming"]}},{& ...
- pycharm支持react
安装nodejs插件 使能node 出现下面的变化,在scope里可以定义使用的范围 创建react项目 使能eslint规则检查功能 配置前端启动脚本: https://www.jetbrains. ...
- 表单验证的完美解决方案Validform
具体使用方法参考官网介绍吧,相当的简单: 文档:http://validform.rjboy.cn/document.html demo:http://validform.rjboy.cn/demo. ...
- hive中使用正則表達式不当导致执行奇慢无比
业务保障部有一个需求,须要用hive实时计算上一小时的数据.比方如今是12点,我须要计算11点的数据,并且必须在1小时之后执行出来.可是他们用hive实现的时候发现就单个map任务执行都超过了1小时, ...
- WEB端应该使用DataTable/DataSet吗?
有一次和同事讨论起具体的技术细节,同事说不要用什么实体类,从数据库访问到的数据,直接用DataTable.DataSet 就好.理由是,从获取到的数据集转换成实体类,有一定的性能损耗. 呵呵,性能.我 ...
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
- [mac]WireShark检測不到网卡怎么办?
wireshark是一个很好用的抓包工具,有windows版本号和mac版本号,在mac下安装wireshark,启动后发现提示"There are no interfaces on whi ...