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. clang8.0及以上编译rocks问题

    升级了MacOS 10.15 Beta版,结果编译 CockroachDB 遇到了问题. [ %] Building CXX object CMakeFiles/rocksdb.dir/db/buil ...

  2. 目前校园百晓生APP与CSDN软件的对比

    不忘初心.注入灵魂 设计之初 在做校园百晓生该APP时,初心是为校园的同学提供便捷的查询校园信息的服务,确实目前的软件已经实现了该功能,但是总感觉缺少一些灵魂,感觉大部分人渴望的重点在于信息而不是自己 ...

  3. 设计模式之GOF23代理模式02

    静态代理 模拟经纪人与明星开演唱会 public interface Star { /**  * 面谈  */  void confer();  /**   * 签合同   */  void sign ...

  4. [Selenium] 自动侦测浏览器版本并下载对应的浏览器驱动

    昨天在群里聊天时,有同学说 Appium 官方支持自动下载兼容的浏览器驱动,想来Selenium也有类似的方法,于是在网上搜索一番.参考了Medium上一篇文章的方法,对步骤进行改进,增加了对多浏览器 ...

  5. gets() 、 getchar() 、 getch() 、getche()、gets()、 scanf()的区别

    1.getchar().getche().getch() (1).getchar 函数用于从标准输入设备键盘读入单个字符,返回表示读入字符的ASCII码值,并在屏上显示该字符:头文件是 stdio.h ...

  6. 2020年腾讯实习生C++面试题&持续更新中(5)

    2020年腾讯实习生C++面试题&持续更新中(5) 大家好呀,我是好好学习天天编程的天天~ 昨天一位小伙伴反馈已经拿到了腾讯offer,很是替小伙伴的激动~ 那今天还是持续给大家分享面经,希望 ...

  7. pug(jade) 学习笔记

    from: https://www.cnblogs.com/xiaohuochai/p/7222227.html 对于一些嵌套层次较深的页面,在后期维护和修改时,一不小心少了一个尖括号,或者某个标签的 ...

  8. 使用Html5对图片加水印及多图合成

                                                                             转载请注明原地址:                   ...

  9. Jmeter(二) - 从入门到精通 - 创建测试计划(Test Plan)(详解教程)

    1.简介 上一篇中宏哥已经教你把JMeter的测试环境搭建起来了,那么这一篇我们就将JMeter启动起来,一睹其芳容,首先宏哥给大家介绍一下如何来创建一个测试计划(Test Plan). 2.创建一个 ...

  10. Lightoj1356

    题目链接:https://vjudge.net/problem/LightOJ-1356 题目大意: T个 test case,每个 test case 给出一个 N 个数的集合.请找出一个最大的子集 ...