LeetCode: 312. Burst Balloons

Leetcode 312 打气球 Burst Balloons C++ 史上最详细题解系列   每天更新一道python or C++ leetcode题,力求讲解清晰准确,客官们可以点赞或者关注。   题目: 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。 现在要求你戳破所有的气球。每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币。 这里的 left 和 ri... 阅读详情

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

解题思路 —— 记忆搜索

  1. 记:record[i][j] 为在原气球序列中,区间 [i, j] 的气球炸裂能得到的最大值
  2. 则,record[i][j] = max(record[i][k-1] + record[k+1][j] + nums[i-1]*nums[k]*nums[j+1], ...) 其中 k=i...j (假设区间 [i, j] 中最后爆裂的是下标为 k 的气球)
  3. 由于第 k 个气球最后一个爆裂,因此其左右的气球互相之间都没有了关系(一直都有它在中间隔开)。

AC 代码

func max(lhv, rhv int) int {
    if lhv > rhv {
        return lhv
    } else {
        return rhv
    }
}
func maxCoinsRef(nums []int, record *[][]int, beg int, end int) int{
    if beg > end || beg < 0 || end >= len(nums) {
        return 0
    } else if (*record)[beg][end] !=  -1 {
        return (*record)[beg][end]
    } else if beg == end {
        burstVal := nums[beg]
        if 0 != beg { 
            burstVal *= nums[beg-1]
        }
        if len(nums)-1 != beg {
            burstVal *= nums[beg+1]
        }
        
        (*record)[beg][end] = burstVal
    } else {
        // [beg, end] 的气球 i 最后 burst
        for i := beg; i <= end; i++ {
            burstVal := nums[i]
            if beg != 0 { 
                burstVal *= nums[beg-1]
            }
            if end != len(nums)-1 {
                burstVal *= nums[end+1]
            }
            
            (*record)[beg][end] = max((*record)[beg][end], 
            	maxCoinsRef(nums, record, beg, i-1) + 
            	burstVal +  
            	maxCoinsRef(nums, record, i+1, end))
        }
    }

    return (*record)[beg][end]
}

func maxCoins(nums []int) int {
    if len(nums)== 0 { return 0 }
    
    // record[i][j]: maxCoins(nums[i,j+1])
    record := make([][]int, len(nums))
    for i := 0; i < len(record); i++ {
        record[i] = make([]int, len(nums))
        
        for j := 0; j < len(record[i]); j++ {
            record[i][j] = -1
        }
    }
    
    maxCoinsRef(nums, &record, 0, len(nums)-1)
    
    return record[0][len(nums)-1]
}
DANN实战:用Python手把手教你实现对抗迁移学习(附完整代码) 本文通过Python实战教程,手把手教你实现DANN对抗迁移学习模型,以解决领域偏移问题。文章详细解析了领域对抗神经网络的核心机制,包括梯度反转层和领域判别器的构建,并提供了从数据准备、模型搭建到训练评估的完整代码,帮助开发者快速掌握这一强大的无监督领域自适应技术。 阅读详情

相关推荐

OpenWRT系统启动流程全解析:从procd到ubus的完整链路(附源码分析)

本文深入解析了OpenWRT系统从内核移交控制权到用户空间服务就绪的完整启动链路。核心聚焦于进程管理器procd与通信总线ubus的协同工作机制,通过源码分析揭示了状态机转换、服务注册与事件分发的内在逻辑,为系统定制、启动优化与故障调试提供了清晰指南。

weixin_28335283的博客 538

Leetcode 312: Burst Balloons

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

丫丫狸笫的专栏 668

激光可燃气体传感器:核心技术环节分析

激光可燃气体传感器核心技术涵盖七个维度:1)TDLAS光谱吸收与调制技术(检测下限ppm级);2)DFB激光器与双闭环温控系统(波长稳定性±0.0036nm);3)微弱信号处理技术(信噪比提升60dB);4)环境补偿算法(温压补偿误差<±1%);5)本质安全防爆设计(ExiaIICT4Ga等级);6)智能边缘计算(STM32L476芯片);7)多重性能验证(响应时间<1秒)。通过微型化、多气体检测等创新,该技术已在工业安全等领域替代传统方案,实现高精度检测。(149字)

