当前位置:网站首页>LeetCode 312. 戳气球 每日一题
LeetCode 312. 戳气球 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
有 n 个气球,编号为0 到 n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
现在要求你戳破所有的气球。戳破第 i 个气球,你可以获得 nums[i - 1] * nums[i] * nums[i + 1] 枚硬币。 这里的 i - 1 和 i + 1 代表和 i 相邻的两个气球的序号。如果 i - 1或 i + 1 超出了数组的边界,那么就当它是一个数字为 1 的气球。
求所能获得硬币的最大数量。
示例 1:
输入:nums = [3,1,5,8]
输出: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
示例 2:输入:nums = [1,5]
输出:10
提示:
n == nums.length
1 <= n <= 300
0 <= nums[i] <= 100来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/burst-balloons
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
public int maxCoins(int[] nums) {
int n = nums.length;
int[] arr = new int[n + 2];
arr[0] = arr[n + 1] = 1;
for(int i = 0;i < n;i++) arr[i + 1] = nums[i];
int[][] dp = new int[n + 2][n + 2];
for(int i = n - 1;i >= 0;i--){
for(int j = i + 2;j <= n + 1;j++){
//先找到两个边界i和j,然后戳他们之间的气球,开区间
for(int k = i + 1;k < j;k++){
//编号i到编号j最大得分为:编号i到编号k的得分+编号k到编号j的得分+戳编号为k的得分
dp[i][j] = Math.max(
dp[i][j],
dp[i][k] + dp[k][j] + arr[i] * arr[k] * arr[j]
);
}
}
}
//编号为1到编号为n的气球最高得分
return dp[0][n + 1];
}
}边栏推荐
- 两类更新丢失及解决办法
- 模块六
- Cesium (4): the reason why gltf model is very dark after loading
- LeetCode 1186. 删除一次得到子数组最大和 每日一题
- 正在准备面试,分享面经
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
- 【MySql进阶】索引详解(一):索引数据页结构
- The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
- 面向接口编程
- Laravel post shows an exception when submitting data
猜你喜欢

Introduction and use of gateway

Interface oriented programming

面向接口编程

如何选择合适的自动化测试工具?

Record the migration process of a project

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

Binary search tree (features)

【DesignMode】代理模式(proxy pattern)

HAVE FUN | “飞船计划”活动最新进展

node:504报错
随机推荐
As an Android Developer programmer, Android advanced interview
作为Android开发程序员,android高级面试
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
Three. JS series (3): porting shaders in shadertoy
Deep listening array deep listening watch
[designmode] template method pattern
【C 语言】 题集 of Ⅹ
What is the difference between IP address and physical address
Tidb cannot start after modifying the configuration file
【DesignMode】外观模式 (facade patterns)
The difference and working principle between compiler and interpreter
typescript ts基础知识之tsconfig.json配置选项
os、sys、random标准库主要功能
打造All-in-One应用开发平台,轻流树立无代码行业标杆
SqlServer2014+: 创建表的同时创建索引
Inner monologue of accidental promotion
logback. XML configure logs of different levels and set color output
【医学分割】attention-unet
A tour of gRPC:03 - proto序列化/反序列化
[designmode] facade patterns