LeetCode OJ: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

dp问题,开始没想出来唉,看了下别人的。就是从left和right间的间距从2开始,一直递推到相差n-1这种情况。代码如下所示:

 1 class Solution {
 2 public:
 3     static bool noCoins(int a)
 4     {
 5         return a == 0;
 6     }
 7     int maxCoins(vector<int>& nums) {
 8         nums.erase(remove_if(nums.begin(), nums.end(), noCoins), nums.end());//注意这里的处理方法。先调用remove在调用erase
 9         if(nums.size() == 0)
10             return 0;
11         nums.resize(nums.size() + 2);
12         copy(nums.begin(), nums.end() - 2, nums.begin() + 1);
13         nums[0] = nums[nums.size() - 1] = 1;
14         int sz = nums.size();
15         int dp[sz][sz] = {};
16         for(int interval = 2; interval < sz; ++interval){//interval指的是left与right之间的间隔
17             for(int left = 0; left < sz - interval; ++left){
18                 int right = left + interval;
19                 for(int j = left + 1; j < right; ++j){//穷尽left-right之间的每一种可能值
20                     dp[left][right] = max(dp[left][right], dp[left][j] + nums[left]*nums[j]*nums[right] + dp[j][right]);
21                  }                                                       //注意这里取left以及right的原因是left到j之间的数字以及被dp[left][j]所覆盖了
22             }
23         }
24         return dp[0][sz-1];//这是最终结果了
25     }
26 };
时间: 2024-08-09 06:38:00

LeetCode OJ:Burst Balloons(击破气球)的相关文章

LeetCode 312. Burst Balloons(戳气球)

参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/article/details/51208865 public int maxCoins(int[] nums) { int[] balls = new int[nums.length+2]; balls[0] = 1; balls[balls.length - 1] = 1; int[][] coins

[LeetCode][Java]Burst Balloons

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.

Leetcode 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 r

312 Burst Balloons 戳气球

现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币. 这里的 left 和 right 代表和 i 相邻的气球的序号. 注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球.求所能获得硬币数量的最大值.注意:(1) 你可以认为 nums[-1] = nums[n] = 1,但注意它们不是真

452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinate

Leetcode-312 Burst Balloons(戳气球)

1 #define maxn 1000000 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 #define pb push_back 4 class Solution 5 { 6 public: 7 int dp[503][503]; 8 int ms(vector<int>& nums,int le,int ri) 9 { 10 11 if(le==ri-1) 12 return 0; 13 if(dp[le][ri

LeetCode | 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球【Python】

LeetCode 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球[Medium][Python][区间贪心] Problem LeetCode There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar