参考: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. Scrapy学习篇(一)之框架

    概览 在具体的学习scrapy之前,我们先对scrapy的架构做一个简单的了解,之后所有的内容都是基于此架构实现的,在初学阶段只需要简单的了解即可,之后的学习中,你会对此架构有更深的理解.下面是scr ...

  2. HDOJ 2001 ASCII码排序

    #include<set> #include<iostream> using namespace std; int main() { char a, b, c; while ( ...

  3. PopupWindows 在2.3.3下报java.lang.NullPointerException

    03-05 01:20:56.040: E/AndroidRuntime(1396): java.lang.NullPointerException 03-05 01:20:56.040: E/And ...

  4. [UE4]自定义服务器Service

  5. spi and sensor

    http://blog.csdn.net/DroidPhone/article/details/23367051 https://www.kernel.org/doc/html/v4.14/drive ...

  6. js中基本数据类型和引用数据类型的区别

    1.基本数据类型和引用数据类型 ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型. 基本数据类型指的是简单的数据段,引用数据类型指的是有多个值构成的对象. 当我们把变量赋值给一个变 ...

  7. Java 工程名上有个红色叹号

    biuldpath把带×号的jar包remove保存,重新添加jar包

  8. TessorFlow学习 之 手写数字识别的搭建

    手写数字识别的搭建

  9. spring揭密学习笔记(2)-spring ioc容器:IOC的基本概念

    1. IoC的理念就是,让别人为你服务!2. 其实IoC就这么简单!原来是需要什么东西自己去拿,现在是需要什么东西就让别人送过来.一个生动的示例 3.三种依赖注入的方式 IoC模式最权威的总结和解释, ...

  10. python中的swapcase

    swapcase()将字符串中的字母小写变大写.大写变小写,举个例子: 1 a = "hELLO wORLD" 2 a1 = a.swapcase() 3 print(a1) 输出 ...