[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
Explanation:
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
这个题目的思路也是利用 区间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 ...
随机推荐
- 安卓下junit测试
安卓下junit测试 第一种方法: 1,在AndroidManifest.xml下,加入如下红色代码 <manifest xmlns:android="http://schemas.a ...
- shell截取字符串的一些简单方法
一.使用${} 1.${var##*/}该命令的作用是去掉变量var从左边算起的最后一个'/'字符及其左边的内容,返回从左边算起的最后一个'/'(不含该字符)的右边的内容.使用例子及结果如下:
- Qt编写气体安全管理系统(界面超漂亮)
自从把Qt样式表葵花宝典这个pdf文件看完以后,将所有的qss内容都轮了一遍,还写了个皮肤生成器工具,https://blog.csdn.net/feiyangqingyun/article/deta ...
- 自己实现atoi
bool myatoi(const char *s,int &num) { cout<<(&s)<<endl; num=; while (*s) { ') { ...
- C# 多线程ManualResetEvent、等待所有线程
需求:成员A可能有几十个,我需要更新所有的A,然后根据A的数据,去更新成员B. 解决方案:思路是想通过多线程更新所有的A,然后通过等待线程来确定所有的A是否都更新完,最后更新B. Member B = ...
- 【转】RTMP/RTP/RTSP/RTCP协议对比与区别介绍
用一句简单的话总结:RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步. 之所以以前对这几个有点分不清,是因为CTC标准里没有对RTCP进行要求,因此在标准RTSP的代码 ...
- centos6.8升级python3.5.2
1.查看系统python版本 [root@myserver01 Python-]# python -V Python 2.升级3.5.2 A.下载:wget https://www.python.or ...
- 7.18python进程池
主要方法 常用的就是 from multiprocessing import Pool map() 同步 apply() 同步 apply_async() 异步 手动 close() jo ...
- 对crf++的template的理解 ©seven_clear
这是以前的一篇草稿,当初没写完,今天发出来,但总觉得水平有限,越学越觉得自己菜,写的博客水准低,发完这篇以后就谨慎发博了,毕竟自己菜,不能老吹B,下面是原稿. 好久没更了,本来年前想写篇关于爬虫的总结 ...
- Timer应用之Interval优化
开发中, 有时有这种场景,使用 Timer 的 Timer_Elapsed 间隔 执行(如:从数据库)获取数据 与 现有 应用服务器中的 静态变量数据(起到缓存的目的)做 对比 ,若有改变,则 更新 ...