LeetCode OJ: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问题,开始没想出来唉,看了下别人的。就是从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(击破气球)的更多相关文章
- [LeetCode] 312. Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] 312. Burst Balloons 爆气球
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- 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 ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- 312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- [LeetCode] Burst Balloons (Medium)
Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...
随机推荐
- TOSCA自动化测试工具--识别元素唯一性的控件
当Modules模块通过Scan识别出页面元素后,选择需要测试的对象,然后判断对象唯一性
- Python的进程、线程和threading模块
(注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 怀念在学校念书的时候,我不小心触碰到了错误,老师会说:你错了:而我却总是倔强得以为自己没错.我的内心是不屑的,直到在真理面前 ...
- iOS开发之HelloKitty(移动社交平台项目)
iOS开发之HelloKitty(移动社交平台项目,2015.3,parishe)
- ABP官方文档翻译 1.4 启动配置
启动配置 配置ABP 替换内置服务 配置模块 创建模块配置 ABP提供了基础设施和模型在启动的时候对它及模块进行配置. 配置ABP 在模块的PreInitialize事件中配置ABP.示例配置如下: ...
- 使用C++11实现一个半同步半异步线程池
前言 C++11之前我们使用线程需要系统提供API.posix线程库或者使用boost提供的线程库,C++11后就加入了跨平台的线程类std::thread,线程同步相关类std::mutex.std ...
- PHP递归算法
/** * 获取菜单 * @param number $id * @return multitype: */ public function menu($id = 0) { $menu = M ( ' ...
- 20145313Java第五次实验
实验内容 网络编程TCP代码的结对完成,一人服务器,一人客户端,进行数据传输. 结伴对象:20145313卢鑫 实验步骤 本次实验中,需要两台电脑互联.一台电脑开启无线网,充当客户端,另一台连入局域网 ...
- 20145329 《Java程序设计》实验二总结
实验指导教师:娄嘉鹏老师 实验日期:2016.4.12 实验时间:15:30~17:30 实验序号:实验二 实验名称:Java面向对象程序设计 实验目的与要求: 1.初步掌握单元测试和TDD 2.理解 ...
- Centos6优化系统服务脚本
#!/bin/bash SysVer=`cat /etc/redhat-release | awk -F'release' '{print $2}' | awk -F'[ .]+' '{print $ ...
- svn常用维护命令
公司版本管理同时用的svn和gitlab,有互补作用 这边写一写慢慢积累自己用过的svn常用维护 查看版本范围之间的变化: [root@192-168-2-82 mnt]# svn log -r 45 ...