lc 312 Burst Balloons


312 Burst Balloons

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

DP Accepted

这道题目应该从逆向来思考,选择以某个气球为分割点,那么其左边部分和右边部分都要依赖与那个气球。dp[i][j]表示到最后只剩下i和j所产生金币的最大值,对于i和j之间的k,如果引爆k,那么dp[i][j]被更新成max(dp[i][j], nums[i]*nums[k]*nums[j]+dp[i][k]+dp[k][j]),所以想要得到这个数组最大金币值,需要在首尾各加上一个元素,值为1,此时答案即为dp[0][len-1]。

class Solution {
public:
int maxCoins(vector<int>& nums) {
if (nums.size() == 0) return 0;
nums.insert(nums.begin(), 1);
nums.insert(nums.end(), 1);
int len = nums.size();
vector<vector<int>> dp(len, vector<int>(len, 0));
for (int i = len-3; i >= 0; i--) {
for (int j = i+2; j < len; j++) {
for (int k = i+1; k < j; k++) {
dp[i][j] = max(dp[i][j], nums[i]*nums[k]*nums[j]+dp[i][k]+dp[k][j]);
}
}
}
return dp[0][len-1];
}
};

LN : leetcode 312 Burst Balloons的更多相关文章

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

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

  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. [LeetCode] 312. Burst Balloons 爆气球

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

  4. 【LeetCode】312. Burst Balloons 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...

  5. 【LeetCode】312. Burst Balloons

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

  6. 312. Burst Balloons - LeetCode

    Question https://leetcode.com/problems/burst-balloons/description/ Solution 题目大意是,有4个气球,每个气球上有个数字,现在 ...

  7. 312. Burst Balloons

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

  8. [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming

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

  9. 312 Burst Balloons 戳气球

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

随机推荐

  1. 算法导论学习之线性时间求第k小元素+堆思想求前k大元素

    对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...

  2. Android 使用图片异步载入框架Universal Image Loader的问题

    使用的Jar包 问题:        optionsm = new DisplayImageOptions.Builder()         .displayer(new RoundedBitmap ...

  3. Linux上ln命令详细说明及软链接和硬链接的区别

    硬链接(hard link) UNIX文件系统提供了一种将不同文件链接至同一个文件的机制,我们称这种机制为链接.它可以使得单个程序对同一文件使用不同的名字.这样的好处是文件系 统只存在一个文件的副本, ...

  4. ajax跨域问题解决(spring boot)

    之前用的服务器响应头部添加Access-Control-Allow-Origin: *来解决的 public static void setResp(HttpServletResponse resp) ...

  5. URAL1099 Work Scheduling —— 一般图匹配带花树

    题目链接:https://vjudge.net/problem/URAL-1099 1099. Work Scheduling Time limit: 0.5 secondMemory limit: ...

  6. bzoj 3872 [ Poi 2014 ] Ant colony —— 二分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 从食蚁兽所在的边向叶节点推,会得到一个渐渐放大的取值区间,在叶子节点上二分有几群蚂蚁符 ...

  7. Robotframework之Run Keyword And Return Status和Run Keyword

    今天在android项目中遇到一个问题,场景达人每天第一次接单时,会弹出提示框:每日完成订单配送后将扣取1元保险费,是否确认接单?,点击确定后,才能接单成功,并且只有每天第一次接单才会弹出 如下图 此 ...

  8. appium学习【三】:截图时,图片命令中包含当前的函数名,以区分错误是在哪个函数报的

    import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 print sys._getframe().f_code.co ...

  9. 杂项-Java-百科:jar

    ylbtech-杂项-Java-百科:jar 在软件领域,JAR文件(Java归档,英语:Java ARchive)是一种软件包文件格式,通常用于聚合大量的Java类文件.相关的元数据和资源(文本.图 ...

  10. property_get 与 property_set 的返回值(转载)

    转自:http://wzw19191.blog.163.com/blog/static/13113547020103218265162/ /* property_get: returns the le ...