[LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming
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:
- You may imagine
nums[-1] = nums[n] = 1
. They are not real therefore you can not burst them. - 0 ≤
n
≤ 500, 0 ≤nums[i]
≤ 100
Example:
Input:[3,1,5,8]
Output:167
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
Explanation:
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
这个题目的思路也是利用 区间Dynamic Programming, 思想跟[LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈类似,
动态表达式为:
dp[i][j] 是得到的 [i,j] 里面气球的最大值
for k in [i,j]:
value_m = ballons[i-1] * ballons[k] * ballons[j+1]
dp[i][j] = max(dp[i][j], dp[i][k-1] + value_m + dp[k+1][j])
init:
dp[i][i] = ballons[i-1]* ballons[i]* ballons[i+1]
dp[i][j] = 0 if j < i
1. Constraints
1) size [0, 500]
2) element [0, 100]
2. Ideas
Dynamic Programming T: O(n^2) S; O(n^2)
3. Code
class Solution:
def burstBallons(self, nums):
n = len(nums)
ballons = [1] + nums + [1] # plus the corner case
dp = [[0]*(n+2) for _ in range(n+2)] # for corner case
flag = [[0]*(n+2) for _ in range(n+2)]
def helper(l, r):
if flag[l][r]:
return dp[l][r]
if l == r:
dp[l][r] = ballons[l-1] * ballons[l] * ballons[l+1]
elif l < r:
for k in range(l, r+1): # k belongs [l, r], so it is r + 1
value_m = ballons[l-1] * ballons[k] * ballons[r+1]
value_l = helper(l, k-1)
value_r = helper(k+1, r)
dp[l][r] = max(dp[l][r], value_l + value_m + value_r)
flag[l][r] = 1
return dp[l][r]
return helper(1, n)
[LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming的更多相关文章
- [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- [LeetCode] 312. Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- 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 爆气球
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] questions conclusion_ Dynamic Programming
Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...
- 动态规划(Dynamic Programming)算法与LC实例的理解
动态规划(Dynamic Programming)算法与LC实例的理解 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Le ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
随机推荐
- C/C++注册动态对象到Lu系统并进行运算符重载
欢迎访问Lu程序设计 C/C++注册动态对象到Lu系统并进行运算符重载 1 说明 要演示本文的例子,你必须下载Lu32脚本系统.本文的例子需要lu32.dll.lu32.lib.C格式的头文件lu32 ...
- WEB-DICT词库计划
欢迎大家支持晓阳童鞋的词库计划,建立一个庞大的中文词库 地址如下:http://webdict.info/ 什么是WEB-DICT词库计划? WEB-DICT词表计划目标是通过机器学习算法以及人工标注 ...
- 【Spring源码分析系列】bean的加载
前言 以 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过 ...
- 关于windows下基于php7.0.2下编写的第一个扩展
网上的教程是比较多的,但是基于php7+windows的教程非常之少,通过几天的摸索及参考很多资料,终于发现如下可以运行. php7要求使用vc2015,同时安装sdk,我使用的是8.1的window ...
- cmake编译android平台的libPoco
1.下载源代码,解压到POCO-1.7.8-ALL 2.从$ANDROID_NDK\cmake下复制android.toolchain.cmake AndroidNdkGdb.cmake Androi ...
- Elasticsearch学习之多种查询方式
1. query string search 搜索全部商品:GET /ecommerce/product/_search took:耗费了几毫秒 timed_out:是否超时,这里是没有 _shard ...
- 3-2 axios基础介绍
1.静态引用 <script src="https://unpkg.com/axios/dist/axios.min.js"></script> 2.npm ...
- 【CF720D】Slalom 扫描线+线段树
[CF720D]Slalom 题意:一个n*m的网格,其中有k个矩形障碍,保证这些障碍不重叠.问你从(1,1)走到(n,m),每步只能往右或往上走,不经过任何障碍的方案数.两种方案被视为不同,当且仅当 ...
- 有哪些sql优化工具
https://www.oschina.net/p/soar-xiaomi https://www.oschina.net/news/101034/xiaomi-opensource-soar SOA ...
- mysql判断一个字符串是否包含某几个字符
使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0