LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球)
java代码如下
class Solution {
//参考:https://blog.csdn.net/jmspan/article/details/51208865
public int maxCoins(int[] nums) {
int[] balls = new int[nums.length+2];
balls[0] = 1;
balls[balls.length - 1] = 1;
int[][] coins = new int[balls.length][balls.length];
for(int i = 0; i < nums.length; i++)
{
balls[i+1] = nums[i];
} for(int j = 2; j < balls.length; j++)
{
for(int i = j - 2; i >= 0; i--)
{
for(int k = j -1; k > i; k--)
{
/* 这个里面的coins[i][k] + balls[i]*balls[k]*balls[j] + coins[k][j]
是最大的可以这样理解:以k分割(为什么会有k,因为无论怎么戳,最后剩三个
的时候,一定是i,j和另一个,另一个就是k,那么要总的结果最大,那么要k
两边的值都取最
大,两边值最大是什么:即coins[i][k],coins[k][j],先戳两边的把两个最
大找出来,最后左右两边剩什么呢,正是最左边的balls[i]和最右边的balls[j],
最后处理k处的即balls[i]*balls[k]*balls[j]
*/
coins[i][j] = Math.max(coins[i][j], coins[i][k]
+ balls[i]*balls[k]*balls[j] + coins[k][j]);
}
}
} return coins[0][balls.length - 1];
}
}
代码中分析了重点代码,采用了动态规划,从最小的情况出发,慢慢扩大,每次都取最大(最优)的结果,最终的结果也是最优的
三层循环:j:最右值,i:最左值,k:中间值,三层循环从小到大获取不同长度,不同位置的数组的一段的最值coins[i][j],coins[i][j]不包括两端,初始时coins是int数组默认都是0,符合实际情况,最后返回一个最长的也即最大的结果,难点就是分析到数组的某一段剩3个元素i,j,k时的情形,可以根据这个情形反推之前要如何处理,即k前后都取最大,即这时的最大值为coins[i][k] + balls[i]*balls[k]*balls[j] + coins[k][j],个人见解,希望对你有帮助。
LeetCode 312. Burst Balloons(戳气球)的更多相关文章
- [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 ...
- 312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...
- 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 ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- [LeetCode] 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 - LeetCode
Question https://leetcode.com/problems/burst-balloons/description/ Solution 题目大意是,有4个气球,每个气球上有个数字,现在 ...
随机推荐
- 1125 Chain the Ropes (25 分)
1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...
- arduino mega 避障报距小车
流程图 硬件 mega2560 // Pin 13 has an LED connected on most Arduino boards. // give it a name: #include&l ...
- python中变量的缓存机制
同一文件中, 变量的缓存机制 (在此范围内的相同值内存地址一样) Number: int: -5 ~ 正无穷 float: 非负数 bool: ...
- Unity中进程间通信——使用Protobuf-net序列化与反序列化
基于ProtoBuf协议实现网络传输(上) Protobuf 全称Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格 ...
- Spark操作实战
1. local模式 $SPARK_HOME/bin/spark-shell --master local import org.apache.log4j.{Level,Logger} // 导入ja ...
- json与bson的区别
bson是由10gen开发的一个数据格式,目前主要用于mongoDB中,是mongoDB的数据存储格式.bson基于json格式,选择json进行改造的原因主要是json的通用性及json的schem ...
- MyBatis批量增删改查操作
前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...
- mac maven lombok报错
maven已导入lombok的jar包,注解@Data,但是用到getter,setter时依然出错.解决办法: 打开eclipse.ini文件,加上如下两句: -Xbootclaspath//Use ...
- 安全测试8_Web安全实战1(DVWA部署)
1.渗透神器的打造(火狐浏览器的打造) Firebug HackBar(渗透插件) Tamper Data(抓包.截包.改包等功能) Proxy Switcher(代理设置) 2.PHP环境安装(ph ...
- Oracle Service Bus (OSB) 12c 的配置安装
Oracle Service Bus (OSB) 12c 的配置安装 1.OSB配置环境: Oracle Database Oracle Fusion Middleware Infrastructur ...