参考: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. Oracle之ora-01031 insufficient privileges

              解决ora-01031insufficient privileges错误 解决system用户不能登录的问题 alter user system account unlock id ...

  2. Activity的启动模式--总结

    3. Activity的任务栈Task以及启动模式与Intent的Flag详解? 2,Activity次级页面和主页间来回跳转,防止重复创建Activity实例 1, activity的启动模式: / ...

  3. [UE4]在蓝图中设置图片

  4. man iptables 8

    IPTABLES(8) iptables 1.6.0 IPTABLES(8) NAME iptables/ip6tables — administration tool for IPv4/IPv6 p ...

  5. (转)Linux 系统服务的启动顺序解析 rc.*

    介绍系统按照不同级别启动时需要启动的服务. 进入目录:etc 执行命令:ls -l | grep "rc.*" | sort 结果如下图:   1 系统在启动时,通过inittab ...

  6. CentOS用户和用户组管理

    groupadd grptest1  按照系统默认的gid创建组.根uid一样,gid也是从1000开始的. groupadd -g 1008 grptest2    创建gid=1008的用户组:g ...

  7. 面向对象javascript编程

    以构造函数的方式定义对象 function Person(name, age) { this.name = name; this.age = age; this.sayName = function ...

  8. Java虚拟机------JVM分析工具

    主要介绍JVM的分析工具: jps jps:Java Virtual Machine Process Status Tool http://docs.oracle.com/javase/1.5.0/d ...

  9. tkinter简单使用

    第一个运行程序 # -*- coding: utf-8 -*- import tkinter as tk //引入 root = tk.Tk() // 实例化root T大写k小写 root.titl ...

  10. jQuery操作标签

    jQuery操作标签 样式操作: 对标签的样式进行修改,那么操作样式的方法是什么? 样式类: addClass();// 添加指定的CSS类名. removeClass();// 移除指定的CSS类名 ...