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的更多相关文章

  1. [Swift]LeetCode312. 戳气球 | Burst Balloons

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  2. 动态规划-Burst Balloons

    Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it ...

  3. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  4. [LeetCode] Burst Balloons (Medium)

    Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...

  5. 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 ...

  6. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  7. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  8. [LeetCode] 312. Burst Balloons 爆气球

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  9. [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 ...

随机推荐

  1. 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢

    目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...

  2. Spring 学习——Bean容器

    Bean容器初始化 基础 org.springframework.beans org.springframework.context BeanFactory提供配置结构和基本功能,加载并初始化Bean ...

  3. ODAC(V9.5.15) 学习笔记(四)TOraDataSet

    名称 类型 说明 SequenceMode TSequenceMode ODAC可以直接利用Oracle中的序列对象为表的主键赋值,从而实现主键自动增长的功能.该属性决定了在什么场合下使用序列: sm ...

  4. 【做题】cf603E——线段树分治

    首先感谢题解小哥,他在标算外又总结了三种做法. 此处仅提及最后一种做法. 首先考虑题目中要求的所有结点度数为奇数的限制. 对于每一个联通块,因为所有结点总度数是偶数,所以总结点数也必须是偶数的.即所有 ...

  5. 【配置详解】Quartz配置文件详解

    我们通常是通过quartz.properties属性配置文件(默认情况下均使用该文件)结合StdSchedulerFactory 来使用Quartz的.StdSchedulerFactory 会加载属 ...

  6. centos6.5下安装mysql5.6

    链接: https://blog.csdn.net/liangzhuoxun/article/details/81572094 该链接有个错误: 让修改的profile文件立即生效的命令为./etc/ ...

  7. node.js 学习笔记一

    2017-05-01 安装node 我没安装,下载即使用.要全局使用的话把node加入到环境变量中即可. 以下命令环境均为 cmd . 体验 体验一: 在命令行输入 node ,即进入 node 程序 ...

  8. 1、Ansible简介及简单安装、使用

    参考Ansible权威指南:https://ansible-tran.readthedocs.io/en/latest/index.html 以下内容学习自马哥教育 Ansible: 运维工作:系统安 ...

  9. Redux 管理React Native数据

    现在让我们看看大致的流程: React 可以触发 Action,比如按钮点击按钮. Action 是对象,包含一个类型以及相关的数据,通过 Store 的 dispatch() 函数发送到 Store ...

  10. 网站项目所有js css无法引用问题解决方案

    网站页面中的所有js css引用失效,路径确保正确,但是浏览器就是报找不到引用.仔细查找发现问题所在: 报错信息很详细了,就是.NET Framework 版本不同导致.同时也提供了两个解决方案:将. ...