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:Ubuntu系统的安装

    好久没更了,今天就更完这一期的Linux系统吧,这次主要安装的是常用Linux系统的之一:Ubuntu(乌班图)系统,这个系统和CentOS 7的安装步骤也是类似的,(我不采取用虚拟机的方法来安装,当 ...

  2. FreeSWITCH添加中文语音

    1.准备中文语音包 可以到freeswitch官网下载,也可以自己录制 2.中文资源的安装路径:  英文资源的路径为conf/sounds/en/us/callie/...  类似的设置中文资源的路径 ...

  3. WKWebView 加载本地HTML随笔

    一天的时间 解决两个坑~~ 1.加载不出来本地HTML 的JS  CSS 样式,问题是引用到项目中 是用的group 这个是错的 直接上图 就知道了 像上图一样,加入相对路径即可,因为如果使用了gro ...

  4. git 本地重命名文件夹大小写并提交到远程分支

    git branch 查看本地分支 git branch -a 查看本地 本地分支可直接切换:git checkout name 进入正题: 1.文件夹备份 2.git config core.ign ...

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

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

  6. idea xml版本修改问题

    STEP 1.选中模块 STEP 2.file---->Project Structure---->Facets---->把相应模块的给删除"-"----> ...

  7. cookie mapping 原理理解

    深入浅出理解 COOKIE MAPPING Cookie mapping技术 利用javascript跨域访问cookie之广告推广

  8. C++类中一个构造函数调用另一个构造函数

    class A { int a; int b; int c; public: A(int aa, int bb) : a(aa), b(bb),c(0) { cout << "a ...

  9. 在IDEA中实战Git-branch

    工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...

  10. zombodb 索引管理

    zombodb 支持标准的index 管理(create .alter.drop) 创建索引 CREATE INDEX index_name ON table_name USING zombodb ( ...