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问题,开始没想出来唉,看了下别人的。就是从left和right间的间距从2开始,一直递推到相差n-1这种情况。代码如下所示:

 class Solution {
public:
static bool noCoins(int a)
{
return a == ;
}
int maxCoins(vector<int>& nums) {
nums.erase(remove_if(nums.begin(), nums.end(), noCoins), nums.end());//注意这里的处理方法。先调用remove在调用erase
if(nums.size() == )
return ;
nums.resize(nums.size() + );
copy(nums.begin(), nums.end() - , nums.begin() + );
nums[] = nums[nums.size() - ] = ;
int sz = nums.size();
int dp[sz][sz] = {};
for(int interval = ; interval < sz; ++interval){//interval指的是left与right之间的间隔
for(int left = ; left < sz - interval; ++left){
int right = left + interval;
for(int j = left + ; j < right; ++j){//穷尽left-right之间的每一种可能值
dp[left][right] = max(dp[left][right], dp[left][j] + nums[left]*nums[j]*nums[right] + dp[j][right]);
} //注意这里取left以及right的原因是left到j之间的数字以及被dp[left][j]所覆盖了
}
}
return dp[][sz-];//这是最终结果了
}
};

LeetCode OJ: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 爆气球

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

    参考: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] Burst Balloons 打气球游戏

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

  6. 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

    [抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...

  7. 312 Burst Balloons 戳气球

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

  8. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

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

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

  10. [LeetCode] Burst Balloons (Medium)

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

随机推荐

  1. configparser模块来生成和修改配置文件

    1. 安装configparser模块 pip3 install configparser ##python2.7模块名为ConfigParser 2. 创建配置文件 import configpar ...

  2. AJAX POST请求中参数以form data和request payload形式在php中的获取方式

    一.MINE TYPE问题: php对mime type为“application/x-www-form-urlencoded”(表单提交)和“multipart/form-data”(文件上传)的P ...

  3. Linux CentOS6环境下MySQL5.1升级至MySQL5.5版本过程

    转载地址:http://www.laozuo.org/6145.html 老左今天有在帮朋友的博客搬迁到另外一台VPS主机环境,其环境采用的是LLSMP架构的,原先的服务器采用的是LNMP网站环境,其 ...

  4. OpenStack、KVM、VMWare和Docker

    一.虚拟化 1.什么是虚拟化 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内 ...

  5. Java-JVM调优常见配置举例

    常见配置举例 堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统 下,一般限制在1.5G~ ...

  6. The SO_REUSEPORT socket option

    One of the features merged in the 3.9 development cycle was TCP and UDP support for the SO_REUSEPORT ...

  7. C#:手把手教你用C#打包应用程序(安装程序卸载程序)

    摘要:本文介绍在C#中手把手教你用C#打包应用程序(安装程序卸载程序) 1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点 ...

  8. python:使用Fabric自动化你的任务

    http://www.th7.cn/Program/Python/2012/03/05/62236.shtml

  9. 蓄水池抽样算法 Reservoir Sampling

    2018-03-05 14:06:40 问题描述:给出一个数据流,这个数据流的长度很大或者未知.并且对该数据流中数据只能访问一次.请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等. 问题求 ...

  10. 树 & 二叉树

    2018-01-04 19:13:46 一.树 在计算机科学中,树(英语:tree)是一种数据结构,用来模拟具有树状结构性质的数据集合.它是由n(n>0)个有限节点组成一个具有层次关系的集合.把 ...