动态规划-击爆气球 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 ...
随机推荐
- 关于使用spring mvc或者resteasy构建restful服务的差别与比较
resteasy 是 jboss的一个开源java api for restful service(JSR 311,sun 2008年发布,最新GA版本是2.0, JAX-RS 2.0 (JSR-33 ...
- CCF 推荐国际国内会议及中文核心期刊要目总览
CCF 推荐国际国内会议及<中文核心期刊要目总览> Ref :http://www.ccf.org.cn/xspj/rgzn/ Notes: dblp 是一个好网站,上面有各种主要会议的论 ...
- python简说(二十)操作excel
一.pip install xlrdpip install xlwtpip install xlutils 二.写excel import xlwtbook = xlwt.Workbook() #新建 ...
- ldap集成nexus
nexus版本:2.14.4 添加nexus支持ldap认证: 管理员登录,点击 Administration --> Server -->Security Settings,将 OSS ...
- bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
- jsp+ajax+servlet+jquery从后台取json数据示例
<span style="font-size:18px;"><%@ page language="java" import="jav ...
- (转)JPA & Restful
参考博客: JPA: https://www.jianshu.com/p/b6932740f3c0 https://shimo.im/docs/zOer2qMVEggbF33d/ Restful: h ...
- oracle单行函数 之 转换函数
to_char(字符串 \ 列, 格式字符串):将日期或者数字变成为字符串显示 注意点:时间字符串或时间类型列 与 格式字符串 必须是一一对应,若是多了少了相关字符会报错(除了使用systemd ...
- SHOI 2017 相逢是问候(扩展欧拉定理+线段树)
题意 https://loj.ac/problem/2142 思路 一个数如果要作为指数,那么它不能直接对模数取模,这是常识: 诸如 \(c^{c^{c^{c..}}}\) 的函数递增飞快,不是高精度 ...
- js点击显示隐藏
这个栗子…… 可以不吃,先预设一个变量表示div的状态,例子中0是显示的,一开始是隐藏的.当点击时判断状态是显示0的还是隐藏1的:如果是显示的就把div隐藏,再把变量改变为1.再次点击时把会判断到变量 ...