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 ...
随机推荐
- 使用MediaPlayer播放、暂停、停止音乐
package com.pingyijinren.test; import android.media.MediaPlayer; import android.os.Environment; impo ...
- Codeforces 284E(概率)
题意: 在T秒内,按输入顺序给出n首曲子的播放顺序,玩家需要从第一曲向后听. 第i首曲子有pi和ti两个属性,pi代表在当前这1秒内有pi/100的概率听出这首曲子是什么, ti代表如果一直持续听ti ...
- GNS3模拟的硬件
Hardware emulated by GNS3 Cisco 1700 Series 1700s have one or more interfaces on the motherboard, 2 ...
- HDU 2870 Largest Submatrix (单调栈)
http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...
- Linux系统下ssh登陆很慢的解决办法
很多的Linux用户发现连接上Linux服务器在输入用户名之后还要再等一下才能输入密码,时间过长了,现在小白与大家分享一下如何解决ssh登陆问题的问题,希望对您有所帮助. 1.我们平时登陆Linux服 ...
- Android小技巧总结1
0.Android彻底退出引用程序的方法 1) Dalvik VM的本地方法 android.os.Process.killProcess(android.os.Process.myPid()); / ...
- Objective-C 内存管理retain和release
OC使用引用计数来管理内存,每个继承NSObject的对象,内部都维护了一个引用计数器retainCount.当对象创建时(调用alloc或者new)引用计数器会+1, 手动调用retain()方法能 ...
- 用jquery给元素动态绑定事件及样式
网页输出的时候,可以用jquery给各种元素绑定事件,或设置样式. 之所以这样做,好处是节省代码,尤其适合元素很多,并且元素的事件对应的函数雷同的情况. 看看以下代码: <div id=&quo ...
- java Bean及其使用
1 getter/setter方法 java例子: public class student { private String name; public String getName() { retu ...
- C++new失败的处理(如果 new 分配内存失败,默认是抛出异常的,但也可以取消异常)
我们都知道,使用 malloc/calloc 等分配内存的函数时,一定要检查其返回值是否为“空指针”(亦即检查分配内存的操作是否成功),这是良好的编程习惯,也是编写可靠程序所必需的.但是,如果你简单地 ...