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. 忘记Linux用户密码怎么办?

           忘记密码解决办法(centos6.5版本) 1.开机时,在此页面一直按ESC 2.然后进入以下界面时,按一下字母  “E”    键 (3)再按字母   “E”   键 (4)之后 输入 ...

  2. CDMA码片序列问题

    要想知道到底是怎么算的 建议看见这篇博客的任何一位去先看一下这篇博客:https://blog.csdn.net/dog250/article/details/6420427 在CDMA中.每一个比特 ...

  3. MySQL数据库内置加密函数总结

    首先,我认识的加密函数有以下几个: password(plainText):旧版(OLD_PASSWORD())加密后长度16位,新版41位select length(password("1 ...

  4. brand new start

    做了约两年半的安全,留下了约五十多篇笔记,从电脑搬过来,免的丢了

  5. JDBC driver连接MySQL运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than

    出错原因: 因为安装mysql的时候时区设置的不正确. mysql默认的是美国的时区,而我们中国大陆要比他们迟8小时,采用GMT+8:00格式. 也就是说是数据库和系统时区差异所造成的. 验证:运行c ...

  6. LADP(Lightweight Directory Access Protocol)轻量目录访问协议~小知识

    What is LDAP and how does it work(implementation)? LDAP stands for “Lightweight Directory Access Pro ...

  7. css文本是否换行

    关于文本换行有三个属性: white-space word-break word-wrap white-space normal 默认.空白会被浏览器忽略 pre 空白会被浏览器保留.其行为方式类似 ...

  8. 求数组的相邻子数组的最大值(txt文件存储)

    package mypackage; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...

  9. jquery中filter的用法

    一.filter的参数类型可分为两种 1.传递选择器 $('a').filter('.external') 2.传递过滤函数 $('a').filter(function(index) {       ...

  10. Xamarin SearchView 用法摘记

    与Windows开发不同,这个控件的事件比较难找,费了半天劲才知道应该用哪个事件.核心代码如下: public class MainActivity : Activity { protected ov ...