动态规划-击爆气球 Burst Balloons
2018-10-03 19:29:43
问题描述:

问题求解:
很有意思的题目,首先想到的是暴力遍历解空间,当然也用到了memo,可惜还是TLE,因为时间复杂度确实有点过高了,应该是O(n!)。
Map<LinkedList, Integer> map = new HashMap<>();
public int maxCoins(int[] nums) {
if (nums.length == 0) return 0;
LinkedList<Integer> list = new LinkedList<>();
for (int i : nums) list.add(i);
return helper(list);
} private int helper(LinkedList<Integer> list) {
if (list.size() == 1) return list.get(0);
if (map.containsKey(list)) return map.get(list);
int res = 1;
ArrayList<Integer> arr = new ArrayList<>(list);
for (int i = 0; i < list.size(); i++) {
int pre = i == 0 ? 1 : arr.get(i - 1);
int cur = arr.get(i);
int next = i == arr.size() - 1 ? 1 : arr.get(i + 1);
list.remove(i);
res = Math.max(res, pre * cur * next + helper(list));
list.add(i, cur);
}
map.put(list, res);
return res;
}
本题给出了数据规模,基本已经提示了时间复杂度为O(n^3)左右比较合适。下面给出本题的标准解法,使用的是动态规划算法。
dp[i][j]:nums[i]到nums[j]能获得的最高分数
dp[i][j] = max(dp[i][k - 1] + nums[i - 1] * nums[k] * nums[j + 1] + dp[k + 1][j]) i <= k <= j // 每次挑选一个最后打爆的气球可以将问题规模下降
给原问题加上padding可以更方便编程。
public int maxCoins(int[] nums) {
if (nums.length == 0) return 0;
int n = nums.length;
int[] numsPadding = new int[n + 2];
numsPadding[0] = 1;
numsPadding[n + 1] = 1;
for (int i = 1; i <= n; i++) numsPadding[i] = nums[i - 1];
int[][] dp = new int[n + 2][n + 2];
for (int len = 1; len <= n; len++) {
for (int i = 1; i <= n - len + 1; i++) {
int j = i + len - 1;
for (int k = i; k <= j; k++) {
dp[i][j] = Math.max(dp[i][j], dp[i][k - 1] + numsPadding[i - 1] * numsPadding[k] * numsPadding[j + 1] + dp[k + 1][j]);
}
}
}
return dp[1][n];
}
错误记录:
初始化的时候想当然的认为len == 1的时候结果的数值为numspadding[i],导致失败。
动态规划-击爆气球 Burst Balloons的更多相关文章
- [Swift]LeetCode312. 戳气球 | Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 动态规划-Burst Balloons
Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- [LeetCode] Burst Balloons (Medium)
Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...
- 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 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- [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] 452. Minimum Number of Arrows to Burst Balloons 最少箭数爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
随机推荐
- Elasticsearch.Net使用(一)【入门篇】
http://blog.csdn.net/wulex/article/details/52138564 加数据 //在调用下面的index方法的时候,如果没有指定使用哪个index,ElasticSe ...
- (2编写网络)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署
基于<神经网络和深度学习>这本绝好的教材提供的相关资料和代码,我们自己动手编写"随机取样的梯度下降神经网络".为了更好地说明问题,我们先从简单的开始: 1.sigmod ...
- Android Studio报错view is not constrained
在活动上面创建了两个按钮,在Design上看上去是两个按钮分开的,run一下,按钮就重合在一起了,而且一直报错,这个时候再去看一下Design,两个按钮重在一块,只显示一个按钮.如下图: button ...
- dede的应用
gbk和utf-8版本选择 gbk是国家编码,所有的内容编码,包括中文,英文,英文字符都占两个字节. utf-8是国际编码,中文三个字节,英文1个字节 现阶段,网站都用gbk编码, 一方面节省本地/网 ...
- tomcat使用spring-loaded实现应用热部署
springloaded官方说明: Spring Loaded is a JVM agent for reloading class file changes whilst a JVM is runn ...
- javascript创建函数的20种方式汇总
http://www.jb51.net/article/68285.htm 工作中常常会创建一个函数来解决一些需求问题,以下是个人在工作中总结出来的创建函数20种方式,你知道多少? function ...
- Ubuntu 18.04版本下安装网易云音乐
这是我迄今为止发现的最完美的解决方法,不用改任何东西,只需要安装然后打开即可,后台也有. 参考:http://archive.ubuntukylin.com:10006/ubuntukylin/poo ...
- 7、nginx的upstream及fastcgi模块应用
ngx_http_proxy_module, ngx_http_upstream_module ngx_http_proxy_module:实现反向代理及缓存功能 proxy_pass http: ...
- K-近邻
概述 KNN算法本身简单有效,是一种lazy-learning算法: 分类器不需要使用训练集进行训练,训练时间复杂度为0: KNN分类的计算复杂度和训练集中的文档数目成正比,也就是说,如果训练集中文档 ...
- 【Selenium2】【环境搭建】
Windows7 64位 Mozilla Firefox 36.0.4 + Firebug 2.0.19 + FirePath 0.9.7.1.1-signed.1-signed 火狐历史版本:ht ...