LN : leetcode 312 Burst Balloons
lc 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
DP Accepted
这道题目应该从逆向来思考,选择以某个气球为分割点,那么其左边部分和右边部分都要依赖与那个气球。dp[i][j]表示到最后只剩下i和j所产生金币的最大值,对于i和j之间的k,如果引爆k,那么dp[i][j]被更新成max(dp[i][j], nums[i]*nums[k]*nums[j]+dp[i][k]+dp[k][j]),所以想要得到这个数组最大金币值,需要在首尾各加上一个元素,值为1,此时答案即为dp[0][len-1]。
class Solution {
public:
int maxCoins(vector<int>& nums) {
if (nums.size() == 0) return 0;
nums.insert(nums.begin(), 1);
nums.insert(nums.end(), 1);
int len = nums.size();
vector<vector<int>> dp(len, vector<int>(len, 0));
for (int i = len-3; i >= 0; i--) {
for (int j = i+2; j < len; j++) {
for (int k = i+1; k < j; k++) {
dp[i][j] = max(dp[i][j], nums[i]*nums[k]*nums[j]+dp[i][k]+dp[k][j]);
}
}
}
return dp[0][len-1];
}
};
LN : leetcode 312 Burst Balloons的更多相关文章
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- 312. Burst Balloons - LeetCode
Question https://leetcode.com/problems/burst-balloons/description/ Solution 题目大意是,有4个气球,每个气球上有个数字,现在 ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...
随机推荐
- Swift开发--Storyboard的使用教程
假设App中包含非常多不同的页面,使用Storyboard能够帮你降低实现页面间跳转的胶合代码. 过去的开发人员相应每一个视图控制器分别创建界面设计文件(即"nib"或" ...
- 深入分析JavaWeb Item13 -- jsp指令具体解释
一.JSP指令简单介绍 JSP指令(directive)是为JSP引擎而设计的.它们并不直接产生不论什么可见输出,而仅仅是告诉引擎怎样处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指 ...
- Django框架之ORM
1,字段和字段的参数 1.1>ORM的概念:对象映射模型(Objects Relational Model)是一种为了解决面向对象和关系型数据库存在的互不匹配的现象和技术,简单的说,ORM是通过 ...
- java8--网络编程(java疯狂讲义3复习笔记)
重点复习一下网络通信和代理 java的网络通信很简单,服务器端通过ServerSocket建立监听,客户端通过Socket连接到指定服务器后,通信双方就可以通过IO流进行通信. 需要重点看的工具类:I ...
- make的特殊之处
1 规则的先后顺序问题 规则的先后顺序只会影响默认的目标,没有其它的影响. 2 make对具有相同目标的规则的处理方式 2.1 如果是单冒号 只能有一个规则是有命令的,然后对它们进行合并,即依赖合并. ...
- 什么叫强类型的DATASET ?对DATASET的操作处理?强类型DataSet的使用简明教程
强类型DataSet,是指需要预先定义对应表的各个字段的属性和取值方式的数据集.对于所有这些属性都需要从DataSet, DataTable, DataRow继承,生成相应的用户自定义类.强类型的一个 ...
- 【SCOI 2003】 严格n元树
[题目链接] 点击打开链接 [算法] f[i]表示深度小于等于i的严格n元树 显然,一棵深度小于等于i的严格n元树,就是一个根节点,下面有n棵子树,这n棵子树都是深度小于等于i-1的严格n元树,每棵子 ...
- 数据库无法访问,用户 NT AUTHORITY/SYSTEM或NT AUTHORITY\NETWORK SERVICE登录失败的解决办法
问题:win7中的在IIS 7.0中,在 Default Web Site 目录下挂一虚拟目录. 在相应的应用程序池 DefaultAppPool 设置标识设置成NetworkService. 但是打 ...
- 利用Redis Sentinel实现redis主从自动切换
redis主从配置很简单,只需要在slave的配置里加slaveof 192.168.0.100 6379(master的ip和端口) 如果master有密码再设置 masterauth passwo ...
- 4 Java 如何判定是否存活或者死亡
在堆中存放着几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事就是要确定这些对象之中哪些还活着,哪些对象已经死去. 判断对象是否已经死亡有以下几种算法: 引用计数法算法 定义 : 给对象中添加一 ...