Java实现 LeetCode 312 戳气球
312. 戳气球
有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
现在要求你戳破所有的气球。每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币。 这里的 left 和 right 代表和 i 相邻的两个气球的序号。注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球。
求所能获得硬币的最大数量。
说明:
你可以假设 nums[-1] = nums[n] = 1,但注意它们不是真实存在的所以并不能被戳破。
0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
示例:
输入: [3,1,5,8]
输出: 167
解释: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 315 + 358 + 138 + 181 = 167
class Solution {
private int[][] dp;
public void fill(int[] nums, int from, int to) {
int max = 0, maxLeft, maxRight, result;
//假设第i个气球是最后被戳破的
for (int i = from; i <= to; i++) {
maxLeft = dp[from][i - 1];
maxRight = dp[i + 1][to];
result = maxLeft + maxRight + nums[from - 1] * nums[i] * nums[to + 1];
max = result > max ? result : max;
}
dp[from][to] = max;
}
public int maxCoins(int[] nums) {
int length = nums.length;
dp = new int[length + 2][length + 2];
for (int i = length + 1; i >= 0; i--) {
Arrays.fill(dp[i], 0);
}
int[] expandNums = new int[length + 2];
expandNums[0] = expandNums[length + 1] = 1;
System.arraycopy(nums, 0, expandNums, 1, length);
for (int span = 0; span < length; span++) {
//fill a diagonal
//assume the span (of array of length i) is i-1
for (int from = length - span; from > 0; from--) {
//fill dp[from][from + span]
fill(expandNums, from, from + span);
}
}
return dp[1][length];
}
}
Java实现 LeetCode 312 戳气球的更多相关文章
- Leetcode 312.戳气球
戳气球 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * n ...
- 312. 戳气球【困难】【区间DP】
题目链接 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- 初探numpy
安装numpy 通过python pip安装numpy pip install numpy numpy ndarray对象 创建ndarray对象只需调用numpy的array函数即可 numpy.a ...
- JDBC06 其他操作及批处理Batch
灵活指定SQL语句中的变量 -PreparedStatement 对存储过程进行调用 -CallableStatement 运用事务处理 -Transaction 批处理 -Batch -对于大量的批 ...
- 使用反射模拟struts2属性注入功能
1.在项目开发中,如果没有使用框架进行数据绑定与封装,则可能会写大量的类似下面的代码: String value=request.getParameter("v"); if(nul ...
- 201843 2019-2020-2 《Python程序设计》实验二报告
201843 2019-2020-2 <Python程序设计>实验二报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:20184302 实验教师:王 ...
- 【Socket编程】【第一节】【Socket基本原理和套接字】
参考http://c.biancheng.net/view/2351.html 一.scoket套接字(告诉你使用哪种数据传输方式) 这个世界上有很多种套接字(socket),比如 DARPA Int ...
- 00002-layui 右侧呼出页面,PopupLayer
我这里的功能是弹出 右侧搜索 的页面: top.layui.admin.popupRight({ id: 'LAY_business_PopupLayer' ,area: '350px' ,succe ...
- 走向统一的 .NET 旅程
这是微软第一次完全线上举办的Build大会,也是第一次完全属于开发者的大会.几乎所有的新产品都是属于开发者,开发者成为了唯一的主角. 现在的微软比以往任何时候都贴近开发者,重视开发者的作用,为他们打造 ...
- python中的字典dict
字典的常用操作及方法 增: dic[key]=value 有则修改,无则添加 dic.setdefault( ) 有则不变,无则添加:有键无值则值为None, 删: dic.pop(key) 删除后 ...
- 最全面的Android Studio使用教程
http://www.admin10000.com/document/5496.html
- Havel定理 poj1659
http://blog.csdn.net/xcszbdnl/article/details/14174669 代码风格这里的 Frogs' Neighborhood Time Limit: 5000M ...