312. 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:
(1) You may imagine nums[-1] = nums[n] = 1
. They are not real therefore you can not burst them.
(2) 0 ≤ n
≤ 500, 0 ≤ nums[i]
≤ 100
Example:
Given [3, 1, 5, 8]
Return 167
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
链接: http://leetcode.com/problems/burst-balloons/
题解:
射气球游戏,射中气球以后得分是nums[i] * nums[i - 1] * nums[i - 2],之后左右两边气球相连。 这道题思路也很绕,看了dietpepsi的解答也很模糊,跟买卖股票with cooldown一样。方法应该是用dp或者divide and conquer,大概想法是每burst掉一个气球,我们做一遍2d dp。代码并不长,所以我把答案背下来了...擦。理解交给二刷了。
Time Complexity - O(n3), Space Complexity - O(n2)
public class Solution {
public int maxCoins(int[] orgNums) {
if(orgNums == null || orgNums.length == 0) {
return 0;
}
int len = orgNums.length + 2;
int[] nums = new int[len];
nums[0] = nums[len - 1] = 1; // boundary
for(int i = 0; i < orgNums.length; i++) {
nums[i + 1] = orgNums[i];
}
int[][] dp = new int[len][len]; for(int i = 1; i < len; i++) { // first balloon
for(int lo = 0; lo < len - i; lo++) { // left part
int hi = lo + i; // right part boundary
for(int k = lo + 1; k < hi; k++) {
dp[lo][hi] = Math.max(dp[lo][hi], nums[lo] * nums[k] * nums[hi] + dp[lo][k] + dp[k][hi]);
}
}
} return dp[0][len - 1];
}
}
Reference:
https://leetcode.com/discuss/72216/share-some-analysis-and-explanations
https://leetcode.com/discuss/72186/c-dynamic-programming-o-n-3-32-ms-with-comments
https://leetcode.com/discuss/72215/java-dp-solution-with-detailed-explanation-o-n-3
https://leetcode.com/discuss/72683/my-c-code-dp-o-n-3-20ms
https://leetcode.com/discuss/73288/python-dp-n-3-solutions
https://leetcode.com/discuss/72802/share-my-both-dp-and-divide-conquer-solutions
https://leetcode.com/discuss/73924/my-36ms-c-solution
312. Burst Balloons的更多相关文章
- 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] 312. Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- [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】312. Burst Balloons 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...
- 312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...
- 312. Burst Balloons - LeetCode
Question https://leetcode.com/problems/burst-balloons/description/ Solution 题目大意是,有4个气球,每个气球上有个数字,现在 ...
- [LeetCode] Burst Balloons (Medium)
Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...
随机推荐
- java 参数化类型
package com.gxf.collection; import java.util.LinkedList; public class TestForT<T> { private Li ...
- c++实例化对象
今天看到c++实例化对象,有点懵了.Activity_Log the_log (theLogPtr, Tree->GetBranch());这是那一段小代码,开始没看懂.java看习惯了总喜欢n ...
- Eclipse中的常用快捷键
快捷修复 Command+1 //int a=100L; //int a=(int) 100L; 快捷删除行 Command+D 快速起新行 Shift+Enter (当本行代码很长时,将光标定在本行 ...
- iOS Automation Test
google resource for KIF: http://www.oschina.net/translate/ios-ui-testing-with-kif
- HttpWatch 安装后在IE上打开
启动浏览器, 在空白地方左键, 显示出菜单栏 菜单栏中选择"查看">"浏览器栏">"HttpWatch"启动HttpWatch ...
- 全面认识JVM技术
本文向大家描述一下JVM的概念,JVM(Java虚拟机)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该系统上运行. J ...
- input输入框的border-radius属性在IE8下的完美兼容
在工作中我们发现搜索框大部分都是有圆角的,为此作为经验不足的前端人员很容易就想到,给input标签添加border-radius属性不就解决了嘛.不错方法确实是这样,但是不要忘了border-radi ...
- ASP.NET 大文件上传的简单处理
在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...
- Why we have to use epsg:900913 in OpenLayers
reference:http://docs.openlayers.org/library/spherical_mercator.html epsg:900913 is spicfy the Soher ...
- centos mysql 操作
安装mysqlyum -y install mysql-server 修改mysql配置 vi /etc/my.cnf 这里会有很多需要注意的配置项,后面会有专门的笔记 暂时修改一下编码(添加在密码下 ...