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的更多相关文章

  1. [LeetCode] Burst Balloons (Medium)

    Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...

  2. 动态规划-Burst Balloons

    Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it ...

  3. 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 ...

  4. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  5. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  6. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  7. [LeetCode] Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  8. LeetCode Burst Balloons

    原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...

  9. Burst Balloons

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  10. 312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

随机推荐

  1. 前端调试工具DevTools处理网络请求

    DevTools处理网络请求 位置:network 1.是否启用网络处理功能 2.清除历史 3.过滤器,自定义筛选 4.是否保留先前的历史,因为每次刷新会删除历史重新加载,选中后新老请求都在可做对比 ...

  2. FZU ICPC 2020 寒假训练 1

    B - Sum Problem In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input The i ...

  3. 巧用Python快速构建网页服务器

    经常做web开发,要调试一个网页,直接打开文件,用file模式显然是业余的. 但动辄要部署个IIS或APACHE站点,也确实太累,怎么办? 逐浪君此前有分享过通过http-server来构建快速的we ...

  4. Excel 读取写入数据库

    // Excel 读取写入数据库 // 3.8版本的poi  4.0 可以不用写  parseCell  这个方法,可以直接赋值 STRING 类型 import org.apache.poi.hss ...

  5. 菜鸡的Java笔记 第八 - java 面向对象

    面向对象的特点以及开发过程.    java中最大的特点是其支持面向对象编程设计思想.在面向对象之前广泛流传的是面向过程的编程思想,例如:C语言的开发就属于面向过程    如果要想更简单的去理解面向过 ...

  6. Prometheus+Grafana监控Kubernetes

    涉及文件下载地址:链接:https://pan.baidu.com/s/18XHK7ex_J0rzTtfW-QA2eA 密码:0qn6 文件中需要下载的镜像需要自己提前下载好,eg:prom/node ...

  7. hexo+腾讯云

    hexo+腾讯云主机搭建博客 参考链接1 参考链接2 参考链接3 说明:不建议用hexo在云主机上搭建博客,感觉多此一举,建议hexo+github, wordpress+云主机(宝塔界面更快哦) 一 ...

  8. 简易发号SQL,可用于生成指定前缀自增序列--改进版

    使用merge语法实现新增or更新 首先创建表 CREATE TABLE Test.dbo.Increments ( Prefix varchar(50) NOT NULL, [MaxNum ] bi ...

  9. [atARC121E]Directed Tree

    令$b_{a_{i}}=i$,那么问题即要求$i$不是$b_{i}$的祖先,也即$b_{i}$不严格在$i$的子树中 显然$a_{i}$和$b_{i}$一一对应,因此我们不妨统计$b_{i}$的个数 ...

  10. [hdu6582]Path

    首先,从1和n跑一次dij,判断每一条边能否出现在最短路上,不能出现就删掉,然后将所有边建在图上,流量为边权,跑最小割即可. 1 #include<bits/stdc++.h> 2 usi ...