LeetCode 312. Burst Balloons--动态规划
2017-09-23 17:05
197 查看
题目链接
312. Burst Balloons
Given
Each balloon is painted with a number on it represented by array
You are asked to burst all the balloons. If the you burst balloon
will get
adjacent indices of
becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
(1) You may imagine
you can not burst them.
(2) 0 ≤
100
Example:
Given
Return
解:
真的是很难想的一题,参考了在线疯狂的博客的思路:
(看到这个思路时我发现我一直想不出是因为我没有仔细考虑 “(1) You
may imagine
They are not real therefore you can not burst them.” 这句话)
1.子问题的解是父问题解的一部分,满足这个条件,符合动态规划的要求
2.注意到“求数组得到的最大值在最前和最后分别加一个1,1不能爆”,用dp[i][j]代表子序列(i,j)产生的最大值,不包括 i 和 j ,value[]表示每一位置的数值,value的大小是nums大小(size)加2,首位分别都是1,我们最后想得到的结果就可以用dp[0][size+1]表示
3.dp[i,j] = max(dp[i,last]+dp[last,j]+value[last]*value[i]*value[j]),i < last < j ,last的含义是在i和j之间最后爆的气球,last可以将一个序列分隔为2部分,序列的最大值就是2个子序列的最大值加上last爆时产生的值
代码(c++):
class Solution {
public:
int maxCoins(vector<int>& nums) {
int size = nums.size();
int dp[502][502] = {0};
int value[502];
value[0] = 1;
for (int i = 1; i <= size; i++) value[i] = nums[i-1];
value[size+1] = 1;
for (int i = 0; i < size-1; i++) dp[i][i+1] = 0;
for (int len = 2; len < size+2; len++) {
for (int i = 0; i < size; i++) {
int j = i+len;
for (int last = i+1; last <= j-1; last++) {
dp[i][j] = max(dp[i][j], dp[i][last] + dp[last][j] + value[last]*value[i]*value[j]);
}
}
}
return dp[0][size+1];
}
};
312. Burst Balloons
Given
nballoons, indexed from
0to
n-1.
Each balloon is painted with a number on it represented by array
nums.
You are asked to burst all the balloons. If the you burst balloon
iyou
will get
nums[left] * nums[i] * nums[right]coins. Here
leftand
rightare
adjacent indices of
i. After the burst, the
leftand
rightthen
becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
(1) You may imagine
nums[-1] = nums = 1. They are not real therefore
you can not burst them.
(2) 0 ≤
n≤ 500, 0 ≤
nums[i]≤
100
Example:
Given
[3, 1, 5, 8]
Return
167
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
解:
真的是很难想的一题,参考了在线疯狂的博客的思路:
(看到这个思路时我发现我一直想不出是因为我没有仔细考虑 “(1) You
may imagine
nums[-1] = nums = 1.
They are not real therefore you can not burst them.” 这句话)
1.子问题的解是父问题解的一部分,满足这个条件,符合动态规划的要求
2.注意到“求数组得到的最大值在最前和最后分别加一个1,1不能爆”,用dp[i][j]代表子序列(i,j)产生的最大值,不包括 i 和 j ,value[]表示每一位置的数值,value的大小是nums大小(size)加2,首位分别都是1,我们最后想得到的结果就可以用dp[0][size+1]表示
3.dp[i,j] = max(dp[i,last]+dp[last,j]+value[last]*value[i]*value[j]),i < last < j ,last的含义是在i和j之间最后爆的气球,last可以将一个序列分隔为2部分,序列的最大值就是2个子序列的最大值加上last爆时产生的值
代码(c++):
class Solution {
public:
int maxCoins(vector<int>& nums) {
int size = nums.size();
int dp[502][502] = {0};
int value[502];
value[0] = 1;
for (int i = 1; i <= size; i++) value[i] = nums[i-1];
value[size+1] = 1;
for (int i = 0; i < size-1; i++) dp[i][i+1] = 0;
for (int len = 2; len < size+2; len++) {
for (int i = 0; i < size; i++) {
int j = i+len;
for (int last = i+1; last <= j-1; last++) {
dp[i][j] = max(dp[i][j], dp[i][last] + dp[last][j] + value[last]*value[i]*value[j]);
}
}
}
return dp[0][size+1];
}
};
相关文章推荐
- Leetcode 动态规划DP类题目
- LeetCode 70. Climbing Stairs 动态规划
- (动态规划)LeetCode#152. Maximum Product Subarray
- 【LeetCode】120. Triangle 基于C++和Java的分析及解法,动态规划
- LeetCode Interleaving String(动态规划)
- LeetCode 376. Wiggle Subsequence(动态规划)
- LeetCode 64. Minimum Path Sum 动态规划
- Leetcode 动态规划 Candy
- LeetCode 72 Edit Distance (动态规划)
- 343. Integer Break(LeetCode, C++, 动态规划)
- 动态规划——Decode Ways[LeetCode]
- 字符串+哈希表+小动态规划(Longest Substring Without Repeating Characters -- LeetCode)
- Leetcode解题笔记 70. Climbing Stairs [Easy] 动态规划
- 【Leetcode】70. Climbing Stairs 【动态规划】
- LeetCode Unique Paths 动态规划与大数
- leetcode 264. Ugly Number II-丑数|动态规划
- [LeetCode - 动态规划] 120. Triangle
- [LeetCode]Longest Palindromic Substring题解(动态规划)
- leetcode 337. House Robber III-动态规划|Java|Python简洁高效
- LeetCode 474. Ones and Zeroes(动态规划)