0312. Burst Balloons (H)

Stella981
• 阅读 472

Burst Balloons (H)

题目

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:

  • You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
  • 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Input: [3,1,5,8]
Output: 167 
Explanation: 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

题意

打气球游戏。每个气球有对应的分值,打爆一个气球i,能得到的积分为气球i、气球i-1、气球i+1积分的乘积。要求计算能够获得的最大积分。

思路

动态规划问题。dp[left, right]表示打爆从left到right所有气球能得到的最大积分。每一次我们在[left, right]这个区间中选择一个i,代表该区间中最后一个被打爆的气球,那么可以得到递推关系式:

\[dp[left,\ right]=\max_{left \le i \le right}(dp[left,\ i-1]+dp[i+1,\ right]+nums[left-1]*nums[i]*nums[right+1]) \]


代码实现

Java

记忆化搜索

class Solution {
    public int maxCoins(int[] nums) {
        int[][] memo = new int[nums.length][nums.length];
        for (int i = 0; i < nums.length; i++) {
            Arrays.fill(memo[i], -1);
        }
        return dfs(nums, 0, nums.length - 1, memo);
    }

    private int dfs(int[] nums, int left, int right, int[][] memo) {
        if (right < left) {
            return 0;
        }
        if (memo[left][right] != -1) {
            return memo[left][right];
        }
        int max = 0;
        for (int i = left; i <= right; i++) {
            int x = dfs(nums, left, i - 1, memo)
                    + dfs(nums, i + 1, right, memo)
                    + nums[i] * (left == 0 ? 1 : nums[left - 1]) * (right == nums.length - 1 ? 1 : nums[right + 1]);
            max = Math.max(max, x);
        }
        memo[left][right] = max;
        return max;
    }
}

动态规划

class Solution {
    public int maxCoins(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        int[][] dp = new int[nums.length][nums.length];
        for (int right = 0; right < nums.length; right++) {
            for (int left = right; left >= 0; left--) {
                for (int i = left; i <= right; i++) {
                    int leftMax = i == left ? 0 : dp[left][i - 1];
                    int rightMax = i == right ? 0 : dp[i + 1][right];
                    int last = nums[i] * (left == 0 ? 1 : nums[left - 1]) * (right == nums.length - 1 ? 1 : nums[right + 1]);
                    dp[left][right] = Math.max(dp[left][right], leftMax + rightMax + last);
                }
            }
        }
        return dp[0][nums.length - 1];
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
mysql timestamp
 select from\_unixtime(m.createdAt, '%Y%m%d %H:%i:%s') from kfrobotaidlog m;select m.customeruid, from\_unixtime(m.createtime, '%Y%m%d %H:%i:%s') as \datetime\, m.kfui
可莉 可莉
3年前
0312. Burst Balloons (H)
BurstBalloons(H)题目Givennballoons,indexedfrom0ton1.Eachballoonispaintedwithanumberonitrepresentedbyarraynums.Youareasked
Stella981 Stella981
3年前
Android 从 Web 唤起 APP
前言!(http://7q5c2h.com1.z0.glb.clouddn.com/WebToAPP2.png?watermark/2/text/5ZC05bCP6b6Z5ZCM5a24/font/5qW35L2T/fontsize/500/fill/I0VGRUZFRg/dissolve/100/gravity/SouthEast/dx/
Stella981 Stella981
3年前
PHP接收前端传值各种情况整理
<h1PHP接收前端传值各种情况整理</h1<h2服务端代码:</h2header('AccessControlAllowOrigin:');var_dump($_POST);exit;<h2情况</h2<h31)传null</h3$.pos
Stella981 Stella981
3年前
Python time模块 返回格式化时间
常用命令  strftimetime.strftime("%Y%m%d%H:%M:%S",formattime)第二个参数为可选参数,不填第二个参数则返回格式化后的当前时间日期201812112:00:00time.strftime('%H:%M:%S')返回当前时间的时分秒time.strftim
Stella981 Stella981
3年前
MFC_TCP_Server
!(https://oscimg.oschina.net/oscnet/44fdf01cd2e73dc5aa35a55874419856565.png)1//New_MFC_TCP_SreverDlg.h:头文件2//3defineWM_SOCKETWM_USER1004pragm
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这