[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
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
思路:
看到所有的题解都是考虑最后一个气球爆炸的状态,这里有一些解释。
Well, the nature way to divide the problem is burst one balloon and separate the balloons into 2 sub sections one on the left and one one the right. However, in this problem the left and right become adjacent and have effects on the maxCoins in the future.
Then another interesting idea come up. Which is quite often seen in dp problem analysis. That is reverse thinking. Like I said the coins you get for a balloon does not depend on the balloons already burst. Therefore
instead of divide the problem by the first balloon to burst, we divide the problem by the last balloon to burst.
Why is that? Because only the first and last balloons we are sure of their adjacent balloons before hand!
For the first we have nums[i-1]*nums[i]*nums[i+1] for the last we have nums[-1]*nums[i]*nums[n].
OK. Think about n balloons if i is the last one to burst, what now?
参考大牛分析:http://www.cnblogs.com/grandyang/p/5006441.html
像这种求极值问题,我们一般都要考虑用动态规划Dynamic Programming来做,我们维护一个二维动态数组dp,其中dp[i][j]表示打爆区间[i,j]中的所有气球能得到的最多金币。题目中说明了边界情况,当气球周围没有气球的时候,旁边的数字按1算,这样我们可以在原数组两边各填充一个1,这样方便于计算。这道题的最难点就是找递归式,如下所示:
dp[i][j] = max(dp[i][j], nums[i - 1]*nums[k]*nums[j + 1] + dp[i][k - 1] + dp[k + 1][j]) ( i ≤ k ≤ j )
有了递推式,我们可以写代码,我们其实只是更新了dp数组的右上三角区域,我们最终要返回的值存在dp[1][n]中,其中n是两端添加1之前数组nums的个数。参见代码如下:
int maxCoins(vector<int>& nums)
{
int n = nums.size();
nums.insert(nums.end(), );
nums.insert(nums.begin(), ); vector<vector<int>>dp(n+,vector<int>(n+,));
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], dp[left][k-]+dp[k+][right]+ nums[left-]*nums[k]*nums[right+]);
}
}
}
return dp[][n];
}
参考:
http://www.cnblogs.com/grandyang/p/5006441.html
https://www.hrwhisper.me/leetcode-burst-balloons/
https://discuss.leetcode.com/topic/30746/share-some-analysis-and-explanations
[leetcode-312-Burst Balloons]的更多相关文章
- 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 ... 
- [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] * ... 
随机推荐
- Redis学习-SortedSet
			Sorted-Sets和Sets类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted-Sets中的每一个成员都会有一个分数(score)与之关联, ... 
- docker安装hadoop集群
			docker安装hadoop集群?图啥呢?不图啥,就是图好玩.本篇博客主要是来教大家如何搭建一个docker的hadoop集群.不要问 为什么我要做这么无聊的事情,答案你也许知道,因为没有女票.... ... 
- Hive的分区操作~~~~~~
			一.Hive分区(一).分区概念:为什么要创建分区:单个表数据量越来越大的时候,在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据, ... 
- iOS Regex匹配关键字并修改颜色
			引入第三方框架RegexKitLite /** * 根据传入的文字返回一个符合规则的富文本 * * @param title 匹配的文字 * * @return 创建的富文本 */ -(NSAttri ... 
- java 空指针异常造成的原因有哪些
			实际上 空指针异常 也就是你用了一个没有实际值的对象 1. 某一对象没有被实例化,就拿来使用,如调用此对象的方法,会抛空指针异常. 2. 获取从别的对象传过来的对象为空 3. 数据库查询方面的空指 ... 
- 10分钟学会JAVA注解(annotation)
			(原) 先认识注解(Annotation) 定义类用class,定义接口用interface,定义注解用@interface 如public @interface AnnotationTest{} 所 ... 
- 安卓ios和angularjs相互调用解决首次调用ios传递标题失败的问题
			1.angular 调用客户端方法放在 try catch中 try { js_invoke.showShareDialog(angular.toJson(obj)); // 在这里放客户端的方法即 ... 
- Common.Logging源码解析一
			Common.Logging是Apache下的一个开源日志接口组件,主要用于切换不同的日志库,因为当前流行的日志库有很多向log4j.log4net(log4j的.net版本)等等,所以为了能灵活的切 ... 
- ActionContext、ServletContext、pageContext的区别?
			ActionContext是当前的Action的上下文环境,通过ActionContext可以获取到request.session.ServletContext等与Action有关的对象的引用: Se ... 
- 《物联网框架ServerSuperIO教程》-21.终端控制传感器或设备,形成回路控制。附:demo源代码
			21.1 概述 ServerSuperIO以前所做的工作逐步为形成回路控制或级联控制打下基础,例如:服务连接器和设备驱动连接器的开发与应用.总之,是通过多种形式下发命令控制设备(驱动)或传感器 ... 
