312. Burst Balloons - LeetCode
Question
https://leetcode.com/problems/burst-balloons/description/

Solution
题目大意是,有4个气球,每个气球上有个数字,现在依次打这4个气球(可以看成两边还各有一个气球即1,3,1,5,8,1),第一次打5这处气球,你的得分是左边气球上的数乘右边气球上的数再乘被打气球上的数。按任意顺序打这4个气球,求最终得分最高的值。
思考:正向思考

反向思考:

public int maxCoins(int[] nums) {
// 假设输入是[3,1,5,8]
// 构造一个新的气球数组[1,3,1,5,8,1]
int[] fullNums = new int[nums.length + 2];
fullNums[0] = 1;
fullNums[fullNums.length - 1] = 1;
for (int i = 1; i < fullNums.length - 1; i++) fullNums[i] = nums[i - 1];
// 存储从a气球到b气球的最高得分,初始化为-1,由于b>=a所以存储一半就可以了
int[][] result = new int[fullNums.length][fullNums.length];
for (int i = 1; i < result.length; i++) {
for (int j = i; j < result[0].length; j++) {
result[i][j] = -1;
}
}
// 相对于构造的数组 从1到4号气球才是我们要打的气球
return getMax(fullNums, result, 1, fullNums.length - 2);
}
private int getMax(int[] fullNums, int[][] result, int begin, int end) {
if (begin > end) return 0;
// 如果不是初始值,说明已经计算过该值了,直接返回结果
if (result[begin][end] != -1) return result[begin][end];
// 最后结果有4种,最后打3或1或5或8这四种可能,比较取最大值
for (int pos = begin; pos <= end; pos++) {
int left = getMax(fullNums, result, begin, pos - 1);
int mid = fullNums[begin - 1] * fullNums[pos] * fullNums[end + 1];
int right = getMax(fullNums, result, pos + 1, end);
int coin = left + mid + right;
if (coin > result[begin][end]) result[begin][end] = coin;
}
return result[begin][end];
}
Reference
312. Burst Balloons - LeetCode的更多相关文章
- 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 by ...
- 【LeetCode】312. Burst Balloons 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- 312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
随机推荐
- 【HTML5版】导出Table数据并保存为Excel
首发我的博客 http://blog.meathill.com/tech/js/export-table-data-into-a-excel-file.html 最近接到这么个需求,要把<tab ...
- 腾讯云+社区开发者大会开启报名,WeGeek 邀你一起聊聊小程序
刚满 2 岁的微信小程序,正给我们带来一种全新轻便的生活方式. 内测时的青涩还历历在目,到现在,小程序生态已日渐成熟.超过 150 万开发者在这里找到了自己的新天地,打磨出超过 100 万个小程序. ...
- js读取cookie 根据cookie名称获取值的方法
//方法1 //存在问题:如果cookie中存在 aaaname=aa;name=bb 获取name的值就会出现错误function getCookie(c_name){ if (document.c ...
- EMS邮箱数据库全局监控设置
案例任务:监控TestDB01邮箱数据库的所有邮件,监控邮箱为用户"王淑江"的邮箱. 1.EMS全局监控设置 使用PowerShell完成操作:"王淑江"监控T ...
- uni-app中实现左侧导航栏效果
HTML: 1 <view class="list"> 2 <!-- 一级 --> 3 <scroll-view class="list-l ...
- 在Wireshark中使用过滤器——显示过滤器
在Wireshark运行过程中选择搜索(Ctrl-F),第一个默认的搜索选项就是显示过滤器. 显示过滤器用于捕获文件,用来告诉Wireshark只显示那些符合过滤条件的数据包. 显示过滤器比捕获过滤器 ...
- XShell免费版的安装配置教程以及使用教程(超级详细)
一. XShell的作用 XShell可以在Windows界面下来访问远端不同系统下的服务器,从而比较好的达到远程控制终端的目的.它支持 RLOGIN.SFTP.SERIAL.TELNET.SSH2 ...
- LC-76
给你一个字符串 s .一个字符串 t .返回 s 中涵盖 t 所有字符的最小子串.如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" . 注意: 对于 t 中重复字符 ...
- random模块、os模块、序列化模块、sy模块s、subprocess模块
random随机数模块 random.random( ) 随机产生一个0-1之间的小数 print(random.random()) # 0.31595547439342897 random.rand ...
- 时序数据库influxDB介绍
https://www.jianshu.com/p/68c471bf5533 https://www.cnblogs.com/wzbk/p/10569683.html