参考: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(戳气球)的更多相关文章

  1. [LeetCode] 312. Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  2. [LeetCode] 312. Burst Balloons 爆气球

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  3. 312 Burst Balloons 戳气球

    现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...

  4. 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 ...

  5. 312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  6. [LeetCode] Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  7. 【LeetCode】312. Burst Balloons 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...

  8. 【LeetCode】312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  9. 312. Burst Balloons - LeetCode

    Question https://leetcode.com/problems/burst-balloons/description/ Solution 题目大意是,有4个气球,每个气球上有个数字,现在 ...

随机推荐

  1. PHPer常见的面试题总结

    1.平时喜欢哪些php书籍及博客?CSDN.虎嗅.猎云 2.js闭包是什么? 3.for与foreach哪个更快? 4.php鸟哥是谁?能不能讲一下php执行原理? 5.php加速器有哪些?apc.z ...

  2. IBM MQ常用命令

    常用命令 创建队列管理器crtmqm –q QMgrName-q是指创建缺省的队列管理器删除队列管理器dltmqm QmgrName启动队列管理器strmqm QmgrName如果是启动默认的队列管理 ...

  3. SCCM 2012 R2实战系列之三:独立主站点部署

    3.1 SCCM 2012 R2主站点的安装 SCCM 2012 R2跟以前的SCCM 2007不同的是多了一个管理中心站点的角色, 管理中心站点主要负责SCCM管理控制和报表查看. 主站点跟以往的S ...

  4. Redis-Migrate-Tool 使用详解

    注意:目前不支持4.0.X及以上的redis使用 Redis 集群迁移工具,基于redis复制,快速,稳定. github链接:https://github.com/vipshop/redis-mig ...

  5. (转)C#操作Word文档

    原文1地址:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 原文2地址: http://www.cnblogs.com ...

  6. C Mysql API连接Mysql

    最近都在查看MYsql C API文档,也遇到了很多问题,下面来简单的做一个总结. mysql多线程问题 mysql多线程处理不好,经常会发生coredump,见使用Mysql出core一文. 单线程 ...

  7. windows下解决python输出utf-8中文

    class UnicodeStreamFilter: def __init__(self, target): self.target = target self.encoding = 'utf-8' ...

  8. Java分布式锁的三种实现方案(redis)

    方案一:数据库乐观锁 乐观锁通常实现基于数据版本(version)的记录机制实现的,比如有一张红包表(t_bonus),有一个字段(left_count)记录礼物的剩余个数,用户每领取一个奖品,对应的 ...

  9. cat & 文件结束符

    语法: 连接显示 选项: -n,显示行号. -v,显示不可见打印符. -E,显示“行结束符”($). 显示行号 $ cat -n /etc/fstab /dev/VolGroup00/LogVol00 ...

  10. gentoo rt-thread pkgs --update except Exception, e:

    使用 rt-thread pkgs --update 时候, 提示出错,信息如下: Traceback (most recent call last): File "/home/ptz/.e ...