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 get nums[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.

Note: 
(1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Given [3, 1, 5, 8]

Return 167

    nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

给n个气球,每个气球都对应一个数字,每次打爆一个气球,得到的金币数是被打爆的气球的数字和其两边的气球上的数字相乘,如果旁边没有气球了,则按1算,求能得到的最多金币数。

解法:动态规划DP,

State: dp[i][j],表示打爆区间[i,j]中的所有气球能得到的最多金币。题目中说明了边界情况,当气球周围没有气球的时候,旁边的数字按1算,这样我们可以在原数组两边各填充一个1,这样方便于计算。

Function: 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 )

Return: dp[1][n]中,其中n是两端添加1之前数组nums的个数。

Java:

public class Solution {
public int maxCoins(int[] iNums) {
int n = iNums.length;
int[] nums = new int[n + 2];
for (int i = 0; i < n; i++) nums[i + 1] = iNums[i];
nums[0] = nums[n + 1] = 1;
int[][] dp = new int[n + 2][n + 2];
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n - k + 1; i++) {
int j = i + k - 1;
for (int x = i; x <= j; x++) {
dp[i][j] = Math.max(dp[i][j], dp[i][x - 1] + nums[i - 1] * nums[x] * nums[j + 1] + dp[x + 1][j]);
}
}
}
return dp[1][n];
}
}  

Python:

class Solution(object):
def maxCoins(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
coins = [1] + [i for i in nums if i > 0] + [1]
n = len(coins)
max_coins = [[0 for _ in xrange(n)] for _ in xrange(n)] for k in xrange(2, n):
for left in xrange(n - k):
right = left + k
for i in xrange(left + 1, right):
max_coins[left][right] = max(max_coins[left][right], \
coins[left] * coins[i] * coins[right] + \
max_coins[left][i] + max_coins[i][right]) return max_coins[0][-1]  

C++:

class Solution {
public:
int maxCoins(vector<int>& nums) {
int n = nums.size();
nums.insert(nums.begin(), 1);
nums.push_back(1);
vector<vector<int> > dp(nums.size(), vector<int>(nums.size() , 0));
for (int len = 1; len <= n; ++len) {
for (int left = 1; left <= n - len + 1; ++left) {
int right = left + len - 1;
for (int k = left; k <= right; ++k) {
dp[left][right] = max(dp[left][right], nums[left - 1] * nums[k] * nums[right + 1] + dp[left][k - 1] + dp[k + 1][right]);
}
}
}
return dp[1][n];
}
};

  

All LeetCode Questions List 题目汇总

[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(戳气球)

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

  3. 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 ...

  4. 312 Burst Balloons 戳气球

    现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...

  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. QA流程

    一.测试人员的介入时间 1.当产品经理与业务人员制定需求的时候,测试人员不宜介入: 2.当下一期的需求原型出来以后,这个时候就进入了需求评审.需求分析阶段,此时,测试人员应该介入: 3.当开发人员在编 ...

  2. 修改cloud image密码

    安装libguestfs-tools yum -y install libguestfs-tools.noarch 设置固定密码 virt-customize -a CentOS-7-x86_64-G ...

  3. 11.vue-router编程式导航

    页面导航的两种方式 声明式导航:通过点击链接实现导航的方式,叫做声明式导航 例如:普通网页中的链接或vue中的 编程式导航:通过调用JavaScrip形式的API实现导航的方式,叫做编程式导航 例如: ...

  4. How Open Source Became The Default Business Model For Software

    https://www.forbes.com/sites/forbestechcouncil/2018/07/16/how-open-source-became-the-default-busines ...

  5. 4-html图片与链接

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 使用Simian进行重复代码检测

    一.概述 Simian是一个可跨平台使用的重复代码检测工具,有商用和免费两种使用渠道,官方网址为:http://www.harukizaemon.com/simian/installation.htm ...

  7. 思科ASA对象组NAT

    ACL对象组NAT配置 ciscoasa#conf t ciscoasa(config)#hostname ASA ASA(config)#domain-name asa.com ASA(config ...

  8. 洛谷 P1908 逆序对 题解

    每日一题 day43 打卡 Analysis 因为数据规模,所以我们需要对其进行离散化,新创一个数组a里面来放在我们的初始序列中在这个位置上的数是第几大的这里还要用一个小技巧排序,关于离散化的技巧我们 ...

  9. 洛谷 题解 P3984 【高兴的津津】

    潇洒の开始 大水题一枚/小声说话 先吐槽一下: 为什么津津经历了魔鬼训练就可以Au日常QWQ,人家也是早起晚睡的好吧 (谁说魔鬼训练就一定是早起晚睡的) 思路 津津的高兴是持续m天的,但是一旦在这m中 ...

  10. Hyperspectral Image Classification Using Similarity Measurements-Based Deep Recurrent Neural Networks

    用RNN来做像素分类,输入是一系列相近的像素,长度人为指定为l,相近是利用像素相似度或是范围相似度得到的,计算个欧氏距离或是SAM. 数据是两个高光谱数据 1.Pavia University,Ref ...