有n个气球,编号为0到n-1,每个气球都有一个分数,存在nums数组中。每次吹气球i可以得到的分数为 nums[left] * nums[i] * nums[right],left和right分别表示i气球相邻的两个气球。当i气球被吹爆后,其左右两气球即为相邻。要求吹爆所有气球,得到最多的分数。
样例
给出 [4, 1, 5, 10]
返回 270
nums = [4, 1, 5, 10] burst 1, 得分 4 * 1 * 5 = 20
nums = [4, 5, 10] burst 5, 得分 4 * 5 * 10 = 200
nums = [4, 10] burst 4, 得分 1 * 4 * 10 = 40
nums = [10] burst 10, 得分 1 * 10 * 1 = 10
总共的分数为 20 + 200 + 40 + 10 = 270
思想:
动态规划
dp[left][right] = max{dp[left][right] , nums[left] * nums[i] * nums[right] + dp[left][i]+dp[i][right]};

#ifndef C168_H
#define C168_H
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:/*** @param nums a list of integer* @return an integer, maximum coins*/int maxCoins(vector<int>& nums) {// Write your code hereint len = nums.size();if (len <= 0)return 0;vector<vector<int>> dp(len + 2, vector<int>(len + 2));vector<int> val(len + 2);for (int i = 1; i <= len; ++i){val[i] = nums[i - 1];}val[0] = 1;val[len + 1] = 1;int m = len + 2;for (int i = 2; i < m; ++i){for (int left = 0; left < m - i; ++left){int right = left + i;for (int j = left + 1; j < right; ++j){dp[left][right] = max(dp[left][right], val[left] * val[j] * val[right] + dp[left][j] + dp[j][right]);}}}return dp[0][m - 1];}
};
#endif

吹气球-LintCode相关推荐

  1. LintCode 249. 统计前面比自己小的数的个数

    给定一个整数数组(下标由 0 到 n-1, n 表示数组的规模,取值范围由 0 到10000).对于数组中的每个 ai 元素,请计算 ai 前的数中比它小的元素的数量. 注意事项 We suggest ...

  2. LintCode: Max Tree

    题目 Given an integer array with no duplicates. A max tree building on this array is defined as follow ...

  3. 【LintCode: 3. 统计数字】算法题解析

    这是一道来自LintCode的算法题目,本文用C++来解答这道题,链接为: https://www.lintcode.com/problem/digit-counts/description 题目描述 ...

  4. LintCode 1.A+B的问题

    LintCode 1.A+B的问题 描述 给出两个整数 a 和 b , 求他们的和. 答案 public class Solution {/*** @param a: An integer* @par ...

  5. 关于爬楼梯的lintcode代码

    讲真的,这个我只会用递归去做,但是lintcode上面超时,所以只有在网上找了个动态规划的,虽然这个程序懂了,但是我觉得还是挺不容易的真正弄懂的话-- class Solution { public: ...

  6. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  7. LintCode Longest Increasing Continuous Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/ 题目: Give an in ...

  8. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

  9. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  10. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

最新文章

  1. 序列信号产生器的verilog HDL 设计
  2. .jar中没有主清单属性_为什么 Spring Boot 的 jar 可以直接运行?
  3. ML与math:机器学习与高等数学基础概念、代码实现、案例应用之详细攻略——进阶篇
  4. 计算机组成原理第一阶段测试,计算机组成原理随堂测验1附答案
  5. armbian docker Chrome_一起学docker06-docker网络
  6. 残差曲线意义_生存曲线(三):统计分析方法这么多,到底选哪个?
  7. golang(7 方法重写)
  8. C语言实现读取elf文件某section
  9. flask 中upload()上传文件相关操作及出现的问题
  10. CentOS 7 新建桌面快捷方式,实现一键跳转到指定的文件夹路径
  11. 用正则表达式抓取网页图片
  12. 乐优商城(四十八)评论微服务(一)
  13. simulink反差表
  14. Idea的全局搜索快捷键
  15. 早起—怎样开启高效的一天?
  16. 地下迷宫探索 java_Java 8:探索可能性
  17. 【python】利用两层神经网络(网络必须用类)来训练mnist数据(要求准确率90%以上)
  18. c语言中类似于大括号的符号,大括号符号 c语言大括号的用法
  19. 学生管理系统(JAVA版) —— 按学号查询信息页面
  20. 废旧计算机cpu diy,手把手教你从废旧CPU提取金银

热门文章

  1. 数据分析系列:Z 检验和 T 检验的应用及代码实现
  2. CANCELLED: io.grpc.Context was canclled without error
  3. SpringCloud分布式开发理解
  4. 关于连接数据库出现Connection failed: Access denied for user ‘root‘@‘localhost‘ (using password: YES)解决方案(最有用)
  5. 基于JavaWeb的新闻发布管理系统设计与实现 毕业论文+任务书+开题报告+答辩PPT+项目源码及数据库文件
  6. pythonai人脸识别_AI的强大!用Python实现一个简单的人脸识别
  7. (matlab)地震数据频谱分析-频谱图代码
  8. html中<img src=““ alt=““>标签里面alt的作用
  9. java下的Http多线程下载与断点续传分析【转自酷勤网】
  10. dcx矩阵 - 打表 - 找规律