码迷,mamicode.com
首页 > 其他好文 > 详细

Burst Balloons

时间:2016-07-06 09:52:12      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:

 1 public class Solution {
 2     public int maxCoins(int[] nums) {
 3         if (nums.length == 0) {
 4             return 0;
 5         }
 6         
 7         int[] bl = new int[nums.length + 2];
 8         int index = 1;
 9         for (int num : nums) {
10             if (num != 0) {
11                 bl[index++] = num;
12             }
13         }
14         bl[0] = 1;
15         bl[index++] = 1;
16         
17         int[][] dp = new int[index][index];
18         
19         for (int i = 2; i < index; i++) {
20             for (int left = 0; left < index - i; left++) {
21                 int right = left + i;
22                 for (int k = left + 1; k < right; k++) {
23                     dp[left][right] = Math.max(dp[left][right], dp[left][k] + dp[k][right] + bl[k] * bl[left] * bl[right]);
24                 }
25             }
26         }
27         return dp[0][index - 1];
28         
29     }
30 }

1. Make final balloon as the position i.

2. try to optimize the solution from (left , i - 1) to (i + 1, right).

3. Since it garantee to cover i position, right = left + i. right < index. So left < index - k.

 

Burst Balloons

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/5645591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!