Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will getnums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.
- You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
- 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

 
Example

Given [4, 1, 5, 10]
Return 270

nums = [4, 1, 5, 10] burst 1, get coins 4 * 1 * 5 = 20
nums = [4, 5, 10] burst 5, get coins 4 * 5 * 10 = 200
nums = [4, 10] burst 4, get coins 1 * 4 * 10 = 40
nums = [10] burst 10, get coins 1 * 10 * 1 = 10 Total coins 20 + 200 + 40 + 10 = 270 递归做法:
 public class Solution {
public int maxCoins(int[] nums) {
if (nums == null || nums.length == ) return ;
List<Integer> list = new LinkedList<>();
for (int index = ; index < nums.length; index++) {
list.add(nums[index]);
}
return helper(list);
} public int helper(List<Integer> list) {
if (list.size() == ) return list.get();
int max = ;
for (int i = ; i < list.size(); i++) {
int value = getValue(list, i) * getValue(list, i - ) * getValue(list, i + );
List<Integer> temp = new LinkedList<>(list);
temp.remove(i);
max = Math.max(max, value + helper(temp));
}
return max;
} private int getValue(List<Integer> list, int index) {
if (index < || index >= list.size()) {
return ;
}
return list.get(index);
}
}

方法二:

既然递归通不过,只能用DP,关键是那个递归式怎么写啊?

在从1到n个气球里,哪个会是最后一个呢?你不知道吧?我也不知道。所以我们得从头到尾假设第k个球是最后一个球。如果第k个球是最后一个球,会有什么性质呢?我们可以保证一定第k个球左边和右边的球永远不会靠在一起,换句话说,我们可以得到下面的递推式:

dp[i][j] = max(dp[i][j], nums[i - 1]*nums[k]*nums[j + 1] + dp[i][k - 1] + dp[k + 1][j]) ( i ≤ k ≤ j )
 public class Solution {
public int maxCoins(int[] nums) {
if (nums == null || nums.length == ) return ; List<Integer> list = new ArrayList<>();
list.add();
for (int index = ; index < nums.length; index++) {
list.add(nums[index]);
}
list.add(); int[][] dp = new int[list.size()][list.size()];
int n = nums.length;
for (int step = ; step <= n - ; step++) {
for (int i = ; i <= n - step; i++) {
int j = i + step;
for (int k = i; k <= j; k++) {
dp[i][j] = Math.max(dp[i][j],
list.get(i - ) * list.get(k) * list.get(j + ) + dp[i][k - ] + dp[k + ][j]);
}
}
}
return dp[][n];
}
}

Burst Balloons的更多相关文章

  1. [LeetCode] Burst Balloons (Medium)

    Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...

  2. 动态规划-Burst Balloons

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

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

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

  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. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  6. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  7. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

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

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

  9. [LeetCode] 452 Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  10. LeetCode Burst Balloons

    原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...

随机推荐

  1. DVR分布式路由

    1. 背景 没有使用DVR的场景: 从图中可以明显看到东西向和南北向的流量会集中到网络节点,这会使网络节点成为瓶颈. 如果启用DVR,如下图: 对于东西向的流量, 流量会直接在计算节点之间传递. 对于 ...

  2. Day1 三级目录

    d_city = { "河南" : {"郑州" : ["二七区","中原区","回族管城区",&qu ...

  3. [IOS+PHP Jason格式的发送与解析]

    服务器端PHP文件connect.php: <?php $q = mysql_connect("localhost","root","" ...

  4. java获取每个月的最后一天

    package timeUtil; import java.text.SimpleDateFormat; import java.util.Calendar; public class LastDay ...

  5. nginx&apache比较

    1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx ...

  6. configure: error: Please reinstall the libcurl distribution

    configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/ 基本上 ...

  7. nginx 启动/停止/重启 BAT

    cls @ECHO OFF SET NGINX_PATH=D: SET NGINX_DIR=D:\Hack\nginx\color 0a TITLE Nginx 管理程序 Power By AntsG ...

  8. matlab 之cov 协方差

    COV 1.cov(x) 如果x为向量,返回x的方差 计算方法为: S为方差. 2.cov(X) 如果X为矩阵,把矩阵X的行作为观察值,把列作为变量,返回X的协方差矩阵: diag(cov(X))是每 ...

  9. dedecms首页调用的简介一直修改不了是自动文章摘要在作怪

    一位美女问:dedecms首页调用的简介一直修改不了,ytkah让她到具体的文章修改,然后再重新生成一下首页.她说还是不行.那就奇了怪了,点击到具体的文章页面是显示已经修改好了,为什么首页还是原来的呢 ...

  10. zstu.4022.旋转数阵(模拟)

    旋转数阵 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1477  Solved: 102 Description 把1到n2的正整数从左上角开始由外层 ...