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个气球,每个气球上有个数字,现在 ...
随机推荐
- 电脑不能上网win7 解决办法
情况一览: 电脑连的是WIFI 手机能上,局域网其他电脑能上 电脑浏览器打不开网页 问题解决: //win+R 快捷键进入cmd 1.ipconfig 看电脑有没有ip 2.ipconfig nslo ...
- mongodb windows的安装方法和添加到任务管理器中、检测是否成功、增删改查命令
转: mongodb安装方法: https://blog.csdn.net/heshushun/article/details/77776706 mongodb检测安装成功 .以及增删改 ...
- [UE4]控制流
虽然官方文档说复杂的蓝图循环是会跨域多帧运行,但实际上测试下来,如果在循环体进行大量复杂的运算,不足以在一帧内完成时,游戏就会在当前帧卡住,直到循环结束为止. 一.Switch Switch可以在所有 ...
- Linux打开TCP BBR拥塞控制算法
要求内核为4.9以上,如果不是,请升级内核. modprobe tcp_bbr echo "tcp_bbr" >> /etc/modules-load.d/module ...
- TP微信扫码支付
1.官网下载php扫码支付adk,放在项目引入第三方类库中 2.配置config中相关参数 注意:可能会遇到问题 微信支付错误问题的解决:curl出错,错误码:60 Fatal error: Unca ...
- CS229 6.16 Neurons Networks linear decoders and its implements
Sparse AutoEncoder是一个三层结构的网络,分别为输入输出与隐层,前边自编码器的描述可知,神经网络中的神经元都采用相同的激励函数,Linear Decoders 修改了自编码器的定义,对 ...
- python—正则表达式
我们平时上网的时候,经常需要在一些网站上注册帐号,而注册帐号的时候对帐号信息会有一些要求. 比如: 上面的图片中,输入的邮件地址.密码.手机号 符合要求才可以注册成功. 我们是我们自己写的网站,那么我 ...
- How The Kernel Manages Your Memory.内核是如何管理内存的
原文标题:How The Kernel Manages Your Memory 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩 ...
- python 中logging模块
logging的作用:python中,logging模块主要是处理日志的.所谓日志,可理解为在软件运行过程中,所记录的的一些运行情况信息,软件开发人员可以根据自己的需求添加日志,日志可以帮助软件开发人 ...
- three.js学习:三维空间下的直线
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...