class Solution {
public int maxCoins(int[] iNums) {
int[] nums = new int[iNums.length + 2];
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1; int[][] dp = new int[n][n];
for (int k = 2; k < n; ++k)
for (int left = 0; left < n - k; ++left) {
int right = left + k;
for (int i = left + 1; i < right; ++i)
dp[left][right] = Math.max(dp[left][right],
nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right]);
} return dp[0][n - 1];
}
}

参考:https://leetcode.com/problems/burst-balloons/discuss/76228/Share-some-analysis-and-explanations

补充一个python的实现:

 class Solution:
def maxCoins(self, nums: 'List[int]') -> 'int':
nums = [1] + nums + [1]
n = len(nums)
dp = [[0 for _ in range(n)]for _ in range(n)]
for k in range(2,n):
for left in range(n-k):
right = left + k
for i in range(left+1,right):
dp[left][right] = max(dp[left][right],nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right])
return dp[0][n-1]

leetcode312的更多相关文章

  1. [Swift]LeetCode312. 戳气球 | Burst Balloons

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

  2. LeetCode312. Burst Balloons

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

  3. leetcode312 戳气球

    动态规划 time O class Solution { public: int maxCoins(vector<int>& nums) { nums.insert(nums.be ...

  4. leetcode 日常清单

    a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...

  5. DP-Burst Balloons

    leetcode312: https://leetcode.com/problems/burst-balloons/#/description Given n balloons, indexed fr ...

随机推荐

  1. elasticsearch 的查询 /_nodes/stats 各字段意思

    /_nodes/stats 字段意思   “”   1 {  2  "_nodes": {3 "total": 1, "successful" ...

  2. 2017年3月30日15:00:19 fq以后的以后 动态代理

    代理与继承,组合不同的是,继承是继承父类特性,组合是拼装组合类的特性,代理是使用代理类的指定方法并可以做自定义. 静态类是应用单个类,当代理的类数量较多时可用动态代理,动态代理在概念上很好理解 htt ...

  3. Linux装agent

    解压Linux.zip  Linux的负载机链接:https://pan.baidu.com/s/1yrmsT3PYfuL9Wlh4FEYxaA 密码:s72n unzip Linux.zip chm ...

  4. python txt文件常用读写操作

    文件的打开的两种方式 f = open("data.txt","r") #设置文件对象 f.close() #关闭文件 #为了方便,避免忘记close掉这个文件 ...

  5. linux的基本操作2

    /dev/ha[a-d] IDE硬盘(过时了)/dev/sd[a-p] U盘,scsi,sata,ssd硬盘(流行)/dev/cdrom   光盘 CD-ROM/dev/mouse 鼠标 fdisk ...

  6. 面向对象的编程思想和Java中类的概念与设计

    面向对象的编程思想学习,面向对象内容的三条主线;1.java类及类的对象2.面向对象的三大特征3.其他关键字学习内容:3.1面向对象与面向过程面向对象与面向过程在应用上的区别 Java中类的概念与设计 ...

  7. H5入门须知

    ---恢复内容开始--- 首先,让我们来了解一下H5是做什么的,H5全称为“超文本标记语言”.是对网页进行编辑的技术.H5运用Hbulider进行网页编辑.网页可以分为三部分分别是title(主题)u ...

  8. 原生JS怎样给div添加链接

    html: <div href="http://www.atigege.com" target="_blank">个人网站</div> ...

  9. bvlc_reference_caffenet网络权值可视化

    一.网络结构 models/bvlc_reference_caffenet/deploy.prototxt 二.显示conv1的网络权值 clear; clc; close all; addpath( ...

  10. MySQL Point in Time Recovery the Right Way

    In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly. Sometimes we nee ...