[leetcode]312. Burst Balloons
来源:互联网 发布:python string转list 编辑:程序博客网 时间:2024/06/14 03:03
题目链接: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
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
the visualization help me to understand hope it can help you too.
1···················1
add two 1 at beginning and end of nums, each · represent each number in nums.
len is the subinterval length, it grows from 1 to full length of orignal nums string.
the following illustrations demonstrate how the subinterval shift from left to right. (len = 7 in the illustration)
for each len, when shifted to rightmost, increase len and do the shift again. this way we can evaluate all possible subintervals.
for each subinterval, in the innermost for loop, find which balloon to burst LAST that will give us the most coins for that subinterval. <- IMPORTANT TO UNDERSTAND THIS
dp[left][right]
is the maximum coins we can get from left to right. note when left > right, it is 0
1···················1 (len = 7) | | left right 1···················1 | | left right 1···················1 | | left right
for the example [3, 1, 5, 8], the dp matrix is updated like this
0 0 0 0 0 00 3 0 0 0 00 0 15 0 0 00 0 0 40 0 00 0 0 0 40 00 0 0 0 0 0
then
0 0 0 0 0 00 3 30 0 0 00 0 15 135 0 00 0 0 40 48 00 0 0 0 40 00 0 0 0 0 0
at last
0 0 0 0 0 00 3 30 159 167 00 0 15 135 159 00 0 0 40 48 00 0 0 0 40 00 0 0 0 0 0
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][n]; }};
- leetcode 312. Burst Balloons
- [leetcode] 312. Burst Balloons
- LeetCode 312. Burst Balloons
- LeetCode 312. Burst Balloons
- leetcode.312. Burst Balloons
- 【leetcode】312. Burst Balloons
- LeetCode 312. Burst Balloons
- leetcode 312. Burst Balloons
- leetcode 312.Burst Balloons
- Leetcode-312. Burst Balloons
- 【Leetcode】312. Burst Balloons
- 【leetcode】312. Burst Balloons
- 【leetcode】312. Burst Balloons
- LeetCode 312. Burst Balloons
- 【Leetcode】312. Burst Balloons
- 【LeetCode】312. Burst Balloons
- 【LeetCode】312. Burst Balloons
- leetcode 312. Burst Balloons
- Android ViewPager切换动画PageTransformer
- QT日常代码三
- shell 脚本为php 执行订单过期接口
- 界面加载位图
- Linux关于时间同步命令
- [leetcode]312. Burst Balloons
- [SpringMVC] SpringMVC介绍
- Java高级特性之一动态代理
- 现代OpenGL教程-lesson1
- 安卓高级控件之ViewPager
- 设计模式笔记——桥接者模式
- 【Python】 Missing parentheses in call to 'print'
- 如何获取 chosen chosen.jquery.js 选中的值
- SSH—Struts2拦截器的应用(防止未登录用户进行操作)