动态规划-击爆气球 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 ...
随机推荐
- bzoj 1095 Hide 捉迷藏 - 动态点分治 -堆
Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条双 ...
- android 去掉主题
1 可以在xml中配置2.0之后 <application android:allowBackup="true" android:icon="@drawable ...
- Python3基础 list pop(含参) 取出列表中的指定索引的元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 如何查看sonarqube的版本
Server Logs & System Info The System Info page is found at Administration > System. It gives ...
- hihoCoder week6 01背包
01背包 题目链接 https://hihocoder.com/contest/hiho6/problem/1 #include <bits/stdc++.h> using namespa ...
- Redis-Sentinel
Redis-Sentinel是Redis官方推荐的高可用性(HA) 解决方案,Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能 ...
- eclipse创建maven web项目工程步骤示例
参考链接:https://www.cnblogs.com/noteless/p/5213075.html 需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的ecli ...
- 搭建springboot环境
1.前戏准备: SpringBoot核心jar包:这里直接从Spring官网下载了1.5.9版本. jdk:jdk1.8.0_45. maven项目管理工具:3.5版本. tomcat:8.5版本. ...
- Qt基础学习(3)-----滑动条之QSlider
//mydialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class QLineEdit; class Q ...
- 【Selenium2】【Jenkins】
1. 下载Tomcat ,Windows7 环境,http://tomcat.apache.org/ 我下载的是版本8 2. 下载Jenkins,Windows7 环境,http://jenkins ...