312. 戳气球

有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。

现在要求你戳破所有的气球。每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币。 这里的 left 和 right 代表和 i 相邻的两个气球的序号。注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球。

求所能获得硬币的最大数量。

说明:

你可以假设 nums[-1] = nums[n] = 1,但注意它们不是真实存在的所以并不能被戳破。

0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

示例:

输入: [3,1,5,8]

输出: 167

解释: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []

coins = 315 + 358 + 138 + 181 = 167

class Solution {
private int[][] dp; public void fill(int[] nums, int from, int to) {
int max = 0, maxLeft, maxRight, result;
//假设第i个气球是最后被戳破的
for (int i = from; i <= to; i++) {
maxLeft = dp[from][i - 1];
maxRight = dp[i + 1][to];
result = maxLeft + maxRight + nums[from - 1] * nums[i] * nums[to + 1];
max = result > max ? result : max;
}
dp[from][to] = max;
} public int maxCoins(int[] nums) {
int length = nums.length;
dp = new int[length + 2][length + 2];
for (int i = length + 1; i >= 0; i--) {
Arrays.fill(dp[i], 0);
} int[] expandNums = new int[length + 2];
expandNums[0] = expandNums[length + 1] = 1;
System.arraycopy(nums, 0, expandNums, 1, length); for (int span = 0; span < length; span++) {
//fill a diagonal
//assume the span (of array of length i) is i-1
for (int from = length - span; from > 0; from--) {
//fill dp[from][from + span]
fill(expandNums, from, from + span);
}
}
return dp[1][length];
}
}

Java实现 LeetCode 312 戳气球的更多相关文章

  1. Leetcode 312.戳气球

    戳气球 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * n ...

  2. 312. 戳气球【困难】【区间DP】

    题目链接 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * ...

  3. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  4. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. [hdu3068 最长回文]Manacher算法,O(N)求最长回文子串

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题意:求一个字符串的最长回文子串 思路: 枚举子串的两个端点,根据回文串的定义来判断其是否是回文 ...

  2. JVM入门--类加载器

    一.基础架构 概览 我们平时说的栈是指的Java栈,native method stack 里面装的都是native方法 细节架构图 二.类加载器 1.类的加载 方法区并不是存放方法的区域,其是存放类 ...

  3. Linux常用命令~新手必知

    ifconfig查看本机IP ls  查看当前目录下所有文件及文件夹 ll      以详细方式显示所有文件与文件夹.(相当于 ls -l 命令) pwd     显示当前目录路径 cd  /user ...

  4. mybatis随记

    JDBC问题:1.数据库配置信息硬编码 2.频繁创建,释放数据库连接 3.sql,设置参数,获取结果集硬编码,不通用   解决方案:1.配置文件 2.采用连接池 3.使用反射和内省   自定义持久层框 ...

  5. 9.3 Go json

    9.3 Go json json是一种轻量级的数据交换格式,易于机器解析,是种key-value格式. JavaScript Object Notation是中种主流的数据格式. json常用于网络传 ...

  6. 安装的SQL Server2008 R2版的连接不到本地数据,提示未找到或无法访问服务器。----复制自百度知道

    安装的SQL Server2008 R2版的连接不到本地数据,提示未找到或无法访问服务器.使用Windows身份验证 2012-09-17 00:23hj168926 | 分类:数据库DB | 浏览3 ...

  7. Sql Server数据库导入Excel、txt数据详解,新人必看

    转自个人原创 https://blog.csdn.net/qq_15170495/article/details/104591606 数据库的要想导入数据,列的映射很是关键,只有列名匹配好,系统才知道 ...

  8. 【网络安全】【02】Windows系统如何屏蔽自动更新

    一.win键 + r   ==>  输入  services.msc 二. win键 + r   ==>  输入  gpedit.msc PS:我的电脑基本两年没有更新过了  -_-

  9. Poj2109 (2) k^n = p.

    二分法,由p最大值最小值的中间值开始猜k,通过比较pow(k,n)与p的大小来进一步精确k,直到找到. #include<stdio.h> #include<math.h> # ...

  10. Istio DestinationRule 目标规则

    概念及示例 与VirtualService一样,DestinationRule也是 Istio 流量路由功能的关键部分.您可以将虚拟服务视为将流量如何路由到给定目标地址,然后使用目标规则来配置该目标的 ...