312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示。
现在要求你戳破所有的气球。每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币。 这里的 left 和 right 代表和 i 相邻的气球的序号。 注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球。
求所能获得硬币数量的最大值。
注意:
(1) 你可以认为 nums[-1] = nums[n] = 1,但注意它们不是真实存在的所以并不能被戳破。
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
例子:
输入 [3, 1, 5, 8]
输出 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
详见:https://leetcode.com/problems/burst-balloons/description/
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];
}
};
参考:https://www.cnblogs.com/grandyang/p/5006441.html
312 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 ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- [LeetCode] 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
随机推荐
- 自定义View实现跟随手指的小球
package com.pingyijinren.test; import android.content.Context; import android.graphics.Canvas; impor ...
- POJ 2431 Expedition【贪心】
题意: 卡车每走一个单元消耗一升汽油,中途有加油站,可以进行加油,问能否到达终点,求最少加油次数. 分析: 优先队列+贪心 代码: #include<iostream> #include& ...
- css3 模拟标牌震荡效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- python类变量以及应用场景
类变量是python 中class 的变量,区别于实例的变量.我们通过一些例子具体了解一下 先看下面的例子 >>> class Demo(object): ... v1 = 1 .. ...
- mysql 排序order by可以根据权重,进行表达式计算。再排序
1.select * from tbl_actor order by (follower_count+Recommend_weight)*weight_ratio desc limit 3; 2.or ...
- 018 cisco 3560 MAC地址绑定
在3560交换机上show ip dhcp binding 可以看到通过DHCP服务广播出去的IP地址与MAC地址的对应表: 比如: Switch#show ip dhcp binding IP ad ...
- 操作系统——IO管理
一.IO系统结构 在计算机系统中.cpu要和很多外设进行交互.比方鼠标,键盘,网卡等等. 1.IO是怎样协调工作的那? (1)对于设备来说,其有两部分组成,一部分是机械部分,还有一部分是电子控制部分. ...
- HDU 4950 Monster(公式)
HDU 4950 Monster 题目链接 题意:给定怪兽血量h,你攻击力a.怪物回血力b,你攻击k次要歇息一次,问是否能杀死怪兽 思路:签到题,注意最后一下假设打死了怪,那么怪就不会回血了 思路: ...
- Ajax使用JSON数据格式
1: •JSON(JavaScriptObject Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不须 ...
- 另类创业招聘(REV#2)
项目一 项目名:苏格拉底网 项目性质:人才測评为主.辅助以简易人才招聘功能的小众功能站点.项目使用了自主研发的人才測评算法以及人格分类模型(与MBTI非常相似). 项目相关网址:sugeladi.ne ...