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; // boundaryfor(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 balloonfor(int lo = 0; lo < len - i; lo++) { // left partint hi = lo + i; // right part boundaryfor(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
转载于:https://www.cnblogs.com/yrbbest/p/5062373.html
312. Burst Balloons相关推荐
- LeetCode 312. Burst Balloons(戳气球)
原题网址:https://leetcode.com/problems/burst-balloons/ Given n balloons, indexed from 0 to n-1. Each bal ...
- LeetCode 312. Burst Balloons
一看就是DP题,但是递推公式比较难想.为了简化问题,给 nums 开始和最后都加上1. 记 dp[i][j] 表示 nums[i~j] 能得到的最大coin. k 表示保留着的气球, dp[i][j] ...
- 312. Burst Balloons 戳气球
Title 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.如果你戳破气球 i ,就可以获得 nums[left] * nu ...
- leetcode 312. Burst Balloons | 312. 戳气球(暴力递归->DP)
题目 https://leetcode.com/problems/burst-balloons/ 题解 好久不 DP 了,DP 一下吧,结果被坑了很久,看了答案. 递归的时候,我知道分成L,R两边,但 ...
- 【Leetcode】312. Burst Balloons
第一种思路可以采用backtracking.把一个list传递下去,每一步可以选择任意一个删除,然后再递归处理下一个,比较简单. public int maxCoins1(int[] nums) {i ...
- 452 Minimum Number of Arrows to Burst Balloons
452 Minimum Number of Arrows to Burst Balloons 文章目录 452 Minimum Number of Arrows to Burst Balloons 1 ...
- LeetCode 312. 戳气球(Burst Balloons)
题目描述: 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * ...
- 打破气球所能获得的最大积分 Burst Balloons
2019独角兽企业重金招聘Python工程师标准>>> 问题: Given n balloons, indexed from 0 to n-1. Each balloon is pa ...
- 贪心:Burst Balloons 最少次数完成射击气球
已知在一个平面上有一定数量的气球,平面可以看作一个坐标系,在平面的x轴的不同位 置安排弓箭手向y轴方向射箭,弓箭可以向y轴走无穷远;给定气球的宽度 xstart ≤ x ≤ xend,问至少需要多少弓 ...
最新文章
- mysql二进制日志管理_MYSQL二进制日志管理脚本
- 阿里云盘又送福利啦?空间大时间长,不要犹豫快上车!
- labview求n阶乘的和_LABVIEW求1到N所有数的阶乘之和
- SQL注入学习——sqli-labs闯关(Basic Challenges)
- SAP IBASE的创建实现逻辑
- Auto.JS 开发
- NHibernate使用之详细图解
- html5 mask,HTML5 Canvas渐进填充与透明实现图像的Mask效果
- mysql统计姓名为小明_Mysql 统计查询相同字段只统计一条
- squid via检测转发循环
- STL 源代码剖析 算法 stl_algo.h -- search
- springcloud之eureka集群搭建
- 常用Linux命令--CPU和GPU查看
- 手机连接adb操作步骤
- 未来教育考试系统无法答题,点击选项后没有反应,不会记录答题
- 增加设备分类号之后,设备名称SOM聚类前九个子类
- php使用phpword教程,使用PHPWord生成word文档
- 动态水印跟踪去除_视频动态水印如何去除 视频中不定时出现的图片加文字广告如何尽量模糊处理...
- 数据库45道SQL作业题及答案
- Django-admin后台LOGO字样修改方法
热门文章
- java jquery easyui_java中用jquery-easyui插件做可编辑datagird列表
- VM 虚拟机 centos7 安装redis
- oracle主机自增,Oracle中实现ID自增
- 计算机网络八校联考试题,2019届高三信息技术3月联考试卷有解析与答案
- android 黄金颜色,一加手机3有哪几种颜色 一加3 薄荷金与冰川灰哪种颜色好看?...
- 将本地文件push到gitee上面
- 操作系统—数组的定义和存储结构
- 第九届蓝桥杯省赛C++A组第二题 ---星期一
- 线性代数笔记 -- A = LU的应用
- 计算机网络复习题大全(各种题型)