题目地址
题目描述
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
1 | nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] |
解题思路
题目是打气球,每打一个气球的得分是这个气球左右两边的数字以及这个气球的数字之积,如果左右两边没有数字就用1代替,最终需要得到可以得到的最大值。
求全局极值问题,首先考虑递归或者动态规划!
这道题使用动态规划,dp[i][j]存放的是nums[i]到nums[j]内可以打爆所有气球获得的最大值,最后我们返回dp[0][n]就可以了。
递推式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 )
解题代码【.CPP】
1 | class Solution { |