leetcode312
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的更多相关文章
- [Swift]LeetCode312. 戳气球 | Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- LeetCode312. Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- leetcode312 戳气球
动态规划 time O class Solution { public: int maxCoins(vector<int>& nums) { nums.insert(nums.be ...
- leetcode 日常清单
a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...
- DP-Burst Balloons
leetcode312: https://leetcode.com/problems/burst-balloons/#/description Given n balloons, indexed fr ...
随机推荐
- 1) 嵌套的 div ,或者 ul ol .li 阻止冒泡 ,特别是 对应onclick="test(event)" 通过传递event 阻止 冒泡. cancelBubble , stopPropagation
1 .html 结构: <ul class="ul_2 hide" data-first="5"> <li class="li_2& ...
- groupmod语法
语法 groupmod [-g <群组识别码> <-o>][-n <新群组名称>][群组名称] 参数: -g <群组识别码> 设置欲使用的群组识别码. ...
- 4.App非功能测试总结
移动app测试的另一重要方面是移动app的非功能需求.移动app在推出市场或进行进一步开发前,移动测试员有许多需要测试的问题. 早期开发阶段要进行的第一个测试应该是实用性测试.通常是由alpha用户或 ...
- python 前端 html
web 服务本质: 浏览器发出请求--HTTP协议--服务端接收信息----服务端返回响应---服务端把HTML文件发给浏览器--浏览器渲染页面. HTML: 超文本标记语言是一种用于创建网页的标记语 ...
- 实验 2:备份和还原IOS
SW1配置 Switch>en Switch#conf t Enter configuration commands, one per line. End with CNTL/Z. Switch ...
- C/C++ 宏技巧
1. C 也可以模板化 #define DEFINE_ARRAY_TYPE(array_type_, element_type_) \ static inline int array_type_ ## ...
- Spring Boot配置文件详解
挖个坑先 http://www.cnblogs.com/itdragon/p/8686554.html http://www.cnblogs.com/jtlgb/p/8532280.html
- 导入maven项目导入依赖不会报错,但使用的jar会标红
方法1: 1.通过编译找到报错的jar; 2.在 repository找到此jar,一般未下载完大小为1k我的是这样(); 3.删除未下载完全的jar,在项目上执行maven reimport会重新下 ...
- 阿里java开发规范 强制约束
http://news.51cto.com/art/201901/591018.htm :阿里开发强制要求的11条索引创建规范,提高性能 https://blog.csdn.net/Lujunwei ...
- 排序算法练习--JAVA(:内部排序:插入、选择、冒泡、快速排序)
排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... 内部排序: 插入排序:直接插入排序 选 ...