LintCode_168 Burst Balloons
来源:互联网 发布:淘宝保健品代理 编辑:程序博客网 时间:2024/06/12 00:12
Given n
balloons, indexed from 0
to 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 i
you will get nums[left] * nums[i] * nums[right]
coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum
coins you can collect by bursting the balloons wisely.
- You may imagine nums[-1] = nums[n] = 1
. They are not real therefore you can not burst them.
- 0 ≤ n
≤ 500, 0 ≤ nums[i]
≤ 100
Example
Given [4, 1, 5, 10]
Return 270
nums = [4, 1, 5, 10] burst 1, get coins 4 * 1 * 5 = 20nums = [4, 5, 10] burst 5, get coins 4 * 5 * 10 = 200 nums = [4, 10] burst 4, get coins 1 * 4 * 10 = 40nums = [10] burst 10, get coins 1 * 10 * 1 = 10Total coins 20 + 200 + 40 + 10 = 270
思路是使用dp, dp[l][r] 是刺破l至r(不包括l和r)所以只要确定中间最后一个刺破的就行了,别忘了在原始数据头尾都加1;
class Solution {public: /** * @param nums a list of integer * @return an integer, maximum coins */ int maxCoins(vector<int>& nums) { // Write your code here nums.push_back(1); nums.insert(nums.begin(), 1); const int len = (int)nums.size(); int dp[len][len]; memset(dp, 0, sizeof(dp)); for (int k = 2; k < len; k++) { for (int l = 0; l + k < len; l++) { int r = l + k; for (int mid = l + 1; mid < r; mid++) { dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r] + nums[l] * nums[mid] * nums[r]); } } } return dp[0][len - 1]; }};
0 0
- LintCode_168 Burst Balloons
- Burst Balloons
- Burst Balloons
- Burst Balloons
- Burst Balloons
- Burst Balloons
- Burst Balloons
- Burst Balloons
- Burst Balloons
- leetcode Burst Balloons
- 312 Burst Balloons
- Burst Balloons | LeetCode
- leetcode之Burst Balloons
- leetcode 312 Burst Balloons
- [LeetCode]Burst Balloons
- leetcode 312 : Burst Balloons
- 【LEETCODE】312-Burst Balloons
- LeetCode Burst Balloons
- java—原子性
- 0-1背包 多阶段决策
- Android 获取屏幕尺寸与密度
- Android 工作记录二 之 viewpager 动态删除fragment
- LintCode做题记录-两数组的交 II
- LintCode_168 Burst Balloons
- C#入门6.8——字符串的合并
- 通过cmd将文件夹的隐藏
- 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。 虽然使用其他方法也可以对外共享数
- Vijos 1060 盒子(DP)
- LintCode做题记录- 数字三角形
- Eclipse修改编码格式
- Android强制设置横屏或竖屏
- MyEclipse2014的若干问题-乱码解决方案