[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:167nums = [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 ...
随机推荐
- 安装与使用 Trac 来管理项目
1 概要 以 Windows 平台为例,讲述一下有关 Trac 的基本配置与使用,同时分享一下有关 Trac 的项目管理经验. 2 安装 Trac 前的准备工作 2.1 准备工作 安装 Pytho ...
- css笔记 - 张鑫旭css课程笔记之 line-height 篇
一.line-height line-height: 指两行文字基线之间的距离. 行高200px表示两行文字基线之间的距离是200px: 二.基线:baseline 字母x下边缘的位置 基线是任意线定 ...
- css3整理--border-radius
1.border-radius 标准: border-top-left-radius: x y // 左上角,x 圆角水平半径, y 圆角垂直半径 border-top-right-radius:x ...
- php curl-class post
use \Curl\Curl; $curl = new Curl();$curl->setHeader('Content-Type', 'application/json');$curl-> ...
- myisam innodb 次级 索引的区别
MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.下图是MyISAM索引的原理图: 这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索 ...
- 【转】.Net+MySQL组合开发 乱码篇
所用工具MySQL5.022VS2005 Team SuiteMySQL Connector Net 5.0.3EMS SQL Manage 2005 For MySQL使用过MySQL的朋友都知道有 ...
- Android aapt使用小结
Android打包成Apk后,其实是一个压缩文件,我们用winrar打开也能看到里面的文件结构.还能看到AndroidManifest.但是里面的内容有点问题. 不知道是因为加密还是Android就是 ...
- mysql概要(四)order by ,limit ,group by和聚合函数的特点,子查询
1.order by 默认按升序排列(asc/desc),多字段排序 order by 字段 排序方式,字段2 排序方式,..: 在分组排序中,排序是对分组后的结果进行排序,而不是在组中进行排序. s ...
- Spring Cloud Eureka 服务消费者
参考<spring cloud 微服务实战> 现在已经构建了服务注册中心和服务提供中心,下面就来构建服务消费者: 服务消费者主要完成:发现服务和消费服务.其中服务的发现主要由Eureka的 ...
- Java-查询已创建了多少个对象
//信1603 //查询创建了多少个对象//2017.10.19public class Lei {//记录对象个数 ;//生成一个对象就自加加 public Lei() { x++; }public ...