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. python简说(八)random

    import randomprint(random.randint(1,23))#随机整数l=[1,2,3,4]s='abcdefeg'print(random.choice(s))#随机选择一个元素 ...

  2. js之 data-*自定义属性

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. Computer Study

    2017.12.15日开始制定长期学习计划: 大二上: 目前短期计划: 1.每天一道PAT乙级(弄完PAT乙级)+校OJ(打基础).   2.先复习C++,17周后开始复习数据结构.(补任务  刚刚发 ...

  4. 牛客网数据库SQL实战(16-20)

    16.统计出当前各个title类型对应的员工当前薪水对应的平均工资.结果给出title以及平均工资avg.CREATE TABLE `salaries` (`emp_no` int(11) NOT N ...

  5. WebSocket、Socket

    https://www.cnblogs.com/jingmoxukong/p/7755643.html#undefined WebSocket 详解教程 WebSocket介绍,与Socket的区别 ...

  6. 如何清除浮动塌陷? float:left 塌陷

    参考文章: http://www.jb51.net/css/502268.html 原文链接:http://www.jianshu.com/p/a0500b54da13 只要给li的宽度, 规定得小一 ...

  7. #pragma data_seg() 共享数据// MyData段 // 进程 // DLL

    https://www.cnblogs.com/dongsheng/p/4476157.html http://www.cnblogs.com/CBDoctor/archive/2013/01/26/ ...

  8. BZOJ 3707 圈地

    闲扯 BZOJ权限题,没有权限,哭了 然后DBZ不知道怎么回事,\(O(n^3)\)直接压过去了... 备忘 叉积的计算公式\(x_1y_2\) 思路 n^3 暴力枚举显然 n^2 正解的思路有点神, ...

  9. vscode "没有活动的源代码控制提供程序" 解决办法

    确保安装git F1,输入Show built-in Extensions, 左侧列表中找Git,然后启用即可

  10. js 字符串加密解密

    Welcome to jzzy.com