Burst Balloons
https://leetcode.com/problems/burst-balloons/description/
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
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
Thoughts
给定一个数组,元素值代表收益,每次选择一个元素去掉,得到的收益为它当前的左边元素它它右边元素。action为选择一个元素k去掉,相当于根据k做划分。如果正着想很难理清状态,但倒着可以知道最后一次只会爆掉一个元素k,因此让dp[j][i]表示爆j和i之间的(不包括i和j)所有气球能获得的最大收益,让k表示j和i之间中最后一个爆掉的气球, 那么爆k时的收益为coins[j] coins[k] coins[i] + dp[j][k] + dp[k][i]。 让k在[j + 1, i - 1]依次遍历即可。
Code
Analysis
Errors:
j从小到大遍历
时间复杂度O(N^3), 空间O(N^2).
Last updated
Was this helpful?