[leetcode]解决Burst Balloons的一点小心得
2018-01-13 00:56
411 查看
本次选择的题目是
Given n balloons, indexed from 0 to n-1. Each balloon is painted witha number on it represented by array nums. You are asked to burst allthe 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.
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
Solution:
这道题提出了一种打气球的游戏,每个气球都对应着一个数字,我们每次打爆一个气球,得到的金币数是被打爆的气球的数字和其两边的气球上的数字相乘,如果旁边没有气球了,则按1算,以此类推,求能得到的最多金币数。像这种求极值问题,我们一般都要考虑用动态规划Dynamic Programming来做,我们维护一个二维动态数组dp,其中dp[i][j]表示打爆区间[i,j]中的所有气球能得到的最多金币。题目中说明了边界情况,当气球周围没有气球的时候,旁边的数字按1算,这样我们可以在原数组两边各填充一个1,这样方便于计算。这道题的最难点就是找递归式,如下所示:
dp[i][j] = max(dp[i][j], nums[i - 1]*nums[k]*nums[j + 1] + dp[i][k - 1] + dp[k + 1][j]) ( i ≤ k ≤ j )
有了递推式,我们可以写代码,我们其实只是更新了dp数组的右上三角区域,我们最终要返回的值存在dp[1]
中,其中n是两端添加1之前数组nums的个数。参见代码如下:
Given n balloons, indexed from 0 to n-1. Each balloon is painted witha number on it represented by array nums. You are asked to burst allthe 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.
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
Solution:
这道题提出了一种打气球的游戏,每个气球都对应着一个数字,我们每次打爆一个气球,得到的金币数是被打爆的气球的数字和其两边的气球上的数字相乘,如果旁边没有气球了,则按1算,以此类推,求能得到的最多金币数。像这种求极值问题,我们一般都要考虑用动态规划Dynamic Programming来做,我们维护一个二维动态数组dp,其中dp[i][j]表示打爆区间[i,j]中的所有气球能得到的最多金币。题目中说明了边界情况,当气球周围没有气球的时候,旁边的数字按1算,这样我们可以在原数组两边各填充一个1,这样方便于计算。这道题的最难点就是找递归式,如下所示:
dp[i][j] = max(dp[i][j], nums[i - 1]*nums[k]*nums[j + 1] + dp[i][k - 1] + dp[k + 1][j]) ( i ≤ k ≤ j )
有了递推式,我们可以写代码,我们其实只是更新了dp数组的右上三角区域,我们最终要返回的值存在dp[1]
中,其中n是两端添加1之前数组nums的个数。参见代码如下:
// Non-recursion class Solution { public: int maxCoins(vector<int>& nums) { int n = nums.size(); nums.insert(nums.begin(), 1); nums.push_back(1); vector<vector<int> > dp(nums.size(), vector<int>(nums.size() , 0)); for (int len = 1; len <= n; ++len) { for (int left = 1; left <= n - len + 1; ++left) { int right = left + len - 1; for (int k = left; k <= right; ++k) { dp[left][right] = max(dp[left][right], nums[left - 1] * nums[k] * nums[right + 1] + dp[left][k - 1] + dp[k + 1][right]); } } } return dp[1] ; } };
相关文章推荐
- [leetcode]解决Search a 2D Matrix II的一点小心得
- [leetcode]解决Minimum Height Trees的一点小心得
- [leetcode]解决Same Tree的一点小心得
- [leetcode]解决Symmetric Tree的一点小心得
- [leetcode]解决Majority Element的一点小心得
- [leetcode]解决Assign Cookies的一点小心得
- [leetcode]解决Maximum Subarray的一点小心得
- [leetcode]解决Move Zeroes的一点小心得
- [leetcode]解决Clone Graph的一点小心得
- [leetcode]解决Kth Largest Element in an Array的一点小心得
- [leetcode]解决Climbing Stairs的一点小心得
- [leetcode]解决House Robber的一点小心得
- 关于信号槽连接地方的一点心得,一个问题困扰了一下午才解决,纪念一下。
- 解决Dynamips使用时CPU占用率100%的一点个人心得
- 系统很慢一点解决心得
- 解决堆损坏的一点心得
- CGAL配置的一点心得(各种错误的解决办法)
- Python字符串的encode与decode研究心得乱码问题解决方法
- 使用CImageList的一点心得
- 买票--关于命令行的一点心得