LeetCode 312 Burst Balloons (区间dp)
来源:互联网 发布:淘宝好评怎么走返利 编辑:程序博客网 时间:2024/05/29 12:21
Given n
balloons, indexed from 0
to n-1
. Each balloon is painted with a number on it represented by arraynums
. You are asked to burst all the balloons. If the you burst ballooni
you will get nums[left] * nums[i] * nums[right]
coins. Hereleft
and right
are adjacent indices of i
. After the burst, theleft
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
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
题目分析:设dp[i][j]为i到j这段区间所能得到的最大值,状态转移方程为dp[i][j] = max(i < k < j) (dp[i][k] + dp[k][j] + a[i] * a[k] * a[j])
为了方便计算,将数组扩充一头一尾,值均为1,击败了84%
public class Solution { public int maxCoins(int[] nums) { int n = nums.length + 2; int []a = new int[n]; a[0] = 1; a[n - 1] = 1; for (int i = 0; i < n - 2; i ++) { a[i + 1] = nums[i]; } int [][]dp = new int[n][n]; for (int l = 2; l < n; l ++) { for (int i = 0; i + l < n; i ++) { int j = i + l; for (int k = i + 1; k < j; k ++) { dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[j] * a[k]); } } } return dp[0][n - 1]; }}
0 0
- LeetCode 312 Burst Balloons (区间dp)
- Leetcode 312 - Burst Balloons(区间dp)
- leetcode 312 Burst Balloons
- leetcode 312 : Burst Balloons
- 【LEETCODE】312-Burst Balloons
- LeetCode #312: Burst Balloons
- Leetcode (312) Burst Balloons
- LeetCode 312 - Burst Balloons
- Leetcode #312 Burst Balloons
- Leetcode 312: Burst Balloons
- leetcode -- Burst Balloons -- 重点dp
- LeetCode #312 - Burst Balloons - Hard
- LeetCode 312 Burst Balloons 题解
- [LeetCode 312] Burst Ballons (区间DP)
- 区间DP:POJ 2955括号匹配 + NYOJ 737 石子归并(一) + No.312 Burst Balloons
- 312. Burst Balloons dp
- DP: Burst Balloons
- leetcode Burst Balloons
- MyBatis处理日期和大对象(Blob)
- JS基本语法
- hdu 3549 Flow Problem (最大流—EK—Dinic)
- JS内置对象
- laravel session用法
- LeetCode 312 Burst Balloons (区间dp)
- Python matplotlib生成图片背景透明
- 随便写
- 采集常用的两个函数
- 学习JavaWeb之Servlet_Response
- C#--设计模式之工厂方法模式
- 【框架】[MyBatis]DAO层只写接口,不用写实现类
- 自定义百度网盘分享密码 提取码
- Myeclipse/eclipse开发工具反编译插件JadClpse