【LEETCODE】312-Burst Balloons
来源:互联网 发布:数据库安全性控制 编辑:程序博客网 时间:2024/05/18 02:09
Given n balloons, indexed from0 ton-1. Each balloon is painted with a number on it represented by arraynums. You are asked to burst all the balloons. If the you burst ballooni you will getnums[left] * nums[i] * nums[right] coins. Hereleft andright are adjacent indices ofi. After the burst, theleft andright 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
参考:
http://www.hrwhisper.me/leetcode-burst-balloons/
我们可以想象:最后的剩下一个气球为i的时候,可以获得的分数为:nums[-1]*nums[i]*nums[n].
那么介于i,j之间的x,有:
dp[i][j]=max(dp[i][j],DP(i,x-1)+nums[i-1]*nums[x]*nums[j+1]+DP(x+1,j));
小白+自学=都快怀疑自己的智商了 不开森
class Solution(object): def maxCoins(self, nums): """ :type nums: List[int] :rtype: int """ n=len(nums) nums=[1]+nums+[1] dp=[[0 for j in range(n+2)] for i in range(n+2)] # def DP(i,j): if dp[i][j]>0: return dp[i][j] for x in range(i,j+1): #在 i,j 这个当前状态中,x为最后剩下的那个数 dp[i][j]=max(dp[i][j],DP(i,x-1)+nums[i-1]*nums[x]*nums[j+1]+DP(x+1,j)) #矩阵存着最大值of 把x爆破后的分+x被剩下前的状态 return dp[i][j] return DP(1,n) #最终要求的就是状态就是 1,n
- leetcode 312 Burst Balloons
- leetcode 312 : Burst Balloons
- 【LEETCODE】312-Burst Balloons
- LeetCode #312: Burst Balloons
- Leetcode (312) Burst Balloons
- LeetCode 312 - Burst Balloons
- Leetcode #312 Burst Balloons
- Leetcode 312: Burst Balloons
- LeetCode #312 - Burst Balloons - Hard
- LeetCode 312 Burst Balloons 题解
- LeetCode 312 Burst Balloons (区间dp)
- Leetcode 312 - Burst Balloons(区间dp)
- leetcode Burst Balloons
- Burst Balloons | LeetCode
- leetcode之Burst Balloons
- [LeetCode]Burst Balloons
- LeetCode Burst Balloons
- leetcode 312. Burst Balloons
- JAVA性能优化之设计模式
- Struts2学习——(十)Struts2标签IV
- ZZULIOJ 1812: sort (字符串乱搞)
- C#中两个static函数互相递归调用时,递归结果不正确的问题
- Mac系统的隐藏文件查看与隐藏
- 【LEETCODE】312-Burst Balloons
- 非关系型数据库的学习笔记
- ubuntu正确输入用户名密码后重复跳回登录界面
- NuGet-步步为营,发送类包。
- udp发送
- 网址收藏记录
- Linux下的一些SSH常用命令总结
- 链表中环的入口结点
- tinyos学习笔记2--由"="引出的问题(Blink例程的学习)