[LeetCode]Burst Balloons
2015-12-01 07:07
323 查看
相当tricky的一道题目,我怀疑在面试的时候有人能在45分钟想出来,我参考了一下https://leetcode.com/discuss/72215/java-dp-solution-with-detailed-explanation-o-n-3这个人的思路。
public class Solution { public int maxCoins(int[] nums) { if (nums == null || nums.length == 0) return 0; int length = nums.length; int[][] record = new int[length][length]; for (int len = 1; len <= length; len++) { for (int i = 0; i < length - len + 1; i++) { int start = i; int end = i + len - 1; int s = start - 1 == -1 ? 1 : nums[start - 1]; int e = end + 1 == length ? 1: nums[end + 1]; for (int j = start; j <= end; j++) { int tmp = nums[j] * s * e; tmp += j != start ? record[start][j - 1] : 0; tmp += j != end ? record[j + 1][end] : 0; record[start][end] = Math.max(record[start][end], tmp); } } } return record[0][length - 1]; } }
相关文章推荐
- [LeetCode]Paint House II
- OSChina 周二乱弹 —— 嗑瓜子是种寂寞
- HTML基础
- 计算机视觉牛人博客和代码汇总(全)
- AngularJS Best Practices: ngRoute
- 一个互联网+应用的创意
- PHP基础知识
- 248. Strobogrammatic Number III
- [LeetCode]Remove Linked List Elements
- 在主机中通过xdebug远程调试Vagrant虚拟机中drush脚本的方法
- Android volley全局请求队列和图片加载
- 247. Strobogrammatic Number II
- 学习Python (七)
- 用Canvas获取图像平均颜色
- 普法知识(13):什么是法人?
- CALayer基础
- [LeetCode] Populating Next Right Pointers in Each Node
- linux 下安装opencv3.0在编译时出现的问题undefined reference to `parallel_pthreads_set_threads_num(int)'
- 51Nod 1459 迷宫游戏
- C/C++中位运算杂谈