[LeetCode]Burst Balloons

相当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];
    }
}

 

posted @ 2015-12-01 07:07  Weizheng_Love_Coding  阅读(694)  评论(0编辑  收藏  举报