312 Burst Balloons
312. Burst Balloons
1. Question
Givenn
balloons, indexed from0
ton-1
. Each balloon is painted with a number on it represented by arraynums
. You are asked to burst all the balloons. If the you burst ballooni
you will getnums[left] * nums[i] * nums[right]
coins. Hereleft
andright
are adjacent indices ofi
. After the burst, theleft
andright
then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
(1) You may imaginenums[-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]
Return167
2. Implementation
(1) DP
3. Time & Space Complexity
DP: 时间复杂度O(n^3), 空间复杂度O(n^2)
Last updated
Was this helpful?