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... 阅读详情

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

扎气球的游戏,每个气球上写的有coin数,每次扎破的时候可以得到扎破的气球* 它左边 * 它右边的coin奖励,问采用怎样的扎破顺序可以得到最大的coin

在只有一个气球的时候默认它左边右边都是coin=1

思路:
先把左右两端默认的1补上作padding
[3, 1, 5, 8]补成[1, 3, 1, 5, 8, 1]

然后选一个左端的气球,选一个右端的气球,在它们中间可以选择任意一个不被扎破
比如选左边是3,右边是8,那么中间有两种选择,可以不扎1,或者不扎5
不扎1的时候,1留到最后,就先扎5,先得到扎5的coin
然后得到3* 1 * 8 + 扎5得到的coin

同理如果不扎5,5留到最后,就先扎1,得到扎1的coin
然后得到3* 5* 8 + 扎1得到的coin

所以把从left到right的气球可以得到的coin定义成dp
中间从左到右选择一个mid,即保留到最后的那个
coin[left][right] =
coin[left][mid - 1] + coin[mid + 1][right] + nums[left - 1] * nums[mid] * nums[right + 1]

mid: 保留到最后
left-1, left, … mid- 1, mid, mid + 1, …,right, right + 1

刚开始先算长度为1的,即只有1个气球,然后慢慢增加长度,直到长度为nums.length

// 8ms
    int maxCoins(vector<int>& nums) {
        if (nums.empty()) {
            return 0;
        }
        
        int bal_num = nums.size();
        
        nums.insert(nums.begin() , 1);
        nums.push_back(1);
        
        //dp..      
        vector<vector<int>> coins(bal_num + 2, vector<int>(bal_num + 2, 0));
        
        for (int len = 1; len <= bal_num; len++) {
            for (int left = 1; left <= bal_num - len + 1; left++) {
                int right = left + len - 1;
                for (int mid = left; mid <= right; mid++) {
                    coins[left][right] = max(coins[left][right],
                           coins[left][mid - 1] + coins[mid + 1][right] + nums[left - 1] * nums[mid] * nums[right + 1]); 
                }
            }
        }
        
        return coins[1][bal_num];
    }

java版

    public int maxCoins(int[] nums) {
        if(nums == null || nums.length == 0) {
            return 0;
        }
        
        int n = nums.length;
        int[][] dp = new int[n+2][n+2];
        
        //区间段的长度
        for(int len = 1; len <= n; len++) {
            for(int left = 1; left <= n - len + 1; left++) {
                int right = left + len - 1;
                for(int mid = left; mid <= right; mid ++) {
                //nums的index从0开始,left从1开始,要left-1对应nums的index,同理right,mid
                    int leftBorder = (left == 1) ? 1 : nums[left - 2];
                    int rightBorder = (right == n) ? 1 : nums[right];
                    //选不同的mid分别算出所有的dp[left][right],取最大的
                    dp[left][right] = Math.max(dp[left][right], 
                            dp[left][mid-1] + leftBorder * nums[mid-1] * rightBorder
                                    + dp[mid+1][right]);
                }
            }
        }
        
        return dp[1][n];
    }
DFT设计绪论 DFT设计的主要目的是为了将defect-free的芯片交给客户。 产品质量,通常使用Parts Per million(PPM)来衡量。 但是随着IC从SSI到VLSI的发展,在test上花销的时间越来越多,test的quality却很难提高,这使得DFT的engineer不断的发展着DFT的技术。 DFT engineer面对的第一个问题是设计内部的状态的可测试性问题。在... 阅读详情

相关推荐

基于UCD3138的数字控制硬开关全桥DC-DC电源设计实战解析

在开关电源设计中,数字控制技术正逐步取代传统模拟方案,以其卓越的灵活性、可编程性和强大的数据处理能力,成为应对复杂动态响应与智能化需求的关键。其核心原理在于通过高精度模数转换器(ADC)实时采样电压、电流信号,经由数字信号处理器(如ARM内核)运行PID等控制算法,动态调整数字脉宽调制(DPWM)输出,从而实现对功率级的精确控制。这种技术价值体现在能够轻松实现高级算法(如输入电压前馈、非线性补偿)、灵活配置保护参数,并通过PMBus等接口实现智能化监控与管理。其典型应用场景包括通信基站、服务器电源以及工业设

1329

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

(最新更新!)2024全国大学生数学建模竞赛(国赛)B题决策优化+鲁棒模型完整建模全解全析

2024全国大学生数学建模竞赛(国赛)B题完整建模全解全析(已更新!)

Yilu264的博客 9428

Leetcode 312. Burst Balloons

【代码】Leetcode 312. Burst Balloons。

小白菜又菜 821

高通QCS2290 SecureBoot全流程实战:从密钥生成到NON-HLOS.bin生成

本文详细拆解了高通QCS2290平台SecureBoot安全启动技术的端到端实战流程。从开发环境搭建、RSA密钥对生成与证书链构建,到关键XML配置文件修改、固件编译与数字签名,最终完成NON-HLOS.bin集成打包,为嵌入式开发者提供了一份清晰、可操作的安全启动部署指南。

weixin_28273593的博客 305

麒麟V10 ARM环境离线部署MySQL:从依赖缺失到服务自启的完整避坑指南

本文详细介绍了在麒麟V10 ARM环境下离线部署MySQL的完整流程,包括依赖缺失解决方案、目录配置、服务自启设置及性能调优。特别针对国产操作系统和ARM架构的兼容性问题,提供了实用的避坑指南和优化建议,帮助用户高效完成MySQL部署与维护。

