LeetCode312. Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
- You may imagine
nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. - 0 ≤
n≤ 500, 0 ≤nums[i]≤ 100
Example:
Input:[3,1,5,8]
Output:167nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
Explanation:
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
思路
解法有backtrack和dp两种,首先还是来分析题目。
这题的要素有:n 个气球,从0到n-1编号,每个气球都标记了一个硬币数字。戳爆其中一个会得到指定的coings,与相邻的两个数字的乘积是所获得的硬币数。那么如何确定戳爆顺序使得获得的硬币数目最大?难点在于不同的戳爆顺序会相互影响。
从最后戳爆的气球位置入手,如果最后戳爆的气球是位置i上的气球,那么可以肯定的是在它之前的 0~i-1 位置上的气球肯定被戳爆了,i+1到n-1位置上的气球也被戳爆了。递归遍历所有的i即可。
public int maxCoins(int[] iNums) {
int[] nums = new int[iNums.length + 2]; // 构造新数组,方便计算
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1; // 这里一直用n++是因为要忽视iNums的0,这样n最后不一定等于iNum的长度
int[][] memo = new int[n][n]
return burst(memo, nums, 0, n - 1); // 从1到n-1位置上搜索,即left+1~right,之所以n-1是因为上面的n++使得n到了末尾1的后一位
}
public int burst(int[][] memo, int[] nums, int left, int right) {
if (left + 1 == right) return 0; // 如果left+1==right,因为是从left+1到right-1处遍历的。遍历结束,直接返回
if (memo[left][right] > 0) return memo[left][right]; // 如果已计算过,则不需再次计算
int ans = 0;
for (int i = left + 1; i < right; ++i) // 初始从left+1到right-1,对应起来正好是iNums中的0~n-1
ans = Math.max(ans, nums[left] * nums[i] * nums[right]
+ burst(memo, nums, left, i) + burst(memo, nums, i, right));
memo[left][right] = ans;
return ans;
}
DP解法如下:
public int maxCoins(int[] iNums) {
int[] nums = new int[iNums.length + 2];
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1;
int[][] dp = new int[n][n];
// k表示计算步长,从right=left+2开始。因为i从left+1到right-1遍历,所以一开始只计算left和right之间只隔了一位的情况
for (int k = 2; k < n; ++k)
for (int left = 0; left < n - k; ++left) {
int right = left + k;
for (int i = left + 1; i < right; ++i) // 在子问题dp[left][right]下,遍历每个可能的最后引爆的气球,以求出子问题的最优解
dp[left][right] = Math.max(dp[left][right],
nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right]); // 左边界是left,右边界是right
}
return dp[0][n - 1];
}
LeetCode312. Burst Balloons的更多相关文章
- [LeetCode] Burst Balloons (Medium)
Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...
- 动态规划-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 ...
- 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 ...
- [Swift]LeetCode312. 戳气球 | Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
随机推荐
- 被动式pocscan扫描神器搭建
1.搭建环境: 操作系统为:ubuntu16.04 x64位系统,内核版本3.0.10以上 2.安装docker镜像 root@backlion-virtual-machine:/# apt-get ...
- [CodeVs1515]跳(lucas定理+费马小定理)
嘿嘿嘿好久没写数学题了,偶尔看到一道写一写... 题目大意:一个(n+1)*(m+1)[0<=n, m<=10^12,n*m<=10^12]的矩阵,C(0,0)=1,C(x,y)=C ...
- 【loj2639】[Tjoi2017]不勤劳的图书管理员
#2639. 「TJOI2017」不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产 ...
- 单例 ------ C++实现
基础知识掌握: 单例考虑三点:内存何时释放.运行速度如何.多线程下能否保证只有一个实例 如果获取对象的返回值类型是引用,返回值赋值给变量而不是引用会进行对象的拷贝,这样就会出现两个对象,可以把显示声明 ...
- js 30分钟倒计时
<html> <head> <meta charset="UTF-8"> <title></title> </he ...
- centos6.5 mqtt安装
CentOs 6.5 MQTT 安装部署 所需安装包: libwebsockets-v1.6-stable.tar.gz,mosquitto-1.4.8.tar.gz 1.安装依赖 # yum -y ...
- /etc/rc.d/里文件的作用
rc.sysinit指的是系统启动不管进哪个运行级别必须做的初始化工作, rcn.d目录指的是系统进对应n的运行级别时候系统必须做的工作,目录下S打头的服务指进此运行级别时候启动的服务,而K打头的指离 ...
- UVA-10779 Collectors Problem
https://vjudge.net/problem/UVA-10779 题意:n个人,m种贴纸,每个人开始有一些贴纸 第一个人可以跟任何人交换任何贴纸 其余人只能用重复的贴纸 跟第一个人交换他们没有 ...
- HDU 1452 欧拉定理
让你求$2004^x$所有因子之和,因子之和函数是积性函数$\sigma(n)=\sum_{d|n}d=\prod_{i=0}^{m}(\sum_{j=0}^{k_i}{P_i^{j}})$可用二项式 ...
- (四)伪分布式下jdk1.6+Hadoop1.2.1+HBase0.94+Eclipse下运行wordCount例子
本篇先介绍HBase在伪分布式环境下的安装方式,然后将MapReduce编程和HBase结合起来使用,完成WordCount这个例子. HBase在伪分布环境下安装 一. 前提条件 已经成功地安装 ...