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. UNIX网络编程学习(9)--getsockname和getpeername的用法及实例(转)

    getsockname和getpeername #include <sys/socket.h>int getsockname(int sockfd, struct sockaddr *lo ...

  2. ArrayAdapter requires the resource ID to be a TextView

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABDUAAADFCAIAAADLz168AAAgAElEQVR4nO2d368kxZXnQ37dn9qdWa

  3. Chart.js docs

    原文链接:http://www.bootcss.com/p/chart.js/docs/ 引入Chart.js文件 首先我们需要在页面中引入Chart.js文件.此工具库在全局命名空间中定义了Char ...

  4. jeesite快速开发平台

    兴致勃勃地下载下来准备好好研究一番,安装启动结果报错啦: java.lang.ClassNotFoundException: com.thinkgem.jeesite.modules.sys.list ...

  5. leetcode 783. Minimum Distance Between BST Nodes 以及同样的题目 530. Minimum Absolute Difference in BST

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  6. OSD锁定怎么解锁?

    方法是这样的: 先按中间的建关掉显示器电源,关了显示器后按住左键,在按中间的建开机,这时屏幕闪一下就解锁了.在按中间的建打开显示器就行了. 加锁的方法和解锁一样

  7. I.MX6 bq27441 GPOUT interrupt

    /******************************************************************** * I.MX6 bq27441 GPOUT interrup ...

  8. QT笔记1

    1 第一个就是helloworld窗体啦 #include <qapplication.h> #include <qpushbutton.h> int main( int ar ...

  9. 堆与栈(JAVA)——以String str="abc"的深度含义解释

    栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆.   栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的 ...

  10. 【黑金教程笔记之004】【建模篇】【Lab 03 消抖模块之一】—笔记

    设计思路: (1)       一旦检测到按键资源按下(从高电平到低电平),“电平检测模块”就会拉高H2L_Sig电平,然后拉低. (2)       “10ms延迟模块”检测到H2L_Sig高电平, ...