public class Solution {
    public int maxCoins(int[] nums) {
        int n = nums.length;
        int[] newNum = new int[n+2];
        newNum[0] = 1; newNum[n+1] = 1;
        for(int i=0; i<n; i++){
            newNum[i+1] = nums[i];
        }
        
        int[][] memo = new int[n+2][n+2];
        return dfs(newNum, memo, 0, n+1);
    }
    
    public int dfs(int[] nums, int[][] memo, int left, int right){
        if(right - left <= 1) return 0;
        if(memo[left][right] != 0) return memo[left][right];
        int ans = 0;
        for(int i = left+1; i<right; i++){      // 最后一个戳破, i means last brust balloon
            ans = Math.max(ans, dfs(nums, memo, left, i) + dfs(nums, memo, i, right) + nums[left]*nums[i]*nums[right]);
        }
        memo[left][right] = ans;
        return ans;
    }
}
// a balloon does not depend on the balloons already burst
// only the first and last balloons we are sure of their adjacent balloons before hand

大米中的大米
12 声望5 粉丝

你的code是面向面试编程的,收集和整理leetcode discussion里个人认为的最优且最符合我个人思维逻辑的解法。