weixin_28741413的博客 414

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

杨领well的专栏 266

C++游戏开发入门:从环境配置到第一个完整项目

C++ 作为游戏开发领域的关键语言,其学习路径常因语法枯燥而令初学者却步。以项目驱动的方式学编程,能帮助学习者将变量、循环、函数、指针等基础概念融入实际游戏逻辑中,获得即时反馈与完整项目经验。掌握合适的开发环境是高效实践的前提,例如在 Windows 下使用 VS Code 搭配 MinGW-w64 编译器,配合调试器与构建脚本,即可搭建轻量可扩展的 C++ 游戏开发工作流。此类课程通常面向无基础人群,从控制台小游戏逐步过渡到带游戏循环的完整作品,过程中还需关注性能观察、素材授权与版本管理。无论你是刚接触编

weixin_30621959的博客 552

LeetCode 312. Burst Balloons 解题报告

LeetCode 230. Kth Smallest Element in a BST 解题报告

骆小坑的填坑之旅 813

从零认识 VQGAN:开源项目选择、核心原理与复现路线指南【taming-transformers】

VQGAN 可以理解为 Vector Quantized Generative Adversarial Network,即向量量化生成对抗网络。Encoder;Decoder;离散 Codebook;向量量化器;感知损失;GAN 判别器。VQGAN 的主要任务不是直接随机生成图像,而是学习一种高质量的图像压缩表示。编码器输出的潜变量仍然是连续浮点数。为了让 Transformer 能像处理文字 token 一样处理图像,VQGAN 使用一个离散码本。Ee0e1eK。

u013250861的博客 38

医学图像的对抗攻击:模型安全性与鲁棒性分析

医学图像的对抗攻击:模型安全性与鲁棒性分析 ,人工智能,计算机视觉,大模型,AI,本文围绕医学图像的对抗攻击展开,分析了其对AI模型的威胁。介绍了对抗样本定义、攻击原理,以及FGSM、BIM、PGD等常见攻击方法,还给出了相关代码示例。阐述了攻击对模型诊断准确性和可信度的影响,提出对抗训练、输入预处理、模型集成等提升模型安全性与鲁棒性的方法,探讨了评估指标、数据集和流程,分析了实际应用中的挑战与应对策略,最后展望了未来发展趋势。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 18万+

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

【EB-03】 AUTOSAR builder与EB RTE集成

打开EB工具后,在EB的步骤是:执行MultiTask(2)-> Mapping Component to Partitions(4)->Task Mapping(5)-> SvcAs_Trigger(7)-> Code Generation(8)如涉及新增BSW Module,也需要重复以上步骤。

libuping2023的博客 975

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 bur

Gregzeng的博客 595

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Camera2Demo

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

LeetCode 312. Burst Balloons

A very interesting question! Think that we have N balloons to shot. 0 - 1 - 2 .... - n-1  in order to get the best score, we want to shot a special balloon, name it as k. Then, we can get that. 0

ActiveCoder的博客 1197

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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 bu

nevermorezjh的专栏 633

[leetcode] 312. 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...

Learning from the mistakes 298

LeetCode 312. Burst Balloons 题解

本题是区间动态规划的经典难题。区间动态规划的状态定义从长度较小的区间开始计算虚拟气球的添加,简化边界处理考虑最后戳破的气球,将问题分解通过本题可以深入理解区间动态规划的思想。

cannonjinx的博客 1014

leetcode 312 Burst Balloons c++

312.Burst Balloons Hard 124534FavoriteShare Givennballoons, indexed from0ton-1. Each balloon is painted with a number on it represented by arraynums. You are asked to burst all the balloons....

cmy203的博客 295

[LeetCode 312] Burst Balloons

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

ExcitedZhang的博客 142

LeetCode-312. Burst Balloons [C++][Java]

You are givennballoons, indexed from0ton - 1. Each balloon is painted with a number on it represented by an arraynums. You are asked to burst all the balloons.

贫道绝缘子的博客 628

【leetcode】-312. Burst Balloons戳气球

312. Burst Balloons题目动态规划python代码 题目 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] * num

。 345

【Leetcode】312. Burst Balloons

题目地址: https://leetcode.com/problems/burst-balloons/ 给定一个数组AAA,A[i]A[i]A[i]代表下标为iii的气球所带的权值。可以按某种顺序戳破气球,每次戳破气球之后,会得到一个分数,这个分数等于A[i]A[i]A[i]与其左边最近的未戳破的气球的权值的乘积再乘以其右边最近的未戳破的气球的权值。如果要戳的气球刚好在边上,则设想边界上有一个权值为111的气球即可。问最多能得多少分。 法1:记忆化搜索。先把AAA的左右两边都添上一个111,接着设f[i][

数学、算法爱好者的博客 186

LeetCode //C - 312. Burst Balloons

【代码】LeetCode //C - 312. Burst Balloons。

Made in Code 922

【leetcode】312. Burst Balloons

312. Burst Balloonshttps://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 a

xxxxzr的博客 440

Leetcode - 312 Burst Balloons

Leetcode - 312 Burst Balloons解题思路动态规划 为了方便,做如下处理 nums = [1] + nums + [1] dp = [[0] * len(nums) for i in range(len(nums)) ] 动态方案 dp[i][j]表示从nums[i:j+1]的最佳方案,注意这里的最佳是在指定nums[i-1]和nums[j+1]为临下的最佳 递推关系

ZhengJingWuLai的专栏 352
上一篇: leetcode 309.Best Time to Buy and Sell Stock with Cooldown (time : O(n))
下一篇: leetcode 322. Coin Change(交换硬币)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址