[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 ...
随机推荐
- 【Laravel5.5】 laravel5 数据库配置(MySQL)
1 进入laravel根目录. 在config目录下找到database.php文件. 显而易见这个文件是数据库相关的配置文件. 2 修改 .env 配置完database. ...
- liunx trac 安装记录
1,下载地址 http://trac.edgewall.org/ 2.安装 apache,python, mysql 3,安装trac (我的是0.12) tar -zxvf 你下载的安装包 ...
- 修改JS文件不能及时在页面中体现,需重启浏览器?
对JS文件做个小小的改动,哪怕是加一句简单的ALERT语句,都要重启浏览器才能看到.你有这样的烦恼吗?怎样不用重启浏览器就能及时体现JS的变化呢? 对浏览器(IE)做如下设置即可:1.点击 工具栏 - ...
- State Server实现多机器多站点 Session 共享 全手记
网络环境有2台windows 2008 (192.168.1.71,192.168.1.72) 需要部署成 WebFarm,提高容错性. 网站部署在2台机器上的2个站点,如何才能做到Session的共 ...
- 美团开源 SQL 优化工具 SQLAdvisor
https://www.oschina.net/news/82725/sqladvisor-opensource https://github.com/Meituan-Dianping/SQLAdvi ...
- flask跨域请求
跨域文件上传的时候,浏览器会自动发起一个 OPTIONS 方法到服务器,现在后台解决前端跨域解决前端跨域请求的问题 客户端发起的这个 OPTIONS 可以说是一个“预请求”,用于探测后续真正需要发起的 ...
- python的十进制与任意进制的转换
将任意进制转换成十进制 ", 8)) # 表示把8进制的54转换成十进制数并输出结果. # 8可以是2.8,10,16等进制数 将十进制转换成任意进制 def f(n,x): #n为待转换的 ...
- CH0101 a^b & CH0102 64位整数乘法
大数取模的两道题. 虐狗宝典学习笔记: 两个数值执行算术运算时,以参与运算的最高数值类型为基准,与保存结果的变量类型无关.两个32位整数的成绩可能超过int类型的表示范围,但是CPU只会用一个32位寄 ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- Oracle to_date()函数的用法介绍
to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,需要的朋友可以参考下 在Oracle数据库中,Oracle t ...