动态规划——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 ...
随机推荐
- Mock6 moco框架中如何加入header
新建一个 startupWithHeader.json,这次在request里面添加了headers属性 [ { "description": "这是一个带header的 ...
- Vue(小案例_vue+axios仿手机app)_公共组件(路由组件传参)
一.前言 1.公共轮播图的实现 2.组件传参,公共组件的实现 二.主要内容 1.公共轮播图的实现 (1)分析:当渲染不同的轮 ...
- jenkins持续集成原理
转载: 原文地址:http://www.2cto.com/kf/201609/544550.html 持续集成 开发中,我们经常遇到一些奇怪问题,比如本地可以编译成功的代码但是同事们更新代码后编译出错 ...
- Self-organizing Maps及其改进算法Neural gas聚类在异常进程事件识别可行性初探
catalogue . SOM简介 . SOM模型在应用中的设计细节 . SOM功能分析 . Self-Organizing Maps with TensorFlow . SOM在异常进程事件中自动分 ...
- Matlab调用Java类
第一步:定位Matlab中Java环境的ext目录 新建一个M script文件,或者直接在Matlab的交互式命令行中输入: >> disp(java.lang.System.getPr ...
- ZooKeeper 概述
ZooKeeper:分布式应用程序的分布式协调服务 ZooKeeper:是用于分布式应用程序的分布式开源协调服务 可以简单理解:ZooKeeper = 文件系统 + 通知机制 从设计模式角度理解:Zo ...
- [物理学与PDEs]第2章习题8 一维定常粘性不可压缩流体的求解
考察固定在 $y=0$ 与 $y=1$ 处两个平板之间的定常粘性不可压缩流体沿 $x$ 方向的流动. 设 $p=p(x)$, 且已知 $p(0) =p_1$, $p(L)=p_2$, $p_1> ...
- MacOS下使用远程桌面VNC
1 分为一个server 和 viewer,server端就是可以加入组然后允许远程连接,viewer就是远程连接端 2 server下载地址 https://www.realvnc.com/en/c ...
- Vue技术内幕 出去看看吧 挂载
src\platforms\web\runtime\index.js 挂载 Vue.prototype.$mount = function ( el?: string | Element, hydra ...
- springMVC入门思路整理