DP-Burst Balloons
leetcode312:
https://leetcode.com/problems/burst-balloons/#/description
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
原文链接:
http://blog.csdn.net/xyqzki/article/details/50255345
代码:
class Solution(object):
def maxCoins(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0: return 0
nums = [1] + nums + [1]#这里可以像ref一样,把nums里面的0排除掉
size = len(nums)
dp = [[0]*len(nums) for x in range(len(nums))]#这样初始化,[0]*len(nums)是[0,0,0...] for k in xrange(2, size):
for i in xrange(size - k):
j = i + k
p = i + 1
while p < j:
dp[i][j] = max(dp[i][j], dp[i][p] + dp[p][j] + nums[i]*nums[p]*nums[j])
p += 1
return dp[0][size - 1]
一开始我自己的想法是把这个问题转化为背包问题
开始时气球队列为空,从0到n依次加入气球
对每个新加入的气球有如下选项:最先爆炸和在先前的某一个气球爆炸后爆炸
但是经过思考发现这样的思路难以实现.要求对于每一个部分记录详细的爆炸轨迹,时间开销十分昂贵
看到xyqzki的攻略后豁然开朗
dp[l][r]表示扎破(l, r)范围内所有气球获得的最大硬币数,不含边界;
dp矩阵所有初值置0
因为计算的value不含边界
所以开始的时候长度为3 即 0-2 1-3 2-4
左边界为l 右边界为r 访问的值为i
对于每一个i 考虑: 当i为最后一个爆炸的气球时value为多少,判断是否更新dp[l][r]的值
当i最后爆炸时,所产生的value为num[i]*num[l]*num[r]+dp[l][i]+dp[i][r]
红字部分是因为i最后爆炸而扎破的气球不包含边界,所以i爆炸时边界lr必定还存在而(l,r)中除了i的气球已经全部爆炸,所以i爆炸时产生的局部value为红字部分
这题题解看完总算对dp有了点感觉了
DP-Burst Balloons的更多相关文章
- [LeetCode] Burst Balloons (Medium)
Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...
- 动态规划-Burst Balloons
Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it ...
- 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(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- LeetCode Burst Balloons
原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...
- Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
随机推荐
- dotnet OpenXML 转换 PathFillModeValues 为颜色特效
在 OpenXml 预设形状,有一些形状设置了 PathFillModeValues 枚举,此枚举提供了亮暗的蒙层特效.具体的特效是让形状选择一个画刷,在画刷上加上特效.如立体几何 Cube 形状,在 ...
- django HTML 数据处理
一.介绍 dgango HTML 对 各种数据类型数据的调用展示 的个人工作总结 二.数据处理 1.元祖数据 t1 =('a','b','c',) 示例: {{ t1.0 }} {{ ...
- Cesium实现右键框选
思路 1.先取消地图的右键事件 2.右键框选事件,屏幕坐标转为经纬度坐标 取消地图的右键事件 //此处容易犯一个错误:以为右键事件绑定了缩放功能,伪代码即 Cesium.MouseEvent.右键事件 ...
- java中的泛型设计
1.为什么要使用泛型程序设计 ArrayList<String> files = new ArrayList<>() 等价于 var files = new ArrayList ...
- RedHat 7.0 下 FTP 服务的安装,启动,配置,以及虚拟用户的建立
(注意! 区分shell命令和往配置文件里加的代码不同) 一:ftp服务的安装,启动和启用. 1:vim /etc/sysconfig/selinux 改为disabled后重启 ...
- 基于Mui与H5+开发webapp的Android原生工程打包步骤(使用新版本5+SDK与Android studio)(部分内容转自dcloud官网)
文章背景: dcloud官网给出的打包步骤对于有一定安卓打包基础的同学来说比较容易掌握,但是对于webapp小白来讲有的地方可能没有说的太具体.下面我给大家介绍的详细一点,保证大家按照步骤就能学会打包 ...
- Unity——有限状态机FSM修改
FSM状态机改 一.前言 FSM状态机初版 之前写过一版有限状态机,后来发现很多问题: 前一个版本是记录了当前的状态,切换状态时,要等下一帧状态机Update的时候才会调动上个状态的退出,总会有一帧的 ...
- JS表格显示时间格式
<!-- JS代码区 --> <script type='text/javascript'> $(function() { var grid_selector23 = &quo ...
- CSS学习笔记:grid布局
目录 一.Grid布局简介 二.Grid布局的一些概念 三. 容器元素属性 1. grid-template-* 1.1 网格行和列的设置 1.2 repeat的使用 1.3 使用fr 1.4 aut ...
- 剖析虚幻渲染体系(12)- 移动端专题Part 3(渲染优化)
目录 12.6 移动端渲染优化 12.6.1 渲染管线优化 12.6.1.1 使用新特性 12.6.1.2 管线优化 12.6.1.3 带宽优化 12.6.2 资源优化 12.6.2.1 纹理优化 1 ...