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. JMH 使用指南

    简介 JMH(Java Microbenchmark Harness)是用于代码微基准测试的工具套件,主要是基于方法层面的基准测试,精度可以达到纳秒级.该工具是由 Oracle 内部实现 JIT 的大 ...

  2. 动手个性化设置自己的 IntelliJ IDEA

    前言 IDEA 是一个智能开发工具,每个开发者的使用习惯不同,如何个性化自己的IDEA? 我们可以通过 Settings 功能来设置. Settings文件是 IDEA 的配置文件,通过它可以设置主题 ...

  3. OOP作业总结一

    PS:建议用 Edge 查看此博客,Chrome 的话文章界面会有点窄,看起来可能会比较难受,我想改宽点但是不会改. 我会改了!改宽了一些,现在看起来舒服了很多,芜湖. 问题数据已修复,我们胜利辣! ...

  4. Linux usb 5. usbip (USB Over IP) 使用实例

    文章目录 0. 简介 1. Server 配置 2. Client 配置 参考资料 0. 简介 USB Over IP 是一种应用很多的场景,目前已经有现成的解决方案 usbip.linux 和 wi ...

  5. [cf1515G]Phoenix and Odometers

    显然这条路径只能在$v_{i}$所在的强连通分量内部,不妨仅考虑这个强连通分量 对这个强连通分量dfs,得到一棵外向树(不妨以1为根) 考虑一条边$(u,v,l)$,由于强连通,总存在一条从$v$到$ ...

  6. [noi713]魔法

    分治,维护一个dp数组,当递归到区间[l,r]时,需要保证这个dp数组维护的是除去[l,r]以外的dp数组维护其实很简单,就是递归左区间是先将右区间加入,然后再将左区间加入(要先复原)然后递归右区间即 ...

  7. cube+FreeRTOS联合开发采坑笔记

    加了看门狗之后不断重启的可能 原因: 任务容量分配不足,在"FreeRTOSConfig.h"的配置中,有个configTOTAL_HEAP_SIZE中将堆大小调到最大.

  8. Jetpack架构组件学习(2)——ViewModel和Livedata使用

    要看本系列其他文章,可访问此链接Jetpack架构学习 | Stars-One的杂货小窝 原文地址:Jetpack架构组件学习(2)--ViewModel和Livedata使用 | Stars-One ...

  9. buu

    buuCTFwp(1~32) 1.签到题 题里就有flag flag{buu_ctf} 2.二维码 1.题目是一个二维码,用010发现提示四位数字,想到应该是暗藏压缩包 2.虚拟机foremost分离 ...

  10. CSS 基础 - Cascade and Inheritance

    CSS 基础 - Cascade and Inheritance MDN学习笔记:https://developer.mozilla.org/zh-CN/docs/Learn/CSS/Building ...