LeetCode 312. Burst Balloons(戳气球)
原题网址:https://leetcode.com/problems/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
方法:动态规划,定义二维数组coins,coins[a][b]表示把第a个和第b个气球之间(不含a和b)的气球戳烂,最大能得到的分值。
public class Solution {public int maxCoins(int[] nums) {int[] dpnums = new int[nums.length+2];dpnums[0] = 1;dpnums[dpnums.length-1] = 1;for(int i=0, j=1; i<nums.length; i++, j++) dpnums[j] = nums[i];int[][] coins = new int[dpnums.length][dpnums.length];for(int i=2; i<dpnums.length; i++) {for(int j=0; j+i<dpnums.length; j++) {for(int k=j+1; k<j+i; k++) {coins[j][j+i] = Math.max(coins[j][j+i], coins[j][k] + coins[k][j+i] +dpnums[j] * dpnums[k] * dpnums[j+i]);}}}return coins[0][dpnums.length-1];}
}
另一种实现方式:
public class Solution {public int maxCoins(int[] nums) {int[] dpnums = new int[nums.length+2];dpnums[0] = 1;dpnums[dpnums.length-1] = 1;System.arraycopy(nums, 0, dpnums, 1, nums.length);int[][] coins = new int[dpnums.length][dpnums.length];for(int i=2; i<dpnums.length; i++) {for(int j=i-2; j>=0; j--) {for(int k=i-1; k>j; k--) {coins[j][i] = Math.max(coins[j][i], coins[j][k] + dpnums[j] * dpnums[k] * dpnums[i] + coins[k][i]);}}}return coins[0][dpnums.length-1];}
}
LeetCode 312. Burst Balloons(戳气球)相关推荐
- 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
一看就是DP题,但是递推公式比较难想.为了简化问题,给 nums 开始和最后都加上1. 记 dp[i][j] 表示 nums[i~j] 能得到的最大coin. k 表示保留着的气球, dp[i][j] ...
- 力扣312题:戳气球
力扣312题:戳气球 题目描述 有 n 个气球,编号为0 到 n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.戳破第 i 个气球,你可以获得 nums ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- [Leetcode][第312题][JAVA][戳气球][动态规划][记忆化搜索]
[问题描述][困难] [解答思路] 1. 记忆化搜索 时间复杂度:O(n^3) 空间复杂度:O(n^2) class Solution {public int[][] rec;public int[] ...
- 【Leetcode】312. Burst Balloons
第一种思路可以采用backtracking.把一个list传递下去,每一步可以选择任意一个删除,然后再递归处理下一个,比较简单. public int maxCoins1(int[] nums) {i ...
- 【LeetCode】312. 戳气球
312. 戳气球(困难) 解法一:动态规划 首先看一个区间: 区间(i,j) 是一个开区间,因为我们只能戳爆 i 和 j 之间的气球,不能戳爆索引为 i 和 j 的气球. 我们不妨考虑该区间内被戳爆的 ...
- Leetcode.312 戳气球
题目链接 Leetcode.312 戳气球 题目描述 有 n个气球,编号为0到 n - 1,每个气球上都标有一个数字,这些数字存在数组 nums中. 现在要求你戳破所有的气球.戳破第 i 个气球,你可 ...
最新文章
- 获得汉字拼音的首字母
- request.getParameter如何获取radio的属性值
- linux 系统优化,调优
- 5个在线代码编辑器,供云计算爱好者
- centos7 php7 httpd
- 关于ViewTreeObserver的理解
- Python 爬取斗图啦图片
- Java 8 Friday Goodies:精益并发
- JavaScript笔记-使用JS管理URL链接(前端小技巧)
- Zxing使用及常见错误(iOS)
- 百度地图检索以及路径规划
- Practical Mathematical Handwriting
- java托盘图标变白在linux,升级Ubuntu后系统托盘图标消失
- java word加粗_java word文档进行填充使用 ${xxx} 的形式
- mysql查询hash分区数据_mysql分区管理 - hash分区
- WinCE --- 调试RS485串口
- 二重积分x^2+y^2_计算二重积分∫∫y^2dxdy,其中D是由圆周x^2+y^2=1所围成的闭区域...
- 单片机胡汉才第四版答案_单片机课后习题答案--胡汉才编
- Winedit7.0自定义一键编译
- CSU——1043克里莫
热门文章
- Unity 参数的基本设置
- Prometheus核心概念:你是如何在项目中使用Summary类型的Metric的?
- Noisy label learning
- 125KHz三通道低功耗唤醒接收器芯片SI3933概述
- 二值图的骨架提取matlab
- 做好微信营销第一步,塑造成功的微信形象
- php通过谷歌身份验证实现动态口令
- protoc-gen-micro: error:bad Go source code was generated: 7:1: expected ‘IDENT‘, found ‘import‘
- IDEA代码警告(warning)整理以及解决办法
- 交易所交易规则--股票分红