题目:
312. Burst Balloons
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.
Note:
(1) You may imagine nums[-1] = nums[n] = 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
class Solution {
public:
int maxCoins(vector<int>& nums) {
if (nums.size() == 1)return nums[0];
if (nums.size() == 2)return (nums[0] * nums[1] + (nums[1] > nums[0] ? nums[1] : nums[0]));
int size = nums.size();
//前后加1
nums.insert(nums.begin(), 1);
nums.push_back(1);
int size1 = size + 2,j=0;
vector<long int>tmp(size1, 0);
vector < vector < long int >> dp;
for (int i = 0; i < size1; i++)dp.push_back(tmp);
//填表
for (int gap = 2; gap <size1; gap++){//间隔的最小距离
for (int i = 0; i< size1; i++){
j = (i + gap)>size1-1?size1-1:i+gap;
long int temp = 0;
for (int k = i + 1; k < j; k++){//最后一个捅破k
temp = dp[i][k] + nums[i] * nums[k] * nums[j] + dp[k][j];
if (temp>dp[i][j])dp[i][j] = temp;
}
}
}
return dp[0][size1 - 1];
}
};
<h4 style="box-sizing: border-box; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(51, 51, 51); margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Submission Result: <a target=_blank id="result-state" ng-href="/submissions/detail/81977347/" ng-class="{'blue': 'text-info', 'green': 'text-success', 'red': 'text-danger'}[statusColor]" class="ng-binding text-success" href="https://leetcode.com/submissions/detail/81977347/" style="box-sizing: border-box; color: rgb(60, 118, 61); text-decoration: none; background-color: transparent;">Accepted </a><span class="fa fa-question-circle text-success" ng-show="statusText === 'Accepted'" data-toggle="popover" data-placement="top" data-content="Accepted means your program passes all test cases, however it does not necessarily mean your algorithm is optimal. If you believe your code has bugs, please submit a 'missing test case' report in Discuss or sending an email to admin." data-trigger="hover" data-original-title="" title="" style="box-sizing: border-box; color: rgb(60, 118, 61); display: inline-block; font-stretch: normal; line-height: 1; font-family: FontAwesome;font-size:undefined; text-rendering: auto; -webkit-font-smoothing: antialiased;"></span> <span data-ng-show="statusText !== 'Pending' && statusText !== 'Judging' && url_detail" style="box-sizing: border-box;"><a target=_blank id="more-details" class="btn-left-space btn btn-outline btn-alt" ng-href="/submissions/detail/81977347/" href="https://leetcode.com/submissions/detail/81977347/" style="box-sizing: border-box; color: rgb(0, 136, 204); text-decoration: none; display: inline-block; padding: 8px 18px; margin-bottom: 0px; font-size: 14px; line-height: 1.42857; text-align: center; white-space: nowrap; vertical-align: middle; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(207, 207, 207); border-radius: 2em; margin-left: 15px; background: 0px 0px;">More Details <span class="fa fa-chevron-right" style="box-sizing: border-box; display: inline-block; font-stretch: normal; line-height: 1; font-family: FontAwesome;font-size:undefined; text-rendering: auto; -webkit-font-smoothing: antialiased;"></span></a></span></h4><div data-ng-show="statusText === 'Accepted'" class="ng-fade" style="box-sizing: border-box; opacity: 1; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 18.1818px;"><div style="box-sizing: border-box;"><div ng-show="aceCtrl.interpret.next_challenge_pairs.length" style="box-sizing: border-box;"><div style="box-sizing: border-box;"><span style="box-sizing: border-box;">Next challenges: </span><span ng-repeat="pair in aceCtrl.interpret.next_challenge_pairs" class="ng-scope" style="box-sizing: border-box;"><a target=_blank class="btn btn-xs btn-primary ng-binding" ng-href="/problems/maximal-rectangle" ng-click="ga('send', 'event', 'next-challenge', 'click');" href="https://leetcode.com/problems/maximal-rectangle" style="box-sizing: border-box; color: rgb(255, 255, 255); text-decoration: none; display: inline-block; padding: 1px 5px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(46, 109, 164); border-radius: 3px; background-image: none; background-color: rgb(51, 122, 183);">(H) Maximal Rectangle</a> </span><span ng-repeat="pair in aceCtrl.interpret.next_challenge_pairs" class="ng-scope" style="box-sizing: border-box;"><a target=_blank class="btn btn-xs btn-primary ng-binding" ng-href="/problems/maximal-square" ng-click="ga('send', 'event', 'next-challenge', 'click');" href="https://leetcode.com/problems/maximal-square" style="box-sizing: border-box; color: rgb(255, 255, 255); text-decoration: none; display: inline-block; padding: 1px 5px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(46, 109, 164); border-radius: 3px; background-image: none; background-color: rgb(51, 122, 183);">(M) Maximal Square</a> </span><span ng-repeat="pair in aceCtrl.interpret.next_challenge_pairs" class="ng-scope" style="box-sizing: border-box;"><a target=_blank class="btn btn-xs btn-primary ng-binding" ng-href="/problems/android-unlock-patterns" ng-click="ga('send', 'event', 'next-challenge', 'click');" href="https://leetcode.com/problems/android-unlock-patterns" style="box-sizing: border-box; color: rgb(255, 255, 255); text-decoration: none; display: inline-block; padding: 1px 5px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(46, 109, 164); border-radius: 3px; background-image: none; background-color: rgb(51, 122, 183);">(M) Android Unlock Patterns</a></span></div></div></div><h4 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 18px;"><small style="box-sizing: border-box; font-size: 13.5px; line-height: 1; color: rgb(119, 119, 119);">Share your acceptance!</small></h4></div>
分析:
1.利用分治法,当求dp[i][j],我们假设k是(i,j)之间的开区间的数,其中k表示最后一个被弄破的气球是k时的情况,显然dp[i][j]=max{dp[i][i]+num[i]*num[k]*num[j]+dp[k][j]};
2. 用K表示气球k是最后一个弄破的,刚开始看到这道题的时候第一反应就是动态规划算法,但是只想到用dp[i][k]的k表示第一个被弄破的气球,这样对于后续转态很不好表示,
如果用k表示是最后一个弄破的气球,则很好地利用了二分法!!!