动态规划——Burst Ballons
给定n个气球。每次你可以打破一个,打破第i个,那么你会获得nums[left] * nums[i] * nums[right]个积分。 (nums[-1] = nums[n] = 1)求你可以获得的最大积分数。
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
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
我们可以想象:最后的剩下一个气球为i的时候,可以获得的分数为:nums[-1]*nums[i]*nums[n].
那么介于i,j之间的x,有: dp[i][j] = max(dp[i][j], dp[i][x – 1] + nums[i – 1] * nums[x] * nums[j + 1] + dp[x + 1][j]); 这个非常的像矩阵连乘法那个题。
public int maxCoins(int[] nums) {
int len = nums.length;
int[]num = new int[len+2];
int[][]dp = new int[len+2][len+2];
for(int i = 0;i<=len+1;i++)
if(i==0||i==len+1)num[i] = 1;
else num[i] = nums[i-1];
int j = 0,temp = 0;
for(int k = 1;k<=len;k++) {
for(int i = 1;i<=len-k+1;i++) {
j = i+k-1;
for(int x = i;x<=j;x++) {
temp = dp[i][x-1]+num[i-1]*num[x]*num[j+1]+dp[x+1][j];
dp[i][j] = dp[i][j]>temp?dp[i][j]:temp;
}
}
}
return dp[1][len];
}
动态规划——Burst Ballons的更多相关文章
- 动态规划-Burst Balloons
Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it ...
- 动态规划-击爆气球 Burst Balloons
2018-10-03 19:29:43 问题描述: 问题求解: 很有意思的题目,首先想到的是暴力遍历解空间,当然也用到了memo,可惜还是TLE,因为时间复杂度确实有点过高了,应该是O(n!). Ma ...
- Burst Balloons(leetcode戳气球,困难)从指数级时间复杂度到多项式级时间复杂度的超详细优化思路(回溯到分治到动态规划)
这道题目做了两个晚上,发现解题思路的优化过程非常有代表性.文章详细说明了如何从回溯解法改造为分治解法,以及如何由分治解法过渡到动态规划解法.解法的用时从 超时 到 超过 95.6% 提交者,到超过 9 ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- LeetCode Burst Balloons
原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...
- UVA1627-Team them up!(动态规划)
Problem UVA1627-Team them up! Total Submissions:3577 Solved:648 Time Limit: 3000 mSec Problem Descr ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- [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 ...
- [LeetCode] 312. Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
随机推荐
- Day038--Python--Gevent , IO多路复用
1. 协程: gevent (遇到IO自动切换) import gevent import time from gevent import monkey; monkey.patch_all() # ...
- mac安装postman
[在线方式]: 1. chrome扩展中搜postman,选择Postman Interceptor 添加到chrome. 2.安装完毕后,点击地址栏右边对应的按钮 点击 Postman Chrome ...
- Linux shell脚本启动 停止 重启jar包
最近做的微服务jar包想弄在持续集成中自动化部署,所以首先得有一个操作jar包的脚本 只需将jar文件的路径替换到APP_NAME的值就可以了,其他不用改 注意:window编辑的shell文件,通过 ...
- css浮动(float)全方位案例解析
前言 浮动最早的使用是出自<img src="#" align="right" />,用于文本环绕图片的排版处理.当然也是一种常用的布局方式. fl ...
- npm cnpm yarn
npm 如何下载指定版本的组件 先确保文件目录下含有 package.json 文件, 没有的话,可以通过 npm init 创建, 然后只需要在组件的后面加上 @2.8.1 版本号即可, 例如:re ...
- 【OpenGL】搭建opgl环境
*GLFW+GLEW环境. 工具: GLFW库(下载) GLEW库 cMake软件(下载) 用cMake编译GLFW和GLEW成vs工程文件包,运行得到编译后文件. 在编译后文件夹中找到各个必需文件, ...
- RT-SA-2019-005 Cisco RV320 Command Injection Retrieval
Advisory: Cisco RV320 Command Injection RedTeam Pentesting discovered a command injection vulnerabil ...
- win10安装VMware v14.1.1.28517
一.下载 VMware v14.1.1.28517 下载地址(包含安装说明):http://www.downza.cn/soft/74728.html 二.VMware Workstation 14 ...
- IDEAL字体颜色修改
IDEA 炫酷的主题字体颜色设置(基于IDEA 2018)前言: IDEA中主题可以更换,大家可以直接到 http://www.riaway.com/ 网站或 http://color-themes ...
- Django模型层-多表操作
多表操作 一.创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是 ...