上市公司前技术负责人,20 年软硬件深耕 + 百人研发团队管理经验,名校理工科背景加持。从技术攻坚到团队掌舵,沉淀海量项目实战与管理心得,专注分享软硬件干货、疑难问题解决方案,既是靠谱知识传播者,也盼链接优质项目共成长。 1063

LeetCode312: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[le...

卷卷萌的博客 607

Altium Designer20-柔性PCB绘制技巧

Altium Designer2020-柔性PCB绘制技巧 注:教程所用PCB随意找的,并没有完全绘制完,大家学习PCB折弯方法即可。 1.打开PCB,点击视图->板子规划模式1,如下图所示 2.点击设计->定义分割线,用两条分割线绘出要折弯的区域 3.点击设计->定义弯曲线(弯曲线呈现黄色) 4.用快捷键d,k进入layer manage stack(层叠管理器),在f...

weixin_44450279的博客 1万+

SAR成像处理基本知识2——图像尺寸与回波尺寸的关系

阅读本文前,请先了解上一篇文章(SAR成像处理基本知识1——常用专用名词解释) 1. SAR图像尺寸预估方法 以C波段2m@20km的SAR图像为例,假设天线中心下视角为35° ,说明图像尺寸的估计方法。 1.1 距离向尺寸估计 地距分辨率为2m时,距离向分辨率约为1.15m,则要求信号带宽不小于130MHz,假设信号带宽为150MHz,采样率为180MHz,那么距离向分辨单元为0.833m。 地距幅宽为20km时,距离向幅宽约为20*sind(3...

爱学习的小伟的博客 4959

leetcode 312 : Burst Balloons

1、原题如下: 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 ge

l3368bcttqnqn的博客 920

告别迷茫!手把手教你用ArcGIS+GTB搞定生态源地MSPA分析(附避坑指南)

本文详细介绍了如何使用ArcGIS和GTB进行生态源地MSPA分析的全流程,包括软件安装、数据预处理、参数设置、结果可视化和常见问题排查。特别针对ArcGIS和GTB组合使用时的常见问题提供了避坑指南,帮助研究者高效完成生态规划任务。

weixin_30756499的博客 445

LeetCode:312. Burst Balloons

很有意思的一道如何转换思路的题目。 一开始,我是这么想的: dp[i][j]代表的是i->j获得的最大收益。然后找一个位置k(i <= k <= j)戳破这儿的气球。现在问题来了,如果戳破这儿的气球,那么比如dp[k+1][j]就不能用了。因为dp[k+1][j]算的时候是以k位置为左边的,但是戳破了k,此时左边是k-1,所以dp[k+1][j]在这种情况下就用不起来。那怎么办...

weixin_43462819的博客 129

避坑指南:用逐飞开源库驱动MT9V03X摄像头时容易忽略的3个细节(附TFT显示优化技巧)

本文详细解析了在智能汽车竞赛中使用MT9V03X摄像头(总钻风)时常见的硬件连接、图像采集和TFT显示优化问题。重点介绍了逐飞开源库中容易被忽略的三个关键细节,包括电源管理、看门狗设置和中断优先级配置,并提供了针对IPS200和TFT180显示屏的优化技巧,帮助参赛队伍提升图像处理效率和显示质量。

study的专栏 757

STM32F103用软件I2C读取IP2366电量数据,实测避坑指南(附完整源码)

本文详细介绍了如何使用STM32F103的软件I2C驱动读取IP2366电池管理芯片的电量数据,包括GPIO初始化、I2C时序实现、关键延时优化及常见问题解决方案。通过实测避坑指南和完整源码,帮助开发者快速实现稳定的电量数据读取,适用于嵌入式电池管理系统开发。

weixin_33725722的博客 529

题解——Leetcode 312. Burst Balloons 难度:Hard

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 num

dxk3881345的博客 411

【YOLOv8科研级优化】集成YOLOv10的SCDown采样模块,让你的模型下采样更高效更精准

打开确保模型解析器能识别SCDown模块。通过将YOLOv10的SCDown模块集成到YOLOv8中,我们实现了一个“细节保留+高效下采样”的目标检测模型。这个改进不仅在工程上能提升小目标、细粒度场景的检测效果,更在科研上提供了“分阶段下采样”的创新思路——这种思路可以延伸到其他卷积网络(如ResNet、MobileNet)的下采样层改进中,帮你在论文中打造独特的研究点。

m0_71589036的博客 139

LeetCode #312: Burst Balloons

Problem Statement(Source) 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 ballo

浮生琴弦 500

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

[算法分析与设计] leetcode 每周一题: 312. Burst Balloons

题目链接:  312. Burst Balloons 题目大意: 给定 n 个气球, 序号为 1 到 n-1, 每个气球又对应一个分值, 分值用长度为 n 的数组 nums 表示 ; 现要求逐个打爆所有气球, 而每打爆一个序号为 i 的气球, 就可以得到 nums[left] * num[i] * nums[right] 的分值(即 coins), 其中 nu

sinat_37893507的博客 366

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

LeetCode(312):戳气球 Burst Balloons(Java)

2019.7.23 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 这道智力题一下子看起来让人无从下手,因为每个气球本身的分值取决于相邻气球的分值之和,而戳破后,气球之间的相邻状态会发生改变。那么对于不同的n个戳气球的选择,会生成n个不同的结果,实质反映会是一棵深度为n,结点的度从n开始逐层减少的决策树。 那么其实可以想到一种递归的做法,但是很明显,当n比较大的时候会超时...

NJU_ChopinXBP的博客 530

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

java在某射击场有n个靶_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[le...

weixin_34088048的博客 209

LeetCode:312. Burst Balloons - Python

问题描述: 312. 戳气球 有n个气球,编号为0到 n-1,每个气球上都标有一个数字,这些数字存在数组nums 中。 现在要求你戳破所有的气球。每当你戳破一个气球i时,你可以获得nums[left] * nums[i] * nums[right]个硬币。 这里的left和 right代表和i相邻的两个气球的序号。注意当你戳破了气球 i后,气球left和气球right就变成了相邻的气球。 ...

Growth Diary 1101

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 balloo

sinat_37607788的博客 255

leetcode : 312. Burst Balloons : dp矩阵加括号

public class Solution { public int maxCoins(int[] nums) { int n=nums.length; int[] tnums=new int[n+2]; int[][]maxdp=new int[n+2][n+2]; System.arraycopy(nums, 0, tnums, 1, n); tnums[0]=1;

smileyk的专栏 711

leetcode_c++:Divide and Conquer:Burst Balloons(312)

https://leetcode.com/problems/burst-balloons/class Solution { public: int maxCoins(vector<int>& nums) { //remove all of zero item nums.erase(remove_if(nums.begin(), nums.end(), [](i

LandscapeMi 517

[LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming

Givennballoons, indexed from0ton-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 ballooniyou will getnums[le...

weixin_30564785的博客 101

LeetCode 312. Burst Balloons[hard]

原题地址题目内容题目分析首先理解题目的意思:假设有一圈气球,每个气球上都有一个数字,假设扎破第i个气球,就会得到一个值res = ball[left]xball[i]xball[right],此时,left和right变成相邻的了,因为i气球被扎破。每扎破一个气球就进行一次计算,并且把值叠加到res上,使得所有气球都被扎破,最后得到的res的值最大。设立一个动态数组dp[left][right]表示

YuMingJing_的博客 500

LeetCode 312. Burst Balloons(Hard)

Burst Balloons(Hard)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 burs

Katrina_Pan的博客 693

Leetcode 312. Burst Balloons

【代码】Leetcode 312. Burst Balloons。

小白菜又菜 819
上一篇: LeetCode: 310. Minimum Height Trees
下一篇: LeetCode: 313. Super Ugly Number
IT技术小密圈
博客等级 码龄12年 1385粉丝 322原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值