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 rightare 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 ...
随机推荐
- Downward API —— 在容器内部获取 Pod 信息
我们知道,每个 Pod 在被超过创建出来之后,都会被系统分配唯一的名字.IP地址,并且处于某个 Namespace 中,那么我们如何在 Pod 的容器内获取 Pod 的这些重要信息呢? 答案就是使用 ...
- JetBrains IntelliJ IDEA汉化
JetBrains IntelliJ IDEA汉化 开启 IntelliJ IDEA,点击右下角Configure菜单,选择 Plugins.在弹出的 Plugins窗口里,切换至 Marketpla ...
- [linux]centos7.4安装nginx
下载nginx wget http://nginx.org/download/nginx-1.5.6.tar.gz 解压包安装在/opt/nginx. 目录下, 1.安装gcc(centos 7之后一 ...
- pipeline学习
目录 一.常用语法 二.基础使用 三.使用 Groovy 沙盒 四.参数化构建过程 五.pipeline script from SCM 六.参考 一.常用语法 1.拉取git仓库代码 checkou ...
- 大一C语言学习笔记(5)---函数篇-定义函数需要了解注意的地方;定义函数的易错点;详细说明函数的每个组合部分的功能及注意事项
博主学习C语言是通过B站上的<郝斌C语言自学教程>,对于C语言初学者来说,我认为郝斌真的是在全网C语言学习课程中讲的最全面,到位的一个,这个不是真不是博主我吹他哈,大家可以去B站去看看,C ...
- 全面的Docker快速入门教程
前言: 都2021年了,你还在为了安装一个开发或者部署环境.软件而花费半天的时间吗?你还在解决开发环境能够正常访问,而发布测试环境无法正常访问的问题吗?你还在为持续集成和持续交付(CI / CD)工作 ...
- 菜鸡的Java笔记 第三十一 扩展结构
.新特性 可变参数的意义以及实现 那么下面通过一个简单的思考来分析可变参数的存在意义 范例:如果说现在要定义一个方法,这个方法可以实现任意多个 ...
- [gym102220I]Temperature Survey
(为了方便,以下记$a_{0}=0,a_{n+1}=n$,并将$n$加上1) 构造一个$n$行的网格图,从上到下第$i$行有$a_{i}$个格子,格子左对齐 记第$i$行第$j$个格子为$(i ...
- [loj3277]星座3
如果不合法,利用贪心发现当且仅当某两个星星所构成的矩形中没有白点 反过来,考虑留下若干个星星,那么即要求留下的星星两两之间满足:$\max_{x_{1}\le i\le x_{2}}a_{i}\ge ...
- 同时在多个 Git 分支上工作,老板要榨干我
背景 上一篇文章 保持清洁的Git提交记录,三招就够了 ,大家看过后有私下留言说这是非常好用的功能,我突然想到工作中用到的另外一个 Git 功能那也是相当好用,必须全盘托出 作为程序员的我们应该都有一 ...